buffer_queue.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright 2015 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef RTC_BASE_BUFFER_QUEUE_H_
  11. #define RTC_BASE_BUFFER_QUEUE_H_
  12. #include <stddef.h>
  13. #include <deque>
  14. #include <vector>
  15. #include "rtc_base/buffer.h"
  16. #include "rtc_base/constructor_magic.h"
  17. #include "rtc_base/synchronization/sequence_checker.h"
  18. #include "rtc_base/system/no_unique_address.h"
  19. #include "rtc_base/thread_annotations.h"
  20. namespace rtc {
  21. class BufferQueue final {
  22. public:
  23. // Creates a buffer queue with a given capacity and default buffer size.
  24. BufferQueue(size_t capacity, size_t default_size);
  25. ~BufferQueue();
  26. // Return number of queued buffers.
  27. size_t size() const;
  28. // Clear the BufferQueue by moving all Buffers from |queue_| to |free_list_|.
  29. void Clear();
  30. // ReadFront will only read one buffer at a time and will truncate buffers
  31. // that don't fit in the passed memory.
  32. // Returns true unless no data could be returned.
  33. bool ReadFront(void* data, size_t bytes, size_t* bytes_read);
  34. // WriteBack always writes either the complete memory or nothing.
  35. // Returns true unless no data could be written.
  36. bool WriteBack(const void* data, size_t bytes, size_t* bytes_written);
  37. bool is_writable() const {
  38. RTC_DCHECK_RUN_ON(&sequence_checker_);
  39. return queue_.size() < capacity_;
  40. }
  41. bool is_readable() const {
  42. RTC_DCHECK_RUN_ON(&sequence_checker_);
  43. return !queue_.empty();
  44. }
  45. private:
  46. RTC_NO_UNIQUE_ADDRESS webrtc::SequenceChecker sequence_checker_;
  47. const size_t capacity_;
  48. const size_t default_size_;
  49. std::deque<Buffer*> queue_ RTC_GUARDED_BY(sequence_checker_);
  50. std::vector<Buffer*> free_list_ RTC_GUARDED_BY(sequence_checker_);
  51. RTC_DISALLOW_COPY_AND_ASSIGN(BufferQueue);
  52. };
  53. } // namespace rtc
  54. #endif // RTC_BASE_BUFFER_QUEUE_H_