blob: ca26851414da2de72655658b96872c433400e556 [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)
31
32namespace rtc {
33class PacketTransportInternal;
34class Thread;
35} // namespace rtc
36
37namespace webrtc {
38
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -070039// A collection of settings for creation of media transport.
40struct MediaTransportSettings final {
41 MediaTransportSettings();
Piotr (Peter) Slatalaed7b8b12018-10-29 10:43:16 -070042 MediaTransportSettings(const MediaTransportSettings&);
43 MediaTransportSettings& operator=(const MediaTransportSettings&);
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -070044 ~MediaTransportSettings();
45
46 // Group calls are not currently supported, in 1:1 call one side must set
47 // is_caller = true and another is_caller = false.
48 bool is_caller;
49
50 // Must be set if a pre-shared key is used for the call.
51 absl::optional<std::string> pre_shared_key;
52};
53
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040054// Represents encoded audio frame in any encoding (type of encoding is opaque).
55// To avoid copying of encoded data use move semantics when passing by value.
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -070056class MediaTransportEncodedAudioFrame final {
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040057 public:
58 enum class FrameType {
59 // Normal audio frame (equivalent to webrtc::kAudioFrameSpeech).
60 kSpeech,
61
62 // DTX frame (equivalent to webrtc::kAudioFrameCN).
Niels Möller7d76a312018-10-26 12:57:07 +020063 // DTX frame (equivalent to webrtc::kAudioFrameCN).
64 kDiscontinuousTransmission,
65 // TODO(nisse): Mis-spelled version, update users, then delete.
66 kDiscountinuousTransmission = kDiscontinuousTransmission,
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040067 };
68
69 MediaTransportEncodedAudioFrame(
70 // Audio sampling rate, for example 48000.
71 int sampling_rate_hz,
72
73 // Starting sample index of the frame, i.e. how many audio samples were
74 // before this frame since the beginning of the call or beginning of time
75 // in one channel (the starting point should not matter for NetEq). In
76 // WebRTC it is used as a timestamp of the frame.
77 // TODO(sukhanov): Starting_sample_index is currently adjusted on the
78 // receiver side in RTP path. Non-RTP implementations should preserve it.
79 // For NetEq initial offset should not matter so we should consider fixing
80 // RTP path.
81 int starting_sample_index,
82
83 // Number of audio samples in audio frame in 1 channel.
84 int samples_per_channel,
85
86 // Sequence number of the frame in the order sent, it is currently
87 // required by NetEq, but we can fix NetEq, because starting_sample_index
88 // should be enough.
89 int sequence_number,
90
91 // If audio frame is a speech or discontinued transmission.
92 FrameType frame_type,
93
94 // Opaque payload type. In RTP codepath payload type is stored in RTP
95 // header. In other implementations it should be simply passed through the
96 // wire -- it's needed for decoder.
97 uint8_t payload_type,
98
99 // Vector with opaque encoded data.
Niels Möller3a742392018-10-08 11:13:58 +0200100 std::vector<uint8_t> encoded_data);
101
102 ~MediaTransportEncodedAudioFrame();
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700103 MediaTransportEncodedAudioFrame(const MediaTransportEncodedAudioFrame&);
104 MediaTransportEncodedAudioFrame& operator=(
105 const MediaTransportEncodedAudioFrame& other);
106 MediaTransportEncodedAudioFrame& operator=(
107 MediaTransportEncodedAudioFrame&& other);
108 MediaTransportEncodedAudioFrame(MediaTransportEncodedAudioFrame&&);
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400109
110 // Getters.
111 int sampling_rate_hz() const { return sampling_rate_hz_; }
112 int starting_sample_index() const { return starting_sample_index_; }
113 int samples_per_channel() const { return samples_per_channel_; }
114 int sequence_number() const { return sequence_number_; }
115
116 uint8_t payload_type() const { return payload_type_; }
117 FrameType frame_type() const { return frame_type_; }
118
119 rtc::ArrayView<const uint8_t> encoded_data() const { return encoded_data_; }
120
121 private:
122 int sampling_rate_hz_;
123 int starting_sample_index_;
124 int samples_per_channel_;
125
126 // TODO(sukhanov): Refactor NetEq so we don't need sequence number.
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700127 // Having sample_index and samples_per_channel should be enough.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400128 int sequence_number_;
129
130 FrameType frame_type_;
131
132 // TODO(sukhanov): Consider enumerating allowed encodings and store enum
133 // instead of uint payload_type.
134 uint8_t payload_type_;
135
136 std::vector<uint8_t> encoded_data_;
137};
138
139// Interface for receiving encoded audio frames from MediaTransportInterface
140// implementations.
141class MediaTransportAudioSinkInterface {
142 public:
143 virtual ~MediaTransportAudioSinkInterface() = default;
144
145 // Called when new encoded audio frame is received.
146 virtual void OnData(uint64_t channel_id,
147 MediaTransportEncodedAudioFrame frame) = 0;
148};
149
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700150// Represents encoded video frame, along with the codec information.
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700151class MediaTransportEncodedVideoFrame final {
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700152 public:
153 MediaTransportEncodedVideoFrame(int64_t frame_id,
154 std::vector<int64_t> referenced_frame_ids,
155 VideoCodecType codec_type,
Niels Möller3a742392018-10-08 11:13:58 +0200156 const webrtc::EncodedImage& encoded_image);
157 ~MediaTransportEncodedVideoFrame();
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700158 MediaTransportEncodedVideoFrame(const MediaTransportEncodedVideoFrame&);
159 MediaTransportEncodedVideoFrame& operator=(
160 const MediaTransportEncodedVideoFrame& other);
161 MediaTransportEncodedVideoFrame& operator=(
162 MediaTransportEncodedVideoFrame&& other);
163 MediaTransportEncodedVideoFrame(MediaTransportEncodedVideoFrame&&);
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700164
165 VideoCodecType codec_type() const { return codec_type_; }
166 const webrtc::EncodedImage& encoded_image() const { return encoded_image_; }
167
168 int64_t frame_id() const { return frame_id_; }
169 const std::vector<int64_t>& referenced_frame_ids() const {
170 return referenced_frame_ids_;
171 }
172
173 private:
174 VideoCodecType codec_type_;
175
176 // The buffer is not owned by the encoded image by default. On the sender it
177 // means that it will need to make a copy of it if it wants to deliver it
178 // asynchronously.
179 webrtc::EncodedImage encoded_image_;
180
181 // Frame id uniquely identifies a frame in a stream. It needs to be unique in
182 // a given time window (i.e. technically unique identifier for the lifetime of
183 // the connection is not needed, but you need to guarantee that remote side
184 // got rid of the previous frame_id if you plan to reuse it).
185 //
186 // It is required by a remote jitter buffer, and is the same as
187 // EncodedFrame::id::picture_id.
188 //
189 // This data must be opaque to the media transport, and media transport should
190 // itself not make any assumptions about what it is and its uniqueness.
191 int64_t frame_id_;
192
193 // A single frame might depend on other frames. This is set of identifiers on
194 // which the current frame depends.
195 std::vector<int64_t> referenced_frame_ids_;
196};
197
198// Interface for receiving encoded video frames from MediaTransportInterface
199// implementations.
200class MediaTransportVideoSinkInterface {
201 public:
202 virtual ~MediaTransportVideoSinkInterface() = default;
203
204 // Called when new encoded video frame is received.
205 virtual void OnData(uint64_t channel_id,
206 MediaTransportEncodedVideoFrame frame) = 0;
207
208 // Called when the request for keyframe is received.
209 virtual void OnKeyFrameRequested(uint64_t channel_id) = 0;
210};
211
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700212// State of the media transport. Media transport begins in the pending state.
213// It transitions to writable when it is ready to send media. It may transition
214// back to pending if the connection is blocked. It may transition to closed at
215// any time. Closed is terminal: a transport will never re-open once closed.
216enum class MediaTransportState {
217 kPending,
218 kWritable,
219 kClosed,
220};
221
222// Callback invoked whenever the state of the media transport changes.
223class MediaTransportStateCallback {
224 public:
225 virtual ~MediaTransportStateCallback() = default;
226
227 // Invoked whenever the state of the media transport changes.
228 virtual void OnStateChanged(MediaTransportState state) = 0;
229};
230
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400231// Media transport interface for sending / receiving encoded audio/video frames
232// and receiving bandwidth estimate update from congestion control.
233class MediaTransportInterface {
234 public:
235 virtual ~MediaTransportInterface() = default;
236
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700237 // Start asynchronous send of audio frame. The status returned by this method
238 // only pertains to the synchronous operations (e.g.
239 // serialization/packetization), not to the asynchronous operation.
240
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400241 virtual RTCError SendAudioFrame(uint64_t channel_id,
242 MediaTransportEncodedAudioFrame frame) = 0;
243
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700244 // Start asynchronous send of video frame. The status returned by this method
245 // only pertains to the synchronous operations (e.g.
246 // serialization/packetization), not to the asynchronous operation.
247 virtual RTCError SendVideoFrame(
248 uint64_t channel_id,
249 const MediaTransportEncodedVideoFrame& frame) = 0;
250
251 // Requests a keyframe for the particular channel (stream). The caller should
252 // check that the keyframe is not present in a jitter buffer already (i.e.
253 // don't request a keyframe if there is one that you will get from the jitter
254 // buffer in a moment).
255 virtual RTCError RequestKeyFrame(uint64_t channel_id) = 0;
256
257 // Sets audio sink. Sink must be unset by calling SetReceiveAudioSink(nullptr)
258 // before the media transport is destroyed or before new sink is set.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400259 virtual void SetReceiveAudioSink(MediaTransportAudioSinkInterface* sink) = 0;
260
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700261 // Registers a video sink. Before destruction of media transport, you must
262 // pass a nullptr.
263 virtual void SetReceiveVideoSink(MediaTransportVideoSinkInterface* sink) = 0;
264
Piotr (Peter) Slatala6b9d8232018-10-26 07:59:46 -0700265 // Sets a target bitrate observer. Before media transport is destructed
266 // the observer must be unregistered (set to nullptr).
267 // A newly registered observer will be called back with the latest recorded
268 // target rate, if available.
269 virtual void SetTargetTransferRateObserver(
270 webrtc::TargetTransferRateObserver* observer) = 0;
271
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700272 // Sets a state observer callback. Before media transport is destroyed, the
273 // callback must be unregistered by setting it to nullptr.
274 // A newly registered callback will be called with the current state.
275 // Media transport does not invoke this callback concurrently.
276 // TODO(mellem): Make this pure virtual once all implementations support it.
277 virtual void SetMediaTransportStateCallback(
278 MediaTransportStateCallback* callback) {}
279
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400280 // TODO(sukhanov): RtcEventLogs.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400281};
282
283// If media transport factory is set in peer connection factory, it will be
284// used to create media transport for sending/receiving encoded frames and
285// this transport will be used instead of default RTP/SRTP transport.
286//
287// Currently Media Transport negotiation is not supported in SDP.
288// If application is using media transport, it must negotiate it before
289// setting media transport factory in peer connection.
290class MediaTransportFactory {
291 public:
292 virtual ~MediaTransportFactory() = default;
293
294 // Creates media transport.
295 // - Does not take ownership of packet_transport or network_thread.
296 // - Does not support group calls, in 1:1 call one side must set
297 // is_caller = true and another is_caller = false.
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -0700298 // TODO(bugs.webrtc.org/9938) This constructor will be removed and replaced
299 // with the one below.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400300 virtual RTCErrorOr<std::unique_ptr<MediaTransportInterface>>
301 CreateMediaTransport(rtc::PacketTransportInternal* packet_transport,
302 rtc::Thread* network_thread,
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -0700303 bool is_caller);
304
305 // Creates media transport.
306 // - Does not take ownership of packet_transport or network_thread.
307 // TODO(bugs.webrtc.org/9938): remove default implementation once all children
308 // override it.
309 virtual RTCErrorOr<std::unique_ptr<MediaTransportInterface>>
310 CreateMediaTransport(rtc::PacketTransportInternal* packet_transport,
311 rtc::Thread* network_thread,
Piotr (Peter) Slatalaed7b8b12018-10-29 10:43:16 -0700312 const MediaTransportSettings& settings);
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400313};
314
315} // namespace webrtc
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400316#endif // API_MEDIA_TRANSPORT_INTERFACE_H_