blob: c289d99378cae79a5faa441ae5f85256aabb37e7 [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>
24#include <vector>
25
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -070026#include "absl/types/optional.h"
Yves Gerey988cc082018-10-23 12:03:01 +020027#include "api/array_view.h"
Steve Anton10542f22019-01-11 09:11:00 -080028#include "api/rtc_error.h"
Niels Möllerec3b9ff2019-02-08 00:28:39 +010029#include "api/transport/media/audio_transport.h"
Piotr (Peter) Slatala48c54932019-01-28 06:50:38 -080030#include "api/units/data_rate.h"
Niels Möller3a742392018-10-08 11:13:58 +020031#include "api/video/encoded_image.h"
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
75 // If present, provides the event log that media transport should use.
76 // Media transport does not own it. The lifetime of |event_log| will exceed
77 // the lifetime of the instance of MediaTransportInterface instance.
78 RtcEventLog* event_log = nullptr;
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -070079};
80
Piotr (Peter) Slatalaada077f2018-11-08 07:43:31 -080081// Callback to notify about network route changes.
82class MediaTransportNetworkChangeCallback {
83 public:
84 virtual ~MediaTransportNetworkChangeCallback() = default;
85
86 // Called when the network route is changed, with the new network route.
87 virtual void OnNetworkRouteChanged(
88 const rtc::NetworkRoute& new_network_route) = 0;
89};
90
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -070091// Represents encoded video frame, along with the codec information.
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -070092class MediaTransportEncodedVideoFrame final {
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -070093 public:
94 MediaTransportEncodedVideoFrame(int64_t frame_id,
95 std::vector<int64_t> referenced_frame_ids,
Niels Möllerd8a1b7a2018-12-06 13:00:27 +010096 int payload_type,
Niels Möller3a742392018-10-08 11:13:58 +020097 const webrtc::EncodedImage& encoded_image);
98 ~MediaTransportEncodedVideoFrame();
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -070099 MediaTransportEncodedVideoFrame(const MediaTransportEncodedVideoFrame&);
100 MediaTransportEncodedVideoFrame& operator=(
101 const MediaTransportEncodedVideoFrame& other);
102 MediaTransportEncodedVideoFrame& operator=(
103 MediaTransportEncodedVideoFrame&& other);
104 MediaTransportEncodedVideoFrame(MediaTransportEncodedVideoFrame&&);
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700105
Niels Möllerd8a1b7a2018-12-06 13:00:27 +0100106 int payload_type() const { return payload_type_; }
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700107 const webrtc::EncodedImage& encoded_image() const { return encoded_image_; }
108
109 int64_t frame_id() const { return frame_id_; }
110 const std::vector<int64_t>& referenced_frame_ids() const {
111 return referenced_frame_ids_;
112 }
113
Niels Möller938dd9f2019-02-07 00:02:17 +0100114 // Hack to workaround lack of ownership of the EncodedImage buffer. If we
Niels Möllerd5696fb2018-11-28 15:34:37 +0100115 // don't already own the underlying data, make a copy.
Niels Möller938dd9f2019-02-07 00:02:17 +0100116 void Retain() { encoded_image_.Retain(); }
Niels Möllerd5696fb2018-11-28 15:34:37 +0100117
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700118 private:
Niels Möllerd5696fb2018-11-28 15:34:37 +0100119 MediaTransportEncodedVideoFrame();
120
Niels Möllerd8a1b7a2018-12-06 13:00:27 +0100121 int payload_type_;
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700122
Niels Möller938dd9f2019-02-07 00:02:17 +0100123 // The buffer is not always owned by the encoded image. On the sender it means
124 // that it will need to make a copy using the Retain() method, if it wants to
Niels Möllerd5696fb2018-11-28 15:34:37 +0100125 // deliver it asynchronously.
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700126 webrtc::EncodedImage encoded_image_;
127
128 // Frame id uniquely identifies a frame in a stream. It needs to be unique in
129 // a given time window (i.e. technically unique identifier for the lifetime of
130 // the connection is not needed, but you need to guarantee that remote side
131 // got rid of the previous frame_id if you plan to reuse it).
132 //
133 // It is required by a remote jitter buffer, and is the same as
134 // EncodedFrame::id::picture_id.
135 //
136 // This data must be opaque to the media transport, and media transport should
137 // itself not make any assumptions about what it is and its uniqueness.
138 int64_t frame_id_;
139
140 // A single frame might depend on other frames. This is set of identifiers on
141 // which the current frame depends.
142 std::vector<int64_t> referenced_frame_ids_;
143};
144
145// Interface for receiving encoded video frames from MediaTransportInterface
146// implementations.
147class MediaTransportVideoSinkInterface {
148 public:
149 virtual ~MediaTransportVideoSinkInterface() = default;
150
151 // Called when new encoded video frame is received.
152 virtual void OnData(uint64_t channel_id,
153 MediaTransportEncodedVideoFrame frame) = 0;
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700154};
155
Niels Möller1c7f5f62018-12-10 11:06:02 +0100156// Interface for video sender to be notified of received key frame request.
157class MediaTransportKeyFrameRequestCallback {
158 public:
159 virtual ~MediaTransportKeyFrameRequestCallback() = default;
160
161 // Called when a key frame request is received on the transport.
162 virtual void OnKeyFrameRequested(uint64_t channel_id) = 0;
163};
164
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700165// State of the media transport. Media transport begins in the pending state.
166// It transitions to writable when it is ready to send media. It may transition
167// back to pending if the connection is blocked. It may transition to closed at
168// any time. Closed is terminal: a transport will never re-open once closed.
169enum class MediaTransportState {
170 kPending,
171 kWritable,
172 kClosed,
173};
174
175// Callback invoked whenever the state of the media transport changes.
176class MediaTransportStateCallback {
177 public:
178 virtual ~MediaTransportStateCallback() = default;
179
180 // Invoked whenever the state of the media transport changes.
181 virtual void OnStateChanged(MediaTransportState state) = 0;
182};
183
Niels Möller46879152019-01-07 15:54:47 +0100184// Callback for RTT measurements on the receive side.
185// TODO(nisse): Related interfaces: CallStatsObserver and RtcpRttStats. It's
186// somewhat unclear what type of measurement is needed. It's used to configure
187// NACK generation and playout buffer. Either raw measurement values or recent
188// maximum would make sense for this use. Need consolidation of RTT signalling.
189class MediaTransportRttObserver {
190 public:
191 virtual ~MediaTransportRttObserver() = default;
192
193 // Invoked when a new RTT measurement is available, typically once per ACK.
194 virtual void OnRttUpdated(int64_t rtt_ms) = 0;
195};
196
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700197// Supported types of application data messages.
198enum class DataMessageType {
199 // Application data buffer with the binary bit unset.
200 kText,
201
202 // Application data buffer with the binary bit set.
203 kBinary,
204
205 // Transport-agnostic control messages, such as open or open-ack messages.
206 kControl,
207};
208
209// Parameters for sending data. The parameters may change from message to
210// message, even within a single channel. For example, control messages may be
211// sent reliably and in-order, even if the data channel is configured for
212// unreliable delivery.
213struct SendDataParams {
214 SendDataParams();
Niels Möllere0446cb2018-11-30 09:35:52 +0100215 SendDataParams(const SendDataParams&);
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700216
217 DataMessageType type = DataMessageType::kText;
218
219 // Whether to deliver the message in order with respect to other ordered
220 // messages with the same channel_id.
221 bool ordered = false;
222
223 // If set, the maximum number of times this message may be
224 // retransmitted by the transport before it is dropped.
225 // Setting this value to zero disables retransmission.
226 // Must be non-negative. |max_rtx_count| and |max_rtx_ms| may not be set
227 // simultaneously.
228 absl::optional<int> max_rtx_count;
229
230 // If set, the maximum number of milliseconds for which the transport
231 // may retransmit this message before it is dropped.
232 // Setting this value to zero disables retransmission.
233 // Must be non-negative. |max_rtx_count| and |max_rtx_ms| may not be set
234 // simultaneously.
235 absl::optional<int> max_rtx_ms;
236};
237
238// Sink for callbacks related to a data channel.
239class DataChannelSink {
240 public:
241 virtual ~DataChannelSink() = default;
242
243 // Callback issued when data is received by the transport.
244 virtual void OnDataReceived(int channel_id,
245 DataMessageType type,
246 const rtc::CopyOnWriteBuffer& buffer) = 0;
247
248 // Callback issued when a remote data channel begins the closing procedure.
249 // Messages sent after the closing procedure begins will not be transmitted.
250 virtual void OnChannelClosing(int channel_id) = 0;
251
252 // Callback issued when a (remote or local) data channel completes the closing
253 // procedure. Closing channels become closed after all pending data has been
254 // transmitted.
255 virtual void OnChannelClosed(int channel_id) = 0;
256};
257
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400258// Media transport interface for sending / receiving encoded audio/video frames
259// and receiving bandwidth estimate update from congestion control.
260class MediaTransportInterface {
261 public:
Piotr (Peter) Slatala309aafe2019-01-15 14:24:34 -0800262 MediaTransportInterface();
263 virtual ~MediaTransportInterface();
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400264
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700265 // Start asynchronous send of audio frame. The status returned by this method
266 // only pertains to the synchronous operations (e.g.
267 // serialization/packetization), not to the asynchronous operation.
268
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400269 virtual RTCError SendAudioFrame(uint64_t channel_id,
270 MediaTransportEncodedAudioFrame frame) = 0;
271
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700272 // Start asynchronous send of video frame. The status returned by this method
273 // only pertains to the synchronous operations (e.g.
274 // serialization/packetization), not to the asynchronous operation.
275 virtual RTCError SendVideoFrame(
276 uint64_t channel_id,
277 const MediaTransportEncodedVideoFrame& frame) = 0;
278
Niels Möller1c7f5f62018-12-10 11:06:02 +0100279 // Used by video sender to be notified on key frame requests.
280 virtual void SetKeyFrameRequestCallback(
281 MediaTransportKeyFrameRequestCallback* callback);
282
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700283 // Requests a keyframe for the particular channel (stream). The caller should
284 // check that the keyframe is not present in a jitter buffer already (i.e.
285 // don't request a keyframe if there is one that you will get from the jitter
286 // buffer in a moment).
287 virtual RTCError RequestKeyFrame(uint64_t channel_id) = 0;
288
289 // Sets audio sink. Sink must be unset by calling SetReceiveAudioSink(nullptr)
290 // before the media transport is destroyed or before new sink is set.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400291 virtual void SetReceiveAudioSink(MediaTransportAudioSinkInterface* sink) = 0;
292
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700293 // Registers a video sink. Before destruction of media transport, you must
294 // pass a nullptr.
295 virtual void SetReceiveVideoSink(MediaTransportVideoSinkInterface* sink) = 0;
296
Piotr (Peter) Slatalaada077f2018-11-08 07:43:31 -0800297 // Adds a target bitrate observer. Before media transport is destructed
298 // the observer must be unregistered (by calling
299 // RemoveTargetTransferRateObserver).
300 // A newly registered observer will be called back with the latest recorded
301 // target rate, if available.
302 virtual void AddTargetTransferRateObserver(
Niels Möller46879152019-01-07 15:54:47 +0100303 TargetTransferRateObserver* observer);
Piotr (Peter) Slatalaada077f2018-11-08 07:43:31 -0800304
305 // Removes an existing |observer| from observers. If observer was never
306 // registered, an error is logged and method does nothing.
307 virtual void RemoveTargetTransferRateObserver(
Niels Möller46879152019-01-07 15:54:47 +0100308 TargetTransferRateObserver* observer);
309
Piotr (Peter) Slatala309aafe2019-01-15 14:24:34 -0800310 // Sets audio packets observer, which gets informed about incoming audio
311 // packets. Before destruction, the observer must be unregistered by setting
312 // nullptr.
313 //
314 // This method may be temporary, when the multiplexer is implemented (or
315 // multiplexer may use it to demultiplex channel ids).
316 virtual void SetFirstAudioPacketReceivedObserver(
317 AudioPacketReceivedObserver* observer);
318
Niels Möller46879152019-01-07 15:54:47 +0100319 // Intended for receive side. AddRttObserver registers an observer to be
320 // called for each RTT measurement, typically once per ACK. Before media
321 // transport is destructed the observer must be unregistered.
322 virtual void AddRttObserver(MediaTransportRttObserver* observer);
323 virtual void RemoveRttObserver(MediaTransportRttObserver* observer);
Piotr (Peter) Slatalaada077f2018-11-08 07:43:31 -0800324
325 // Returns the last known target transfer rate as reported to the above
326 // observers.
327 virtual absl::optional<TargetTransferRate> GetLatestTargetTransferRate();
328
329 // Gets the audio packet overhead in bytes. Returned overhead does not include
330 // transport overhead (ipv4/6, turn channeldata, tcp/udp, etc.).
331 // If the transport is capable of fusing packets together, this overhead
332 // might not be a very accurate number.
333 virtual size_t GetAudioPacketOverhead() const;
334
Niels Möllerd70a1142019-02-06 17:36:29 +0100335 // Registers an observer for network change events. If the network route is
336 // already established when the callback is added, |callback| will be called
337 // immediately with the current network route. Before media transport is
338 // destroyed, the callback must be removed.
Niels Möller30b182a2019-02-05 00:59:35 +0100339 virtual void AddNetworkChangeCallback(
340 MediaTransportNetworkChangeCallback* callback);
341 virtual void RemoveNetworkChangeCallback(
342 MediaTransportNetworkChangeCallback* callback);
Piotr (Peter) Slatala6b9d8232018-10-26 07:59:46 -0700343
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700344 // Sets a state observer callback. Before media transport is destroyed, the
345 // callback must be unregistered by setting it to nullptr.
346 // A newly registered callback will be called with the current state.
347 // Media transport does not invoke this callback concurrently.
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700348 virtual void SetMediaTransportStateCallback(
Bjorn Mellemeb2c6412018-10-31 15:25:32 -0700349 MediaTransportStateCallback* callback) = 0;
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700350
Piotr (Peter) Slatala48c54932019-01-28 06:50:38 -0800351 // Updates allocation limits.
352 // TODO(psla): Make abstract when downstream implementation implement it.
353 virtual void SetAllocatedBitrateLimits(
354 const MediaTransportAllocatedBitrateLimits& limits);
355
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700356 // Sends a data buffer to the remote endpoint using the given send parameters.
357 // |buffer| may not be larger than 256 KiB. Returns an error if the send
358 // fails.
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700359 virtual RTCError SendData(int channel_id,
360 const SendDataParams& params,
Bjorn Mellemeb2c6412018-10-31 15:25:32 -0700361 const rtc::CopyOnWriteBuffer& buffer) = 0;
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700362
363 // Closes |channel_id| gracefully. Returns an error if |channel_id| is not
364 // open. Data sent after the closing procedure begins will not be
365 // transmitted. The channel becomes closed after pending data is transmitted.
Bjorn Mellemeb2c6412018-10-31 15:25:32 -0700366 virtual RTCError CloseChannel(int channel_id) = 0;
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700367
368 // Sets a sink for data messages and channel state callbacks. Before media
369 // transport is destroyed, the sink must be unregistered by setting it to
370 // nullptr.
Bjorn Mellemeb2c6412018-10-31 15:25:32 -0700371 virtual void SetDataSink(DataChannelSink* sink) = 0;
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700372
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400373 // TODO(sukhanov): RtcEventLogs.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400374};
375
376// If media transport factory is set in peer connection factory, it will be
377// used to create media transport for sending/receiving encoded frames and
378// this transport will be used instead of default RTP/SRTP transport.
379//
380// Currently Media Transport negotiation is not supported in SDP.
381// If application is using media transport, it must negotiate it before
382// setting media transport factory in peer connection.
383class MediaTransportFactory {
384 public:
385 virtual ~MediaTransportFactory() = default;
386
387 // Creates media transport.
388 // - Does not take ownership of packet_transport or network_thread.
389 // - Does not support group calls, in 1:1 call one side must set
390 // is_caller = true and another is_caller = false.
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -0700391 // TODO(bugs.webrtc.org/9938) This constructor will be removed and replaced
392 // with the one below.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400393 virtual RTCErrorOr<std::unique_ptr<MediaTransportInterface>>
394 CreateMediaTransport(rtc::PacketTransportInternal* packet_transport,
395 rtc::Thread* network_thread,
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -0700396 bool is_caller);
397
398 // Creates media transport.
399 // - Does not take ownership of packet_transport or network_thread.
400 // TODO(bugs.webrtc.org/9938): remove default implementation once all children
401 // override it.
402 virtual RTCErrorOr<std::unique_ptr<MediaTransportInterface>>
403 CreateMediaTransport(rtc::PacketTransportInternal* packet_transport,
404 rtc::Thread* network_thread,
Piotr (Peter) Slatalaed7b8b12018-10-29 10:43:16 -0700405 const MediaTransportSettings& settings);
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400406};
407
408} // namespace webrtc
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400409#endif // API_MEDIA_TRANSPORT_INTERFACE_H_