blob: 4518301d3b65e9e6fdf89586d258b519e42a4677 [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"
nisse08582ff2016-02-04 01:24:52 -080030#include "webrtc/media/base/videosinkinterface.h"
nisse2ded9b12016-04-08 02:23:55 -070031#include "webrtc/media/base/videosourceinterface.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,
Honghai Zhang52dce732016-03-31 12:37:31 -0700212 CandidatePairInterface* selected_candidate_pair,
213 int last_sent_packet_id);
Honghai Zhangcc411c02016-03-29 17:27:21 -0700214
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215 bool PacketIsRtcp(const TransportChannel* channel, const char* data,
216 size_t len);
stefanc1aeaf02015-10-15 07:26:07 -0700217 bool SendPacket(bool rtcp,
jbaucheec21bd2016-03-20 06:15:43 -0700218 rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700219 const rtc::PacketOptions& options);
jbaucheec21bd2016-03-20 06:15:43 -0700220 virtual bool WantsPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet);
221 void HandlePacket(bool rtcp, rtc::CopyOnWriteBuffer* packet,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000222 const rtc::PacketTime& packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000223
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000224 void EnableMedia_w();
225 void DisableMedia_w();
deadbeefcbecd352015-09-23 11:50:27 -0700226 void UpdateWritableState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000227 void ChannelWritable_w();
228 void ChannelNotWritable_w();
229 bool AddRecvStream_w(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200230 bool RemoveRecvStream_w(uint32_t ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000231 bool AddSendStream_w(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200232 bool RemoveSendStream_w(uint32_t ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000233 virtual bool ShouldSetupDtlsSrtp() const;
234 // Do the DTLS key expansion and impose it on the SRTP/SRTCP filters.
235 // |rtcp_channel| indicates whether to set up the RTP or RTCP filter.
236 bool SetupDtlsSrtp(bool rtcp_channel);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800237 void MaybeSetupDtlsSrtp_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000238 // Set the DTLS-SRTP cipher policy on this channel as appropriate.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800239 bool SetDtlsSrtpCryptoSuites(TransportChannel* tc, bool rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000240
241 virtual void ChangeState() = 0;
242
243 // Gets the content info appropriate to the channel (audio or video).
244 virtual const ContentInfo* GetFirstContent(
245 const SessionDescription* sdesc) = 0;
246 bool UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000247 ContentAction action,
248 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000249 bool UpdateRemoteStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000250 ContentAction action,
251 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000253 ContentAction action,
254 std::string* error_desc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000256 ContentAction action,
257 std::string* error_desc) = 0;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700258 bool SetRtpTransportParameters_w(const MediaContentDescription* content,
259 ContentAction action,
260 ContentSource src,
261 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000262
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000263 // Helper method to get RTP Absoulute SendTime extension header id if
264 // present in remote supported extensions list.
265 void MaybeCacheRtpAbsSendTimeHeaderExtension(
stefanc1aeaf02015-10-15 07:26:07 -0700266 const std::vector<RtpHeaderExtension>& extensions);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000267
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000268 bool CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
269 bool* dtls,
270 std::string* error_desc);
271 bool SetSrtp_w(const std::vector<CryptoParams>& params,
272 ContentAction action,
273 ContentSource src,
274 std::string* error_desc);
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700275 void ActivateRtcpMux_w();
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000276 bool SetRtcpMux_w(bool enable,
277 ContentAction action,
278 ContentSource src,
279 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000280
281 // From MessageHandler
rlesterec9d1872015-10-27 14:22:16 -0700282 void OnMessage(rtc::Message* pmsg) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000283
284 // Handled in derived classes
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800285 // Get the SRTP crypto suites to use for RTP media
286 virtual void GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const = 0;
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000287 virtual void OnConnectionMonitorUpdate(ConnectionMonitor* monitor,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000288 const std::vector<ConnectionInfo>& infos) = 0;
289
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000290 // Helper function for invoking bool-returning methods on the worker thread.
291 template <class FunctorT>
292 bool InvokeOnWorker(const FunctorT& functor) {
293 return worker_thread_->Invoke<bool>(functor);
294 }
295
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000296 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000297 rtc::Thread* worker_thread_;
deadbeefcbecd352015-09-23 11:50:27 -0700298 TransportController* transport_controller_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299 MediaChannel* media_channel_;
300 std::vector<StreamParams> local_streams_;
301 std::vector<StreamParams> remote_streams_;
302
pthatcher@webrtc.org990a00c2015-03-13 18:20:33 +0000303 const std::string content_name_;
deadbeefcbecd352015-09-23 11:50:27 -0700304 std::string transport_name_;
305 bool rtcp_transport_enabled_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000306 TransportChannel* transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700307 std::vector<std::pair<rtc::Socket::Option, int> > socket_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000308 TransportChannel* rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700309 std::vector<std::pair<rtc::Socket::Option, int> > rtcp_socket_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000310 SrtpFilter srtp_filter_;
311 RtcpMuxFilter rtcp_mux_filter_;
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000312 BundleFilter bundle_filter_;
kwiberg31022942016-03-11 14:18:21 -0800313 std::unique_ptr<ConnectionMonitor> connection_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000314 bool enabled_;
315 bool writable_;
316 bool rtp_ready_to_send_;
317 bool rtcp_ready_to_send_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000318 bool was_ever_writable_;
319 MediaContentDirection local_content_direction_;
320 MediaContentDirection remote_content_direction_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000321 bool has_received_packet_;
322 bool dtls_keyed_;
323 bool secure_required_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000324 int rtp_abs_sendtime_extn_id_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000325};
326
327// VoiceChannel is a specialization that adds support for early media, DTMF,
328// and input/output level monitoring.
329class VoiceChannel : public BaseChannel {
330 public:
deadbeefcbecd352015-09-23 11:50:27 -0700331 VoiceChannel(rtc::Thread* thread,
332 MediaEngineInterface* media_engine,
333 VoiceMediaChannel* channel,
334 TransportController* transport_controller,
335 const std::string& content_name,
336 bool rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000337 ~VoiceChannel();
338 bool Init();
solenberg1dd98f32015-09-10 01:57:14 -0700339
340 // Configure sending media on the stream with SSRC |ssrc|
341 // If there is only one sending stream SSRC 0 can be used.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200342 bool SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -0700343 bool enable,
deadbeefcbecd352015-09-23 11:50:27 -0700344 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800345 AudioSource* source);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346
347 // downcasts a MediaChannel
348 virtual VoiceMediaChannel* media_channel() const {
349 return static_cast<VoiceMediaChannel*>(BaseChannel::media_channel());
350 }
351
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000352 void SetEarlyMedia(bool enable);
353 // This signal is emitted when we have gone a period of time without
354 // receiving early media. When received, a UI should start playing its
355 // own ringing sound
356 sigslot::signal1<VoiceChannel*> SignalEarlyMediaTimeout;
357
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000358 // Returns if the telephone-event has been negotiated.
359 bool CanInsertDtmf();
360 // Send and/or play a DTMF |event| according to the |flags|.
361 // The DTMF out-of-band signal will be used on sending.
362 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000363 // The valid value for the |event| are 0 which corresponding to DTMF
364 // event 0-9, *, #, A-D.
solenberg1d63dd02015-12-02 12:35:09 -0800365 bool InsertDtmf(uint32_t ssrc, int event_code, int duration);
solenberg4bac9c52015-10-09 02:32:53 -0700366 bool SetOutputVolume(uint32_t ssrc, double volume);
deadbeef2d110be2016-01-13 12:00:26 -0800367 void SetRawAudioSink(uint32_t ssrc,
kwiberg31022942016-03-11 14:18:21 -0800368 std::unique_ptr<webrtc::AudioSinkInterface> sink);
skvladdc1c62c2016-03-16 19:07:43 -0700369 webrtc::RtpParameters GetRtpParameters(uint32_t ssrc) const;
370 bool SetRtpParameters(uint32_t ssrc, const webrtc::RtpParameters& parameters);
Tommif888bb52015-12-12 01:37:01 +0100371
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000372 // Get statistics about the current media session.
373 bool GetStats(VoiceMediaInfo* stats);
374
375 // Monitoring functions
376 sigslot::signal2<VoiceChannel*, const std::vector<ConnectionInfo>&>
377 SignalConnectionMonitor;
378
379 void StartMediaMonitor(int cms);
380 void StopMediaMonitor();
381 sigslot::signal2<VoiceChannel*, const VoiceMediaInfo&> SignalMediaMonitor;
382
383 void StartAudioMonitor(int cms);
384 void StopAudioMonitor();
385 bool IsAudioMonitorRunning() const;
386 sigslot::signal2<VoiceChannel*, const AudioInfo&> SignalAudioMonitor;
387
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000388 int GetInputLevel_w();
389 int GetOutputLevel_w();
390 void GetActiveStreams_w(AudioInfo::StreamList* actives);
skvladdc1c62c2016-03-16 19:07:43 -0700391 webrtc::RtpParameters GetRtpParameters_w(uint32_t ssrc) const;
392 bool SetRtpParameters_w(uint32_t ssrc, webrtc::RtpParameters parameters);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000393
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000394 private:
395 // overrides from BaseChannel
396 virtual void OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000397 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000398 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000399 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000400 virtual void ChangeState();
401 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
402 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000403 ContentAction action,
404 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000405 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000406 ContentAction action,
407 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000408 void HandleEarlyMediaTimeout();
solenberg1d63dd02015-12-02 12:35:09 -0800409 bool InsertDtmf_w(uint32_t ssrc, int event, int duration);
solenberg4bac9c52015-10-09 02:32:53 -0700410 bool SetOutputVolume_w(uint32_t ssrc, double volume);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000411 bool GetStats_w(VoiceMediaInfo* stats);
412
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000413 virtual void OnMessage(rtc::Message* pmsg);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800414 virtual void GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000415 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000416 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000417 virtual void OnMediaMonitorUpdate(
418 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info);
419 void OnAudioMonitorUpdate(AudioMonitor* monitor, const AudioInfo& info);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000420
421 static const int kEarlyMediaTimeout = 1000;
Fredrik Solenberg0c022642015-08-05 12:25:22 +0200422 MediaEngineInterface* media_engine_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000423 bool received_media_;
kwiberg31022942016-03-11 14:18:21 -0800424 std::unique_ptr<VoiceMediaMonitor> media_monitor_;
425 std::unique_ptr<AudioMonitor> audio_monitor_;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700426
427 // Last AudioSendParameters sent down to the media_channel() via
428 // SetSendParameters.
429 AudioSendParameters last_send_params_;
430 // Last AudioRecvParameters sent down to the media_channel() via
431 // SetRecvParameters.
432 AudioRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000433};
434
435// VideoChannel is a specialization for video.
436class VideoChannel : public BaseChannel {
437 public:
deadbeefcbecd352015-09-23 11:50:27 -0700438 VideoChannel(rtc::Thread* thread,
439 VideoMediaChannel* channel,
440 TransportController* transport_controller,
441 const std::string& content_name,
Fredrik Solenberg0c022642015-08-05 12:25:22 +0200442 bool rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000443 ~VideoChannel();
444 bool Init();
445
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200446 // downcasts a MediaChannel
447 virtual VideoMediaChannel* media_channel() const {
448 return static_cast<VideoMediaChannel*>(BaseChannel::media_channel());
449 }
450
nisse08582ff2016-02-04 01:24:52 -0800451 bool SetSink(uint32_t ssrc, rtc::VideoSinkInterface<VideoFrame>* sink);
nisse2ded9b12016-04-08 02:23:55 -0700452 // Register a source. The |ssrc| must correspond to a registered
453 // send stream.
454 void SetSource(uint32_t ssrc,
455 rtc::VideoSourceInterface<cricket::VideoFrame>* source);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000456 // Get statistics about the current media session.
pbos@webrtc.org058b1f12015-03-04 08:54:32 +0000457 bool GetStats(VideoMediaInfo* stats);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000458
459 sigslot::signal2<VideoChannel*, const std::vector<ConnectionInfo>&>
460 SignalConnectionMonitor;
461
462 void StartMediaMonitor(int cms);
463 void StopMediaMonitor();
464 sigslot::signal2<VideoChannel*, const VideoMediaInfo&> SignalMediaMonitor;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000465
Peter Boström0c4e06b2015-10-07 12:23:21 +0200466 bool SetVideoSend(uint32_t ssrc, bool enable, const VideoOptions* options);
skvladdc1c62c2016-03-16 19:07:43 -0700467 webrtc::RtpParameters GetRtpParameters(uint32_t ssrc) const;
468 bool SetRtpParameters(uint32_t ssrc, const webrtc::RtpParameters& parameters);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000469
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000470 private:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000471 // overrides from BaseChannel
472 virtual void ChangeState();
473 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
474 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000475 ContentAction action,
476 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000478 ContentAction action,
479 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480 bool GetStats_w(VideoMediaInfo* stats);
skvladdc1c62c2016-03-16 19:07:43 -0700481 webrtc::RtpParameters GetRtpParameters_w(uint32_t ssrc) const;
482 bool SetRtpParameters_w(uint32_t ssrc, webrtc::RtpParameters parameters);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000484 virtual void OnMessage(rtc::Message* pmsg);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800485 virtual void GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000486 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000487 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000488 virtual void OnMediaMonitorUpdate(
489 VideoMediaChannel* media_channel, const VideoMediaInfo& info);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490
kwiberg31022942016-03-11 14:18:21 -0800491 std::unique_ptr<VideoMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000492
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700493 // Last VideoSendParameters sent down to the media_channel() via
494 // SetSendParameters.
495 VideoSendParameters last_send_params_;
496 // Last VideoRecvParameters sent down to the media_channel() via
497 // SetRecvParameters.
498 VideoRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499};
500
501// DataChannel is a specialization for data.
502class DataChannel : public BaseChannel {
503 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000504 DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000505 DataMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -0700506 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000507 const std::string& content_name,
508 bool rtcp);
509 ~DataChannel();
510 bool Init();
511
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000512 virtual bool SendData(const SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700513 const rtc::CopyOnWriteBuffer& payload,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000514 SendDataResult* result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000515
516 void StartMediaMonitor(int cms);
517 void StopMediaMonitor();
518
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000519 // Should be called on the signaling thread only.
520 bool ready_to_send_data() const {
521 return ready_to_send_data_;
522 }
523
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524 sigslot::signal2<DataChannel*, const DataMediaInfo&> SignalMediaMonitor;
525 sigslot::signal2<DataChannel*, const std::vector<ConnectionInfo>&>
526 SignalConnectionMonitor;
jbaucheec21bd2016-03-20 06:15:43 -0700527 sigslot::signal3<DataChannel*, const ReceiveDataParams&,
528 const rtc::CopyOnWriteBuffer&> SignalDataReceived;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529 // Signal for notifying when the channel becomes ready to send data.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000530 // That occurs when the channel is enabled, the transport is writable,
531 // both local and remote descriptions are set, and the channel is unblocked.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532 sigslot::signal1<bool> SignalReadyToSendData;
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +0000533 // Signal for notifying that the remote side has closed the DataChannel.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200534 sigslot::signal1<uint32_t> SignalStreamClosedRemotely;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000536 protected:
537 // downcasts a MediaChannel.
538 virtual DataMediaChannel* media_channel() const {
539 return static_cast<DataMediaChannel*>(BaseChannel::media_channel());
540 }
541
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000542 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000543 struct SendDataMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000544 SendDataMessageData(const SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700545 const rtc::CopyOnWriteBuffer* payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546 SendDataResult* result)
547 : params(params),
548 payload(payload),
549 result(result),
550 succeeded(false) {
551 }
552
553 const SendDataParams& params;
jbaucheec21bd2016-03-20 06:15:43 -0700554 const rtc::CopyOnWriteBuffer* payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000555 SendDataResult* result;
556 bool succeeded;
557 };
558
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000559 struct DataReceivedMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000560 // We copy the data because the data will become invalid after we
561 // handle DataMediaChannel::SignalDataReceived but before we fire
562 // SignalDataReceived.
563 DataReceivedMessageData(
564 const ReceiveDataParams& params, const char* data, size_t len)
565 : params(params),
566 payload(data, len) {
567 }
568 const ReceiveDataParams params;
jbaucheec21bd2016-03-20 06:15:43 -0700569 const rtc::CopyOnWriteBuffer payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000570 };
571
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000572 typedef rtc::TypedMessageData<bool> DataChannelReadyToSendMessageData;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000573
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000574 // overrides from BaseChannel
575 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
576 // If data_channel_type_ is DCT_NONE, set it. Otherwise, check that
577 // it's the same as what was set previously. Returns false if it's
578 // set to one type one type and changed to another type later.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000579 bool SetDataChannelType(DataChannelType new_data_channel_type,
580 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000581 // Same as SetDataChannelType, but extracts the type from the
582 // DataContentDescription.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000583 bool SetDataChannelTypeFromContent(const DataContentDescription* content,
584 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000585 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000586 ContentAction action,
587 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000588 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000589 ContentAction action,
590 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591 virtual void ChangeState();
jbaucheec21bd2016-03-20 06:15:43 -0700592 virtual bool WantsPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000593
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000594 virtual void OnMessage(rtc::Message* pmsg);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800595 virtual void GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000596 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000597 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000598 virtual void OnMediaMonitorUpdate(
599 DataMediaChannel* media_channel, const DataMediaInfo& info);
600 virtual bool ShouldSetupDtlsSrtp() const;
601 void OnDataReceived(
602 const ReceiveDataParams& params, const char* data, size_t len);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200603 void OnDataChannelError(uint32_t ssrc, DataMediaChannel::Error error);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000604 void OnDataChannelReadyToSend(bool writable);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200605 void OnStreamClosedRemotely(uint32_t sid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000606
kwiberg31022942016-03-11 14:18:21 -0800607 std::unique_ptr<DataMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608 // TODO(pthatcher): Make a separate SctpDataChannel and
609 // RtpDataChannel instead of using this.
610 DataChannelType data_channel_type_;
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000611 bool ready_to_send_data_;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700612
613 // Last DataSendParameters sent down to the media_channel() via
614 // SetSendParameters.
615 DataSendParameters last_send_params_;
616 // Last DataRecvParameters sent down to the media_channel() via
617 // SetRecvParameters.
618 DataRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000619};
620
621} // namespace cricket
622
perkjc11b1842016-03-07 17:34:13 -0800623#endif // WEBRTC_PC_CHANNEL_H_