Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame^] | 1 | /* |
| 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" |
| 26 | #include "common_types.h" // NOLINT(build/include) |
| 27 | |
| 28 | namespace rtc { |
| 29 | class PacketTransportInternal; |
| 30 | class Thread; |
| 31 | } // namespace rtc |
| 32 | |
| 33 | namespace webrtc { |
| 34 | |
| 35 | // Represents encoded audio frame in any encoding (type of encoding is opaque). |
| 36 | // To avoid copying of encoded data use move semantics when passing by value. |
| 37 | class MediaTransportEncodedAudioFrame { |
| 38 | public: |
| 39 | enum class FrameType { |
| 40 | // Normal audio frame (equivalent to webrtc::kAudioFrameSpeech). |
| 41 | kSpeech, |
| 42 | |
| 43 | // DTX frame (equivalent to webrtc::kAudioFrameCN). |
| 44 | kDiscountinuousTransmission, |
| 45 | }; |
| 46 | |
| 47 | MediaTransportEncodedAudioFrame( |
| 48 | // Audio sampling rate, for example 48000. |
| 49 | int sampling_rate_hz, |
| 50 | |
| 51 | // Starting sample index of the frame, i.e. how many audio samples were |
| 52 | // before this frame since the beginning of the call or beginning of time |
| 53 | // in one channel (the starting point should not matter for NetEq). In |
| 54 | // WebRTC it is used as a timestamp of the frame. |
| 55 | // TODO(sukhanov): Starting_sample_index is currently adjusted on the |
| 56 | // receiver side in RTP path. Non-RTP implementations should preserve it. |
| 57 | // For NetEq initial offset should not matter so we should consider fixing |
| 58 | // RTP path. |
| 59 | int starting_sample_index, |
| 60 | |
| 61 | // Number of audio samples in audio frame in 1 channel. |
| 62 | int samples_per_channel, |
| 63 | |
| 64 | // Sequence number of the frame in the order sent, it is currently |
| 65 | // required by NetEq, but we can fix NetEq, because starting_sample_index |
| 66 | // should be enough. |
| 67 | int sequence_number, |
| 68 | |
| 69 | // If audio frame is a speech or discontinued transmission. |
| 70 | FrameType frame_type, |
| 71 | |
| 72 | // Opaque payload type. In RTP codepath payload type is stored in RTP |
| 73 | // header. In other implementations it should be simply passed through the |
| 74 | // wire -- it's needed for decoder. |
| 75 | uint8_t payload_type, |
| 76 | |
| 77 | // Vector with opaque encoded data. |
| 78 | std::vector<uint8_t> encoded_data) |
| 79 | : sampling_rate_hz_(sampling_rate_hz), |
| 80 | starting_sample_index_(starting_sample_index), |
| 81 | samples_per_channel_(samples_per_channel), |
| 82 | sequence_number_(sequence_number), |
| 83 | frame_type_(frame_type), |
| 84 | payload_type_(payload_type), |
| 85 | encoded_data_(std::move(encoded_data)) {} |
| 86 | |
| 87 | // Getters. |
| 88 | int sampling_rate_hz() const { return sampling_rate_hz_; } |
| 89 | int starting_sample_index() const { return starting_sample_index_; } |
| 90 | int samples_per_channel() const { return samples_per_channel_; } |
| 91 | int sequence_number() const { return sequence_number_; } |
| 92 | |
| 93 | uint8_t payload_type() const { return payload_type_; } |
| 94 | FrameType frame_type() const { return frame_type_; } |
| 95 | |
| 96 | rtc::ArrayView<const uint8_t> encoded_data() const { return encoded_data_; } |
| 97 | |
| 98 | private: |
| 99 | int sampling_rate_hz_; |
| 100 | int starting_sample_index_; |
| 101 | int samples_per_channel_; |
| 102 | |
| 103 | // TODO(sukhanov): Refactor NetEq so we don't need sequence number. |
| 104 | // Having sample_index and sample_count should be enough. |
| 105 | int sequence_number_; |
| 106 | |
| 107 | FrameType frame_type_; |
| 108 | |
| 109 | // TODO(sukhanov): Consider enumerating allowed encodings and store enum |
| 110 | // instead of uint payload_type. |
| 111 | uint8_t payload_type_; |
| 112 | |
| 113 | std::vector<uint8_t> encoded_data_; |
| 114 | }; |
| 115 | |
| 116 | // Interface for receiving encoded audio frames from MediaTransportInterface |
| 117 | // implementations. |
| 118 | class MediaTransportAudioSinkInterface { |
| 119 | public: |
| 120 | virtual ~MediaTransportAudioSinkInterface() = default; |
| 121 | |
| 122 | // Called when new encoded audio frame is received. |
| 123 | virtual void OnData(uint64_t channel_id, |
| 124 | MediaTransportEncodedAudioFrame frame) = 0; |
| 125 | }; |
| 126 | |
| 127 | // Media transport interface for sending / receiving encoded audio/video frames |
| 128 | // and receiving bandwidth estimate update from congestion control. |
| 129 | class MediaTransportInterface { |
| 130 | public: |
| 131 | virtual ~MediaTransportInterface() = default; |
| 132 | |
| 133 | // Start asynchronous send of audio frame. |
| 134 | virtual RTCError SendAudioFrame(uint64_t channel_id, |
| 135 | MediaTransportEncodedAudioFrame frame) = 0; |
| 136 | |
| 137 | // Sets audio sink. Sink should be unset by calling |
| 138 | // SetReceiveAudioSink(nullptr) before the media transport is destroyed or |
| 139 | // before new sink is set. |
| 140 | virtual void SetReceiveAudioSink(MediaTransportAudioSinkInterface* sink) = 0; |
| 141 | |
| 142 | // TODO(sukhanov): RtcEventLogs. |
| 143 | // TODO(sukhanov): Video interfaces. |
| 144 | // TODO(sukhanov): Bandwidth updates. |
| 145 | }; |
| 146 | |
| 147 | // If media transport factory is set in peer connection factory, it will be |
| 148 | // used to create media transport for sending/receiving encoded frames and |
| 149 | // this transport will be used instead of default RTP/SRTP transport. |
| 150 | // |
| 151 | // Currently Media Transport negotiation is not supported in SDP. |
| 152 | // If application is using media transport, it must negotiate it before |
| 153 | // setting media transport factory in peer connection. |
| 154 | class MediaTransportFactory { |
| 155 | public: |
| 156 | virtual ~MediaTransportFactory() = default; |
| 157 | |
| 158 | // Creates media transport. |
| 159 | // - Does not take ownership of packet_transport or network_thread. |
| 160 | // - Does not support group calls, in 1:1 call one side must set |
| 161 | // is_caller = true and another is_caller = false. |
| 162 | virtual RTCErrorOr<std::unique_ptr<MediaTransportInterface>> |
| 163 | CreateMediaTransport(rtc::PacketTransportInternal* packet_transport, |
| 164 | rtc::Thread* network_thread, |
| 165 | bool is_caller) = 0; |
| 166 | }; |
| 167 | |
| 168 | } // namespace webrtc |
| 169 | |
| 170 | #endif // API_MEDIA_TRANSPORT_INTERFACE_H_ |