buffer_queue.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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/critical_section.h"
  18. #include "rtc_base/thread_annotations.h"
  19. namespace rtc {
  20. class BufferQueue {
  21. public:
  22. // Creates a buffer queue with a given capacity and default buffer size.
  23. BufferQueue(size_t capacity, size_t default_size);
  24. virtual ~BufferQueue();
  25. // Return number of queued buffers.
  26. size_t size() const;
  27. // Clear the BufferQueue by moving all Buffers from |queue_| to |free_list_|.
  28. void Clear();
  29. // ReadFront will only read one buffer at a time and will truncate buffers
  30. // that don't fit in the passed memory.
  31. // Returns true unless no data could be returned.
  32. bool ReadFront(void* data, size_t bytes, size_t* bytes_read);
  33. // WriteBack always writes either the complete memory or nothing.
  34. // Returns true unless no data could be written.
  35. bool WriteBack(const void* data, size_t bytes, size_t* bytes_written);
  36. protected:
  37. // These methods are called when the state of the queue changes.
  38. virtual void NotifyReadableForTest() {}
  39. virtual void NotifyWritableForTest() {}
  40. private:
  41. size_t capacity_;
  42. size_t default_size_;
  43. CriticalSection crit_;
  44. std::deque<Buffer*> queue_ RTC_GUARDED_BY(crit_);
  45. std::vector<Buffer*> free_list_ RTC_GUARDED_BY(crit_);
  46. RTC_DISALLOW_COPY_AND_ASSIGN(BufferQueue);
  47. };
  48. } // namespace rtc
  49. #endif // RTC_BASE_BUFFER_QUEUE_H_