blob: e22196cae7336433a59ddc2930168d220199c9ba [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander65c7f672016-02-12 00:05:01 -08002 * Copyright 2004 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander65c7f672016-02-12 00:05:01 -08004 * 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.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
perkjc11b1842016-03-07 17:34:13 -080011#ifndef WEBRTC_PC_CHANNEL_H_
12#define WEBRTC_PC_CHANNEL_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013
deadbeefcbecd352015-09-23 11:50:27 -070014#include <map>
kwiberg31022942016-03-11 14:18:21 -080015#include <memory>
deadbeefcbecd352015-09-23 11:50:27 -070016#include <set>
kjellandera96e2d72016-02-04 23:52:28 -080017#include <string>
deadbeefcbecd352015-09-23 11:50:27 -070018#include <utility>
kjellandera96e2d72016-02-04 23:52:28 -080019#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000020
kjellander@webrtc.org7ffeab52016-02-26 22:46:09 +010021#include "webrtc/audio_sink.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000022#include "webrtc/base/asyncudpsocket.h"
23#include "webrtc/base/criticalsection.h"
24#include "webrtc/base/network.h"
25#include "webrtc/base/sigslot.h"
26#include "webrtc/base/window.h"
kjellandera96e2d72016-02-04 23:52:28 -080027#include "webrtc/media/base/mediachannel.h"
28#include "webrtc/media/base/mediaengine.h"
29#include "webrtc/media/base/streamparams.h"
30#include "webrtc/media/base/videocapturer.h"
nisse08582ff2016-02-04 01:24:52 -080031#include "webrtc/media/base/videosinkinterface.h"
Tommif888bb52015-12-12 01:37:01 +010032#include "webrtc/p2p/base/transportcontroller.h"
33#include "webrtc/p2p/client/socketmonitor.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010034#include "webrtc/pc/audiomonitor.h"
35#include "webrtc/pc/bundlefilter.h"
36#include "webrtc/pc/mediamonitor.h"
37#include "webrtc/pc/mediasession.h"
38#include "webrtc/pc/rtcpmuxfilter.h"
39#include "webrtc/pc/srtpfilter.h"
Tommif888bb52015-12-12 01:37:01 +010040
41namespace webrtc {
42class AudioSinkInterface;
43} // namespace webrtc
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044
45namespace cricket {
46
47struct CryptoParams;
48class MediaContentDescription;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049
50enum SinkType {
51 SINK_PRE_CRYPTO, // Sink packets before encryption or after decryption.
52 SINK_POST_CRYPTO // Sink packets after encryption or before decryption.
53};
54
55// BaseChannel contains logic common to voice and video, including
solenberg1dd98f32015-09-10 01:57:14 -070056// enable, marshaling calls to a worker thread, and
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057// connection and media monitors.
wu@webrtc.org78187522013-10-07 23:32:02 +000058//
59// WARNING! SUBCLASSES MUST CALL Deinit() IN THEIR DESTRUCTORS!
60// This is required to avoid a data race between the destructor modifying the
61// vtable, and the media channel's thread using BaseChannel as the
62// NetworkInterface.
63
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064class BaseChannel
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000065 : public rtc::MessageHandler, public sigslot::has_slots<>,
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +000066 public MediaChannel::NetworkInterface,
67 public ConnectionStatsGetter {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068 public:
deadbeefcbecd352015-09-23 11:50:27 -070069 BaseChannel(rtc::Thread* thread,
70 MediaChannel* channel,
71 TransportController* transport_controller,
72 const std::string& content_name,
73 bool rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074 virtual ~BaseChannel();
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +000075 bool Init();
wu@webrtc.org78187522013-10-07 23:32:02 +000076 // Deinit may be called multiple times and is simply ignored if it's alreay
77 // done.
78 void Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000080 rtc::Thread* worker_thread() const { return worker_thread_; }
deadbeefcbecd352015-09-23 11:50:27 -070081 const std::string& content_name() const { return content_name_; }
82 const std::string& transport_name() const { return transport_name_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083 TransportChannel* transport_channel() const {
84 return transport_channel_;
85 }
86 TransportChannel* rtcp_transport_channel() const {
87 return rtcp_transport_channel_;
88 }
89 bool enabled() const { return enabled_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090
91 // This function returns true if we are using SRTP.
92 bool secure() const { return srtp_filter_.IsActive(); }
93 // The following function returns true if we are using
94 // DTLS-based keying. If you turned off SRTP later, however
95 // you could have secure() == false and dtls_secure() == true.
96 bool secure_dtls() const { return dtls_keyed_; }
97 // This function returns true if we require secure channel for call setup.
98 bool secure_required() const { return secure_required_; }
99
100 bool writable() const { return writable_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700102 // Activate RTCP mux, regardless of the state so far. Once
103 // activated, it can not be deactivated, and if the remote
104 // description doesn't support RTCP mux, setting the remote
105 // description will fail.
106 void ActivateRtcpMux();
deadbeefcbecd352015-09-23 11:50:27 -0700107 bool SetTransport(const std::string& transport_name);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000108 bool PushdownLocalDescription(const SessionDescription* local_desc,
109 ContentAction action,
110 std::string* error_desc);
111 bool PushdownRemoteDescription(const SessionDescription* remote_desc,
112 ContentAction action,
113 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114 // Channel control
115 bool SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000116 ContentAction action,
117 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118 bool SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000119 ContentAction action,
120 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000121
122 bool Enable(bool enable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123
124 // Multiplexing
125 bool AddRecvStream(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200126 bool RemoveRecvStream(uint32_t ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000127 bool AddSendStream(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200128 bool RemoveSendStream(uint32_t ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129
130 // Monitoring
131 void StartConnectionMonitor(int cms);
132 void StopConnectionMonitor();
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000133 // For ConnectionStatsGetter, used by ConnectionMonitor
deadbeefcbecd352015-09-23 11:50:27 -0700134 bool GetConnectionStats(ConnectionInfos* infos) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000136 BundleFilter* bundle_filter() { return &bundle_filter_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137
138 const std::vector<StreamParams>& local_streams() const {
139 return local_streams_;
140 }
141 const std::vector<StreamParams>& remote_streams() const {
142 return remote_streams_;
143 }
144
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000145 sigslot::signal2<BaseChannel*, bool> SignalDtlsSetupFailure;
146 void SignalDtlsSetupFailure_w(bool rtcp);
147 void SignalDtlsSetupFailure_s(bool rtcp);
148
buildbot@webrtc.org6bfd6192014-05-15 16:15:59 +0000149 // Used for latency measurements.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150 sigslot::signal1<BaseChannel*> SignalFirstPacketReceived;
151
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152 // Made public for easier testing.
deadbeefcbecd352015-09-23 11:50:27 -0700153 void SetReadyToSend(bool rtcp, bool ready);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154
guoweis@webrtc.org4f852882015-03-12 20:09:44 +0000155 // Only public for unit tests. Otherwise, consider protected.
rlesterec9d1872015-10-27 14:22:16 -0700156 int SetOption(SocketType type, rtc::Socket::Option o, int val)
157 override;
guoweis@webrtc.org4f852882015-03-12 20:09:44 +0000158
solenberg5b14b422015-10-01 04:10:31 -0700159 SrtpFilter* srtp_filter() { return &srtp_filter_; }
160
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161 protected:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162 virtual MediaChannel* media_channel() const { return media_channel_; }
deadbeefcbecd352015-09-23 11:50:27 -0700163 // Sets the |transport_channel_| (and |rtcp_transport_channel_|, if |rtcp_| is
164 // true). Gets the transport channels from |transport_controller_|.
165 bool SetTransport_w(const std::string& transport_name);
guoweis46383312015-12-17 16:45:59 -0800166
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000167 void set_transport_channel(TransportChannel* transport);
guoweis46383312015-12-17 16:45:59 -0800168 void set_rtcp_transport_channel(TransportChannel* transport,
169 bool update_writablity);
170
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171 bool was_ever_writable() const { return was_ever_writable_; }
172 void set_local_content_direction(MediaContentDirection direction) {
173 local_content_direction_ = direction;
174 }
175 void set_remote_content_direction(MediaContentDirection direction) {
176 remote_content_direction_ = direction;
177 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700178 void set_secure_required(bool secure_required) {
179 secure_required_ = secure_required;
180 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181 bool IsReadyToReceive() const;
182 bool IsReadyToSend() const;
deadbeefcbecd352015-09-23 11:50:27 -0700183 rtc::Thread* signaling_thread() {
184 return transport_controller_->signaling_thread();
185 }
deadbeefcbecd352015-09-23 11:50:27 -0700186 bool rtcp_transport_enabled() const { return rtcp_transport_enabled_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000188 void ConnectToTransportChannel(TransportChannel* tc);
189 void DisconnectFromTransportChannel(TransportChannel* tc);
190
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191 void FlushRtcpMessages();
192
193 // NetworkInterface implementation, called by MediaEngine
jbaucheec21bd2016-03-20 06:15:43 -0700194 bool SendPacket(rtc::CopyOnWriteBuffer* packet,
195 const rtc::PacketOptions& options) override;
196 bool SendRtcp(rtc::CopyOnWriteBuffer* packet,
197 const rtc::PacketOptions& options) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198
199 // From TransportChannel
200 void OnWritableState(TransportChannel* channel);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000201 virtual void OnChannelRead(TransportChannel* channel,
202 const char* data,
203 size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000204 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000205 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206 void OnReadyToSend(TransportChannel* channel);
207
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800208 void OnDtlsState(TransportChannel* channel, DtlsTransportState state);
209
Honghai Zhangcc411c02016-03-29 17:27:21 -0700210 void OnSelectedCandidatePairChanged(
211 TransportChannel* channel,
212 CandidatePairInterface* selected_candidate_pair);
213
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214 bool PacketIsRtcp(const TransportChannel* channel, const char* data,
215 size_t len);
stefanc1aeaf02015-10-15 07:26:07 -0700216 bool SendPacket(bool rtcp,
jbaucheec21bd2016-03-20 06:15:43 -0700217 rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700218 const rtc::PacketOptions& options);
jbaucheec21bd2016-03-20 06:15:43 -0700219 virtual bool WantsPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet);
220 void HandlePacket(bool rtcp, rtc::CopyOnWriteBuffer* packet,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000221 const rtc::PacketTime& packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000222
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000223 void EnableMedia_w();
224 void DisableMedia_w();
deadbeefcbecd352015-09-23 11:50:27 -0700225 void UpdateWritableState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000226 void ChannelWritable_w();
227 void ChannelNotWritable_w();
228 bool AddRecvStream_w(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200229 bool RemoveRecvStream_w(uint32_t ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000230 bool AddSendStream_w(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200231 bool RemoveSendStream_w(uint32_t ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232 virtual bool ShouldSetupDtlsSrtp() const;
233 // Do the DTLS key expansion and impose it on the SRTP/SRTCP filters.
234 // |rtcp_channel| indicates whether to set up the RTP or RTCP filter.
235 bool SetupDtlsSrtp(bool rtcp_channel);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800236 void MaybeSetupDtlsSrtp_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000237 // Set the DTLS-SRTP cipher policy on this channel as appropriate.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800238 bool SetDtlsSrtpCryptoSuites(TransportChannel* tc, bool rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000239
240 virtual void ChangeState() = 0;
241
242 // Gets the content info appropriate to the channel (audio or video).
243 virtual const ContentInfo* GetFirstContent(
244 const SessionDescription* sdesc) = 0;
245 bool UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000246 ContentAction action,
247 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000248 bool UpdateRemoteStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000249 ContentAction action,
250 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000252 ContentAction action,
253 std::string* error_desc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000255 ContentAction action,
256 std::string* error_desc) = 0;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700257 bool SetRtpTransportParameters_w(const MediaContentDescription* content,
258 ContentAction action,
259 ContentSource src,
260 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000261
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000262 // Helper method to get RTP Absoulute SendTime extension header id if
263 // present in remote supported extensions list.
264 void MaybeCacheRtpAbsSendTimeHeaderExtension(
stefanc1aeaf02015-10-15 07:26:07 -0700265 const std::vector<RtpHeaderExtension>& extensions);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000266
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000267 bool CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
268 bool* dtls,
269 std::string* error_desc);
270 bool SetSrtp_w(const std::vector<CryptoParams>& params,
271 ContentAction action,
272 ContentSource src,
273 std::string* error_desc);
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700274 void ActivateRtcpMux_w();
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000275 bool SetRtcpMux_w(bool enable,
276 ContentAction action,
277 ContentSource src,
278 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000279
280 // From MessageHandler
rlesterec9d1872015-10-27 14:22:16 -0700281 void OnMessage(rtc::Message* pmsg) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000282
283 // Handled in derived classes
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800284 // Get the SRTP crypto suites to use for RTP media
285 virtual void GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const = 0;
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000286 virtual void OnConnectionMonitorUpdate(ConnectionMonitor* monitor,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000287 const std::vector<ConnectionInfo>& infos) = 0;
288
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000289 // Helper function for invoking bool-returning methods on the worker thread.
290 template <class FunctorT>
291 bool InvokeOnWorker(const FunctorT& functor) {
292 return worker_thread_->Invoke<bool>(functor);
293 }
294
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000296 rtc::Thread* worker_thread_;
deadbeefcbecd352015-09-23 11:50:27 -0700297 TransportController* transport_controller_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000298 MediaChannel* media_channel_;
299 std::vector<StreamParams> local_streams_;
300 std::vector<StreamParams> remote_streams_;
301
pthatcher@webrtc.org990a00c2015-03-13 18:20:33 +0000302 const std::string content_name_;
deadbeefcbecd352015-09-23 11:50:27 -0700303 std::string transport_name_;
304 bool rtcp_transport_enabled_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000305 TransportChannel* transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700306 std::vector<std::pair<rtc::Socket::Option, int> > socket_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000307 TransportChannel* rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700308 std::vector<std::pair<rtc::Socket::Option, int> > rtcp_socket_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000309 SrtpFilter srtp_filter_;
310 RtcpMuxFilter rtcp_mux_filter_;
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000311 BundleFilter bundle_filter_;
kwiberg31022942016-03-11 14:18:21 -0800312 std::unique_ptr<ConnectionMonitor> connection_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000313 bool enabled_;
314 bool writable_;
315 bool rtp_ready_to_send_;
316 bool rtcp_ready_to_send_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000317 bool was_ever_writable_;
318 MediaContentDirection local_content_direction_;
319 MediaContentDirection remote_content_direction_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000320 bool has_received_packet_;
321 bool dtls_keyed_;
322 bool secure_required_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000323 int rtp_abs_sendtime_extn_id_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000324};
325
326// VoiceChannel is a specialization that adds support for early media, DTMF,
327// and input/output level monitoring.
328class VoiceChannel : public BaseChannel {
329 public:
deadbeefcbecd352015-09-23 11:50:27 -0700330 VoiceChannel(rtc::Thread* thread,
331 MediaEngineInterface* media_engine,
332 VoiceMediaChannel* channel,
333 TransportController* transport_controller,
334 const std::string& content_name,
335 bool rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336 ~VoiceChannel();
337 bool Init();
solenberg1dd98f32015-09-10 01:57:14 -0700338
339 // Configure sending media on the stream with SSRC |ssrc|
340 // If there is only one sending stream SSRC 0 can be used.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200341 bool SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -0700342 bool enable,
deadbeefcbecd352015-09-23 11:50:27 -0700343 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800344 AudioSource* source);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000345
346 // downcasts a MediaChannel
347 virtual VoiceMediaChannel* media_channel() const {
348 return static_cast<VoiceMediaChannel*>(BaseChannel::media_channel());
349 }
350
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000351 void SetEarlyMedia(bool enable);
352 // This signal is emitted when we have gone a period of time without
353 // receiving early media. When received, a UI should start playing its
354 // own ringing sound
355 sigslot::signal1<VoiceChannel*> SignalEarlyMediaTimeout;
356
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357 // Returns if the telephone-event has been negotiated.
358 bool CanInsertDtmf();
359 // Send and/or play a DTMF |event| according to the |flags|.
360 // The DTMF out-of-band signal will be used on sending.
361 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000362 // The valid value for the |event| are 0 which corresponding to DTMF
363 // event 0-9, *, #, A-D.
solenberg1d63dd02015-12-02 12:35:09 -0800364 bool InsertDtmf(uint32_t ssrc, int event_code, int duration);
solenberg4bac9c52015-10-09 02:32:53 -0700365 bool SetOutputVolume(uint32_t ssrc, double volume);
deadbeef2d110be2016-01-13 12:00:26 -0800366 void SetRawAudioSink(uint32_t ssrc,
kwiberg31022942016-03-11 14:18:21 -0800367 std::unique_ptr<webrtc::AudioSinkInterface> sink);
skvladdc1c62c2016-03-16 19:07:43 -0700368 webrtc::RtpParameters GetRtpParameters(uint32_t ssrc) const;
369 bool SetRtpParameters(uint32_t ssrc, const webrtc::RtpParameters& parameters);
Tommif888bb52015-12-12 01:37:01 +0100370
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000371 // Get statistics about the current media session.
372 bool GetStats(VoiceMediaInfo* stats);
373
374 // Monitoring functions
375 sigslot::signal2<VoiceChannel*, const std::vector<ConnectionInfo>&>
376 SignalConnectionMonitor;
377
378 void StartMediaMonitor(int cms);
379 void StopMediaMonitor();
380 sigslot::signal2<VoiceChannel*, const VoiceMediaInfo&> SignalMediaMonitor;
381
382 void StartAudioMonitor(int cms);
383 void StopAudioMonitor();
384 bool IsAudioMonitorRunning() const;
385 sigslot::signal2<VoiceChannel*, const AudioInfo&> SignalAudioMonitor;
386
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000387 int GetInputLevel_w();
388 int GetOutputLevel_w();
389 void GetActiveStreams_w(AudioInfo::StreamList* actives);
skvladdc1c62c2016-03-16 19:07:43 -0700390 webrtc::RtpParameters GetRtpParameters_w(uint32_t ssrc) const;
391 bool SetRtpParameters_w(uint32_t ssrc, webrtc::RtpParameters parameters);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000392
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000393 private:
394 // overrides from BaseChannel
395 virtual void OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000396 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000397 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000398 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000399 virtual void ChangeState();
400 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
401 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000402 ContentAction action,
403 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000404 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000405 ContentAction action,
406 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000407 void HandleEarlyMediaTimeout();
solenberg1d63dd02015-12-02 12:35:09 -0800408 bool InsertDtmf_w(uint32_t ssrc, int event, int duration);
solenberg4bac9c52015-10-09 02:32:53 -0700409 bool SetOutputVolume_w(uint32_t ssrc, double volume);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000410 bool GetStats_w(VoiceMediaInfo* stats);
411
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000412 virtual void OnMessage(rtc::Message* pmsg);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800413 virtual void GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000414 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000415 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000416 virtual void OnMediaMonitorUpdate(
417 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info);
418 void OnAudioMonitorUpdate(AudioMonitor* monitor, const AudioInfo& info);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000419
420 static const int kEarlyMediaTimeout = 1000;
Fredrik Solenberg0c022642015-08-05 12:25:22 +0200421 MediaEngineInterface* media_engine_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000422 bool received_media_;
kwiberg31022942016-03-11 14:18:21 -0800423 std::unique_ptr<VoiceMediaMonitor> media_monitor_;
424 std::unique_ptr<AudioMonitor> audio_monitor_;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700425
426 // Last AudioSendParameters sent down to the media_channel() via
427 // SetSendParameters.
428 AudioSendParameters last_send_params_;
429 // Last AudioRecvParameters sent down to the media_channel() via
430 // SetRecvParameters.
431 AudioRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000432};
433
434// VideoChannel is a specialization for video.
435class VideoChannel : public BaseChannel {
436 public:
deadbeefcbecd352015-09-23 11:50:27 -0700437 VideoChannel(rtc::Thread* thread,
438 VideoMediaChannel* channel,
439 TransportController* transport_controller,
440 const std::string& content_name,
Fredrik Solenberg0c022642015-08-05 12:25:22 +0200441 bool rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000442 ~VideoChannel();
443 bool Init();
444
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200445 // downcasts a MediaChannel
446 virtual VideoMediaChannel* media_channel() const {
447 return static_cast<VideoMediaChannel*>(BaseChannel::media_channel());
448 }
449
nisse08582ff2016-02-04 01:24:52 -0800450 bool SetSink(uint32_t ssrc, rtc::VideoSinkInterface<VideoFrame>* sink);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200451 bool SetCapturer(uint32_t ssrc, VideoCapturer* capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000452 // Get statistics about the current media session.
pbos@webrtc.org058b1f12015-03-04 08:54:32 +0000453 bool GetStats(VideoMediaInfo* stats);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000454
455 sigslot::signal2<VideoChannel*, const std::vector<ConnectionInfo>&>
456 SignalConnectionMonitor;
457
458 void StartMediaMonitor(int cms);
459 void StopMediaMonitor();
460 sigslot::signal2<VideoChannel*, const VideoMediaInfo&> SignalMediaMonitor;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000461
Peter Boström0c4e06b2015-10-07 12:23:21 +0200462 bool SetVideoSend(uint32_t ssrc, bool enable, const VideoOptions* options);
skvladdc1c62c2016-03-16 19:07:43 -0700463 webrtc::RtpParameters GetRtpParameters(uint32_t ssrc) const;
464 bool SetRtpParameters(uint32_t ssrc, const webrtc::RtpParameters& parameters);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000465
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000466 private:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000467 // overrides from BaseChannel
468 virtual void ChangeState();
469 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
470 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000471 ContentAction action,
472 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000473 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000474 ContentAction action,
475 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000476 bool GetStats_w(VideoMediaInfo* stats);
skvladdc1c62c2016-03-16 19:07:43 -0700477 webrtc::RtpParameters GetRtpParameters_w(uint32_t ssrc) const;
478 bool SetRtpParameters_w(uint32_t ssrc, webrtc::RtpParameters parameters);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000479
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000480 virtual void OnMessage(rtc::Message* pmsg);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800481 virtual void GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000482 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000483 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000484 virtual void OnMediaMonitorUpdate(
485 VideoMediaChannel* media_channel, const VideoMediaInfo& info);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000486
kwiberg31022942016-03-11 14:18:21 -0800487 std::unique_ptr<VideoMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000488
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700489 // Last VideoSendParameters sent down to the media_channel() via
490 // SetSendParameters.
491 VideoSendParameters last_send_params_;
492 // Last VideoRecvParameters sent down to the media_channel() via
493 // SetRecvParameters.
494 VideoRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000495};
496
497// DataChannel is a specialization for data.
498class DataChannel : public BaseChannel {
499 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000500 DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000501 DataMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -0700502 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000503 const std::string& content_name,
504 bool rtcp);
505 ~DataChannel();
506 bool Init();
507
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000508 virtual bool SendData(const SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700509 const rtc::CopyOnWriteBuffer& payload,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000510 SendDataResult* result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511
512 void StartMediaMonitor(int cms);
513 void StopMediaMonitor();
514
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000515 // Should be called on the signaling thread only.
516 bool ready_to_send_data() const {
517 return ready_to_send_data_;
518 }
519
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000520 sigslot::signal2<DataChannel*, const DataMediaInfo&> SignalMediaMonitor;
521 sigslot::signal2<DataChannel*, const std::vector<ConnectionInfo>&>
522 SignalConnectionMonitor;
jbaucheec21bd2016-03-20 06:15:43 -0700523 sigslot::signal3<DataChannel*, const ReceiveDataParams&,
524 const rtc::CopyOnWriteBuffer&> SignalDataReceived;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000525 // Signal for notifying when the channel becomes ready to send data.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000526 // That occurs when the channel is enabled, the transport is writable,
527 // both local and remote descriptions are set, and the channel is unblocked.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528 sigslot::signal1<bool> SignalReadyToSendData;
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +0000529 // Signal for notifying that the remote side has closed the DataChannel.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200530 sigslot::signal1<uint32_t> SignalStreamClosedRemotely;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000532 protected:
533 // downcasts a MediaChannel.
534 virtual DataMediaChannel* media_channel() const {
535 return static_cast<DataMediaChannel*>(BaseChannel::media_channel());
536 }
537
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000538 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000539 struct SendDataMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000540 SendDataMessageData(const SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700541 const rtc::CopyOnWriteBuffer* payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000542 SendDataResult* result)
543 : params(params),
544 payload(payload),
545 result(result),
546 succeeded(false) {
547 }
548
549 const SendDataParams& params;
jbaucheec21bd2016-03-20 06:15:43 -0700550 const rtc::CopyOnWriteBuffer* payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000551 SendDataResult* result;
552 bool succeeded;
553 };
554
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000555 struct DataReceivedMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000556 // We copy the data because the data will become invalid after we
557 // handle DataMediaChannel::SignalDataReceived but before we fire
558 // SignalDataReceived.
559 DataReceivedMessageData(
560 const ReceiveDataParams& params, const char* data, size_t len)
561 : params(params),
562 payload(data, len) {
563 }
564 const ReceiveDataParams params;
jbaucheec21bd2016-03-20 06:15:43 -0700565 const rtc::CopyOnWriteBuffer payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000566 };
567
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000568 typedef rtc::TypedMessageData<bool> DataChannelReadyToSendMessageData;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000569
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000570 // overrides from BaseChannel
571 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
572 // If data_channel_type_ is DCT_NONE, set it. Otherwise, check that
573 // it's the same as what was set previously. Returns false if it's
574 // set to one type one type and changed to another type later.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000575 bool SetDataChannelType(DataChannelType new_data_channel_type,
576 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000577 // Same as SetDataChannelType, but extracts the type from the
578 // DataContentDescription.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000579 bool SetDataChannelTypeFromContent(const DataContentDescription* content,
580 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000581 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000582 ContentAction action,
583 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000584 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000585 ContentAction action,
586 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000587 virtual void ChangeState();
jbaucheec21bd2016-03-20 06:15:43 -0700588 virtual bool WantsPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000590 virtual void OnMessage(rtc::Message* pmsg);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800591 virtual void GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000592 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000593 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000594 virtual void OnMediaMonitorUpdate(
595 DataMediaChannel* media_channel, const DataMediaInfo& info);
596 virtual bool ShouldSetupDtlsSrtp() const;
597 void OnDataReceived(
598 const ReceiveDataParams& params, const char* data, size_t len);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200599 void OnDataChannelError(uint32_t ssrc, DataMediaChannel::Error error);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000600 void OnDataChannelReadyToSend(bool writable);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200601 void OnStreamClosedRemotely(uint32_t sid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000602
kwiberg31022942016-03-11 14:18:21 -0800603 std::unique_ptr<DataMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000604 // TODO(pthatcher): Make a separate SctpDataChannel and
605 // RtpDataChannel instead of using this.
606 DataChannelType data_channel_type_;
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000607 bool ready_to_send_data_;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700608
609 // Last DataSendParameters sent down to the media_channel() via
610 // SetSendParameters.
611 DataSendParameters last_send_params_;
612 // Last DataRecvParameters sent down to the media_channel() via
613 // SetRecvParameters.
614 DataRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000615};
616
617} // namespace cricket
618
perkjc11b1842016-03-07 17:34:13 -0800619#endif // WEBRTC_PC_CHANNEL_H_