blob: d333bec5bf854825ecf91f9a37a9a87ca7e99517 [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
Piotr (Peter) Slatalad6f61dd2019-02-26 12:08:27 -080074 // If present, this is a config passed from the caller to the answerer in the
75 // offer. Each media transport knows how to understand its own parameters.
76 absl::optional<std::string> remote_transport_parameters;
77
Piotr (Peter) Slatala0c022502018-12-28 10:39:39 -080078 // If present, provides the event log that media transport should use.
79 // Media transport does not own it. The lifetime of |event_log| will exceed
80 // the lifetime of the instance of MediaTransportInterface instance.
81 RtcEventLog* event_log = nullptr;
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -070082};
83
Piotr (Peter) Slatalaada077f2018-11-08 07:43:31 -080084// Callback to notify about network route changes.
85class MediaTransportNetworkChangeCallback {
86 public:
87 virtual ~MediaTransportNetworkChangeCallback() = default;
88
89 // Called when the network route is changed, with the new network route.
90 virtual void OnNetworkRouteChanged(
91 const rtc::NetworkRoute& new_network_route) = 0;
92};
93
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -070094// State of the media transport. Media transport begins in the pending state.
95// It transitions to writable when it is ready to send media. It may transition
96// back to pending if the connection is blocked. It may transition to closed at
97// any time. Closed is terminal: a transport will never re-open once closed.
98enum class MediaTransportState {
99 kPending,
100 kWritable,
101 kClosed,
102};
103
104// Callback invoked whenever the state of the media transport changes.
105class MediaTransportStateCallback {
106 public:
107 virtual ~MediaTransportStateCallback() = default;
108
109 // Invoked whenever the state of the media transport changes.
110 virtual void OnStateChanged(MediaTransportState state) = 0;
111};
112
Niels Möller46879152019-01-07 15:54:47 +0100113// Callback for RTT measurements on the receive side.
114// TODO(nisse): Related interfaces: CallStatsObserver and RtcpRttStats. It's
115// somewhat unclear what type of measurement is needed. It's used to configure
116// NACK generation and playout buffer. Either raw measurement values or recent
117// maximum would make sense for this use. Need consolidation of RTT signalling.
118class MediaTransportRttObserver {
119 public:
120 virtual ~MediaTransportRttObserver() = default;
121
122 // Invoked when a new RTT measurement is available, typically once per ACK.
123 virtual void OnRttUpdated(int64_t rtt_ms) = 0;
124};
125
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700126// Supported types of application data messages.
127enum class DataMessageType {
128 // Application data buffer with the binary bit unset.
129 kText,
130
131 // Application data buffer with the binary bit set.
132 kBinary,
133
134 // Transport-agnostic control messages, such as open or open-ack messages.
135 kControl,
136};
137
138// Parameters for sending data. The parameters may change from message to
139// message, even within a single channel. For example, control messages may be
140// sent reliably and in-order, even if the data channel is configured for
141// unreliable delivery.
142struct SendDataParams {
143 SendDataParams();
Niels Möllere0446cb2018-11-30 09:35:52 +0100144 SendDataParams(const SendDataParams&);
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700145
146 DataMessageType type = DataMessageType::kText;
147
148 // Whether to deliver the message in order with respect to other ordered
149 // messages with the same channel_id.
150 bool ordered = false;
151
152 // If set, the maximum number of times this message may be
153 // retransmitted by the transport before it is dropped.
154 // Setting this value to zero disables retransmission.
155 // Must be non-negative. |max_rtx_count| and |max_rtx_ms| may not be set
156 // simultaneously.
157 absl::optional<int> max_rtx_count;
158
159 // If set, the maximum number of milliseconds for which the transport
160 // may retransmit this message before it is dropped.
161 // Setting this value to zero disables retransmission.
162 // Must be non-negative. |max_rtx_count| and |max_rtx_ms| may not be set
163 // simultaneously.
164 absl::optional<int> max_rtx_ms;
165};
166
167// Sink for callbacks related to a data channel.
168class DataChannelSink {
169 public:
170 virtual ~DataChannelSink() = default;
171
172 // Callback issued when data is received by the transport.
173 virtual void OnDataReceived(int channel_id,
174 DataMessageType type,
175 const rtc::CopyOnWriteBuffer& buffer) = 0;
176
177 // Callback issued when a remote data channel begins the closing procedure.
178 // Messages sent after the closing procedure begins will not be transmitted.
179 virtual void OnChannelClosing(int channel_id) = 0;
180
181 // Callback issued when a (remote or local) data channel completes the closing
182 // procedure. Closing channels become closed after all pending data has been
183 // transmitted.
184 virtual void OnChannelClosed(int channel_id) = 0;
185};
186
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400187// Media transport interface for sending / receiving encoded audio/video frames
188// and receiving bandwidth estimate update from congestion control.
189class MediaTransportInterface {
190 public:
Piotr (Peter) Slatala309aafe2019-01-15 14:24:34 -0800191 MediaTransportInterface();
192 virtual ~MediaTransportInterface();
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400193
Piotr (Peter) Slatalad6f61dd2019-02-26 12:08:27 -0800194 // Retrieves callers config (i.e. media transport offer) that should be passed
195 // to the callee, before the call is connected. Such config is opaque to SDP
196 // (sdp just passes it through). The config is a binary blob, so SDP may
197 // choose to use base64 to serialize it (or any other approach that guarantees
198 // that the binary blob goes through). This should only be called for the
199 // caller's perspective.
200 //
201 // This may return an unset optional, which means that the given media
202 // transport is not supported / disabled and shouldn't be reported in SDP.
203 //
204 // It may also return an empty string, in which case the media transport is
205 // supported, but without any extra settings.
206 // TODO(psla): Make abstract.
207 virtual absl::optional<std::string> GetTransportParametersOffer() const;
208
209 // Connect the media transport to the ICE transport.
210 // The implementation must be able to ignore incoming packets that don't
211 // belong to it.
212 // TODO(psla): Make abstract.
213 virtual void Connect(rtc::PacketTransportInternal* packet_transport);
214
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700215 // Start asynchronous send of audio frame. The status returned by this method
216 // only pertains to the synchronous operations (e.g.
217 // serialization/packetization), not to the asynchronous operation.
Sergey Silkine049eba2019-02-18 09:52:26 +0000218
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400219 virtual RTCError SendAudioFrame(uint64_t channel_id,
220 MediaTransportEncodedAudioFrame frame) = 0;
221
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700222 // Start asynchronous send of video frame. The status returned by this method
223 // only pertains to the synchronous operations (e.g.
224 // serialization/packetization), not to the asynchronous operation.
225 virtual RTCError SendVideoFrame(
226 uint64_t channel_id,
227 const MediaTransportEncodedVideoFrame& frame) = 0;
228
Niels Möller1c7f5f62018-12-10 11:06:02 +0100229 // Used by video sender to be notified on key frame requests.
230 virtual void SetKeyFrameRequestCallback(
231 MediaTransportKeyFrameRequestCallback* callback);
232
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700233 // Requests a keyframe for the particular channel (stream). The caller should
234 // check that the keyframe is not present in a jitter buffer already (i.e.
235 // don't request a keyframe if there is one that you will get from the jitter
236 // buffer in a moment).
237 virtual RTCError RequestKeyFrame(uint64_t channel_id) = 0;
238
239 // Sets audio sink. Sink must be unset by calling SetReceiveAudioSink(nullptr)
240 // before the media transport is destroyed or before new sink is set.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400241 virtual void SetReceiveAudioSink(MediaTransportAudioSinkInterface* sink) = 0;
242
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700243 // Registers a video sink. Before destruction of media transport, you must
244 // pass a nullptr.
245 virtual void SetReceiveVideoSink(MediaTransportVideoSinkInterface* sink) = 0;
246
Piotr (Peter) Slatalaada077f2018-11-08 07:43:31 -0800247 // Adds a target bitrate observer. Before media transport is destructed
248 // the observer must be unregistered (by calling
249 // RemoveTargetTransferRateObserver).
250 // A newly registered observer will be called back with the latest recorded
251 // target rate, if available.
252 virtual void AddTargetTransferRateObserver(
Niels Möller46879152019-01-07 15:54:47 +0100253 TargetTransferRateObserver* observer);
Piotr (Peter) Slatalaada077f2018-11-08 07:43:31 -0800254
255 // Removes an existing |observer| from observers. If observer was never
256 // registered, an error is logged and method does nothing.
257 virtual void RemoveTargetTransferRateObserver(
Niels Möller46879152019-01-07 15:54:47 +0100258 TargetTransferRateObserver* observer);
259
Piotr (Peter) Slatala309aafe2019-01-15 14:24:34 -0800260 // Sets audio packets observer, which gets informed about incoming audio
261 // packets. Before destruction, the observer must be unregistered by setting
262 // nullptr.
263 //
264 // This method may be temporary, when the multiplexer is implemented (or
265 // multiplexer may use it to demultiplex channel ids).
266 virtual void SetFirstAudioPacketReceivedObserver(
267 AudioPacketReceivedObserver* observer);
268
Niels Möller46879152019-01-07 15:54:47 +0100269 // Intended for receive side. AddRttObserver registers an observer to be
270 // called for each RTT measurement, typically once per ACK. Before media
271 // transport is destructed the observer must be unregistered.
272 virtual void AddRttObserver(MediaTransportRttObserver* observer);
273 virtual void RemoveRttObserver(MediaTransportRttObserver* observer);
Piotr (Peter) Slatalaada077f2018-11-08 07:43:31 -0800274
275 // Returns the last known target transfer rate as reported to the above
276 // observers.
277 virtual absl::optional<TargetTransferRate> GetLatestTargetTransferRate();
278
279 // Gets the audio packet overhead in bytes. Returned overhead does not include
280 // transport overhead (ipv4/6, turn channeldata, tcp/udp, etc.).
281 // If the transport is capable of fusing packets together, this overhead
282 // might not be a very accurate number.
283 virtual size_t GetAudioPacketOverhead() const;
284
Niels Möllerd70a1142019-02-06 17:36:29 +0100285 // Registers an observer for network change events. If the network route is
286 // already established when the callback is added, |callback| will be called
287 // immediately with the current network route. Before media transport is
288 // destroyed, the callback must be removed.
Niels Möller30b182a2019-02-05 00:59:35 +0100289 virtual void AddNetworkChangeCallback(
290 MediaTransportNetworkChangeCallback* callback);
291 virtual void RemoveNetworkChangeCallback(
292 MediaTransportNetworkChangeCallback* callback);
Piotr (Peter) Slatala6b9d8232018-10-26 07:59:46 -0700293
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700294 // Sets a state observer callback. Before media transport is destroyed, the
295 // callback must be unregistered by setting it to nullptr.
296 // A newly registered callback will be called with the current state.
297 // Media transport does not invoke this callback concurrently.
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700298 virtual void SetMediaTransportStateCallback(
Bjorn Mellemeb2c6412018-10-31 15:25:32 -0700299 MediaTransportStateCallback* callback) = 0;
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700300
Piotr (Peter) Slatala48c54932019-01-28 06:50:38 -0800301 // Updates allocation limits.
302 // TODO(psla): Make abstract when downstream implementation implement it.
303 virtual void SetAllocatedBitrateLimits(
304 const MediaTransportAllocatedBitrateLimits& limits);
305
Bjorn Mellemf58e43e2019-02-22 10:31:48 -0800306 // Opens a data |channel_id| for sending. May return an error if the
307 // specified |channel_id| is unusable. Must be called before |SendData|.
Bjorn Mellem9ded4852019-02-28 12:27:11 -0800308 virtual RTCError OpenChannel(int channel_id) = 0;
Bjorn Mellemf58e43e2019-02-22 10:31:48 -0800309
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700310 // Sends a data buffer to the remote endpoint using the given send parameters.
311 // |buffer| may not be larger than 256 KiB. Returns an error if the send
312 // fails.
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700313 virtual RTCError SendData(int channel_id,
314 const SendDataParams& params,
Bjorn Mellemeb2c6412018-10-31 15:25:32 -0700315 const rtc::CopyOnWriteBuffer& buffer) = 0;
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700316
317 // Closes |channel_id| gracefully. Returns an error if |channel_id| is not
318 // open. Data sent after the closing procedure begins will not be
319 // transmitted. The channel becomes closed after pending data is transmitted.
Bjorn Mellemeb2c6412018-10-31 15:25:32 -0700320 virtual RTCError CloseChannel(int channel_id) = 0;
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700321
322 // Sets a sink for data messages and channel state callbacks. Before media
323 // transport is destroyed, the sink must be unregistered by setting it to
324 // nullptr.
Bjorn Mellemeb2c6412018-10-31 15:25:32 -0700325 virtual void SetDataSink(DataChannelSink* sink) = 0;
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700326
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400327 // TODO(sukhanov): RtcEventLogs.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400328};
329
330// If media transport factory is set in peer connection factory, it will be
331// used to create media transport for sending/receiving encoded frames and
332// this transport will be used instead of default RTP/SRTP transport.
333//
334// Currently Media Transport negotiation is not supported in SDP.
335// If application is using media transport, it must negotiate it before
336// setting media transport factory in peer connection.
337class MediaTransportFactory {
338 public:
339 virtual ~MediaTransportFactory() = default;
340
341 // Creates media transport.
342 // - Does not take ownership of packet_transport or network_thread.
343 // - Does not support group calls, in 1:1 call one side must set
344 // is_caller = true and another is_caller = false.
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -0700345 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);
Piotr (Peter) Slatalad6f61dd2019-02-26 12:08:27 -0800349
350 // Creates a new Media Transport in a disconnected state. If the media
351 // transport for the caller is created, one can then call
352 // MediaTransportInterface::GetTransportParametersOffer on that new instance.
353 // TODO(psla): Make abstract.
354 virtual RTCErrorOr<std::unique_ptr<webrtc::MediaTransportInterface>>
355 CreateMediaTransport(rtc::Thread* network_thread,
356 const MediaTransportSettings& settings);
357
358 // Gets a transport name which is supported by the implementation.
359 // Different factories should return different transport names, and at runtime
360 // it will be checked that different names were used.
361 // For example, "rtp" or "generic" may be returned by two different
362 // implementations.
363 // The value returned by this method must never change in the lifetime of the
364 // factory.
365 // TODO(psla): Make abstract.
366 virtual std::string GetTransportName() const;
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400367};
368
369} // namespace webrtc
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400370#endif // API_MEDIA_TRANSPORT_INTERFACE_H_