blob: b10dd63a465c45a982f8589565dee98d23abea5c [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
186 private:
187 VideoCodecType codec_type_;
188
189 // The buffer is not owned by the encoded image by default. On the sender it
190 // means that it will need to make a copy of it if it wants to deliver it
191 // asynchronously.
192 webrtc::EncodedImage encoded_image_;
193
194 // Frame id uniquely identifies a frame in a stream. It needs to be unique in
195 // a given time window (i.e. technically unique identifier for the lifetime of
196 // the connection is not needed, but you need to guarantee that remote side
197 // got rid of the previous frame_id if you plan to reuse it).
198 //
199 // It is required by a remote jitter buffer, and is the same as
200 // EncodedFrame::id::picture_id.
201 //
202 // This data must be opaque to the media transport, and media transport should
203 // itself not make any assumptions about what it is and its uniqueness.
204 int64_t frame_id_;
205
206 // A single frame might depend on other frames. This is set of identifiers on
207 // which the current frame depends.
208 std::vector<int64_t> referenced_frame_ids_;
209};
210
211// Interface for receiving encoded video frames from MediaTransportInterface
212// implementations.
213class MediaTransportVideoSinkInterface {
214 public:
215 virtual ~MediaTransportVideoSinkInterface() = default;
216
217 // Called when new encoded video frame is received.
218 virtual void OnData(uint64_t channel_id,
219 MediaTransportEncodedVideoFrame frame) = 0;
220
221 // Called when the request for keyframe is received.
222 virtual void OnKeyFrameRequested(uint64_t channel_id) = 0;
223};
224
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700225// State of the media transport. Media transport begins in the pending state.
226// It transitions to writable when it is ready to send media. It may transition
227// back to pending if the connection is blocked. It may transition to closed at
228// any time. Closed is terminal: a transport will never re-open once closed.
229enum class MediaTransportState {
230 kPending,
231 kWritable,
232 kClosed,
233};
234
235// Callback invoked whenever the state of the media transport changes.
236class MediaTransportStateCallback {
237 public:
238 virtual ~MediaTransportStateCallback() = default;
239
240 // Invoked whenever the state of the media transport changes.
241 virtual void OnStateChanged(MediaTransportState state) = 0;
242};
243
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700244// Supported types of application data messages.
245enum class DataMessageType {
246 // Application data buffer with the binary bit unset.
247 kText,
248
249 // Application data buffer with the binary bit set.
250 kBinary,
251
252 // Transport-agnostic control messages, such as open or open-ack messages.
253 kControl,
254};
255
256// Parameters for sending data. The parameters may change from message to
257// message, even within a single channel. For example, control messages may be
258// sent reliably and in-order, even if the data channel is configured for
259// unreliable delivery.
260struct SendDataParams {
261 SendDataParams();
262
263 DataMessageType type = DataMessageType::kText;
264
265 // Whether to deliver the message in order with respect to other ordered
266 // messages with the same channel_id.
267 bool ordered = false;
268
269 // If set, the maximum number of times this message may be
270 // retransmitted by the transport before it is dropped.
271 // Setting this value to zero disables retransmission.
272 // Must be non-negative. |max_rtx_count| and |max_rtx_ms| may not be set
273 // simultaneously.
274 absl::optional<int> max_rtx_count;
275
276 // If set, the maximum number of milliseconds for which the transport
277 // may retransmit this message before it is dropped.
278 // Setting this value to zero disables retransmission.
279 // Must be non-negative. |max_rtx_count| and |max_rtx_ms| may not be set
280 // simultaneously.
281 absl::optional<int> max_rtx_ms;
282};
283
284// Sink for callbacks related to a data channel.
285class DataChannelSink {
286 public:
287 virtual ~DataChannelSink() = default;
288
289 // Callback issued when data is received by the transport.
290 virtual void OnDataReceived(int channel_id,
291 DataMessageType type,
292 const rtc::CopyOnWriteBuffer& buffer) = 0;
293
294 // Callback issued when a remote data channel begins the closing procedure.
295 // Messages sent after the closing procedure begins will not be transmitted.
296 virtual void OnChannelClosing(int channel_id) = 0;
297
298 // Callback issued when a (remote or local) data channel completes the closing
299 // procedure. Closing channels become closed after all pending data has been
300 // transmitted.
301 virtual void OnChannelClosed(int channel_id) = 0;
302};
303
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400304// Media transport interface for sending / receiving encoded audio/video frames
305// and receiving bandwidth estimate update from congestion control.
306class MediaTransportInterface {
307 public:
308 virtual ~MediaTransportInterface() = default;
309
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700310 // Start asynchronous send of audio frame. The status returned by this method
311 // only pertains to the synchronous operations (e.g.
312 // serialization/packetization), not to the asynchronous operation.
313
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400314 virtual RTCError SendAudioFrame(uint64_t channel_id,
315 MediaTransportEncodedAudioFrame frame) = 0;
316
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700317 // Start asynchronous send of video frame. The status returned by this method
318 // only pertains to the synchronous operations (e.g.
319 // serialization/packetization), not to the asynchronous operation.
320 virtual RTCError SendVideoFrame(
321 uint64_t channel_id,
322 const MediaTransportEncodedVideoFrame& frame) = 0;
323
324 // Requests a keyframe for the particular channel (stream). The caller should
325 // check that the keyframe is not present in a jitter buffer already (i.e.
326 // don't request a keyframe if there is one that you will get from the jitter
327 // buffer in a moment).
328 virtual RTCError RequestKeyFrame(uint64_t channel_id) = 0;
329
330 // Sets audio sink. Sink must be unset by calling SetReceiveAudioSink(nullptr)
331 // before the media transport is destroyed or before new sink is set.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400332 virtual void SetReceiveAudioSink(MediaTransportAudioSinkInterface* sink) = 0;
333
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700334 // Registers a video sink. Before destruction of media transport, you must
335 // pass a nullptr.
336 virtual void SetReceiveVideoSink(MediaTransportVideoSinkInterface* sink) = 0;
337
Piotr (Peter) Slatalaada077f2018-11-08 07:43:31 -0800338 // Adds a target bitrate observer. Before media transport is destructed
339 // the observer must be unregistered (by calling
340 // RemoveTargetTransferRateObserver).
341 // A newly registered observer will be called back with the latest recorded
342 // target rate, if available.
343 virtual void AddTargetTransferRateObserver(
344 webrtc::TargetTransferRateObserver* observer);
345
346 // Removes an existing |observer| from observers. If observer was never
347 // registered, an error is logged and method does nothing.
348 virtual void RemoveTargetTransferRateObserver(
349 webrtc::TargetTransferRateObserver* observer);
350
351 // Returns the last known target transfer rate as reported to the above
352 // observers.
353 virtual absl::optional<TargetTransferRate> GetLatestTargetTransferRate();
354
355 // Gets the audio packet overhead in bytes. Returned overhead does not include
356 // transport overhead (ipv4/6, turn channeldata, tcp/udp, etc.).
357 // If the transport is capable of fusing packets together, this overhead
358 // might not be a very accurate number.
359 virtual size_t GetAudioPacketOverhead() const;
360
361 // Sets an observer for network change events. If the network route is already
362 // established when the callback is set, |callback| will be called immediately
363 // with the current network route.
364 // Before media transport is destroyed, the callback must be unregistered by
365 // setting it to nullptr.
366 virtual void SetNetworkChangeCallback(
367 MediaTransportNetworkChangeCallback* callback);
Piotr (Peter) Slatala6b9d8232018-10-26 07:59:46 -0700368
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700369 // Sets a state observer callback. Before media transport is destroyed, the
370 // callback must be unregistered by setting it to nullptr.
371 // A newly registered callback will be called with the current state.
372 // Media transport does not invoke this callback concurrently.
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700373 virtual void SetMediaTransportStateCallback(
Bjorn Mellemeb2c6412018-10-31 15:25:32 -0700374 MediaTransportStateCallback* callback) = 0;
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700375
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700376 // Sends a data buffer to the remote endpoint using the given send parameters.
377 // |buffer| may not be larger than 256 KiB. Returns an error if the send
378 // fails.
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700379 virtual RTCError SendData(int channel_id,
380 const SendDataParams& params,
Bjorn Mellemeb2c6412018-10-31 15:25:32 -0700381 const rtc::CopyOnWriteBuffer& buffer) = 0;
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700382
383 // Closes |channel_id| gracefully. Returns an error if |channel_id| is not
384 // open. Data sent after the closing procedure begins will not be
385 // transmitted. The channel becomes closed after pending data is transmitted.
Bjorn Mellemeb2c6412018-10-31 15:25:32 -0700386 virtual RTCError CloseChannel(int channel_id) = 0;
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700387
388 // Sets a sink for data messages and channel state callbacks. Before media
389 // transport is destroyed, the sink must be unregistered by setting it to
390 // nullptr.
Bjorn Mellemeb2c6412018-10-31 15:25:32 -0700391 virtual void SetDataSink(DataChannelSink* sink) = 0;
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700392
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400393 // TODO(sukhanov): RtcEventLogs.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400394};
395
396// If media transport factory is set in peer connection factory, it will be
397// used to create media transport for sending/receiving encoded frames and
398// this transport will be used instead of default RTP/SRTP transport.
399//
400// Currently Media Transport negotiation is not supported in SDP.
401// If application is using media transport, it must negotiate it before
402// setting media transport factory in peer connection.
403class MediaTransportFactory {
404 public:
405 virtual ~MediaTransportFactory() = default;
406
407 // Creates media transport.
408 // - Does not take ownership of packet_transport or network_thread.
409 // - Does not support group calls, in 1:1 call one side must set
410 // is_caller = true and another is_caller = false.
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -0700411 // TODO(bugs.webrtc.org/9938) This constructor will be removed and replaced
412 // with the one below.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400413 virtual RTCErrorOr<std::unique_ptr<MediaTransportInterface>>
414 CreateMediaTransport(rtc::PacketTransportInternal* packet_transport,
415 rtc::Thread* network_thread,
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -0700416 bool is_caller);
417
418 // Creates media transport.
419 // - Does not take ownership of packet_transport or network_thread.
420 // TODO(bugs.webrtc.org/9938): remove default implementation once all children
421 // override it.
422 virtual RTCErrorOr<std::unique_ptr<MediaTransportInterface>>
423 CreateMediaTransport(rtc::PacketTransportInternal* packet_transport,
424 rtc::Thread* network_thread,
Piotr (Peter) Slatalaed7b8b12018-10-29 10:43:16 -0700425 const MediaTransportSettings& settings);
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400426};
427
428} // namespace webrtc
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400429#endif // API_MEDIA_TRANSPORT_INTERFACE_H_