blob: 0bc943ac362689bbcd25f70d42da7df431c01bf1 [file] [log] [blame]
Niels Möller13339482019-03-28 13:30:15 +01001/*
2 * Copyright 2019 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
11#ifndef RTC_BASE_MEMORY_FIFO_BUFFER_H_
12#define RTC_BASE_MEMORY_FIFO_BUFFER_H_
13
14#include <memory>
15
16#include "rtc_base/stream.h"
Markus Handell18523c32020-07-08 17:55:58 +020017#include "rtc_base/synchronization/mutex.h"
Tommi04482982020-10-05 12:43:53 +000018#include "rtc_base/task_utils/pending_task_safety_flag.h"
19#include "rtc_base/task_utils/to_queued_task.h"
Niels Möller13339482019-03-28 13:30:15 +010020
21namespace rtc {
22
23// FifoBuffer allows for efficient, thread-safe buffering of data between
24// writer and reader.
25class FifoBuffer final : public StreamInterface {
26 public:
27 // Creates a FIFO buffer with the specified capacity.
28 explicit FifoBuffer(size_t length);
29 // Creates a FIFO buffer with the specified capacity and owner
30 FifoBuffer(size_t length, Thread* owner);
31 ~FifoBuffer() override;
32 // Gets the amount of data currently readable from the buffer.
33 bool GetBuffered(size_t* data_len) const;
Niels Möller13339482019-03-28 13:30:15 +010034
35 // StreamInterface methods
36 StreamState GetState() const override;
37 StreamResult Read(void* buffer,
38 size_t bytes,
39 size_t* bytes_read,
40 int* error) override;
41 StreamResult Write(const void* buffer,
42 size_t bytes,
43 size_t* bytes_written,
44 int* error) override;
45 void Close() override;
46
47 // Seek to a byte offset from the beginning of the stream. Returns false if
48 // the stream does not support seeking, or cannot seek to the specified
49 // position.
50 bool SetPosition(size_t position);
51
52 // Get the byte offset of the current position from the start of the stream.
53 // Returns false if the position is not known.
54 bool GetPosition(size_t* position) const;
55
56 // Seek to the start of the stream.
57 bool Rewind() { return SetPosition(0); }
58
59 // GetReadData returns a pointer to a buffer which is owned by the stream.
60 // The buffer contains data_len bytes. null is returned if no data is
61 // available, or if the method fails. If the caller processes the data, it
62 // must call ConsumeReadData with the number of processed bytes. GetReadData
63 // does not require a matching call to ConsumeReadData if the data is not
64 // processed. Read and ConsumeReadData invalidate the buffer returned by
65 // GetReadData.
66 const void* GetReadData(size_t* data_len);
67 void ConsumeReadData(size_t used);
68 // GetWriteBuffer returns a pointer to a buffer which is owned by the stream.
69 // The buffer has a capacity of buf_len bytes. null is returned if there is
70 // no buffer available, or if the method fails. The call may write data to
71 // the buffer, and then call ConsumeWriteBuffer with the number of bytes
72 // written. GetWriteBuffer does not require a matching call to
73 // ConsumeWriteData if no data is written. Write and
74 // ConsumeWriteData invalidate the buffer returned by GetWriteBuffer.
75 void* GetWriteBuffer(size_t* buf_len);
76 void ConsumeWriteBuffer(size_t used);
77
Niels Möller13339482019-03-28 13:30:15 +010078 private:
Tommi04482982020-10-05 12:43:53 +000079 void PostEvent(int events, int err) {
80 owner_->PostTask(webrtc::ToQueuedTask(task_safety_, [this, events, err]() {
81 SignalEvent(this, events, err);
82 }));
83 }
84
Niels Mölleracf4f552021-09-27 11:51:17 +020085 // Helper method that implements Read. Caller must acquire a lock
Niels Möller13339482019-03-28 13:30:15 +010086 // when calling this method.
Niels Mölleracf4f552021-09-27 11:51:17 +020087 StreamResult ReadLocked(void* buffer, size_t bytes, size_t* bytes_read)
Markus Handell18523c32020-07-08 17:55:58 +020088 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
Niels Möller13339482019-03-28 13:30:15 +010089
Niels Mölleracf4f552021-09-27 11:51:17 +020090 // Helper method that implements Write. Caller must acquire a lock
Niels Möller13339482019-03-28 13:30:15 +010091 // when calling this method.
Niels Mölleracf4f552021-09-27 11:51:17 +020092 StreamResult WriteLocked(const void* buffer,
93 size_t bytes,
94 size_t* bytes_written)
Markus Handell18523c32020-07-08 17:55:58 +020095 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
Niels Möller13339482019-03-28 13:30:15 +010096
Tommi04482982020-10-05 12:43:53 +000097 webrtc::ScopedTaskSafety task_safety_;
98
Niels Möller13339482019-03-28 13:30:15 +010099 // keeps the opened/closed state of the stream
Markus Handell18523c32020-07-08 17:55:58 +0200100 StreamState state_ RTC_GUARDED_BY(mutex_);
Niels Möller13339482019-03-28 13:30:15 +0100101 // the allocated buffer
Markus Handell18523c32020-07-08 17:55:58 +0200102 std::unique_ptr<char[]> buffer_ RTC_GUARDED_BY(mutex_);
Niels Möller13339482019-03-28 13:30:15 +0100103 // size of the allocated buffer
Niels Mölleracf4f552021-09-27 11:51:17 +0200104 const size_t buffer_length_;
Niels Möller13339482019-03-28 13:30:15 +0100105 // amount of readable data in the buffer
Markus Handell18523c32020-07-08 17:55:58 +0200106 size_t data_length_ RTC_GUARDED_BY(mutex_);
Niels Möller13339482019-03-28 13:30:15 +0100107 // offset to the readable data
Markus Handell18523c32020-07-08 17:55:58 +0200108 size_t read_position_ RTC_GUARDED_BY(mutex_);
Niels Möller13339482019-03-28 13:30:15 +0100109 // stream callbacks are dispatched on this thread
Tommi04482982020-10-05 12:43:53 +0000110 Thread* const owner_;
Niels Möller13339482019-03-28 13:30:15 +0100111 // object lock
Markus Handell18523c32020-07-08 17:55:58 +0200112 mutable webrtc::Mutex mutex_;
Niels Möller13339482019-03-28 13:30:15 +0100113 RTC_DISALLOW_COPY_AND_ASSIGN(FifoBuffer);
114};
115
116} // namespace rtc
117
118#endif // RTC_BASE_MEMORY_FIFO_BUFFER_H_