blob: 6580370a0ebdd10719f05de98cdd9431ce27779c [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
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400212// Media transport interface for sending / receiving encoded audio/video frames
213// and receiving bandwidth estimate update from congestion control.
214class MediaTransportInterface {
215 public:
216 virtual ~MediaTransportInterface() = default;
217
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700218 // Start asynchronous send of audio frame. The status returned by this method
219 // only pertains to the synchronous operations (e.g.
220 // serialization/packetization), not to the asynchronous operation.
221
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400222 virtual RTCError SendAudioFrame(uint64_t channel_id,
223 MediaTransportEncodedAudioFrame frame) = 0;
224
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700225 // Start asynchronous send of video frame. The status returned by this method
226 // only pertains to the synchronous operations (e.g.
227 // serialization/packetization), not to the asynchronous operation.
228 virtual RTCError SendVideoFrame(
229 uint64_t channel_id,
230 const MediaTransportEncodedVideoFrame& frame) = 0;
231
232 // Requests a keyframe for the particular channel (stream). The caller should
233 // check that the keyframe is not present in a jitter buffer already (i.e.
234 // don't request a keyframe if there is one that you will get from the jitter
235 // buffer in a moment).
236 virtual RTCError RequestKeyFrame(uint64_t channel_id) = 0;
237
238 // Sets audio sink. Sink must be unset by calling SetReceiveAudioSink(nullptr)
239 // before the media transport is destroyed or before new sink is set.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400240 virtual void SetReceiveAudioSink(MediaTransportAudioSinkInterface* sink) = 0;
241
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700242 // Registers a video sink. Before destruction of media transport, you must
243 // pass a nullptr.
244 virtual void SetReceiveVideoSink(MediaTransportVideoSinkInterface* sink) = 0;
245
Piotr (Peter) Slatala6b9d8232018-10-26 07:59:46 -0700246 // Sets a target bitrate observer. Before media transport is destructed
247 // the observer must be unregistered (set to nullptr).
248 // A newly registered observer will be called back with the latest recorded
249 // target rate, if available.
250 virtual void SetTargetTransferRateObserver(
251 webrtc::TargetTransferRateObserver* observer) = 0;
252
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400253 // TODO(sukhanov): RtcEventLogs.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400254};
255
256// If media transport factory is set in peer connection factory, it will be
257// used to create media transport for sending/receiving encoded frames and
258// this transport will be used instead of default RTP/SRTP transport.
259//
260// Currently Media Transport negotiation is not supported in SDP.
261// If application is using media transport, it must negotiate it before
262// setting media transport factory in peer connection.
263class MediaTransportFactory {
264 public:
265 virtual ~MediaTransportFactory() = default;
266
267 // Creates media transport.
268 // - Does not take ownership of packet_transport or network_thread.
269 // - Does not support group calls, in 1:1 call one side must set
270 // is_caller = true and another is_caller = false.
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -0700271 // TODO(bugs.webrtc.org/9938) This constructor will be removed and replaced
272 // with the one below.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400273 virtual RTCErrorOr<std::unique_ptr<MediaTransportInterface>>
274 CreateMediaTransport(rtc::PacketTransportInternal* packet_transport,
275 rtc::Thread* network_thread,
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -0700276 bool is_caller);
277
278 // Creates media transport.
279 // - Does not take ownership of packet_transport or network_thread.
280 // TODO(bugs.webrtc.org/9938): remove default implementation once all children
281 // override it.
282 virtual RTCErrorOr<std::unique_ptr<MediaTransportInterface>>
283 CreateMediaTransport(rtc::PacketTransportInternal* packet_transport,
284 rtc::Thread* network_thread,
Piotr (Peter) Slatalaed7b8b12018-10-29 10:43:16 -0700285 const MediaTransportSettings& settings);
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400286};
287
288} // namespace webrtc
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400289#endif // API_MEDIA_TRANSPORT_INTERFACE_H_