blob: e753ddc092bca5bdc44f89480ddf451495bb405b [file] [log] [blame]
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -07001/* Copyright 2018 The WebRTC project authors. All Rights Reserved.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -04002 *
3 * Use of this source code is governed by a BSD-style license
4 * that can be found in the LICENSE file in the root of the source
5 * tree. An additional intellectual property rights grant can be found
6 * in the file PATENTS. All contributing project authors may
7 * be found in the AUTHORS file in the root of the source tree.
8 */
9
10// This is EXPERIMENTAL interface for media transport.
11//
12// The goal is to refactor WebRTC code so that audio and video frames
13// are sent / received through the media transport interface. This will
14// enable different media transport implementations, including QUIC-based
15// media transport.
16
17#ifndef API_MEDIA_TRANSPORT_INTERFACE_H_
18#define API_MEDIA_TRANSPORT_INTERFACE_H_
19
Piotr (Peter) Slatala6b9d8232018-10-26 07:59:46 -070020#include <api/transport/network_control.h>
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040021#include <memory>
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -070022#include <string>
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040023#include <utility>
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040024
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -070025#include "absl/types/optional.h"
Yves Gerey988cc082018-10-23 12:03:01 +020026#include "api/array_view.h"
Steve Anton10542f22019-01-11 09:11:00 -080027#include "api/rtc_error.h"
Niels Möllerec3b9ff2019-02-08 00:28:39 +010028#include "api/transport/media/audio_transport.h"
Niels Möller7e0e44f2019-02-12 14:04:11 +010029#include "api/transport/media/video_transport.h"
Piotr (Peter) Slatala48c54932019-01-28 06:50:38 -080030#include "api/units/data_rate.h"
Steve Anton10542f22019-01-11 09:11:00 -080031#include "rtc_base/copy_on_write_buffer.h"
Steve Anton10542f22019-01-11 09:11:00 -080032#include "rtc_base/network_route.h"
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040033
34namespace rtc {
35class PacketTransportInternal;
36class Thread;
37} // namespace rtc
38
39namespace webrtc {
40
Piotr (Peter) Slatala0c022502018-12-28 10:39:39 -080041class RtcEventLog;
42
Piotr (Peter) Slatala309aafe2019-01-15 14:24:34 -080043class AudioPacketReceivedObserver {
44 public:
45 virtual ~AudioPacketReceivedObserver() = default;
46
47 // Invoked for the first received audio packet on a given channel id.
48 // It will be invoked once for each channel id.
49 virtual void OnFirstAudioPacketReceived(int64_t channel_id) = 0;
50};
51
Piotr (Peter) Slatala48c54932019-01-28 06:50:38 -080052struct MediaTransportAllocatedBitrateLimits {
53 DataRate min_pacing_rate = DataRate::Zero();
54 DataRate max_padding_bitrate = DataRate::Zero();
55 DataRate max_total_allocated_bitrate = DataRate::Zero();
56};
57
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -070058// A collection of settings for creation of media transport.
59struct MediaTransportSettings final {
60 MediaTransportSettings();
Piotr (Peter) Slatalaed7b8b12018-10-29 10:43:16 -070061 MediaTransportSettings(const MediaTransportSettings&);
62 MediaTransportSettings& operator=(const MediaTransportSettings&);
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -070063 ~MediaTransportSettings();
64
65 // Group calls are not currently supported, in 1:1 call one side must set
66 // is_caller = true and another is_caller = false.
67 bool is_caller;
68
69 // Must be set if a pre-shared key is used for the call.
Piotr (Peter) Slatala9f956252018-10-31 08:25:26 -070070 // TODO(bugs.webrtc.org/9944): This should become zero buffer in the distant
71 // future.
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -070072 absl::optional<std::string> pre_shared_key;
Piotr (Peter) Slatala0c022502018-12-28 10:39:39 -080073
74 // If present, provides the event log that media transport should use.
75 // Media transport does not own it. The lifetime of |event_log| will exceed
76 // the lifetime of the instance of MediaTransportInterface instance.
77 RtcEventLog* event_log = nullptr;
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -070078};
79
Piotr (Peter) Slatalaada077f2018-11-08 07:43:31 -080080// Callback to notify about network route changes.
81class MediaTransportNetworkChangeCallback {
82 public:
83 virtual ~MediaTransportNetworkChangeCallback() = default;
84
85 // Called when the network route is changed, with the new network route.
86 virtual void OnNetworkRouteChanged(
87 const rtc::NetworkRoute& new_network_route) = 0;
88};
89
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -070090// State of the media transport. Media transport begins in the pending state.
91// It transitions to writable when it is ready to send media. It may transition
92// back to pending if the connection is blocked. It may transition to closed at
93// any time. Closed is terminal: a transport will never re-open once closed.
94enum class MediaTransportState {
95 kPending,
96 kWritable,
97 kClosed,
98};
99
100// Callback invoked whenever the state of the media transport changes.
101class MediaTransportStateCallback {
102 public:
103 virtual ~MediaTransportStateCallback() = default;
104
105 // Invoked whenever the state of the media transport changes.
106 virtual void OnStateChanged(MediaTransportState state) = 0;
107};
108
Niels Möller46879152019-01-07 15:54:47 +0100109// Callback for RTT measurements on the receive side.
110// TODO(nisse): Related interfaces: CallStatsObserver and RtcpRttStats. It's
111// somewhat unclear what type of measurement is needed. It's used to configure
112// NACK generation and playout buffer. Either raw measurement values or recent
113// maximum would make sense for this use. Need consolidation of RTT signalling.
114class MediaTransportRttObserver {
115 public:
116 virtual ~MediaTransportRttObserver() = default;
117
118 // Invoked when a new RTT measurement is available, typically once per ACK.
119 virtual void OnRttUpdated(int64_t rtt_ms) = 0;
120};
121
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700122// Supported types of application data messages.
123enum class DataMessageType {
124 // Application data buffer with the binary bit unset.
125 kText,
126
127 // Application data buffer with the binary bit set.
128 kBinary,
129
130 // Transport-agnostic control messages, such as open or open-ack messages.
131 kControl,
132};
133
134// Parameters for sending data. The parameters may change from message to
135// message, even within a single channel. For example, control messages may be
136// sent reliably and in-order, even if the data channel is configured for
137// unreliable delivery.
138struct SendDataParams {
139 SendDataParams();
Niels Möllere0446cb2018-11-30 09:35:52 +0100140 SendDataParams(const SendDataParams&);
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700141
142 DataMessageType type = DataMessageType::kText;
143
144 // Whether to deliver the message in order with respect to other ordered
145 // messages with the same channel_id.
146 bool ordered = false;
147
148 // If set, the maximum number of times this message may be
149 // retransmitted by the transport before it is dropped.
150 // Setting this value to zero disables retransmission.
151 // Must be non-negative. |max_rtx_count| and |max_rtx_ms| may not be set
152 // simultaneously.
153 absl::optional<int> max_rtx_count;
154
155 // If set, the maximum number of milliseconds for which the transport
156 // may retransmit this message before it is dropped.
157 // Setting this value to zero disables retransmission.
158 // Must be non-negative. |max_rtx_count| and |max_rtx_ms| may not be set
159 // simultaneously.
160 absl::optional<int> max_rtx_ms;
161};
162
163// Sink for callbacks related to a data channel.
164class DataChannelSink {
165 public:
166 virtual ~DataChannelSink() = default;
167
168 // Callback issued when data is received by the transport.
169 virtual void OnDataReceived(int channel_id,
170 DataMessageType type,
171 const rtc::CopyOnWriteBuffer& buffer) = 0;
172
173 // Callback issued when a remote data channel begins the closing procedure.
174 // Messages sent after the closing procedure begins will not be transmitted.
175 virtual void OnChannelClosing(int channel_id) = 0;
176
177 // Callback issued when a (remote or local) data channel completes the closing
178 // procedure. Closing channels become closed after all pending data has been
179 // transmitted.
180 virtual void OnChannelClosed(int channel_id) = 0;
181};
182
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400183// Media transport interface for sending / receiving encoded audio/video frames
184// and receiving bandwidth estimate update from congestion control.
185class MediaTransportInterface {
186 public:
Piotr (Peter) Slatala309aafe2019-01-15 14:24:34 -0800187 MediaTransportInterface();
188 virtual ~MediaTransportInterface();
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400189
Niels Möller0d8eed62019-02-18 09:10:30 +0100190 // Creates an object representing the send end-point of a audio stream using
191 // this transport.
192 // TODO(bugs.webrtc.org/9719): Make pure virtual after downstream
193 // implementations are updated.
194 virtual std::unique_ptr<MediaTransportAudioSender> CreateAudioSender(
195 uint64_t channel_id);
196
197 // Creates an object representing the receive end-point of a audio stream
198 // using this transport.
199 // TODO(bugs.webrtc.org/9719): Make pure virtual after downstream
200 // implementations are updated.
201 virtual std::unique_ptr<MediaTransportAudioReceiver> CreateAudioReceiver(
202 uint64_t channel_id,
203 // TODO(nisse): Add Rtt observer, or route that via Call to the receive
204 // stream instead?
205 MediaTransportAudioSinkInterface* sink);
206
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700207 // Start asynchronous send of audio frame. The status returned by this method
208 // only pertains to the synchronous operations (e.g.
209 // serialization/packetization), not to the asynchronous operation.
Niels Möller0d8eed62019-02-18 09:10:30 +0100210 // TODO(nisse): Deprecated, should be deleted when implementations are updated
211 // to use CreateAudioSender.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400212 virtual RTCError SendAudioFrame(uint64_t channel_id,
213 MediaTransportEncodedAudioFrame frame) = 0;
214
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700215 // Start asynchronous send of video frame. The status returned by this method
216 // only pertains to the synchronous operations (e.g.
217 // serialization/packetization), not to the asynchronous operation.
218 virtual RTCError SendVideoFrame(
219 uint64_t channel_id,
220 const MediaTransportEncodedVideoFrame& frame) = 0;
221
Niels Möller1c7f5f62018-12-10 11:06:02 +0100222 // Used by video sender to be notified on key frame requests.
223 virtual void SetKeyFrameRequestCallback(
224 MediaTransportKeyFrameRequestCallback* callback);
225
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700226 // Requests a keyframe for the particular channel (stream). The caller should
227 // check that the keyframe is not present in a jitter buffer already (i.e.
228 // don't request a keyframe if there is one that you will get from the jitter
229 // buffer in a moment).
230 virtual RTCError RequestKeyFrame(uint64_t channel_id) = 0;
231
232 // Sets audio sink. Sink must be unset by calling SetReceiveAudioSink(nullptr)
233 // before the media transport is destroyed or before new sink is set.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400234 virtual void SetReceiveAudioSink(MediaTransportAudioSinkInterface* sink) = 0;
235
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700236 // Registers a video sink. Before destruction of media transport, you must
237 // pass a nullptr.
238 virtual void SetReceiveVideoSink(MediaTransportVideoSinkInterface* sink) = 0;
239
Piotr (Peter) Slatalaada077f2018-11-08 07:43:31 -0800240 // Adds a target bitrate observer. Before media transport is destructed
241 // the observer must be unregistered (by calling
242 // RemoveTargetTransferRateObserver).
243 // A newly registered observer will be called back with the latest recorded
244 // target rate, if available.
245 virtual void AddTargetTransferRateObserver(
Niels Möller46879152019-01-07 15:54:47 +0100246 TargetTransferRateObserver* observer);
Piotr (Peter) Slatalaada077f2018-11-08 07:43:31 -0800247
248 // Removes an existing |observer| from observers. If observer was never
249 // registered, an error is logged and method does nothing.
250 virtual void RemoveTargetTransferRateObserver(
Niels Möller46879152019-01-07 15:54:47 +0100251 TargetTransferRateObserver* observer);
252
Piotr (Peter) Slatala309aafe2019-01-15 14:24:34 -0800253 // Sets audio packets observer, which gets informed about incoming audio
254 // packets. Before destruction, the observer must be unregistered by setting
255 // nullptr.
256 //
257 // This method may be temporary, when the multiplexer is implemented (or
258 // multiplexer may use it to demultiplex channel ids).
259 virtual void SetFirstAudioPacketReceivedObserver(
260 AudioPacketReceivedObserver* observer);
261
Niels Möller46879152019-01-07 15:54:47 +0100262 // Intended for receive side. AddRttObserver registers an observer to be
263 // called for each RTT measurement, typically once per ACK. Before media
264 // transport is destructed the observer must be unregistered.
265 virtual void AddRttObserver(MediaTransportRttObserver* observer);
266 virtual void RemoveRttObserver(MediaTransportRttObserver* observer);
Piotr (Peter) Slatalaada077f2018-11-08 07:43:31 -0800267
268 // Returns the last known target transfer rate as reported to the above
269 // observers.
270 virtual absl::optional<TargetTransferRate> GetLatestTargetTransferRate();
271
272 // Gets the audio packet overhead in bytes. Returned overhead does not include
273 // transport overhead (ipv4/6, turn channeldata, tcp/udp, etc.).
274 // If the transport is capable of fusing packets together, this overhead
275 // might not be a very accurate number.
276 virtual size_t GetAudioPacketOverhead() const;
277
Niels Möllerd70a1142019-02-06 17:36:29 +0100278 // Registers an observer for network change events. If the network route is
279 // already established when the callback is added, |callback| will be called
280 // immediately with the current network route. Before media transport is
281 // destroyed, the callback must be removed.
Niels Möller30b182a2019-02-05 00:59:35 +0100282 virtual void AddNetworkChangeCallback(
283 MediaTransportNetworkChangeCallback* callback);
284 virtual void RemoveNetworkChangeCallback(
285 MediaTransportNetworkChangeCallback* callback);
Piotr (Peter) Slatala6b9d8232018-10-26 07:59:46 -0700286
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700287 // Sets a state observer callback. Before media transport is destroyed, the
288 // callback must be unregistered by setting it to nullptr.
289 // A newly registered callback will be called with the current state.
290 // Media transport does not invoke this callback concurrently.
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700291 virtual void SetMediaTransportStateCallback(
Bjorn Mellemeb2c6412018-10-31 15:25:32 -0700292 MediaTransportStateCallback* callback) = 0;
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700293
Piotr (Peter) Slatala48c54932019-01-28 06:50:38 -0800294 // Updates allocation limits.
295 // TODO(psla): Make abstract when downstream implementation implement it.
296 virtual void SetAllocatedBitrateLimits(
297 const MediaTransportAllocatedBitrateLimits& limits);
298
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700299 // Sends a data buffer to the remote endpoint using the given send parameters.
300 // |buffer| may not be larger than 256 KiB. Returns an error if the send
301 // fails.
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700302 virtual RTCError SendData(int channel_id,
303 const SendDataParams& params,
Bjorn Mellemeb2c6412018-10-31 15:25:32 -0700304 const rtc::CopyOnWriteBuffer& buffer) = 0;
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700305
306 // Closes |channel_id| gracefully. Returns an error if |channel_id| is not
307 // open. Data sent after the closing procedure begins will not be
308 // transmitted. The channel becomes closed after pending data is transmitted.
Bjorn Mellemeb2c6412018-10-31 15:25:32 -0700309 virtual RTCError CloseChannel(int channel_id) = 0;
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700310
311 // Sets a sink for data messages and channel state callbacks. Before media
312 // transport is destroyed, the sink must be unregistered by setting it to
313 // nullptr.
Bjorn Mellemeb2c6412018-10-31 15:25:32 -0700314 virtual void SetDataSink(DataChannelSink* sink) = 0;
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700315
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400316 // TODO(sukhanov): RtcEventLogs.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400317};
318
319// If media transport factory is set in peer connection factory, it will be
320// used to create media transport for sending/receiving encoded frames and
321// this transport will be used instead of default RTP/SRTP transport.
322//
323// Currently Media Transport negotiation is not supported in SDP.
324// If application is using media transport, it must negotiate it before
325// setting media transport factory in peer connection.
326class MediaTransportFactory {
327 public:
328 virtual ~MediaTransportFactory() = default;
329
330 // Creates media transport.
331 // - Does not take ownership of packet_transport or network_thread.
332 // - Does not support group calls, in 1:1 call one side must set
333 // is_caller = true and another is_caller = false.
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -0700334 // TODO(bugs.webrtc.org/9938) This constructor will be removed and replaced
335 // with the one below.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400336 virtual RTCErrorOr<std::unique_ptr<MediaTransportInterface>>
337 CreateMediaTransport(rtc::PacketTransportInternal* packet_transport,
338 rtc::Thread* network_thread,
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -0700339 bool is_caller);
340
341 // Creates media transport.
342 // - Does not take ownership of packet_transport or network_thread.
343 // TODO(bugs.webrtc.org/9938): remove default implementation once all children
344 // override it.
345 virtual RTCErrorOr<std::unique_ptr<MediaTransportInterface>>
346 CreateMediaTransport(rtc::PacketTransportInternal* packet_transport,
347 rtc::Thread* network_thread,
Piotr (Peter) Slatalaed7b8b12018-10-29 10:43:16 -0700348 const MediaTransportSettings& settings);
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400349};
350
351} // namespace webrtc
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400352#endif // API_MEDIA_TRANSPORT_INTERFACE_H_