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