blob: fa928fc9ae9237d798c03d6cd0701363d9f064e5 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2004 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_STREAM_H_
12#define RTC_BASE_STREAM_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <memory>
15
Harald Alvestrand1f609c82022-11-07 17:12:07 +000016#include "api/array_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/buffer.h"
Mirko Bonadei35214fc2019-09-23 14:54:28 +020018#include "rtc_base/system/rtc_export.h"
Artem Titove41c4332018-07-25 15:04:28 +020019#include "rtc_base/third_party/sigslot/sigslot.h"
Sebastian Jansson4db28b52020-01-08 14:07:15 +010020#include "rtc_base/thread.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020021
22namespace rtc {
23
24///////////////////////////////////////////////////////////////////////////////
25// StreamInterface is a generic asynchronous stream interface, supporting read,
26// write, and close operations, and asynchronous signalling of state changes.
27// The interface is designed with file, memory, and socket implementations in
28// mind. Some implementations offer extended operations, such as seeking.
29///////////////////////////////////////////////////////////////////////////////
30
31// The following enumerations are declared outside of the StreamInterface
32// class for brevity in use.
33
34// The SS_OPENING state indicates that the stream will signal open or closed
35// in the future.
36enum StreamState { SS_CLOSED, SS_OPENING, SS_OPEN };
37
38// Stream read/write methods return this value to indicate various success
39// and failure conditions described below.
40enum StreamResult { SR_ERROR, SR_SUCCESS, SR_BLOCK, SR_EOS };
41
42// StreamEvents are used to asynchronously signal state transitionss. The flags
43// may be combined.
44// SE_OPEN: The stream has transitioned to the SS_OPEN state
45// SE_CLOSE: The stream has transitioned to the SS_CLOSED state
46// SE_READ: Data is available, so Read is likely to not return SR_BLOCK
47// SE_WRITE: Data can be written, so Write is likely to not return SR_BLOCK
48enum StreamEvent { SE_OPEN = 1, SE_READ = 2, SE_WRITE = 4, SE_CLOSE = 8 };
49
Tommi04482982020-10-05 12:43:53 +000050class RTC_EXPORT StreamInterface {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020051 public:
Tommi04482982020-10-05 12:43:53 +000052 virtual ~StreamInterface() {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020053
Byoungchan Lee14af7622022-01-12 05:24:58 +090054 StreamInterface(const StreamInterface&) = delete;
55 StreamInterface& operator=(const StreamInterface&) = delete;
56
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020057 virtual StreamState GetState() const = 0;
58
59 // Read attempts to fill buffer of size buffer_len. Write attempts to send
60 // data_len bytes stored in data. The variables read and write are set only
61 // on SR_SUCCESS (see below). Likewise, error is only set on SR_ERROR.
62 // Read and Write return a value indicating:
63 // SR_ERROR: an error occurred, which is returned in a non-null error
64 // argument. Interpretation of the error requires knowledge of the
65 // stream's concrete type, which limits its usefulness.
66 // SR_SUCCESS: some number of bytes were successfully written, which is
67 // returned in a non-null read/write argument.
68 // SR_BLOCK: the stream is in non-blocking mode, and the operation would
69 // block, or the stream is in SS_OPENING state.
70 // SR_EOS: the end-of-stream has been reached, or the stream is in the
71 // SS_CLOSED state.
Harald Alvestrand11840ce2022-11-10 10:50:50 +000072
73 // The deprecated method has a default implementation that may be
74 // overridden in subclasses, rather than being =0.
75 // This allows subclasses to delete the method.
76 // TODO(bugs.webrtc.org/14632): Remove when downstream is converted.
77 [[deprecated("Use ArrayView version")]] virtual StreamResult
78 Read(void* buffer, size_t buffer_len, size_t* read, int* error) {
79 RTC_CHECK_NOTREACHED();
80 }
81
82#pragma clang diagnostic push
83#pragma clang diagnostic ignored "-Wdeprecated-declarations"
84 // Preserve backwards compatibility using a default implementation
85 // because there are subclasses
86 // outside of the WebRTC codebase that need to be converted.
87 //
88 // TODO(bugs.webrtc.org/14632): Remove when downstream is converted.
Harald Alvestrand1f609c82022-11-07 17:12:07 +000089 virtual StreamResult Read(rtc::ArrayView<uint8_t> buffer,
90 size_t& read,
91 int& error) {
92 return Read(buffer.data(), buffer.size(), &read, &error);
93 }
Harald Alvestrand11840ce2022-11-10 10:50:50 +000094#pragma clang diagnostic pop
95
96 // The deprecated method has a default implementation that may be
97 // overridden in subclasses, rather than being =0.
98 // This allows subclasses to delete the method.
99 // TODO(bugs.webrtc.org/14632): Remove when downstream is converted.
100 [[deprecated("Use ArrayView version")]] virtual StreamResult
101 Write(const void* data, size_t data_len, size_t* written, int* error) {
102 RTC_CHECK_NOTREACHED();
103 }
104
105#pragma clang diagnostic push
106#pragma clang diagnostic ignored "-Wdeprecated-declarations"
Harald Alvestrand1f609c82022-11-07 17:12:07 +0000107 virtual StreamResult Write(rtc::ArrayView<const uint8_t> data,
108 size_t& written,
109 int& error) {
110 return Write(data.data(), data.size(), &written, &error);
111 }
Harald Alvestrand11840ce2022-11-10 10:50:50 +0000112#pragma clang diagnostic pop
113
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200114 // Attempt to transition to the SS_CLOSED state. SE_CLOSE will not be
115 // signalled as a result of this call.
116 virtual void Close() = 0;
117
118 // Streams may signal one or more StreamEvents to indicate state changes.
119 // The first argument identifies the stream on which the state change occured.
120 // The second argument is a bit-wise combination of StreamEvents.
121 // If SE_CLOSE is signalled, then the third argument is the associated error
122 // code. Otherwise, the value is undefined.
123 // Note: Not all streams will support asynchronous event signalling. However,
124 // SS_OPENING and SR_BLOCK returned from stream member functions imply that
125 // certain events will be raised in the future.
126 sigslot::signal3<StreamInterface*, int, int> SignalEvent;
127
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200128 // Return true if flush is successful.
129 virtual bool Flush();
130
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200131 //
132 // CONVENIENCE METHODS
133 //
134 // These methods are implemented in terms of other methods, for convenience.
135 //
136
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200137 // WriteAll is a helper function which repeatedly calls Write until all the
138 // data is written, or something other than SR_SUCCESS is returned. Note that
139 // unlike Write, the argument 'written' is always set, and may be non-zero
140 // on results other than SR_SUCCESS. The remaining arguments have the
141 // same semantics as Write.
Harald Alvestrand11840ce2022-11-10 10:50:50 +0000142 [[deprecated("Use version with ArrayView")]] StreamResult
143 WriteAll(const void* data, size_t data_len, size_t* written, int* error);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200144
Harald Alvestrand11840ce2022-11-10 10:50:50 +0000145#pragma clang diagnostic push
146#pragma clang diagnostic ignored "-Wdeprecated-declarations"
147 // TODO(bugs.webrc.org/14632): Remove pragmas and change underlying
148 // implementation when downstream code is converted.
149 StreamResult WriteAll(ArrayView<const uint8_t> data,
150 size_t& written,
151 int& error) {
Harald Alvestrand1f609c82022-11-07 17:12:07 +0000152 return WriteAll(data.data(), data.size(), &written, &error);
153 }
Harald Alvestrand11840ce2022-11-10 10:50:50 +0000154#pragma clang diagnostic pop
Harald Alvestrand1f609c82022-11-07 17:12:07 +0000155
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200156 protected:
157 StreamInterface();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200158};
159
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200160} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000161
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200162#endif // RTC_BASE_STREAM_H_