tg2sip/libtgvoip/webrtc_dsp/rtc_base/swap_queue.h

213 lines
6.8 KiB
C
Raw Normal View History

Squashed 'libtgvoip/' changes from 6053cf5..cfd62e6 cfd62e6 Why did it change the OS X project 3a58a16 2.4.3 c4a48b3 Updated OS X project 564eada Fix #63 4f64e2e fixes 0c732e2 fixes 12e76ed better logging f015b79 Merge pull request #62 from xvitaly/big-endian a1df90f Set preferred audio session parameters on iOS 59a975b Fixes 8fd89fc Fixes, mic level testing and volume adjustment 243acfa Backported WebRTC upstream patch with Big Endian support. fed3bb7 Detect when proxy does not support UDP and persist that across calls a7546d4 Merge commit '6d03dd9ae4bf48d7344341cdd2d055ebd3a6a42e' into public 6d03dd9 version 69adf70 Use server config for APM + iOS crash fix 0b42ec8 Update iOS project f1b9e63 packet logging beeea45 I apparently still suck at C++ memory management 24fceba Update project 7f54b91 crash fix f85ce99 Save more data in data saving mode f4c4f79 Collect packet stats and accept json string for server config 78e584c New protocol version: optimized packet size 8cf9177 Fixed build on iOS 9dd089d fixed build on android 5caaaaf Updated WebRTC APM cc0cf35 fixed deadlock 02f4835 Rearranged VoIPController methods and added sections 912f73d Updated OS X project 39376df Fixed audio glitches on Windows dfe1f03 Updated project 81daf3f fix 296187a Merge pull request #58 from telegramdesktop/tdesktop 44956ac Merge pull request #57 from UnigramDev/public fb0a2b0 Fix build for Linux. d6cf1b7 Updated UWP wrapper 0f06289 Merge branch 'public' of github.com:grishka/libtgvoip into public dcfad91 Fix #54 162f447 Merge pull request #56 from telegramdesktop/tdesktop a7ee511 Merge remote-tracking branch 'origin/tdesktop' into HEAD 467b148 Removed unused files b1a0b3d 2.3 9b292fd Fix warning in Xcode 10. 8d8522a Merge pull request #53 from UnigramDev/public 646f7d6 Merge branch 'public' into public 14d782b Fixes 68acf59 Added GetSignalBarsCount and GetConnectionState to CXWrapper 761c586 Added GetStats to CXWrapper f643b02 Prevent crash if UWP WASAPI devices aren't found b2ac10e Fixed UWP project 9a1ec51 Fixed build for Windows Phone, fixed some warnings 4aea54f fix git-subtree-dir: libtgvoip git-subtree-split: cfd62e66a825348ac51f49e5d20bf8827fef7a38
2019-02-06 18:22:38 +00:00
/*
* Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef RTC_BASE_SWAP_QUEUE_H_
#define RTC_BASE_SWAP_QUEUE_H_
#include <algorithm>
#include <utility>
#include <vector>
#include "rtc_base/checks.h"
#include "rtc_base/constructormagic.h"
#include "rtc_base/criticalsection.h"
#include "rtc_base/system/unused.h"
namespace webrtc {
namespace internal {
// (Internal; please don't use outside this file.)
template <typename T>
bool NoopSwapQueueItemVerifierFunction(const T&) {
return true;
}
} // namespace internal
// Functor to use when supplying a verifier function for the queue.
template <typename T,
bool (*QueueItemVerifierFunction)(const T&) =
internal::NoopSwapQueueItemVerifierFunction>
class SwapQueueItemVerifier {
public:
bool operator()(const T& t) const { return QueueItemVerifierFunction(t); }
};
// This class is a fixed-size queue. A producer calls Insert() to insert
// an element of type T at the back of the queue, and a consumer calls
// Remove() to remove an element from the front of the queue. It's safe
// for the producer(s) and the consumer(s) to access the queue
// concurrently, from different threads.
//
// To avoid the construction, copying, and destruction of Ts that a naive
// queue implementation would require, for each "full" T passed from
// producer to consumer, SwapQueue<T> passes an "empty" T in the other
// direction (an "empty" T is one that contains nothing of value for the
// consumer). This bidirectional movement is implemented with swap().
//
// // Create queue:
// Bottle proto(568); // Prepare an empty Bottle. Heap allocates space for
// // 568 ml.
// SwapQueue<Bottle> q(N, proto); // Init queue with N copies of proto.
// // Each copy allocates on the heap.
// // Producer pseudo-code:
// Bottle b(568); // Prepare an empty Bottle. Heap allocates space for 568 ml.
// loop {
// b.Fill(amount); // Where amount <= 568 ml.
// q.Insert(&b); // Swap our full Bottle for an empty one from q.
// }
//
// // Consumer pseudo-code:
// Bottle b(568); // Prepare an empty Bottle. Heap allocates space for 568 ml.
// loop {
// q.Remove(&b); // Swap our empty Bottle for the next-in-line full Bottle.
// Drink(&b);
// }
//
// For a well-behaved Bottle class, there are no allocations in the
// producer, since it just fills an empty Bottle that's already large
// enough; no deallocations in the consumer, since it returns each empty
// Bottle to the queue after having drunk it; and no copies along the
// way, since the queue uses swap() everywhere to move full Bottles in
// one direction and empty ones in the other.
template <typename T, typename QueueItemVerifier = SwapQueueItemVerifier<T>>
class SwapQueue {
public:
// Creates a queue of size size and fills it with default constructed Ts.
explicit SwapQueue(size_t size) : queue_(size) {
RTC_DCHECK(VerifyQueueSlots());
}
// Same as above and accepts an item verification functor.
SwapQueue(size_t size, const QueueItemVerifier& queue_item_verifier)
: queue_item_verifier_(queue_item_verifier), queue_(size) {
RTC_DCHECK(VerifyQueueSlots());
}
// Creates a queue of size size and fills it with copies of prototype.
SwapQueue(size_t size, const T& prototype) : queue_(size, prototype) {
RTC_DCHECK(VerifyQueueSlots());
}
// Same as above and accepts an item verification functor.
SwapQueue(size_t size,
const T& prototype,
const QueueItemVerifier& queue_item_verifier)
: queue_item_verifier_(queue_item_verifier), queue_(size, prototype) {
RTC_DCHECK(VerifyQueueSlots());
}
// Resets the queue to have zero content wile maintaining the queue size.
void Clear() {
rtc::CritScope cs(&crit_queue_);
next_write_index_ = 0;
next_read_index_ = 0;
num_elements_ = 0;
}
// Inserts a "full" T at the back of the queue by swapping *input with an
// "empty" T from the queue.
// Returns true if the item was inserted or false if not (the queue was full).
// When specified, the T given in *input must pass the ItemVerifier() test.
// The contents of *input after the call are then also guaranteed to pass the
// ItemVerifier() test.
bool Insert(T* input) RTC_WARN_UNUSED_RESULT {
RTC_DCHECK(input);
rtc::CritScope cs(&crit_queue_);
RTC_DCHECK(queue_item_verifier_(*input));
if (num_elements_ == queue_.size()) {
return false;
}
using std::swap;
swap(*input, queue_[next_write_index_]);
++next_write_index_;
if (next_write_index_ == queue_.size()) {
next_write_index_ = 0;
}
++num_elements_;
RTC_DCHECK_LT(next_write_index_, queue_.size());
RTC_DCHECK_LE(num_elements_, queue_.size());
return true;
}
// Removes the frontmost "full" T from the queue by swapping it with
// the "empty" T in *output.
// Returns true if an item could be removed or false if not (the queue was
// empty). When specified, The T given in *output must pass the ItemVerifier()
// test and the contents of *output after the call are then also guaranteed to
// pass the ItemVerifier() test.
bool Remove(T* output) RTC_WARN_UNUSED_RESULT {
RTC_DCHECK(output);
rtc::CritScope cs(&crit_queue_);
RTC_DCHECK(queue_item_verifier_(*output));
if (num_elements_ == 0) {
return false;
}
using std::swap;
swap(*output, queue_[next_read_index_]);
++next_read_index_;
if (next_read_index_ == queue_.size()) {
next_read_index_ = 0;
}
--num_elements_;
RTC_DCHECK_LT(next_read_index_, queue_.size());
RTC_DCHECK_LE(num_elements_, queue_.size());
return true;
}
private:
// Verify that the queue slots complies with the ItemVerifier test.
bool VerifyQueueSlots() {
rtc::CritScope cs(&crit_queue_);
for (const auto& v : queue_) {
RTC_DCHECK(queue_item_verifier_(v));
}
return true;
}
rtc::CriticalSection crit_queue_;
// TODO(peah): Change this to use std::function() once we can use C++11 std
// lib.
QueueItemVerifier queue_item_verifier_ RTC_GUARDED_BY(crit_queue_);
// (next_read_index_ + num_elements_) % queue_.size() =
// next_write_index_
size_t next_write_index_ RTC_GUARDED_BY(crit_queue_) = 0;
size_t next_read_index_ RTC_GUARDED_BY(crit_queue_) = 0;
size_t num_elements_ RTC_GUARDED_BY(crit_queue_) = 0;
// queue_.size() is constant.
std::vector<T> queue_ RTC_GUARDED_BY(crit_queue_);
RTC_DISALLOW_COPY_AND_ASSIGN(SwapQueue);
};
} // namespace webrtc
#endif // RTC_BASE_SWAP_QUEUE_H_