blob: feebd88efda93ea9e3ad806b4b5a9f1aa8789b1e [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"
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040028#include "api/rtcerror.h"
Niels Möller3a742392018-10-08 11:13:58 +020029#include "api/video/encoded_image.h"
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -070030#include "rtc_base/copyonwritebuffer.h"
Piotr (Peter) Slatalaada077f2018-11-08 07:43:31 -080031#include "rtc_base/networkroute.h"
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040032
33namespace rtc {
34class PacketTransportInternal;
35class Thread;
36} // namespace rtc
37
38namespace webrtc {
39
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -070040// A collection of settings for creation of media transport.
41struct MediaTransportSettings final {
42 MediaTransportSettings();
Piotr (Peter) Slatalaed7b8b12018-10-29 10:43:16 -070043 MediaTransportSettings(const MediaTransportSettings&);
44 MediaTransportSettings& operator=(const MediaTransportSettings&);
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -070045 ~MediaTransportSettings();
46
47 // Group calls are not currently supported, in 1:1 call one side must set
48 // is_caller = true and another is_caller = false.
49 bool is_caller;
50
51 // Must be set if a pre-shared key is used for the call.
Piotr (Peter) Slatala9f956252018-10-31 08:25:26 -070052 // TODO(bugs.webrtc.org/9944): This should become zero buffer in the distant
53 // future.
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -070054 absl::optional<std::string> pre_shared_key;
55};
56
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040057// Represents encoded audio frame in any encoding (type of encoding is opaque).
58// To avoid copying of encoded data use move semantics when passing by value.
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -070059class MediaTransportEncodedAudioFrame final {
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040060 public:
61 enum class FrameType {
62 // Normal audio frame (equivalent to webrtc::kAudioFrameSpeech).
63 kSpeech,
64
65 // DTX frame (equivalent to webrtc::kAudioFrameCN).
Niels Möller7d76a312018-10-26 12:57:07 +020066 // DTX frame (equivalent to webrtc::kAudioFrameCN).
67 kDiscontinuousTransmission,
68 // TODO(nisse): Mis-spelled version, update users, then delete.
69 kDiscountinuousTransmission = kDiscontinuousTransmission,
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040070 };
71
72 MediaTransportEncodedAudioFrame(
73 // Audio sampling rate, for example 48000.
74 int sampling_rate_hz,
75
76 // Starting sample index of the frame, i.e. how many audio samples were
77 // before this frame since the beginning of the call or beginning of time
78 // in one channel (the starting point should not matter for NetEq). In
79 // WebRTC it is used as a timestamp of the frame.
80 // TODO(sukhanov): Starting_sample_index is currently adjusted on the
81 // receiver side in RTP path. Non-RTP implementations should preserve it.
82 // For NetEq initial offset should not matter so we should consider fixing
83 // RTP path.
84 int starting_sample_index,
85
86 // Number of audio samples in audio frame in 1 channel.
87 int samples_per_channel,
88
89 // Sequence number of the frame in the order sent, it is currently
90 // required by NetEq, but we can fix NetEq, because starting_sample_index
91 // should be enough.
92 int sequence_number,
93
94 // If audio frame is a speech or discontinued transmission.
95 FrameType frame_type,
96
97 // Opaque payload type. In RTP codepath payload type is stored in RTP
98 // header. In other implementations it should be simply passed through the
99 // wire -- it's needed for decoder.
100 uint8_t payload_type,
101
102 // Vector with opaque encoded data.
Niels Möller3a742392018-10-08 11:13:58 +0200103 std::vector<uint8_t> encoded_data);
104
105 ~MediaTransportEncodedAudioFrame();
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700106 MediaTransportEncodedAudioFrame(const MediaTransportEncodedAudioFrame&);
107 MediaTransportEncodedAudioFrame& operator=(
108 const MediaTransportEncodedAudioFrame& other);
109 MediaTransportEncodedAudioFrame& operator=(
110 MediaTransportEncodedAudioFrame&& other);
111 MediaTransportEncodedAudioFrame(MediaTransportEncodedAudioFrame&&);
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400112
113 // Getters.
114 int sampling_rate_hz() const { return sampling_rate_hz_; }
115 int starting_sample_index() const { return starting_sample_index_; }
116 int samples_per_channel() const { return samples_per_channel_; }
117 int sequence_number() const { return sequence_number_; }
118
119 uint8_t payload_type() const { return payload_type_; }
120 FrameType frame_type() const { return frame_type_; }
121
122 rtc::ArrayView<const uint8_t> encoded_data() const { return encoded_data_; }
123
124 private:
125 int sampling_rate_hz_;
126 int starting_sample_index_;
127 int samples_per_channel_;
128
129 // TODO(sukhanov): Refactor NetEq so we don't need sequence number.
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700130 // Having sample_index and samples_per_channel should be enough.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400131 int sequence_number_;
132
133 FrameType frame_type_;
134
135 // TODO(sukhanov): Consider enumerating allowed encodings and store enum
136 // instead of uint payload_type.
137 uint8_t payload_type_;
138
139 std::vector<uint8_t> encoded_data_;
140};
141
Piotr (Peter) Slatalaada077f2018-11-08 07:43:31 -0800142// Callback to notify about network route changes.
143class MediaTransportNetworkChangeCallback {
144 public:
145 virtual ~MediaTransportNetworkChangeCallback() = default;
146
147 // Called when the network route is changed, with the new network route.
148 virtual void OnNetworkRouteChanged(
149 const rtc::NetworkRoute& new_network_route) = 0;
150};
151
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400152// Interface for receiving encoded audio frames from MediaTransportInterface
153// implementations.
154class MediaTransportAudioSinkInterface {
155 public:
156 virtual ~MediaTransportAudioSinkInterface() = default;
157
158 // Called when new encoded audio frame is received.
159 virtual void OnData(uint64_t channel_id,
160 MediaTransportEncodedAudioFrame frame) = 0;
161};
162
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700163// Represents encoded video frame, along with the codec information.
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700164class MediaTransportEncodedVideoFrame final {
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700165 public:
166 MediaTransportEncodedVideoFrame(int64_t frame_id,
167 std::vector<int64_t> referenced_frame_ids,
168 VideoCodecType codec_type,
Niels Möller3a742392018-10-08 11:13:58 +0200169 const webrtc::EncodedImage& encoded_image);
170 ~MediaTransportEncodedVideoFrame();
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700171 MediaTransportEncodedVideoFrame(const MediaTransportEncodedVideoFrame&);
172 MediaTransportEncodedVideoFrame& operator=(
173 const MediaTransportEncodedVideoFrame& other);
174 MediaTransportEncodedVideoFrame& operator=(
175 MediaTransportEncodedVideoFrame&& other);
176 MediaTransportEncodedVideoFrame(MediaTransportEncodedVideoFrame&&);
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700177
178 VideoCodecType codec_type() const { return codec_type_; }
179 const webrtc::EncodedImage& encoded_image() const { return encoded_image_; }
180
181 int64_t frame_id() const { return frame_id_; }
182 const std::vector<int64_t>& referenced_frame_ids() const {
183 return referenced_frame_ids_;
184 }
185
Niels Möllerd5696fb2018-11-28 15:34:37 +0100186 // Hack to workaround lack of ownership of the encoded_image_._buffer. If we
187 // don't already own the underlying data, make a copy.
188 void Retain();
189
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700190 private:
Niels Möllerd5696fb2018-11-28 15:34:37 +0100191 MediaTransportEncodedVideoFrame();
192
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700193 VideoCodecType codec_type_;
194
Niels Möllerd5696fb2018-11-28 15:34:37 +0100195 // The buffer is not owned by the encoded image. On the sender it means that
196 // it will need to make a copy using the Retain() method, if it wants to
197 // deliver it asynchronously.
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700198 webrtc::EncodedImage encoded_image_;
199
Niels Möllerd5696fb2018-11-28 15:34:37 +0100200 // If non-empty, this is the data for the encoded image.
201 std::vector<uint8_t> encoded_data_;
202
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700203 // Frame id uniquely identifies a frame in a stream. It needs to be unique in
204 // a given time window (i.e. technically unique identifier for the lifetime of
205 // the connection is not needed, but you need to guarantee that remote side
206 // got rid of the previous frame_id if you plan to reuse it).
207 //
208 // It is required by a remote jitter buffer, and is the same as
209 // EncodedFrame::id::picture_id.
210 //
211 // This data must be opaque to the media transport, and media transport should
212 // itself not make any assumptions about what it is and its uniqueness.
213 int64_t frame_id_;
214
215 // A single frame might depend on other frames. This is set of identifiers on
216 // which the current frame depends.
217 std::vector<int64_t> referenced_frame_ids_;
218};
219
220// Interface for receiving encoded video frames from MediaTransportInterface
221// implementations.
222class MediaTransportVideoSinkInterface {
223 public:
224 virtual ~MediaTransportVideoSinkInterface() = default;
225
226 // Called when new encoded video frame is received.
227 virtual void OnData(uint64_t channel_id,
228 MediaTransportEncodedVideoFrame frame) = 0;
229
230 // Called when the request for keyframe is received.
231 virtual void OnKeyFrameRequested(uint64_t channel_id) = 0;
232};
233
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700234// State of the media transport. Media transport begins in the pending state.
235// It transitions to writable when it is ready to send media. It may transition
236// back to pending if the connection is blocked. It may transition to closed at
237// any time. Closed is terminal: a transport will never re-open once closed.
238enum class MediaTransportState {
239 kPending,
240 kWritable,
241 kClosed,
242};
243
244// Callback invoked whenever the state of the media transport changes.
245class MediaTransportStateCallback {
246 public:
247 virtual ~MediaTransportStateCallback() = default;
248
249 // Invoked whenever the state of the media transport changes.
250 virtual void OnStateChanged(MediaTransportState state) = 0;
251};
252
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700253// Supported types of application data messages.
254enum class DataMessageType {
255 // Application data buffer with the binary bit unset.
256 kText,
257
258 // Application data buffer with the binary bit set.
259 kBinary,
260
261 // Transport-agnostic control messages, such as open or open-ack messages.
262 kControl,
263};
264
265// Parameters for sending data. The parameters may change from message to
266// message, even within a single channel. For example, control messages may be
267// sent reliably and in-order, even if the data channel is configured for
268// unreliable delivery.
269struct SendDataParams {
270 SendDataParams();
Niels Möllere0446cb2018-11-30 09:35:52 +0100271 SendDataParams(const SendDataParams&);
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700272
273 DataMessageType type = DataMessageType::kText;
274
275 // Whether to deliver the message in order with respect to other ordered
276 // messages with the same channel_id.
277 bool ordered = false;
278
279 // If set, the maximum number of times this message may be
280 // retransmitted by the transport before it is dropped.
281 // Setting this value to zero disables retransmission.
282 // Must be non-negative. |max_rtx_count| and |max_rtx_ms| may not be set
283 // simultaneously.
284 absl::optional<int> max_rtx_count;
285
286 // If set, the maximum number of milliseconds for which the transport
287 // may retransmit this message before it is dropped.
288 // Setting this value to zero disables retransmission.
289 // Must be non-negative. |max_rtx_count| and |max_rtx_ms| may not be set
290 // simultaneously.
291 absl::optional<int> max_rtx_ms;
292};
293
294// Sink for callbacks related to a data channel.
295class DataChannelSink {
296 public:
297 virtual ~DataChannelSink() = default;
298
299 // Callback issued when data is received by the transport.
300 virtual void OnDataReceived(int channel_id,
301 DataMessageType type,
302 const rtc::CopyOnWriteBuffer& buffer) = 0;
303
304 // Callback issued when a remote data channel begins the closing procedure.
305 // Messages sent after the closing procedure begins will not be transmitted.
306 virtual void OnChannelClosing(int channel_id) = 0;
307
308 // Callback issued when a (remote or local) data channel completes the closing
309 // procedure. Closing channels become closed after all pending data has been
310 // transmitted.
311 virtual void OnChannelClosed(int channel_id) = 0;
312};
313
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400314// Media transport interface for sending / receiving encoded audio/video frames
315// and receiving bandwidth estimate update from congestion control.
316class MediaTransportInterface {
317 public:
318 virtual ~MediaTransportInterface() = default;
319
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700320 // Start asynchronous send of audio frame. The status returned by this method
321 // only pertains to the synchronous operations (e.g.
322 // serialization/packetization), not to the asynchronous operation.
323
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400324 virtual RTCError SendAudioFrame(uint64_t channel_id,
325 MediaTransportEncodedAudioFrame frame) = 0;
326
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700327 // Start asynchronous send of video frame. The status returned by this method
328 // only pertains to the synchronous operations (e.g.
329 // serialization/packetization), not to the asynchronous operation.
330 virtual RTCError SendVideoFrame(
331 uint64_t channel_id,
332 const MediaTransportEncodedVideoFrame& frame) = 0;
333
334 // Requests a keyframe for the particular channel (stream). The caller should
335 // check that the keyframe is not present in a jitter buffer already (i.e.
336 // don't request a keyframe if there is one that you will get from the jitter
337 // buffer in a moment).
338 virtual RTCError RequestKeyFrame(uint64_t channel_id) = 0;
339
340 // Sets audio sink. Sink must be unset by calling SetReceiveAudioSink(nullptr)
341 // before the media transport is destroyed or before new sink is set.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400342 virtual void SetReceiveAudioSink(MediaTransportAudioSinkInterface* sink) = 0;
343
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700344 // Registers a video sink. Before destruction of media transport, you must
345 // pass a nullptr.
346 virtual void SetReceiveVideoSink(MediaTransportVideoSinkInterface* sink) = 0;
347
Piotr (Peter) Slatalaada077f2018-11-08 07:43:31 -0800348 // Adds a target bitrate observer. Before media transport is destructed
349 // the observer must be unregistered (by calling
350 // RemoveTargetTransferRateObserver).
351 // A newly registered observer will be called back with the latest recorded
352 // target rate, if available.
353 virtual void AddTargetTransferRateObserver(
354 webrtc::TargetTransferRateObserver* observer);
355
356 // Removes an existing |observer| from observers. If observer was never
357 // registered, an error is logged and method does nothing.
358 virtual void RemoveTargetTransferRateObserver(
359 webrtc::TargetTransferRateObserver* observer);
360
361 // Returns the last known target transfer rate as reported to the above
362 // observers.
363 virtual absl::optional<TargetTransferRate> GetLatestTargetTransferRate();
364
365 // Gets the audio packet overhead in bytes. Returned overhead does not include
366 // transport overhead (ipv4/6, turn channeldata, tcp/udp, etc.).
367 // If the transport is capable of fusing packets together, this overhead
368 // might not be a very accurate number.
369 virtual size_t GetAudioPacketOverhead() const;
370
371 // Sets an observer for network change events. If the network route is already
372 // established when the callback is set, |callback| will be called immediately
373 // with the current network route.
374 // Before media transport is destroyed, the callback must be unregistered by
375 // setting it to nullptr.
376 virtual void SetNetworkChangeCallback(
377 MediaTransportNetworkChangeCallback* callback);
Piotr (Peter) Slatala6b9d8232018-10-26 07:59:46 -0700378
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700379 // Sets a state observer callback. Before media transport is destroyed, the
380 // callback must be unregistered by setting it to nullptr.
381 // A newly registered callback will be called with the current state.
382 // Media transport does not invoke this callback concurrently.
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700383 virtual void SetMediaTransportStateCallback(
Bjorn Mellemeb2c6412018-10-31 15:25:32 -0700384 MediaTransportStateCallback* callback) = 0;
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700385
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700386 // Sends a data buffer to the remote endpoint using the given send parameters.
387 // |buffer| may not be larger than 256 KiB. Returns an error if the send
388 // fails.
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700389 virtual RTCError SendData(int channel_id,
390 const SendDataParams& params,
Bjorn Mellemeb2c6412018-10-31 15:25:32 -0700391 const rtc::CopyOnWriteBuffer& buffer) = 0;
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700392
393 // Closes |channel_id| gracefully. Returns an error if |channel_id| is not
394 // open. Data sent after the closing procedure begins will not be
395 // transmitted. The channel becomes closed after pending data is transmitted.
Bjorn Mellemeb2c6412018-10-31 15:25:32 -0700396 virtual RTCError CloseChannel(int channel_id) = 0;
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700397
398 // Sets a sink for data messages and channel state callbacks. Before media
399 // transport is destroyed, the sink must be unregistered by setting it to
400 // nullptr.
Bjorn Mellemeb2c6412018-10-31 15:25:32 -0700401 virtual void SetDataSink(DataChannelSink* sink) = 0;
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700402
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400403 // TODO(sukhanov): RtcEventLogs.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400404};
405
406// If media transport factory is set in peer connection factory, it will be
407// used to create media transport for sending/receiving encoded frames and
408// this transport will be used instead of default RTP/SRTP transport.
409//
410// Currently Media Transport negotiation is not supported in SDP.
411// If application is using media transport, it must negotiate it before
412// setting media transport factory in peer connection.
413class MediaTransportFactory {
414 public:
415 virtual ~MediaTransportFactory() = default;
416
417 // Creates media transport.
418 // - Does not take ownership of packet_transport or network_thread.
419 // - Does not support group calls, in 1:1 call one side must set
420 // is_caller = true and another is_caller = false.
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -0700421 // TODO(bugs.webrtc.org/9938) This constructor will be removed and replaced
422 // with the one below.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400423 virtual RTCErrorOr<std::unique_ptr<MediaTransportInterface>>
424 CreateMediaTransport(rtc::PacketTransportInternal* packet_transport,
425 rtc::Thread* network_thread,
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -0700426 bool is_caller);
427
428 // Creates media transport.
429 // - Does not take ownership of packet_transport or network_thread.
430 // TODO(bugs.webrtc.org/9938): remove default implementation once all children
431 // override it.
432 virtual RTCErrorOr<std::unique_ptr<MediaTransportInterface>>
433 CreateMediaTransport(rtc::PacketTransportInternal* packet_transport,
434 rtc::Thread* network_thread,
Piotr (Peter) Slatalaed7b8b12018-10-29 10:43:16 -0700435 const MediaTransportSettings& settings);
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400436};
437
438} // namespace webrtc
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400439#endif // API_MEDIA_TRANSPORT_INTERFACE_H_