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