blob: 154fe4a825fb3c70f61155e8d0b1b8474ba09dc8 [file] [log] [blame]
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -04001/*
2 * Copyright 2018 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11// This is EXPERIMENTAL interface for media transport.
12//
13// The goal is to refactor WebRTC code so that audio and video frames
14// are sent / received through the media transport interface. This will
15// enable different media transport implementations, including QUIC-based
16// media transport.
17
18#ifndef API_MEDIA_TRANSPORT_INTERFACE_H_
19#define API_MEDIA_TRANSPORT_INTERFACE_H_
20
21#include <memory>
22#include <utility>
23#include <vector>
24
25#include "api/rtcerror.h"
Niels Möller3a742392018-10-08 11:13:58 +020026#include "api/video/encoded_image.h"
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040027#include "common_types.h" // NOLINT(build/include)
28
29namespace rtc {
30class PacketTransportInternal;
31class Thread;
32} // namespace rtc
33
34namespace webrtc {
35
36// Represents encoded audio frame in any encoding (type of encoding is opaque).
37// To avoid copying of encoded data use move semantics when passing by value.
38class MediaTransportEncodedAudioFrame {
39 public:
40 enum class FrameType {
41 // Normal audio frame (equivalent to webrtc::kAudioFrameSpeech).
42 kSpeech,
43
44 // DTX frame (equivalent to webrtc::kAudioFrameCN).
45 kDiscountinuousTransmission,
46 };
47
48 MediaTransportEncodedAudioFrame(
49 // Audio sampling rate, for example 48000.
50 int sampling_rate_hz,
51
52 // Starting sample index of the frame, i.e. how many audio samples were
53 // before this frame since the beginning of the call or beginning of time
54 // in one channel (the starting point should not matter for NetEq). In
55 // WebRTC it is used as a timestamp of the frame.
56 // TODO(sukhanov): Starting_sample_index is currently adjusted on the
57 // receiver side in RTP path. Non-RTP implementations should preserve it.
58 // For NetEq initial offset should not matter so we should consider fixing
59 // RTP path.
60 int starting_sample_index,
61
62 // Number of audio samples in audio frame in 1 channel.
63 int samples_per_channel,
64
65 // Sequence number of the frame in the order sent, it is currently
66 // required by NetEq, but we can fix NetEq, because starting_sample_index
67 // should be enough.
68 int sequence_number,
69
70 // If audio frame is a speech or discontinued transmission.
71 FrameType frame_type,
72
73 // Opaque payload type. In RTP codepath payload type is stored in RTP
74 // header. In other implementations it should be simply passed through the
75 // wire -- it's needed for decoder.
76 uint8_t payload_type,
77
78 // Vector with opaque encoded data.
Niels Möller3a742392018-10-08 11:13:58 +020079 std::vector<uint8_t> encoded_data);
80
81 ~MediaTransportEncodedAudioFrame();
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040082
83 // Getters.
84 int sampling_rate_hz() const { return sampling_rate_hz_; }
85 int starting_sample_index() const { return starting_sample_index_; }
86 int samples_per_channel() const { return samples_per_channel_; }
87 int sequence_number() const { return sequence_number_; }
88
89 uint8_t payload_type() const { return payload_type_; }
90 FrameType frame_type() const { return frame_type_; }
91
92 rtc::ArrayView<const uint8_t> encoded_data() const { return encoded_data_; }
93
94 private:
95 int sampling_rate_hz_;
96 int starting_sample_index_;
97 int samples_per_channel_;
98
99 // TODO(sukhanov): Refactor NetEq so we don't need sequence number.
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700100 // Having sample_index and samples_per_channel should be enough.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400101 int sequence_number_;
102
103 FrameType frame_type_;
104
105 // TODO(sukhanov): Consider enumerating allowed encodings and store enum
106 // instead of uint payload_type.
107 uint8_t payload_type_;
108
109 std::vector<uint8_t> encoded_data_;
110};
111
112// Interface for receiving encoded audio frames from MediaTransportInterface
113// implementations.
114class MediaTransportAudioSinkInterface {
115 public:
116 virtual ~MediaTransportAudioSinkInterface() = default;
117
118 // Called when new encoded audio frame is received.
119 virtual void OnData(uint64_t channel_id,
120 MediaTransportEncodedAudioFrame frame) = 0;
121};
122
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700123// Represents encoded video frame, along with the codec information.
124class MediaTransportEncodedVideoFrame {
125 public:
126 MediaTransportEncodedVideoFrame(int64_t frame_id,
127 std::vector<int64_t> referenced_frame_ids,
128 VideoCodecType codec_type,
Niels Möller3a742392018-10-08 11:13:58 +0200129 const webrtc::EncodedImage& encoded_image);
130 ~MediaTransportEncodedVideoFrame();
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700131
132 VideoCodecType codec_type() const { return codec_type_; }
133 const webrtc::EncodedImage& encoded_image() const { return encoded_image_; }
134
135 int64_t frame_id() const { return frame_id_; }
136 const std::vector<int64_t>& referenced_frame_ids() const {
137 return referenced_frame_ids_;
138 }
139
140 private:
141 VideoCodecType codec_type_;
142
143 // The buffer is not owned by the encoded image by default. On the sender it
144 // means that it will need to make a copy of it if it wants to deliver it
145 // asynchronously.
146 webrtc::EncodedImage encoded_image_;
147
148 // Frame id uniquely identifies a frame in a stream. It needs to be unique in
149 // a given time window (i.e. technically unique identifier for the lifetime of
150 // the connection is not needed, but you need to guarantee that remote side
151 // got rid of the previous frame_id if you plan to reuse it).
152 //
153 // It is required by a remote jitter buffer, and is the same as
154 // EncodedFrame::id::picture_id.
155 //
156 // This data must be opaque to the media transport, and media transport should
157 // itself not make any assumptions about what it is and its uniqueness.
158 int64_t frame_id_;
159
160 // A single frame might depend on other frames. This is set of identifiers on
161 // which the current frame depends.
162 std::vector<int64_t> referenced_frame_ids_;
163};
164
165// Interface for receiving encoded video frames from MediaTransportInterface
166// implementations.
167class MediaTransportVideoSinkInterface {
168 public:
169 virtual ~MediaTransportVideoSinkInterface() = default;
170
171 // Called when new encoded video frame is received.
172 virtual void OnData(uint64_t channel_id,
173 MediaTransportEncodedVideoFrame frame) = 0;
174
175 // Called when the request for keyframe is received.
176 virtual void OnKeyFrameRequested(uint64_t channel_id) = 0;
177};
178
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400179// Media transport interface for sending / receiving encoded audio/video frames
180// and receiving bandwidth estimate update from congestion control.
181class MediaTransportInterface {
182 public:
183 virtual ~MediaTransportInterface() = default;
184
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700185 // Start asynchronous send of audio frame. The status returned by this method
186 // only pertains to the synchronous operations (e.g.
187 // serialization/packetization), not to the asynchronous operation.
188
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400189 virtual RTCError SendAudioFrame(uint64_t channel_id,
190 MediaTransportEncodedAudioFrame frame) = 0;
191
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700192 // Start asynchronous send of video frame. The status returned by this method
193 // only pertains to the synchronous operations (e.g.
194 // serialization/packetization), not to the asynchronous operation.
195 virtual RTCError SendVideoFrame(
196 uint64_t channel_id,
197 const MediaTransportEncodedVideoFrame& frame) = 0;
198
199 // Requests a keyframe for the particular channel (stream). The caller should
200 // check that the keyframe is not present in a jitter buffer already (i.e.
201 // don't request a keyframe if there is one that you will get from the jitter
202 // buffer in a moment).
203 virtual RTCError RequestKeyFrame(uint64_t channel_id) = 0;
204
205 // Sets audio sink. Sink must be unset by calling SetReceiveAudioSink(nullptr)
206 // before the media transport is destroyed or before new sink is set.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400207 virtual void SetReceiveAudioSink(MediaTransportAudioSinkInterface* sink) = 0;
208
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700209 // Registers a video sink. Before destruction of media transport, you must
210 // pass a nullptr.
211 virtual void SetReceiveVideoSink(MediaTransportVideoSinkInterface* sink) = 0;
212
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400213 // TODO(sukhanov): RtcEventLogs.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400214 // TODO(sukhanov): Bandwidth updates.
215};
216
217// If media transport factory is set in peer connection factory, it will be
218// used to create media transport for sending/receiving encoded frames and
219// this transport will be used instead of default RTP/SRTP transport.
220//
221// Currently Media Transport negotiation is not supported in SDP.
222// If application is using media transport, it must negotiate it before
223// setting media transport factory in peer connection.
224class MediaTransportFactory {
225 public:
226 virtual ~MediaTransportFactory() = default;
227
228 // Creates media transport.
229 // - Does not take ownership of packet_transport or network_thread.
230 // - Does not support group calls, in 1:1 call one side must set
231 // is_caller = true and another is_caller = false.
232 virtual RTCErrorOr<std::unique_ptr<MediaTransportInterface>>
233 CreateMediaTransport(rtc::PacketTransportInternal* packet_transport,
234 rtc::Thread* network_thread,
235 bool is_caller) = 0;
236};
237
238} // namespace webrtc
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400239#endif // API_MEDIA_TRANSPORT_INTERFACE_H_