blob: e0e7453067f2322403ae64b2a56cec41dd429738 [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"
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040030#include "common_types.h" // NOLINT(build/include)
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -070031#include "rtc_base/copyonwritebuffer.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
142// Interface for receiving encoded audio frames from MediaTransportInterface
143// implementations.
144class MediaTransportAudioSinkInterface {
145 public:
146 virtual ~MediaTransportAudioSinkInterface() = default;
147
148 // Called when new encoded audio frame is received.
149 virtual void OnData(uint64_t channel_id,
150 MediaTransportEncodedAudioFrame frame) = 0;
151};
152
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700153// Represents encoded video frame, along with the codec information.
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700154class MediaTransportEncodedVideoFrame final {
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700155 public:
156 MediaTransportEncodedVideoFrame(int64_t frame_id,
157 std::vector<int64_t> referenced_frame_ids,
158 VideoCodecType codec_type,
Niels Möller3a742392018-10-08 11:13:58 +0200159 const webrtc::EncodedImage& encoded_image);
160 ~MediaTransportEncodedVideoFrame();
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700161 MediaTransportEncodedVideoFrame(const MediaTransportEncodedVideoFrame&);
162 MediaTransportEncodedVideoFrame& operator=(
163 const MediaTransportEncodedVideoFrame& other);
164 MediaTransportEncodedVideoFrame& operator=(
165 MediaTransportEncodedVideoFrame&& other);
166 MediaTransportEncodedVideoFrame(MediaTransportEncodedVideoFrame&&);
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700167
168 VideoCodecType codec_type() const { return codec_type_; }
169 const webrtc::EncodedImage& encoded_image() const { return encoded_image_; }
170
171 int64_t frame_id() const { return frame_id_; }
172 const std::vector<int64_t>& referenced_frame_ids() const {
173 return referenced_frame_ids_;
174 }
175
176 private:
177 VideoCodecType codec_type_;
178
179 // The buffer is not owned by the encoded image by default. On the sender it
180 // means that it will need to make a copy of it if it wants to deliver it
181 // asynchronously.
182 webrtc::EncodedImage encoded_image_;
183
184 // Frame id uniquely identifies a frame in a stream. It needs to be unique in
185 // a given time window (i.e. technically unique identifier for the lifetime of
186 // the connection is not needed, but you need to guarantee that remote side
187 // got rid of the previous frame_id if you plan to reuse it).
188 //
189 // It is required by a remote jitter buffer, and is the same as
190 // EncodedFrame::id::picture_id.
191 //
192 // This data must be opaque to the media transport, and media transport should
193 // itself not make any assumptions about what it is and its uniqueness.
194 int64_t frame_id_;
195
196 // A single frame might depend on other frames. This is set of identifiers on
197 // which the current frame depends.
198 std::vector<int64_t> referenced_frame_ids_;
199};
200
201// Interface for receiving encoded video frames from MediaTransportInterface
202// implementations.
203class MediaTransportVideoSinkInterface {
204 public:
205 virtual ~MediaTransportVideoSinkInterface() = default;
206
207 // Called when new encoded video frame is received.
208 virtual void OnData(uint64_t channel_id,
209 MediaTransportEncodedVideoFrame frame) = 0;
210
211 // Called when the request for keyframe is received.
212 virtual void OnKeyFrameRequested(uint64_t channel_id) = 0;
213};
214
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700215// State of the media transport. Media transport begins in the pending state.
216// It transitions to writable when it is ready to send media. It may transition
217// back to pending if the connection is blocked. It may transition to closed at
218// any time. Closed is terminal: a transport will never re-open once closed.
219enum class MediaTransportState {
220 kPending,
221 kWritable,
222 kClosed,
223};
224
225// Callback invoked whenever the state of the media transport changes.
226class MediaTransportStateCallback {
227 public:
228 virtual ~MediaTransportStateCallback() = default;
229
230 // Invoked whenever the state of the media transport changes.
231 virtual void OnStateChanged(MediaTransportState state) = 0;
232};
233
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700234// Supported types of application data messages.
235enum class DataMessageType {
236 // Application data buffer with the binary bit unset.
237 kText,
238
239 // Application data buffer with the binary bit set.
240 kBinary,
241
242 // Transport-agnostic control messages, such as open or open-ack messages.
243 kControl,
244};
245
246// Parameters for sending data. The parameters may change from message to
247// message, even within a single channel. For example, control messages may be
248// sent reliably and in-order, even if the data channel is configured for
249// unreliable delivery.
250struct SendDataParams {
251 SendDataParams();
252
253 DataMessageType type = DataMessageType::kText;
254
255 // Whether to deliver the message in order with respect to other ordered
256 // messages with the same channel_id.
257 bool ordered = false;
258
259 // If set, the maximum number of times this message may be
260 // retransmitted by the transport before it is dropped.
261 // Setting this value to zero disables retransmission.
262 // Must be non-negative. |max_rtx_count| and |max_rtx_ms| may not be set
263 // simultaneously.
264 absl::optional<int> max_rtx_count;
265
266 // If set, the maximum number of milliseconds for which the transport
267 // may retransmit this message before it is dropped.
268 // Setting this value to zero disables retransmission.
269 // Must be non-negative. |max_rtx_count| and |max_rtx_ms| may not be set
270 // simultaneously.
271 absl::optional<int> max_rtx_ms;
272};
273
274// Sink for callbacks related to a data channel.
275class DataChannelSink {
276 public:
277 virtual ~DataChannelSink() = default;
278
279 // Callback issued when data is received by the transport.
280 virtual void OnDataReceived(int channel_id,
281 DataMessageType type,
282 const rtc::CopyOnWriteBuffer& buffer) = 0;
283
284 // Callback issued when a remote data channel begins the closing procedure.
285 // Messages sent after the closing procedure begins will not be transmitted.
286 virtual void OnChannelClosing(int channel_id) = 0;
287
288 // Callback issued when a (remote or local) data channel completes the closing
289 // procedure. Closing channels become closed after all pending data has been
290 // transmitted.
291 virtual void OnChannelClosed(int channel_id) = 0;
292};
293
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400294// Media transport interface for sending / receiving encoded audio/video frames
295// and receiving bandwidth estimate update from congestion control.
296class MediaTransportInterface {
297 public:
298 virtual ~MediaTransportInterface() = default;
299
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700300 // Start asynchronous send of audio frame. The status returned by this method
301 // only pertains to the synchronous operations (e.g.
302 // serialization/packetization), not to the asynchronous operation.
303
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400304 virtual RTCError SendAudioFrame(uint64_t channel_id,
305 MediaTransportEncodedAudioFrame frame) = 0;
306
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700307 // Start asynchronous send of video frame. The status returned by this method
308 // only pertains to the synchronous operations (e.g.
309 // serialization/packetization), not to the asynchronous operation.
310 virtual RTCError SendVideoFrame(
311 uint64_t channel_id,
312 const MediaTransportEncodedVideoFrame& frame) = 0;
313
314 // Requests a keyframe for the particular channel (stream). The caller should
315 // check that the keyframe is not present in a jitter buffer already (i.e.
316 // don't request a keyframe if there is one that you will get from the jitter
317 // buffer in a moment).
318 virtual RTCError RequestKeyFrame(uint64_t channel_id) = 0;
319
320 // Sets audio sink. Sink must be unset by calling SetReceiveAudioSink(nullptr)
321 // before the media transport is destroyed or before new sink is set.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400322 virtual void SetReceiveAudioSink(MediaTransportAudioSinkInterface* sink) = 0;
323
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700324 // Registers a video sink. Before destruction of media transport, you must
325 // pass a nullptr.
326 virtual void SetReceiveVideoSink(MediaTransportVideoSinkInterface* sink) = 0;
327
Piotr (Peter) Slatala6b9d8232018-10-26 07:59:46 -0700328 // Sets a target bitrate observer. Before media transport is destructed
329 // the observer must be unregistered (set to nullptr).
330 // A newly registered observer will be called back with the latest recorded
331 // target rate, if available.
332 virtual void SetTargetTransferRateObserver(
333 webrtc::TargetTransferRateObserver* observer) = 0;
334
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700335 // Sets a state observer callback. Before media transport is destroyed, the
336 // callback must be unregistered by setting it to nullptr.
337 // A newly registered callback will be called with the current state.
338 // Media transport does not invoke this callback concurrently.
339 // TODO(mellem): Make this pure virtual once all implementations support it.
340 virtual void SetMediaTransportStateCallback(
341 MediaTransportStateCallback* callback) {}
342
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700343 // Sends a data buffer to the remote endpoint using the given send parameters.
344 // |buffer| may not be larger than 256 KiB. Returns an error if the send
345 // fails.
346 // TODO(mellem): Make this pure virtual once all implementations support it.
347 virtual RTCError SendData(int channel_id,
348 const SendDataParams& params,
349 const rtc::CopyOnWriteBuffer& buffer);
350
351 // Closes |channel_id| gracefully. Returns an error if |channel_id| is not
352 // open. Data sent after the closing procedure begins will not be
353 // transmitted. The channel becomes closed after pending data is transmitted.
354 // TODO(mellem): Make this pure virtual once all implementations support it.
355 virtual RTCError CloseChannel(int channel_id);
356
357 // Sets a sink for data messages and channel state callbacks. Before media
358 // transport is destroyed, the sink must be unregistered by setting it to
359 // nullptr.
360 // TODO(mellem): Make this pure virtual once all implementations support it.
361 virtual void SetDataSink(DataChannelSink* sink) {}
362
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400363 // TODO(sukhanov): RtcEventLogs.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400364};
365
366// If media transport factory is set in peer connection factory, it will be
367// used to create media transport for sending/receiving encoded frames and
368// this transport will be used instead of default RTP/SRTP transport.
369//
370// Currently Media Transport negotiation is not supported in SDP.
371// If application is using media transport, it must negotiate it before
372// setting media transport factory in peer connection.
373class MediaTransportFactory {
374 public:
375 virtual ~MediaTransportFactory() = default;
376
377 // Creates media transport.
378 // - Does not take ownership of packet_transport or network_thread.
379 // - Does not support group calls, in 1:1 call one side must set
380 // is_caller = true and another is_caller = false.
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -0700381 // TODO(bugs.webrtc.org/9938) This constructor will be removed and replaced
382 // with the one below.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400383 virtual RTCErrorOr<std::unique_ptr<MediaTransportInterface>>
384 CreateMediaTransport(rtc::PacketTransportInternal* packet_transport,
385 rtc::Thread* network_thread,
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -0700386 bool is_caller);
387
388 // Creates media transport.
389 // - Does not take ownership of packet_transport or network_thread.
390 // TODO(bugs.webrtc.org/9938): remove default implementation once all children
391 // override it.
392 virtual RTCErrorOr<std::unique_ptr<MediaTransportInterface>>
393 CreateMediaTransport(rtc::PacketTransportInternal* packet_transport,
394 rtc::Thread* network_thread,
Piotr (Peter) Slatalaed7b8b12018-10-29 10:43:16 -0700395 const MediaTransportSettings& settings);
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400396};
397
398} // namespace webrtc
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400399#endif // API_MEDIA_TRANSPORT_INTERFACE_H_