blob: f99ba851ab420daeb5c12f03e1577f1701c4cbb3 [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 */
10#ifndef WEBRTC_CALL_CALL_H_
11#define WEBRTC_CALL_CALL_H_
12
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
zstein4b979802017-06-02 14:37:37 -070018#include "webrtc/api/rtcerror.h"
ossuf515ab82016-12-07 04:52:58 -080019#include "webrtc/call/audio_receive_stream.h"
20#include "webrtc/call/audio_send_stream.h"
21#include "webrtc/call/audio_state.h"
brandtr7250b392016-12-19 01:13:46 -080022#include "webrtc/call/flexfec_receive_stream.h"
zstein7cb69d52017-05-08 11:52:38 -070023#include "webrtc/call/rtp_transport_controller_send_interface.h"
ossuf515ab82016-12-07 04:52:58 -080024#include "webrtc/common_types.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020025#include "webrtc/rtc_base/networkroute.h"
26#include "webrtc/rtc_base/platform_file.h"
27#include "webrtc/rtc_base/socket.h"
ossuf515ab82016-12-07 04:52:58 -080028#include "webrtc/video_receive_stream.h"
29#include "webrtc/video_send_stream.h"
30
31namespace webrtc {
32
33class AudioProcessing;
34class RtcEventLog;
35
ossuf515ab82016-12-07 04:52:58 -080036enum class MediaType {
37 ANY,
38 AUDIO,
39 VIDEO,
40 DATA
41};
42
zsteina5e0df62017-06-14 11:41:48 -070043// Like std::min, but considers non-positive values to be unset.
44// TODO(zstein): Remove once all callers use rtc::Optional.
45template <typename T>
46static T MinPositive(T a, T b) {
47 if (a <= 0) {
48 return b;
49 }
50 if (b <= 0) {
51 return a;
52 }
53 return std::min(a, b);
54}
55
ossuf515ab82016-12-07 04:52:58 -080056class PacketReceiver {
57 public:
58 enum DeliveryStatus {
59 DELIVERY_OK,
60 DELIVERY_UNKNOWN_SSRC,
61 DELIVERY_PACKET_ERROR,
62 };
63
64 virtual DeliveryStatus DeliverPacket(MediaType media_type,
65 const uint8_t* packet,
66 size_t length,
67 const PacketTime& packet_time) = 0;
68
69 protected:
70 virtual ~PacketReceiver() {}
71};
72
73// A Call instance can contain several send and/or receive streams. All streams
74// are assumed to have the same remote endpoint and will share bitrate estimates
75// etc.
76class Call {
77 public:
78 struct Config {
79 explicit Config(RtcEventLog* event_log) : event_log(event_log) {
80 RTC_DCHECK(event_log);
81 }
82
zhihuang38ede132017-06-15 12:52:32 -070083 static constexpr int kDefaultStartBitrateBps = 300000;
ossuf515ab82016-12-07 04:52:58 -080084
85 // Bitrate config used until valid bitrate estimates are calculated. Also
zstein4b979802017-06-02 14:37:37 -070086 // used to cap total bitrate used. This comes from the remote connection.
ossuf515ab82016-12-07 04:52:58 -080087 struct BitrateConfig {
88 int min_bitrate_bps = 0;
89 int start_bitrate_bps = kDefaultStartBitrateBps;
90 int max_bitrate_bps = -1;
91 } bitrate_config;
92
zstein4b979802017-06-02 14:37:37 -070093 // The local client's bitrate preferences. The actual configuration used
94 // is a combination of this and |bitrate_config|. The combination is
95 // currently more complicated than a simple mask operation (see
96 // SetBitrateConfig and SetBitrateConfigMask). Assumes that 0 <= min <=
97 // start <= max holds for set parameters.
98 struct BitrateConfigMask {
99 rtc::Optional<int> min_bitrate_bps;
100 rtc::Optional<int> start_bitrate_bps;
101 rtc::Optional<int> max_bitrate_bps;
102 };
103
ossuf515ab82016-12-07 04:52:58 -0800104 // AudioState which is possibly shared between multiple calls.
105 // TODO(solenberg): Change this to a shared_ptr once we can use C++11.
106 rtc::scoped_refptr<AudioState> audio_state;
107
108 // Audio Processing Module to be used in this call.
109 // TODO(solenberg): Change this to a shared_ptr once we can use C++11.
110 AudioProcessing* audio_processing = nullptr;
111
112 // RtcEventLog to use for this call. Required.
113 // Use webrtc::RtcEventLog::CreateNull() for a null implementation.
114 RtcEventLog* event_log = nullptr;
sprange5c4a812017-07-11 03:44:17 -0700115
116 // Enables periodic sending if empty keep-alive messages that helps prevent
117 // network time-out events. The packets adhere to RFC6263 section 4.6, and
118 // by default use payload type 20, as described in 3GPP TS 24.229,
119 // Appendix K.5.2.1.
120 RtpKeepAliveConfig keepalive_config;
ossuf515ab82016-12-07 04:52:58 -0800121 };
122
123 struct Stats {
124 std::string ToString(int64_t time_ms) const;
125
126 int send_bandwidth_bps = 0; // Estimated available send bandwidth.
127 int max_padding_bitrate_bps = 0; // Cumulative configured max padding.
128 int recv_bandwidth_bps = 0; // Estimated available receive bandwidth.
129 int64_t pacer_delay_ms = 0;
130 int64_t rtt_ms = -1;
131 };
132
133 static Call* Create(const Call::Config& config);
134
zstein7cb69d52017-05-08 11:52:38 -0700135 // Allows mocking |transport_send| for testing.
136 static Call* Create(
137 const Call::Config& config,
138 std::unique_ptr<RtpTransportControllerSendInterface> transport_send);
139
ossuf515ab82016-12-07 04:52:58 -0800140 virtual AudioSendStream* CreateAudioSendStream(
141 const AudioSendStream::Config& config) = 0;
142 virtual void DestroyAudioSendStream(AudioSendStream* send_stream) = 0;
143
144 virtual AudioReceiveStream* CreateAudioReceiveStream(
145 const AudioReceiveStream::Config& config) = 0;
146 virtual void DestroyAudioReceiveStream(
147 AudioReceiveStream* receive_stream) = 0;
148
149 virtual VideoSendStream* CreateVideoSendStream(
150 VideoSendStream::Config config,
151 VideoEncoderConfig encoder_config) = 0;
152 virtual void DestroyVideoSendStream(VideoSendStream* send_stream) = 0;
153
154 virtual VideoReceiveStream* CreateVideoReceiveStream(
155 VideoReceiveStream::Config configuration) = 0;
156 virtual void DestroyVideoReceiveStream(
157 VideoReceiveStream* receive_stream) = 0;
158
brandtrfb45c6c2017-01-27 06:47:55 -0800159 // In order for a created VideoReceiveStream to be aware that it is
160 // protected by a FlexfecReceiveStream, the latter should be created before
161 // the former.
ossuf515ab82016-12-07 04:52:58 -0800162 virtual FlexfecReceiveStream* CreateFlexfecReceiveStream(
brandtr446fcb62016-12-08 04:14:24 -0800163 const FlexfecReceiveStream::Config& config) = 0;
ossuf515ab82016-12-07 04:52:58 -0800164 virtual void DestroyFlexfecReceiveStream(
165 FlexfecReceiveStream* receive_stream) = 0;
166
167 // All received RTP and RTCP packets for the call should be inserted to this
168 // PacketReceiver. The PacketReceiver pointer is valid as long as the
169 // Call instance exists.
170 virtual PacketReceiver* Receiver() = 0;
171
172 // Returns the call statistics, such as estimated send and receive bandwidth,
173 // pacing delay, etc.
174 virtual Stats GetStats() const = 0;
175
zstein4b979802017-06-02 14:37:37 -0700176 // The greater min and smaller max set by this and SetBitrateConfigMask will
177 // be used. The latest non-negative start value from either call will be used.
178 // Specifying a start bitrate (>0) will reset the current bitrate estimate.
179 // This is due to how the 'x-google-start-bitrate' flag is currently
180 // implemented. Passing -1 leaves the start bitrate unchanged. Behavior is not
181 // guaranteed for other negative values or 0.
ossuf515ab82016-12-07 04:52:58 -0800182 virtual void SetBitrateConfig(
183 const Config::BitrateConfig& bitrate_config) = 0;
184
zstein4b979802017-06-02 14:37:37 -0700185 // The greater min and smaller max set by this and SetBitrateConfig will be
186 // used. The latest non-negative start value form either call will be used.
187 // Specifying a start bitrate will reset the current bitrate estimate.
188 // Assumes 0 <= min <= start <= max holds for set parameters.
189 virtual void SetBitrateConfigMask(
190 const Config::BitrateConfigMask& bitrate_mask) = 0;
191
ossuf515ab82016-12-07 04:52:58 -0800192 // TODO(skvlad): When the unbundled case with multiple streams for the same
193 // media type going over different networks is supported, track the state
194 // for each stream separately. Right now it's global per media type.
195 virtual void SignalChannelNetworkState(MediaType media,
196 NetworkState state) = 0;
197
198 virtual void OnTransportOverheadChanged(
199 MediaType media,
200 int transport_overhead_per_packet) = 0;
201
202 virtual void OnNetworkRouteChanged(
203 const std::string& transport_name,
204 const rtc::NetworkRoute& network_route) = 0;
205
206 virtual void OnSentPacket(const rtc::SentPacket& sent_packet) = 0;
207
208 virtual ~Call() {}
209};
210
211} // namespace webrtc
212
213#endif // WEBRTC_CALL_CALL_H_