ossu | f515ab8 | 2016-12-07 12:52:58 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 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 | */ |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 10 | #ifndef CALL_CALL_H_ |
| 11 | #define CALL_CALL_H_ |
ossu | f515ab8 | 2016-12-07 12:52:58 | [diff] [blame] | 12 | |
zstein | a5e0df6 | 2017-06-14 18:41:48 | [diff] [blame] | 13 | #include <algorithm> |
zstein | 7cb69d5 | 2017-05-08 18:52:38 | [diff] [blame] | 14 | #include <memory> |
ossu | f515ab8 | 2016-12-07 12:52:58 | [diff] [blame] | 15 | #include <string> |
| 16 | #include <vector> |
| 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 18 | #include "api/rtcerror.h" |
| 19 | #include "call/audio_receive_stream.h" |
| 20 | #include "call/audio_send_stream.h" |
| 21 | #include "call/audio_state.h" |
| 22 | #include "call/flexfec_receive_stream.h" |
| 23 | #include "call/rtp_transport_controller_send_interface.h" |
| 24 | #include "call/video_receive_stream.h" |
| 25 | #include "call/video_send_stream.h" |
Mirko Bonadei | 7120742 | 2017-09-15 11:58:09 | [diff] [blame] | 26 | #include "common_types.h" // NOLINT(build/include) |
Alex Narest | 78609d5 | 2017-10-20 08:37:47 | [diff] [blame] | 27 | #include "rtc_base/bitrateallocationstrategy.h" |
Danil Chapovalov | 292a73e | 2017-12-07 16:00:40 | [diff] [blame^] | 28 | #include "rtc_base/copyonwritebuffer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 29 | #include "rtc_base/networkroute.h" |
| 30 | #include "rtc_base/platform_file.h" |
| 31 | #include "rtc_base/socket.h" |
ossu | f515ab8 | 2016-12-07 12:52:58 | [diff] [blame] | 32 | |
| 33 | namespace webrtc { |
| 34 | |
| 35 | class AudioProcessing; |
| 36 | class RtcEventLog; |
| 37 | |
ossu | f515ab8 | 2016-12-07 12:52:58 | [diff] [blame] | 38 | enum class MediaType { |
| 39 | ANY, |
| 40 | AUDIO, |
| 41 | VIDEO, |
| 42 | DATA |
| 43 | }; |
| 44 | |
zstein | a5e0df6 | 2017-06-14 18:41:48 | [diff] [blame] | 45 | // Like std::min, but considers non-positive values to be unset. |
| 46 | // TODO(zstein): Remove once all callers use rtc::Optional. |
| 47 | template <typename T> |
| 48 | static T MinPositive(T a, T b) { |
| 49 | if (a <= 0) { |
| 50 | return b; |
| 51 | } |
| 52 | if (b <= 0) { |
| 53 | return a; |
| 54 | } |
| 55 | return std::min(a, b); |
| 56 | } |
| 57 | |
ossu | f515ab8 | 2016-12-07 12:52:58 | [diff] [blame] | 58 | class PacketReceiver { |
| 59 | public: |
| 60 | enum DeliveryStatus { |
| 61 | DELIVERY_OK, |
| 62 | DELIVERY_UNKNOWN_SSRC, |
| 63 | DELIVERY_PACKET_ERROR, |
| 64 | }; |
| 65 | |
| 66 | virtual DeliveryStatus DeliverPacket(MediaType media_type, |
Danil Chapovalov | 292a73e | 2017-12-07 16:00:40 | [diff] [blame^] | 67 | rtc::CopyOnWriteBuffer packet, |
ossu | f515ab8 | 2016-12-07 12:52:58 | [diff] [blame] | 68 | const PacketTime& packet_time) = 0; |
| 69 | |
| 70 | protected: |
| 71 | virtual ~PacketReceiver() {} |
| 72 | }; |
| 73 | |
| 74 | // A Call instance can contain several send and/or receive streams. All streams |
| 75 | // are assumed to have the same remote endpoint and will share bitrate estimates |
| 76 | // etc. |
| 77 | class Call { |
| 78 | public: |
| 79 | struct Config { |
| 80 | explicit Config(RtcEventLog* event_log) : event_log(event_log) { |
| 81 | RTC_DCHECK(event_log); |
| 82 | } |
| 83 | |
zhihuang | 38ede13 | 2017-06-15 19:52:32 | [diff] [blame] | 84 | static constexpr int kDefaultStartBitrateBps = 300000; |
ossu | f515ab8 | 2016-12-07 12:52:58 | [diff] [blame] | 85 | |
| 86 | // Bitrate config used until valid bitrate estimates are calculated. Also |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 87 | // used to cap total bitrate used. This comes from the remote connection. |
ossu | f515ab8 | 2016-12-07 12:52:58 | [diff] [blame] | 88 | struct BitrateConfig { |
| 89 | int min_bitrate_bps = 0; |
| 90 | int start_bitrate_bps = kDefaultStartBitrateBps; |
| 91 | int max_bitrate_bps = -1; |
| 92 | } bitrate_config; |
| 93 | |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 94 | // The local client's bitrate preferences. The actual configuration used |
| 95 | // is a combination of this and |bitrate_config|. The combination is |
| 96 | // currently more complicated than a simple mask operation (see |
| 97 | // SetBitrateConfig and SetBitrateConfigMask). Assumes that 0 <= min <= |
| 98 | // start <= max holds for set parameters. |
| 99 | struct BitrateConfigMask { |
| 100 | rtc::Optional<int> min_bitrate_bps; |
| 101 | rtc::Optional<int> start_bitrate_bps; |
| 102 | rtc::Optional<int> max_bitrate_bps; |
| 103 | }; |
| 104 | |
ossu | f515ab8 | 2016-12-07 12:52:58 | [diff] [blame] | 105 | // AudioState which is possibly shared between multiple calls. |
| 106 | // TODO(solenberg): Change this to a shared_ptr once we can use C++11. |
| 107 | rtc::scoped_refptr<AudioState> audio_state; |
| 108 | |
| 109 | // Audio Processing Module to be used in this call. |
| 110 | // TODO(solenberg): Change this to a shared_ptr once we can use C++11. |
| 111 | AudioProcessing* audio_processing = nullptr; |
| 112 | |
| 113 | // RtcEventLog to use for this call. Required. |
| 114 | // Use webrtc::RtcEventLog::CreateNull() for a null implementation. |
| 115 | RtcEventLog* event_log = nullptr; |
| 116 | }; |
| 117 | |
| 118 | struct Stats { |
| 119 | std::string ToString(int64_t time_ms) const; |
| 120 | |
| 121 | int send_bandwidth_bps = 0; // Estimated available send bandwidth. |
| 122 | int max_padding_bitrate_bps = 0; // Cumulative configured max padding. |
| 123 | int recv_bandwidth_bps = 0; // Estimated available receive bandwidth. |
| 124 | int64_t pacer_delay_ms = 0; |
| 125 | int64_t rtt_ms = -1; |
| 126 | }; |
| 127 | |
| 128 | static Call* Create(const Call::Config& config); |
| 129 | |
zstein | 7cb69d5 | 2017-05-08 18:52:38 | [diff] [blame] | 130 | // Allows mocking |transport_send| for testing. |
| 131 | static Call* Create( |
| 132 | const Call::Config& config, |
| 133 | std::unique_ptr<RtpTransportControllerSendInterface> transport_send); |
| 134 | |
ossu | f515ab8 | 2016-12-07 12:52:58 | [diff] [blame] | 135 | virtual AudioSendStream* CreateAudioSendStream( |
| 136 | const AudioSendStream::Config& config) = 0; |
| 137 | virtual void DestroyAudioSendStream(AudioSendStream* send_stream) = 0; |
| 138 | |
| 139 | virtual AudioReceiveStream* CreateAudioReceiveStream( |
| 140 | const AudioReceiveStream::Config& config) = 0; |
| 141 | virtual void DestroyAudioReceiveStream( |
| 142 | AudioReceiveStream* receive_stream) = 0; |
| 143 | |
| 144 | virtual VideoSendStream* CreateVideoSendStream( |
| 145 | VideoSendStream::Config config, |
| 146 | VideoEncoderConfig encoder_config) = 0; |
| 147 | virtual void DestroyVideoSendStream(VideoSendStream* send_stream) = 0; |
| 148 | |
| 149 | virtual VideoReceiveStream* CreateVideoReceiveStream( |
| 150 | VideoReceiveStream::Config configuration) = 0; |
| 151 | virtual void DestroyVideoReceiveStream( |
| 152 | VideoReceiveStream* receive_stream) = 0; |
| 153 | |
brandtr | fb45c6c | 2017-01-27 14:47:55 | [diff] [blame] | 154 | // In order for a created VideoReceiveStream to be aware that it is |
| 155 | // protected by a FlexfecReceiveStream, the latter should be created before |
| 156 | // the former. |
ossu | f515ab8 | 2016-12-07 12:52:58 | [diff] [blame] | 157 | virtual FlexfecReceiveStream* CreateFlexfecReceiveStream( |
brandtr | 446fcb6 | 2016-12-08 12:14:24 | [diff] [blame] | 158 | const FlexfecReceiveStream::Config& config) = 0; |
ossu | f515ab8 | 2016-12-07 12:52:58 | [diff] [blame] | 159 | virtual void DestroyFlexfecReceiveStream( |
| 160 | FlexfecReceiveStream* receive_stream) = 0; |
| 161 | |
| 162 | // All received RTP and RTCP packets for the call should be inserted to this |
| 163 | // PacketReceiver. The PacketReceiver pointer is valid as long as the |
| 164 | // Call instance exists. |
| 165 | virtual PacketReceiver* Receiver() = 0; |
| 166 | |
| 167 | // Returns the call statistics, such as estimated send and receive bandwidth, |
| 168 | // pacing delay, etc. |
| 169 | virtual Stats GetStats() const = 0; |
| 170 | |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 171 | // The greater min and smaller max set by this and SetBitrateConfigMask will |
| 172 | // be used. The latest non-negative start value from either call will be used. |
| 173 | // Specifying a start bitrate (>0) will reset the current bitrate estimate. |
| 174 | // This is due to how the 'x-google-start-bitrate' flag is currently |
| 175 | // implemented. Passing -1 leaves the start bitrate unchanged. Behavior is not |
| 176 | // guaranteed for other negative values or 0. |
ossu | f515ab8 | 2016-12-07 12:52:58 | [diff] [blame] | 177 | virtual void SetBitrateConfig( |
| 178 | const Config::BitrateConfig& bitrate_config) = 0; |
| 179 | |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 180 | // The greater min and smaller max set by this and SetBitrateConfig will be |
| 181 | // used. The latest non-negative start value form either call will be used. |
| 182 | // Specifying a start bitrate will reset the current bitrate estimate. |
| 183 | // Assumes 0 <= min <= start <= max holds for set parameters. |
| 184 | virtual void SetBitrateConfigMask( |
| 185 | const Config::BitrateConfigMask& bitrate_mask) = 0; |
| 186 | |
Alex Narest | 78609d5 | 2017-10-20 08:37:47 | [diff] [blame] | 187 | virtual void SetBitrateAllocationStrategy( |
| 188 | std::unique_ptr<rtc::BitrateAllocationStrategy> |
| 189 | bitrate_allocation_strategy) = 0; |
| 190 | |
ossu | f515ab8 | 2016-12-07 12:52:58 | [diff] [blame] | 191 | // TODO(skvlad): When the unbundled case with multiple streams for the same |
| 192 | // media type going over different networks is supported, track the state |
| 193 | // for each stream separately. Right now it's global per media type. |
| 194 | virtual void SignalChannelNetworkState(MediaType media, |
| 195 | NetworkState state) = 0; |
| 196 | |
| 197 | virtual void OnTransportOverheadChanged( |
| 198 | MediaType media, |
| 199 | int transport_overhead_per_packet) = 0; |
| 200 | |
| 201 | virtual void OnNetworkRouteChanged( |
| 202 | const std::string& transport_name, |
| 203 | const rtc::NetworkRoute& network_route) = 0; |
| 204 | |
| 205 | virtual void OnSentPacket(const rtc::SentPacket& sent_packet) = 0; |
| 206 | |
| 207 | virtual ~Call() {} |
| 208 | }; |
| 209 | |
| 210 | } // namespace webrtc |
| 211 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 212 | #endif // CALL_CALL_H_ |