blob: 1b5f735180b6433858eec2a97edc073159598ccf [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"
Niels Möllerd5af4022019-03-05 08:56:48 +010031#include "common_types.h" // NOLINT(build/include)
Steve Anton10542f22019-01-11 09:11:00 -080032#include "rtc_base/copy_on_write_buffer.h"
Steve Anton10542f22019-01-11 09:11:00 -080033#include "rtc_base/network_route.h"
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040034
35namespace rtc {
36class PacketTransportInternal;
37class Thread;
38} // namespace rtc
39
40namespace webrtc {
41
Piotr (Peter) Slatala0c022502018-12-28 10:39:39 -080042class RtcEventLog;
43
Piotr (Peter) Slatala309aafe2019-01-15 14:24:34 -080044class AudioPacketReceivedObserver {
45 public:
46 virtual ~AudioPacketReceivedObserver() = default;
47
48 // Invoked for the first received audio packet on a given channel id.
49 // It will be invoked once for each channel id.
50 virtual void OnFirstAudioPacketReceived(int64_t channel_id) = 0;
51};
52
Piotr (Peter) Slatala48c54932019-01-28 06:50:38 -080053struct MediaTransportAllocatedBitrateLimits {
54 DataRate min_pacing_rate = DataRate::Zero();
55 DataRate max_padding_bitrate = DataRate::Zero();
56 DataRate max_total_allocated_bitrate = DataRate::Zero();
57};
58
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -070059// A collection of settings for creation of media transport.
60struct MediaTransportSettings final {
61 MediaTransportSettings();
Piotr (Peter) Slatalaed7b8b12018-10-29 10:43:16 -070062 MediaTransportSettings(const MediaTransportSettings&);
63 MediaTransportSettings& operator=(const MediaTransportSettings&);
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -070064 ~MediaTransportSettings();
65
66 // Group calls are not currently supported, in 1:1 call one side must set
67 // is_caller = true and another is_caller = false.
68 bool is_caller;
69
70 // Must be set if a pre-shared key is used for the call.
Piotr (Peter) Slatala9f956252018-10-31 08:25:26 -070071 // TODO(bugs.webrtc.org/9944): This should become zero buffer in the distant
72 // future.
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -070073 absl::optional<std::string> pre_shared_key;
Piotr (Peter) Slatala0c022502018-12-28 10:39:39 -080074
Piotr (Peter) Slatalad6f61dd2019-02-26 12:08:27 -080075 // If present, this is a config passed from the caller to the answerer in the
76 // offer. Each media transport knows how to understand its own parameters.
77 absl::optional<std::string> remote_transport_parameters;
78
Piotr (Peter) Slatala0c022502018-12-28 10:39:39 -080079 // If present, provides the event log that media transport should use.
80 // Media transport does not own it. The lifetime of |event_log| will exceed
81 // the lifetime of the instance of MediaTransportInterface instance.
82 RtcEventLog* event_log = nullptr;
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -070083};
84
Piotr (Peter) Slatalaada077f2018-11-08 07:43:31 -080085// Callback to notify about network route changes.
86class MediaTransportNetworkChangeCallback {
87 public:
88 virtual ~MediaTransportNetworkChangeCallback() = default;
89
90 // Called when the network route is changed, with the new network route.
91 virtual void OnNetworkRouteChanged(
92 const rtc::NetworkRoute& new_network_route) = 0;
93};
94
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -070095// State of the media transport. Media transport begins in the pending state.
96// It transitions to writable when it is ready to send media. It may transition
97// back to pending if the connection is blocked. It may transition to closed at
98// any time. Closed is terminal: a transport will never re-open once closed.
99enum class MediaTransportState {
100 kPending,
101 kWritable,
102 kClosed,
103};
104
105// Callback invoked whenever the state of the media transport changes.
106class MediaTransportStateCallback {
107 public:
108 virtual ~MediaTransportStateCallback() = default;
109
110 // Invoked whenever the state of the media transport changes.
111 virtual void OnStateChanged(MediaTransportState state) = 0;
112};
113
Niels Möller46879152019-01-07 15:54:47 +0100114// Callback for RTT measurements on the receive side.
115// TODO(nisse): Related interfaces: CallStatsObserver and RtcpRttStats. It's
116// somewhat unclear what type of measurement is needed. It's used to configure
117// NACK generation and playout buffer. Either raw measurement values or recent
118// maximum would make sense for this use. Need consolidation of RTT signalling.
119class MediaTransportRttObserver {
120 public:
121 virtual ~MediaTransportRttObserver() = default;
122
123 // Invoked when a new RTT measurement is available, typically once per ACK.
124 virtual void OnRttUpdated(int64_t rtt_ms) = 0;
125};
126
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700127// Supported types of application data messages.
128enum class DataMessageType {
129 // Application data buffer with the binary bit unset.
130 kText,
131
132 // Application data buffer with the binary bit set.
133 kBinary,
134
135 // Transport-agnostic control messages, such as open or open-ack messages.
136 kControl,
137};
138
139// Parameters for sending data. The parameters may change from message to
140// message, even within a single channel. For example, control messages may be
141// sent reliably and in-order, even if the data channel is configured for
142// unreliable delivery.
143struct SendDataParams {
144 SendDataParams();
Niels Möllere0446cb2018-11-30 09:35:52 +0100145 SendDataParams(const SendDataParams&);
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700146
147 DataMessageType type = DataMessageType::kText;
148
149 // Whether to deliver the message in order with respect to other ordered
150 // messages with the same channel_id.
151 bool ordered = false;
152
153 // If set, the maximum number of times this message may be
154 // retransmitted by the transport before it is dropped.
155 // Setting this value to zero disables retransmission.
156 // Must be non-negative. |max_rtx_count| and |max_rtx_ms| may not be set
157 // simultaneously.
158 absl::optional<int> max_rtx_count;
159
160 // If set, the maximum number of milliseconds for which the transport
161 // may retransmit this message before it is dropped.
162 // Setting this value to zero disables retransmission.
163 // Must be non-negative. |max_rtx_count| and |max_rtx_ms| may not be set
164 // simultaneously.
165 absl::optional<int> max_rtx_ms;
166};
167
168// Sink for callbacks related to a data channel.
169class DataChannelSink {
170 public:
171 virtual ~DataChannelSink() = default;
172
173 // Callback issued when data is received by the transport.
174 virtual void OnDataReceived(int channel_id,
175 DataMessageType type,
176 const rtc::CopyOnWriteBuffer& buffer) = 0;
177
178 // Callback issued when a remote data channel begins the closing procedure.
179 // Messages sent after the closing procedure begins will not be transmitted.
180 virtual void OnChannelClosing(int channel_id) = 0;
181
182 // Callback issued when a (remote or local) data channel completes the closing
183 // procedure. Closing channels become closed after all pending data has been
184 // transmitted.
185 virtual void OnChannelClosed(int channel_id) = 0;
186};
187
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400188// Media transport interface for sending / receiving encoded audio/video frames
189// and receiving bandwidth estimate update from congestion control.
190class MediaTransportInterface {
191 public:
Piotr (Peter) Slatala309aafe2019-01-15 14:24:34 -0800192 MediaTransportInterface();
193 virtual ~MediaTransportInterface();
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400194
Piotr (Peter) Slatalad6f61dd2019-02-26 12:08:27 -0800195 // Retrieves callers config (i.e. media transport offer) that should be passed
196 // to the callee, before the call is connected. Such config is opaque to SDP
197 // (sdp just passes it through). The config is a binary blob, so SDP may
198 // choose to use base64 to serialize it (or any other approach that guarantees
199 // that the binary blob goes through). This should only be called for the
200 // caller's perspective.
201 //
202 // This may return an unset optional, which means that the given media
203 // transport is not supported / disabled and shouldn't be reported in SDP.
204 //
205 // It may also return an empty string, in which case the media transport is
206 // supported, but without any extra settings.
207 // TODO(psla): Make abstract.
208 virtual absl::optional<std::string> GetTransportParametersOffer() const;
209
210 // Connect the media transport to the ICE transport.
211 // The implementation must be able to ignore incoming packets that don't
212 // belong to it.
213 // TODO(psla): Make abstract.
214 virtual void Connect(rtc::PacketTransportInternal* packet_transport);
215
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700216 // Start asynchronous send of audio frame. The status returned by this method
217 // only pertains to the synchronous operations (e.g.
218 // serialization/packetization), not to the asynchronous operation.
Sergey Silkine049eba2019-02-18 09:52:26 +0000219
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400220 virtual RTCError SendAudioFrame(uint64_t channel_id,
221 MediaTransportEncodedAudioFrame frame) = 0;
222
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700223 // Start asynchronous send of video frame. The status returned by this method
224 // only pertains to the synchronous operations (e.g.
225 // serialization/packetization), not to the asynchronous operation.
226 virtual RTCError SendVideoFrame(
227 uint64_t channel_id,
228 const MediaTransportEncodedVideoFrame& frame) = 0;
229
Niels Möller1c7f5f62018-12-10 11:06:02 +0100230 // Used by video sender to be notified on key frame requests.
231 virtual void SetKeyFrameRequestCallback(
232 MediaTransportKeyFrameRequestCallback* callback);
233
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700234 // Requests a keyframe for the particular channel (stream). The caller should
235 // check that the keyframe is not present in a jitter buffer already (i.e.
236 // don't request a keyframe if there is one that you will get from the jitter
237 // buffer in a moment).
238 virtual RTCError RequestKeyFrame(uint64_t channel_id) = 0;
239
240 // Sets audio sink. Sink must be unset by calling SetReceiveAudioSink(nullptr)
241 // before the media transport is destroyed or before new sink is set.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400242 virtual void SetReceiveAudioSink(MediaTransportAudioSinkInterface* sink) = 0;
243
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700244 // Registers a video sink. Before destruction of media transport, you must
245 // pass a nullptr.
246 virtual void SetReceiveVideoSink(MediaTransportVideoSinkInterface* sink) = 0;
247
Piotr (Peter) Slatalaada077f2018-11-08 07:43:31 -0800248 // Adds a target bitrate observer. Before media transport is destructed
249 // the observer must be unregistered (by calling
250 // RemoveTargetTransferRateObserver).
251 // A newly registered observer will be called back with the latest recorded
252 // target rate, if available.
253 virtual void AddTargetTransferRateObserver(
Niels Möller46879152019-01-07 15:54:47 +0100254 TargetTransferRateObserver* observer);
Piotr (Peter) Slatalaada077f2018-11-08 07:43:31 -0800255
256 // Removes an existing |observer| from observers. If observer was never
257 // registered, an error is logged and method does nothing.
258 virtual void RemoveTargetTransferRateObserver(
Niels Möller46879152019-01-07 15:54:47 +0100259 TargetTransferRateObserver* observer);
260
Piotr (Peter) Slatala309aafe2019-01-15 14:24:34 -0800261 // Sets audio packets observer, which gets informed about incoming audio
262 // packets. Before destruction, the observer must be unregistered by setting
263 // nullptr.
264 //
265 // This method may be temporary, when the multiplexer is implemented (or
266 // multiplexer may use it to demultiplex channel ids).
267 virtual void SetFirstAudioPacketReceivedObserver(
268 AudioPacketReceivedObserver* observer);
269
Niels Möller46879152019-01-07 15:54:47 +0100270 // Intended for receive side. AddRttObserver registers an observer to be
271 // called for each RTT measurement, typically once per ACK. Before media
272 // transport is destructed the observer must be unregistered.
273 virtual void AddRttObserver(MediaTransportRttObserver* observer);
274 virtual void RemoveRttObserver(MediaTransportRttObserver* observer);
Piotr (Peter) Slatalaada077f2018-11-08 07:43:31 -0800275
276 // Returns the last known target transfer rate as reported to the above
277 // observers.
278 virtual absl::optional<TargetTransferRate> GetLatestTargetTransferRate();
279
280 // Gets the audio packet overhead in bytes. Returned overhead does not include
281 // transport overhead (ipv4/6, turn channeldata, tcp/udp, etc.).
282 // If the transport is capable of fusing packets together, this overhead
283 // might not be a very accurate number.
Niels Möllerd5af4022019-03-05 08:56:48 +0100284 // TODO(nisse): Deprecated.
Piotr (Peter) Slatalaada077f2018-11-08 07:43:31 -0800285 virtual size_t GetAudioPacketOverhead() const;
286
Niels Möllerd5af4022019-03-05 08:56:48 +0100287 // Corresponding observers for audio and video overhead. Before destruction,
288 // the observers must be unregistered by setting nullptr.
289
290 // TODO(nisse): Should move to per-stream objects, since packetization
291 // overhead can vary per stream, e.g., depending on negotiated extensions. In
292 // addition, we should move towards reporting total overhead including all
293 // layers. Currently, overhead of the lower layers is reported elsewhere,
294 // e.g., on route change between IPv4 and IPv6.
295 virtual void SetAudioOverheadObserver(OverheadObserver* observer) {}
296
Niels Möllerd70a1142019-02-06 17:36:29 +0100297 // Registers an observer for network change events. If the network route is
298 // already established when the callback is added, |callback| will be called
299 // immediately with the current network route. Before media transport is
300 // destroyed, the callback must be removed.
Niels Möller30b182a2019-02-05 00:59:35 +0100301 virtual void AddNetworkChangeCallback(
302 MediaTransportNetworkChangeCallback* callback);
303 virtual void RemoveNetworkChangeCallback(
304 MediaTransportNetworkChangeCallback* callback);
Piotr (Peter) Slatala6b9d8232018-10-26 07:59:46 -0700305
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700306 // Sets a state observer callback. Before media transport is destroyed, the
307 // callback must be unregistered by setting it to nullptr.
308 // A newly registered callback will be called with the current state.
309 // Media transport does not invoke this callback concurrently.
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700310 virtual void SetMediaTransportStateCallback(
Bjorn Mellemeb2c6412018-10-31 15:25:32 -0700311 MediaTransportStateCallback* callback) = 0;
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700312
Piotr (Peter) Slatala48c54932019-01-28 06:50:38 -0800313 // Updates allocation limits.
314 // TODO(psla): Make abstract when downstream implementation implement it.
315 virtual void SetAllocatedBitrateLimits(
316 const MediaTransportAllocatedBitrateLimits& limits);
317
Bjorn Mellemf58e43e2019-02-22 10:31:48 -0800318 // Opens a data |channel_id| for sending. May return an error if the
319 // specified |channel_id| is unusable. Must be called before |SendData|.
Bjorn Mellem9ded4852019-02-28 12:27:11 -0800320 virtual RTCError OpenChannel(int channel_id) = 0;
Bjorn Mellemf58e43e2019-02-22 10:31:48 -0800321
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700322 // Sends a data buffer to the remote endpoint using the given send parameters.
323 // |buffer| may not be larger than 256 KiB. Returns an error if the send
324 // fails.
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700325 virtual RTCError SendData(int channel_id,
326 const SendDataParams& params,
Bjorn Mellemeb2c6412018-10-31 15:25:32 -0700327 const rtc::CopyOnWriteBuffer& buffer) = 0;
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700328
329 // Closes |channel_id| gracefully. Returns an error if |channel_id| is not
330 // open. Data sent after the closing procedure begins will not be
331 // transmitted. The channel becomes closed after pending data is transmitted.
Bjorn Mellemeb2c6412018-10-31 15:25:32 -0700332 virtual RTCError CloseChannel(int channel_id) = 0;
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700333
334 // Sets a sink for data messages and channel state callbacks. Before media
335 // transport is destroyed, the sink must be unregistered by setting it to
336 // nullptr.
Bjorn Mellemeb2c6412018-10-31 15:25:32 -0700337 virtual void SetDataSink(DataChannelSink* sink) = 0;
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700338
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400339 // TODO(sukhanov): RtcEventLogs.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400340};
341
342// If media transport factory is set in peer connection factory, it will be
343// used to create media transport for sending/receiving encoded frames and
344// this transport will be used instead of default RTP/SRTP transport.
345//
346// Currently Media Transport negotiation is not supported in SDP.
347// If application is using media transport, it must negotiate it before
348// setting media transport factory in peer connection.
349class MediaTransportFactory {
350 public:
351 virtual ~MediaTransportFactory() = default;
352
353 // Creates media transport.
354 // - Does not take ownership of packet_transport or network_thread.
355 // - Does not support group calls, in 1:1 call one side must set
356 // is_caller = true and another is_caller = false.
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -0700357 virtual RTCErrorOr<std::unique_ptr<MediaTransportInterface>>
358 CreateMediaTransport(rtc::PacketTransportInternal* packet_transport,
359 rtc::Thread* network_thread,
Piotr (Peter) Slatalaed7b8b12018-10-29 10:43:16 -0700360 const MediaTransportSettings& settings);
Piotr (Peter) Slatalad6f61dd2019-02-26 12:08:27 -0800361
362 // Creates a new Media Transport in a disconnected state. If the media
363 // transport for the caller is created, one can then call
364 // MediaTransportInterface::GetTransportParametersOffer on that new instance.
365 // TODO(psla): Make abstract.
366 virtual RTCErrorOr<std::unique_ptr<webrtc::MediaTransportInterface>>
367 CreateMediaTransport(rtc::Thread* network_thread,
368 const MediaTransportSettings& settings);
369
370 // Gets a transport name which is supported by the implementation.
371 // Different factories should return different transport names, and at runtime
372 // it will be checked that different names were used.
373 // For example, "rtp" or "generic" may be returned by two different
374 // implementations.
375 // The value returned by this method must never change in the lifetime of the
376 // factory.
377 // TODO(psla): Make abstract.
378 virtual std::string GetTransportName() const;
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400379};
380
381} // namespace webrtc
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400382#endif // API_MEDIA_TRANSPORT_INTERFACE_H_