OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #ifndef WEBRTC_BASE_BUFFERQUEUE_H_ | 11 #ifndef WEBRTC_BASE_BUFFERQUEUE_H_ |
12 #define WEBRTC_BASE_BUFFERQUEUE_H_ | 12 #define WEBRTC_BASE_BUFFERQUEUE_H_ |
13 | 13 |
14 #include <deque> | 14 #include <deque> |
15 #include <vector> | 15 #include <vector> |
16 | 16 |
17 #include "webrtc/base/buffer.h" | 17 #include "webrtc/base/buffer.h" |
18 #include "webrtc/base/criticalsection.h" | 18 #include "webrtc/base/criticalsection.h" |
19 #include "webrtc/base/messagehandler.h" | |
20 #include "webrtc/base/messagequeue.h" | |
21 #include "webrtc/base/stream.h" | |
22 #include "webrtc/base/thread.h" | |
19 | 23 |
20 namespace rtc { | 24 namespace rtc { |
21 | 25 |
22 class BufferQueue { | 26 class BufferQueue : public StreamInterface { |
tommi
2015/11/18 10:10:45
I'm not sure about this change. It adds a lot of
joachim
2015/11/18 10:33:58
Yes. With this change, StreamInterfaceChannel coul
joachim
2015/11/18 11:26:06
What about I add empty methods to the BufferQueue
tommi
2015/11/18 12:20:04
If these methods are only needed for testing purpo
joachim
2015/11/18 13:57:00
Done.
| |
23 public: | 27 public: |
24 // Creates a buffer queue queue with a given capacity and default buffer size. | 28 // Creates a buffer queue with a given capacity, default buffer size |
25 BufferQueue(size_t capacity, size_t default_size); | 29 // and owner thread. |
30 BufferQueue(size_t capacity, size_t default_size, | |
31 Thread* owner = Thread::Current()); | |
26 ~BufferQueue(); | 32 ~BufferQueue(); |
27 | 33 |
28 // Return number of queued buffers. | 34 // Return number of queued buffers. |
29 size_t size() const; | 35 size_t size() const; |
30 | 36 |
31 // ReadFront will only read one buffer at a time and will truncate buffers | 37 // ReadFront will only read one buffer at a time and will truncate buffers |
32 // that don't fit in the passed memory. | 38 // that don't fit in the passed memory. |
39 // Returns true unless no data could be returned. | |
33 bool ReadFront(void* data, size_t bytes, size_t* bytes_read); | 40 bool ReadFront(void* data, size_t bytes, size_t* bytes_read); |
34 | 41 |
35 // WriteBack always writes either the complete memory or nothing. | 42 // WriteBack always writes either the complete memory or nothing. |
43 // Returns true unless no data could be written. | |
36 bool WriteBack(const void* data, size_t bytes, size_t* bytes_written); | 44 bool WriteBack(const void* data, size_t bytes, size_t* bytes_written); |
37 | 45 |
46 // Implementation of abstract StreamInterface methods. | |
47 | |
48 // A buffer queue is always "open". | |
49 StreamState GetState() const override { return SS_OPEN; } | |
50 | |
51 // Reading a buffer queue will either succeed or block. | |
52 StreamResult Read(void* buffer, size_t buffer_len, | |
53 size_t* read, int* error) override { | |
54 if (!ReadFront(buffer, buffer_len, read)) { | |
55 return SR_BLOCK; | |
56 } | |
57 return SR_SUCCESS; | |
58 } | |
59 | |
60 // Writing to a buffer queue will either succeed or block. | |
61 StreamResult Write(const void* data, size_t data_len, | |
62 size_t* written, int* error) override { | |
63 if (!WriteBack(data, data_len, written)) { | |
64 return SR_BLOCK; | |
65 } | |
66 return SR_SUCCESS; | |
67 } | |
68 | |
69 // A buffer queue can not be closed. | |
70 void Close() override {} | |
71 | |
38 private: | 72 private: |
39 size_t capacity_; | 73 size_t capacity_; |
40 size_t default_size_; | 74 size_t default_size_; |
41 std::deque<Buffer*> queue_; | 75 Thread* owner_thread_; |
42 std::vector<Buffer*> free_list_; | 76 mutable CriticalSection crit_; |
43 mutable CriticalSection crit_; // object lock | 77 std::deque<Buffer*> queue_ GUARDED_BY(crit_); |
78 std::vector<Buffer*> free_list_ GUARDED_BY(crit_); | |
44 | 79 |
45 RTC_DISALLOW_COPY_AND_ASSIGN(BufferQueue); | 80 RTC_DISALLOW_COPY_AND_ASSIGN(BufferQueue); |
46 }; | 81 }; |
47 | 82 |
48 } // namespace rtc | 83 } // namespace rtc |
49 | 84 |
50 #endif // WEBRTC_BASE_BUFFERQUEUE_H_ | 85 #endif // WEBRTC_BASE_BUFFERQUEUE_H_ |
OLD | NEW |