blob: b388ac9218d4df79bfcc4e265c9e793e3e65fc47 [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
20#include <memory>
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -070021#include <string>
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040022#include <utility>
23#include <vector>
24
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -070025#include "absl/types/optional.h"
Yves Gerey988cc082018-10-23 12:03:01 +020026#include "api/array_view.h"
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040027#include "api/rtcerror.h"
Niels Möller3a742392018-10-08 11:13:58 +020028#include "api/video/encoded_image.h"
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040029#include "common_types.h" // NOLINT(build/include)
30
31namespace rtc {
32class PacketTransportInternal;
33class Thread;
34} // namespace rtc
35
36namespace webrtc {
37
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -070038// A collection of settings for creation of media transport.
39struct MediaTransportSettings final {
40 MediaTransportSettings();
41 ~MediaTransportSettings();
42
43 // Group calls are not currently supported, in 1:1 call one side must set
44 // is_caller = true and another is_caller = false.
45 bool is_caller;
46
47 // Must be set if a pre-shared key is used for the call.
48 absl::optional<std::string> pre_shared_key;
49};
50
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040051// Represents encoded audio frame in any encoding (type of encoding is opaque).
52// To avoid copying of encoded data use move semantics when passing by value.
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -070053class MediaTransportEncodedAudioFrame final {
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040054 public:
55 enum class FrameType {
56 // Normal audio frame (equivalent to webrtc::kAudioFrameSpeech).
57 kSpeech,
58
59 // DTX frame (equivalent to webrtc::kAudioFrameCN).
Niels Möller7d76a312018-10-26 12:57:07 +020060 // DTX frame (equivalent to webrtc::kAudioFrameCN).
61 kDiscontinuousTransmission,
62 // TODO(nisse): Mis-spelled version, update users, then delete.
63 kDiscountinuousTransmission = kDiscontinuousTransmission,
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040064 };
65
66 MediaTransportEncodedAudioFrame(
67 // Audio sampling rate, for example 48000.
68 int sampling_rate_hz,
69
70 // Starting sample index of the frame, i.e. how many audio samples were
71 // before this frame since the beginning of the call or beginning of time
72 // in one channel (the starting point should not matter for NetEq). In
73 // WebRTC it is used as a timestamp of the frame.
74 // TODO(sukhanov): Starting_sample_index is currently adjusted on the
75 // receiver side in RTP path. Non-RTP implementations should preserve it.
76 // For NetEq initial offset should not matter so we should consider fixing
77 // RTP path.
78 int starting_sample_index,
79
80 // Number of audio samples in audio frame in 1 channel.
81 int samples_per_channel,
82
83 // Sequence number of the frame in the order sent, it is currently
84 // required by NetEq, but we can fix NetEq, because starting_sample_index
85 // should be enough.
86 int sequence_number,
87
88 // If audio frame is a speech or discontinued transmission.
89 FrameType frame_type,
90
91 // Opaque payload type. In RTP codepath payload type is stored in RTP
92 // header. In other implementations it should be simply passed through the
93 // wire -- it's needed for decoder.
94 uint8_t payload_type,
95
96 // Vector with opaque encoded data.
Niels Möller3a742392018-10-08 11:13:58 +020097 std::vector<uint8_t> encoded_data);
98
99 ~MediaTransportEncodedAudioFrame();
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700100 MediaTransportEncodedAudioFrame(const MediaTransportEncodedAudioFrame&);
101 MediaTransportEncodedAudioFrame& operator=(
102 const MediaTransportEncodedAudioFrame& other);
103 MediaTransportEncodedAudioFrame& operator=(
104 MediaTransportEncodedAudioFrame&& other);
105 MediaTransportEncodedAudioFrame(MediaTransportEncodedAudioFrame&&);
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400106
107 // Getters.
108 int sampling_rate_hz() const { return sampling_rate_hz_; }
109 int starting_sample_index() const { return starting_sample_index_; }
110 int samples_per_channel() const { return samples_per_channel_; }
111 int sequence_number() const { return sequence_number_; }
112
113 uint8_t payload_type() const { return payload_type_; }
114 FrameType frame_type() const { return frame_type_; }
115
116 rtc::ArrayView<const uint8_t> encoded_data() const { return encoded_data_; }
117
118 private:
119 int sampling_rate_hz_;
120 int starting_sample_index_;
121 int samples_per_channel_;
122
123 // TODO(sukhanov): Refactor NetEq so we don't need sequence number.
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700124 // Having sample_index and samples_per_channel should be enough.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400125 int sequence_number_;
126
127 FrameType frame_type_;
128
129 // TODO(sukhanov): Consider enumerating allowed encodings and store enum
130 // instead of uint payload_type.
131 uint8_t payload_type_;
132
133 std::vector<uint8_t> encoded_data_;
134};
135
136// Interface for receiving encoded audio frames from MediaTransportInterface
137// implementations.
138class MediaTransportAudioSinkInterface {
139 public:
140 virtual ~MediaTransportAudioSinkInterface() = default;
141
142 // Called when new encoded audio frame is received.
143 virtual void OnData(uint64_t channel_id,
144 MediaTransportEncodedAudioFrame frame) = 0;
145};
146
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700147// Represents encoded video frame, along with the codec information.
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700148class MediaTransportEncodedVideoFrame final {
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700149 public:
150 MediaTransportEncodedVideoFrame(int64_t frame_id,
151 std::vector<int64_t> referenced_frame_ids,
152 VideoCodecType codec_type,
Niels Möller3a742392018-10-08 11:13:58 +0200153 const webrtc::EncodedImage& encoded_image);
154 ~MediaTransportEncodedVideoFrame();
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700155 MediaTransportEncodedVideoFrame(const MediaTransportEncodedVideoFrame&);
156 MediaTransportEncodedVideoFrame& operator=(
157 const MediaTransportEncodedVideoFrame& other);
158 MediaTransportEncodedVideoFrame& operator=(
159 MediaTransportEncodedVideoFrame&& other);
160 MediaTransportEncodedVideoFrame(MediaTransportEncodedVideoFrame&&);
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700161
162 VideoCodecType codec_type() const { return codec_type_; }
163 const webrtc::EncodedImage& encoded_image() const { return encoded_image_; }
164
165 int64_t frame_id() const { return frame_id_; }
166 const std::vector<int64_t>& referenced_frame_ids() const {
167 return referenced_frame_ids_;
168 }
169
170 private:
171 VideoCodecType codec_type_;
172
173 // The buffer is not owned by the encoded image by default. On the sender it
174 // means that it will need to make a copy of it if it wants to deliver it
175 // asynchronously.
176 webrtc::EncodedImage encoded_image_;
177
178 // Frame id uniquely identifies a frame in a stream. It needs to be unique in
179 // a given time window (i.e. technically unique identifier for the lifetime of
180 // the connection is not needed, but you need to guarantee that remote side
181 // got rid of the previous frame_id if you plan to reuse it).
182 //
183 // It is required by a remote jitter buffer, and is the same as
184 // EncodedFrame::id::picture_id.
185 //
186 // This data must be opaque to the media transport, and media transport should
187 // itself not make any assumptions about what it is and its uniqueness.
188 int64_t frame_id_;
189
190 // A single frame might depend on other frames. This is set of identifiers on
191 // which the current frame depends.
192 std::vector<int64_t> referenced_frame_ids_;
193};
194
195// Interface for receiving encoded video frames from MediaTransportInterface
196// implementations.
197class MediaTransportVideoSinkInterface {
198 public:
199 virtual ~MediaTransportVideoSinkInterface() = default;
200
201 // Called when new encoded video frame is received.
202 virtual void OnData(uint64_t channel_id,
203 MediaTransportEncodedVideoFrame frame) = 0;
204
205 // Called when the request for keyframe is received.
206 virtual void OnKeyFrameRequested(uint64_t channel_id) = 0;
207};
208
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400209// Media transport interface for sending / receiving encoded audio/video frames
210// and receiving bandwidth estimate update from congestion control.
211class MediaTransportInterface {
212 public:
213 virtual ~MediaTransportInterface() = default;
214
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700215 // Start asynchronous send of audio frame. The status returned by this method
216 // only pertains to the synchronous operations (e.g.
217 // serialization/packetization), not to the asynchronous operation.
218
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400219 virtual RTCError SendAudioFrame(uint64_t channel_id,
220 MediaTransportEncodedAudioFrame frame) = 0;
221
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700222 // Start asynchronous send of video frame. The status returned by this method
223 // only pertains to the synchronous operations (e.g.
224 // serialization/packetization), not to the asynchronous operation.
225 virtual RTCError SendVideoFrame(
226 uint64_t channel_id,
227 const MediaTransportEncodedVideoFrame& frame) = 0;
228
229 // Requests a keyframe for the particular channel (stream). The caller should
230 // check that the keyframe is not present in a jitter buffer already (i.e.
231 // don't request a keyframe if there is one that you will get from the jitter
232 // buffer in a moment).
233 virtual RTCError RequestKeyFrame(uint64_t channel_id) = 0;
234
235 // Sets audio sink. Sink must be unset by calling SetReceiveAudioSink(nullptr)
236 // before the media transport is destroyed or before new sink is set.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400237 virtual void SetReceiveAudioSink(MediaTransportAudioSinkInterface* sink) = 0;
238
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700239 // Registers a video sink. Before destruction of media transport, you must
240 // pass a nullptr.
241 virtual void SetReceiveVideoSink(MediaTransportVideoSinkInterface* sink) = 0;
242
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400243 // TODO(sukhanov): RtcEventLogs.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400244 // TODO(sukhanov): Bandwidth updates.
245};
246
247// If media transport factory is set in peer connection factory, it will be
248// used to create media transport for sending/receiving encoded frames and
249// this transport will be used instead of default RTP/SRTP transport.
250//
251// Currently Media Transport negotiation is not supported in SDP.
252// If application is using media transport, it must negotiate it before
253// setting media transport factory in peer connection.
254class MediaTransportFactory {
255 public:
256 virtual ~MediaTransportFactory() = default;
257
258 // Creates media transport.
259 // - Does not take ownership of packet_transport or network_thread.
260 // - Does not support group calls, in 1:1 call one side must set
261 // is_caller = true and another is_caller = false.
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -0700262 // TODO(bugs.webrtc.org/9938) This constructor will be removed and replaced
263 // with the one below.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400264 virtual RTCErrorOr<std::unique_ptr<MediaTransportInterface>>
265 CreateMediaTransport(rtc::PacketTransportInternal* packet_transport,
266 rtc::Thread* network_thread,
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -0700267 bool is_caller);
268
269 // Creates media transport.
270 // - Does not take ownership of packet_transport or network_thread.
271 // TODO(bugs.webrtc.org/9938): remove default implementation once all children
272 // override it.
273 virtual RTCErrorOr<std::unique_ptr<MediaTransportInterface>>
274 CreateMediaTransport(rtc::PacketTransportInternal* packet_transport,
275 rtc::Thread* network_thread,
276 const MediaTransportSettings settings);
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400277};
278
279} // namespace webrtc
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400280#endif // API_MEDIA_TRANSPORT_INTERFACE_H_