blob: 11409548e3fa1bee8d4b82c68cca88c2e9870eaa [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef TALK_SESSION_MEDIA_CHANNEL_H_
29#define TALK_SESSION_MEDIA_CHANNEL_H_
30
31#include <string>
32#include <vector>
deadbeefcbecd352015-09-23 11:50:27 -070033#include <map>
34#include <set>
35#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037#include "talk/media/base/mediachannel.h"
38#include "talk/media/base/mediaengine.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039#include "talk/media/base/streamparams.h"
40#include "talk/media/base/videocapturer.h"
deadbeefcbecd352015-09-23 11:50:27 -070041#include "webrtc/p2p/base/transportcontroller.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000042#include "webrtc/p2p/client/socketmonitor.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043#include "talk/session/media/audiomonitor.h"
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +000044#include "talk/session/media/bundlefilter.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045#include "talk/session/media/mediamonitor.h"
46#include "talk/session/media/mediasession.h"
47#include "talk/session/media/rtcpmuxfilter.h"
48#include "talk/session/media/srtpfilter.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000049#include "webrtc/base/asyncudpsocket.h"
50#include "webrtc/base/criticalsection.h"
51#include "webrtc/base/network.h"
52#include "webrtc/base/sigslot.h"
53#include "webrtc/base/window.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054
55namespace cricket {
56
57struct CryptoParams;
58class MediaContentDescription;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059struct ViewRequest;
60
61enum SinkType {
62 SINK_PRE_CRYPTO, // Sink packets before encryption or after decryption.
63 SINK_POST_CRYPTO // Sink packets after encryption or before decryption.
64};
65
66// BaseChannel contains logic common to voice and video, including
solenberg1dd98f32015-09-10 01:57:14 -070067// enable, marshaling calls to a worker thread, and
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068// connection and media monitors.
wu@webrtc.org78187522013-10-07 23:32:02 +000069//
70// WARNING! SUBCLASSES MUST CALL Deinit() IN THEIR DESTRUCTORS!
71// This is required to avoid a data race between the destructor modifying the
72// vtable, and the media channel's thread using BaseChannel as the
73// NetworkInterface.
74
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075class BaseChannel
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000076 : public rtc::MessageHandler, public sigslot::has_slots<>,
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +000077 public MediaChannel::NetworkInterface,
78 public ConnectionStatsGetter {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079 public:
deadbeefcbecd352015-09-23 11:50:27 -070080 BaseChannel(rtc::Thread* thread,
81 MediaChannel* channel,
82 TransportController* transport_controller,
83 const std::string& content_name,
84 bool rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085 virtual ~BaseChannel();
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +000086 bool Init();
wu@webrtc.org78187522013-10-07 23:32:02 +000087 // Deinit may be called multiple times and is simply ignored if it's alreay
88 // done.
89 void Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000091 rtc::Thread* worker_thread() const { return worker_thread_; }
deadbeefcbecd352015-09-23 11:50:27 -070092 const std::string& content_name() const { return content_name_; }
93 const std::string& transport_name() const { return transport_name_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 TransportChannel* transport_channel() const {
95 return transport_channel_;
96 }
97 TransportChannel* rtcp_transport_channel() const {
98 return rtcp_transport_channel_;
99 }
100 bool enabled() const { return enabled_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101
102 // This function returns true if we are using SRTP.
103 bool secure() const { return srtp_filter_.IsActive(); }
104 // The following function returns true if we are using
105 // DTLS-based keying. If you turned off SRTP later, however
106 // you could have secure() == false and dtls_secure() == true.
107 bool secure_dtls() const { return dtls_keyed_; }
108 // This function returns true if we require secure channel for call setup.
109 bool secure_required() const { return secure_required_; }
110
111 bool writable() const { return writable_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700113 // Activate RTCP mux, regardless of the state so far. Once
114 // activated, it can not be deactivated, and if the remote
115 // description doesn't support RTCP mux, setting the remote
116 // description will fail.
117 void ActivateRtcpMux();
deadbeefcbecd352015-09-23 11:50:27 -0700118 bool SetTransport(const std::string& transport_name);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000119 bool PushdownLocalDescription(const SessionDescription* local_desc,
120 ContentAction action,
121 std::string* error_desc);
122 bool PushdownRemoteDescription(const SessionDescription* remote_desc,
123 ContentAction action,
124 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125 // Channel control
126 bool SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000127 ContentAction action,
128 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129 bool SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000130 ContentAction action,
131 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132
133 bool Enable(bool enable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134
135 // Multiplexing
136 bool AddRecvStream(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200137 bool RemoveRecvStream(uint32_t ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000138 bool AddSendStream(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200139 bool RemoveSendStream(uint32_t ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140
141 // Monitoring
142 void StartConnectionMonitor(int cms);
143 void StopConnectionMonitor();
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000144 // For ConnectionStatsGetter, used by ConnectionMonitor
deadbeefcbecd352015-09-23 11:50:27 -0700145 bool GetConnectionStats(ConnectionInfos* infos) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000147 BundleFilter* bundle_filter() { return &bundle_filter_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148
149 const std::vector<StreamParams>& local_streams() const {
150 return local_streams_;
151 }
152 const std::vector<StreamParams>& remote_streams() const {
153 return remote_streams_;
154 }
155
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000156 sigslot::signal2<BaseChannel*, bool> SignalDtlsSetupFailure;
157 void SignalDtlsSetupFailure_w(bool rtcp);
158 void SignalDtlsSetupFailure_s(bool rtcp);
159
buildbot@webrtc.org6bfd6192014-05-15 16:15:59 +0000160 // Used for latency measurements.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161 sigslot::signal1<BaseChannel*> SignalFirstPacketReceived;
162
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163 // Made public for easier testing.
deadbeefcbecd352015-09-23 11:50:27 -0700164 void SetReadyToSend(bool rtcp, bool ready);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165
guoweis@webrtc.org4f852882015-03-12 20:09:44 +0000166 // Only public for unit tests. Otherwise, consider protected.
rlesterec9d1872015-10-27 14:22:16 -0700167 int SetOption(SocketType type, rtc::Socket::Option o, int val)
168 override;
guoweis@webrtc.org4f852882015-03-12 20:09:44 +0000169
solenberg5b14b422015-10-01 04:10:31 -0700170 SrtpFilter* srtp_filter() { return &srtp_filter_; }
171
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000172 protected:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000173 virtual MediaChannel* media_channel() const { return media_channel_; }
deadbeefcbecd352015-09-23 11:50:27 -0700174 // Sets the |transport_channel_| (and |rtcp_transport_channel_|, if |rtcp_| is
175 // true). Gets the transport channels from |transport_controller_|.
176 bool SetTransport_w(const std::string& transport_name);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000177 void set_transport_channel(TransportChannel* transport);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178 void set_rtcp_transport_channel(TransportChannel* transport);
179 bool was_ever_writable() const { return was_ever_writable_; }
180 void set_local_content_direction(MediaContentDirection direction) {
181 local_content_direction_ = direction;
182 }
183 void set_remote_content_direction(MediaContentDirection direction) {
184 remote_content_direction_ = direction;
185 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700186 void set_secure_required(bool secure_required) {
187 secure_required_ = secure_required;
188 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189 bool IsReadyToReceive() const;
190 bool IsReadyToSend() const;
deadbeefcbecd352015-09-23 11:50:27 -0700191 rtc::Thread* signaling_thread() {
192 return transport_controller_->signaling_thread();
193 }
deadbeefcbecd352015-09-23 11:50:27 -0700194 bool rtcp_transport_enabled() const { return rtcp_transport_enabled_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000196 void ConnectToTransportChannel(TransportChannel* tc);
197 void DisconnectFromTransportChannel(TransportChannel* tc);
198
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199 void FlushRtcpMessages();
200
201 // NetworkInterface implementation, called by MediaEngine
rlesterec9d1872015-10-27 14:22:16 -0700202 bool SendPacket(rtc::Buffer* packet,
203 const rtc::PacketOptions& options) override;
204 bool SendRtcp(rtc::Buffer* packet, const rtc::PacketOptions& options)
205 override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206
207 // From TransportChannel
208 void OnWritableState(TransportChannel* channel);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000209 virtual void OnChannelRead(TransportChannel* channel,
210 const char* data,
211 size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000212 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000213 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214 void OnReadyToSend(TransportChannel* channel);
215
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800216 void OnDtlsState(TransportChannel* channel, DtlsTransportState state);
217
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218 bool PacketIsRtcp(const TransportChannel* channel, const char* data,
219 size_t len);
stefanc1aeaf02015-10-15 07:26:07 -0700220 bool SendPacket(bool rtcp,
221 rtc::Buffer* packet,
222 const rtc::PacketOptions& options);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000223 virtual bool WantsPacket(bool rtcp, rtc::Buffer* packet);
224 void HandlePacket(bool rtcp, rtc::Buffer* packet,
225 const rtc::PacketTime& packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000226
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000227 void EnableMedia_w();
228 void DisableMedia_w();
deadbeefcbecd352015-09-23 11:50:27 -0700229 void UpdateWritableState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000230 void ChannelWritable_w();
231 void ChannelNotWritable_w();
232 bool AddRecvStream_w(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200233 bool RemoveRecvStream_w(uint32_t ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000234 bool AddSendStream_w(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200235 bool RemoveSendStream_w(uint32_t ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236 virtual bool ShouldSetupDtlsSrtp() const;
237 // Do the DTLS key expansion and impose it on the SRTP/SRTCP filters.
238 // |rtcp_channel| indicates whether to set up the RTP or RTCP filter.
239 bool SetupDtlsSrtp(bool rtcp_channel);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800240 void MaybeSetupDtlsSrtp_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000241 // Set the DTLS-SRTP cipher policy on this channel as appropriate.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800242 bool SetDtlsSrtpCryptoSuites(TransportChannel* tc, bool rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000243
244 virtual void ChangeState() = 0;
245
246 // Gets the content info appropriate to the channel (audio or video).
247 virtual const ContentInfo* GetFirstContent(
248 const SessionDescription* sdesc) = 0;
249 bool UpdateLocalStreams_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 bool UpdateRemoteStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000253 ContentAction action,
254 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000256 ContentAction action,
257 std::string* error_desc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000258 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000259 ContentAction action,
260 std::string* error_desc) = 0;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700261 bool SetRtpTransportParameters_w(const MediaContentDescription* content,
262 ContentAction action,
263 ContentSource src,
264 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000266 // Helper method to get RTP Absoulute SendTime extension header id if
267 // present in remote supported extensions list.
268 void MaybeCacheRtpAbsSendTimeHeaderExtension(
stefanc1aeaf02015-10-15 07:26:07 -0700269 const std::vector<RtpHeaderExtension>& extensions);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000270
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000271 bool CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
272 bool* dtls,
273 std::string* error_desc);
274 bool SetSrtp_w(const std::vector<CryptoParams>& params,
275 ContentAction action,
276 ContentSource src,
277 std::string* error_desc);
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700278 void ActivateRtcpMux_w();
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000279 bool SetRtcpMux_w(bool enable,
280 ContentAction action,
281 ContentSource src,
282 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000283
284 // From MessageHandler
rlesterec9d1872015-10-27 14:22:16 -0700285 void OnMessage(rtc::Message* pmsg) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000286
287 // Handled in derived classes
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800288 // Get the SRTP crypto suites to use for RTP media
289 virtual void GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const = 0;
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000290 virtual void OnConnectionMonitorUpdate(ConnectionMonitor* monitor,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000291 const std::vector<ConnectionInfo>& infos) = 0;
292
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000293 // Helper function for invoking bool-returning methods on the worker thread.
294 template <class FunctorT>
295 bool InvokeOnWorker(const FunctorT& functor) {
296 return worker_thread_->Invoke<bool>(functor);
297 }
298
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000300 rtc::Thread* worker_thread_;
deadbeefcbecd352015-09-23 11:50:27 -0700301 TransportController* transport_controller_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000302 MediaChannel* media_channel_;
303 std::vector<StreamParams> local_streams_;
304 std::vector<StreamParams> remote_streams_;
305
pthatcher@webrtc.org990a00c2015-03-13 18:20:33 +0000306 const std::string content_name_;
deadbeefcbecd352015-09-23 11:50:27 -0700307 std::string transport_name_;
308 bool rtcp_transport_enabled_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000309 TransportChannel* transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700310 std::vector<std::pair<rtc::Socket::Option, int> > socket_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000311 TransportChannel* rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700312 std::vector<std::pair<rtc::Socket::Option, int> > rtcp_socket_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000313 SrtpFilter srtp_filter_;
314 RtcpMuxFilter rtcp_mux_filter_;
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000315 BundleFilter bundle_filter_;
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000316 rtc::scoped_ptr<ConnectionMonitor> connection_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000317 bool enabled_;
318 bool writable_;
319 bool rtp_ready_to_send_;
320 bool rtcp_ready_to_send_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000321 bool was_ever_writable_;
322 MediaContentDirection local_content_direction_;
323 MediaContentDirection remote_content_direction_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000324 bool has_received_packet_;
325 bool dtls_keyed_;
326 bool secure_required_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000327 int rtp_abs_sendtime_extn_id_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000328};
329
330// VoiceChannel is a specialization that adds support for early media, DTMF,
331// and input/output level monitoring.
332class VoiceChannel : public BaseChannel {
333 public:
deadbeefcbecd352015-09-23 11:50:27 -0700334 VoiceChannel(rtc::Thread* thread,
335 MediaEngineInterface* media_engine,
336 VoiceMediaChannel* channel,
337 TransportController* transport_controller,
338 const std::string& content_name,
339 bool rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000340 ~VoiceChannel();
341 bool Init();
solenberg1dd98f32015-09-10 01:57:14 -0700342
343 // Configure sending media on the stream with SSRC |ssrc|
344 // If there is only one sending stream SSRC 0 can be used.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200345 bool SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -0700346 bool enable,
deadbeefcbecd352015-09-23 11:50:27 -0700347 const AudioOptions* options,
solenberg1dd98f32015-09-10 01:57:14 -0700348 AudioRenderer* renderer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000349
350 // downcasts a MediaChannel
351 virtual VoiceMediaChannel* media_channel() const {
352 return static_cast<VoiceMediaChannel*>(BaseChannel::media_channel());
353 }
354
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000355 void SetEarlyMedia(bool enable);
356 // This signal is emitted when we have gone a period of time without
357 // receiving early media. When received, a UI should start playing its
358 // own ringing sound
359 sigslot::signal1<VoiceChannel*> SignalEarlyMediaTimeout;
360
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000361 // Returns if the telephone-event has been negotiated.
362 bool CanInsertDtmf();
363 // Send and/or play a DTMF |event| according to the |flags|.
364 // The DTMF out-of-band signal will be used on sending.
365 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000366 // The valid value for the |event| are 0 which corresponding to DTMF
367 // event 0-9, *, #, A-D.
solenberg1d63dd02015-12-02 12:35:09 -0800368 bool InsertDtmf(uint32_t ssrc, int event_code, int duration);
solenberg4bac9c52015-10-09 02:32:53 -0700369 bool SetOutputVolume(uint32_t ssrc, double volume);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000370 // Get statistics about the current media session.
371 bool GetStats(VoiceMediaInfo* stats);
372
373 // Monitoring functions
374 sigslot::signal2<VoiceChannel*, const std::vector<ConnectionInfo>&>
375 SignalConnectionMonitor;
376
377 void StartMediaMonitor(int cms);
378 void StopMediaMonitor();
379 sigslot::signal2<VoiceChannel*, const VoiceMediaInfo&> SignalMediaMonitor;
380
381 void StartAudioMonitor(int cms);
382 void StopAudioMonitor();
383 bool IsAudioMonitorRunning() const;
384 sigslot::signal2<VoiceChannel*, const AudioInfo&> SignalAudioMonitor;
385
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000386 int GetInputLevel_w();
387 int GetOutputLevel_w();
388 void GetActiveStreams_w(AudioInfo::StreamList* actives);
389
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000390 private:
391 // overrides from BaseChannel
392 virtual void OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000393 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000394 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000395 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000396 virtual void ChangeState();
397 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
398 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000399 ContentAction action,
400 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000401 virtual bool SetRemoteContent_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 void HandleEarlyMediaTimeout();
solenberg1d63dd02015-12-02 12:35:09 -0800405 bool InsertDtmf_w(uint32_t ssrc, int event, int duration);
solenberg4bac9c52015-10-09 02:32:53 -0700406 bool SetOutputVolume_w(uint32_t ssrc, double volume);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000407 bool GetStats_w(VoiceMediaInfo* stats);
408
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000409 virtual void OnMessage(rtc::Message* pmsg);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800410 virtual void GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000411 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000412 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000413 virtual void OnMediaMonitorUpdate(
414 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info);
415 void OnAudioMonitorUpdate(AudioMonitor* monitor, const AudioInfo& info);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000416
417 static const int kEarlyMediaTimeout = 1000;
Fredrik Solenberg0c022642015-08-05 12:25:22 +0200418 MediaEngineInterface* media_engine_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000419 bool received_media_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000420 rtc::scoped_ptr<VoiceMediaMonitor> media_monitor_;
421 rtc::scoped_ptr<AudioMonitor> audio_monitor_;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700422
423 // Last AudioSendParameters sent down to the media_channel() via
424 // SetSendParameters.
425 AudioSendParameters last_send_params_;
426 // Last AudioRecvParameters sent down to the media_channel() via
427 // SetRecvParameters.
428 AudioRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000429};
430
431// VideoChannel is a specialization for video.
432class VideoChannel : public BaseChannel {
433 public:
deadbeefcbecd352015-09-23 11:50:27 -0700434 VideoChannel(rtc::Thread* thread,
435 VideoMediaChannel* channel,
436 TransportController* transport_controller,
437 const std::string& content_name,
Fredrik Solenberg0c022642015-08-05 12:25:22 +0200438 bool rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000439 ~VideoChannel();
440 bool Init();
441
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200442 // downcasts a MediaChannel
443 virtual VideoMediaChannel* media_channel() const {
444 return static_cast<VideoMediaChannel*>(BaseChannel::media_channel());
445 }
446
Peter Boström0c4e06b2015-10-07 12:23:21 +0200447 bool SetRenderer(uint32_t ssrc, VideoRenderer* renderer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000448 bool ApplyViewRequest(const ViewRequest& request);
449
450 // TODO(pthatcher): Refactor to use a "capture id" instead of an
451 // ssrc here as the "key".
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000452 // Passes ownership of the capturer to the channel.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200453 bool AddScreencast(uint32_t ssrc, VideoCapturer* capturer);
454 bool SetCapturer(uint32_t ssrc, VideoCapturer* capturer);
455 bool RemoveScreencast(uint32_t ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000456 // True if we've added a screencast. Doesn't matter if the capturer
457 // has been started or not.
458 bool IsScreencasting();
Peter Boström0c4e06b2015-10-07 12:23:21 +0200459 int GetScreencastFps(uint32_t ssrc);
460 int GetScreencastMaxPixels(uint32_t ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000461 // Get statistics about the current media session.
pbos@webrtc.org058b1f12015-03-04 08:54:32 +0000462 bool GetStats(VideoMediaInfo* stats);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000463
464 sigslot::signal2<VideoChannel*, const std::vector<ConnectionInfo>&>
465 SignalConnectionMonitor;
466
467 void StartMediaMonitor(int cms);
468 void StopMediaMonitor();
469 sigslot::signal2<VideoChannel*, const VideoMediaInfo&> SignalMediaMonitor;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200470 sigslot::signal2<uint32_t, rtc::WindowEvent> SignalScreencastWindowEvent;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000471
472 bool SendIntraFrame();
473 bool RequestIntraFrame();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000474
Peter Boström0c4e06b2015-10-07 12:23:21 +0200475 bool SetVideoSend(uint32_t ssrc, bool enable, const VideoOptions* options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000476
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477 private:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200478 typedef std::map<uint32_t, VideoCapturer*> ScreencastMap;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000479 struct ScreencastDetailsData;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480
481 // overrides from BaseChannel
482 virtual void ChangeState();
483 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
484 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000485 ContentAction action,
486 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000487 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000488 ContentAction action,
489 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490 bool ApplyViewRequest_w(const ViewRequest& request);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491
Peter Boström0c4e06b2015-10-07 12:23:21 +0200492 bool AddScreencast_w(uint32_t ssrc, VideoCapturer* capturer);
493 bool RemoveScreencast_w(uint32_t ssrc);
494 void OnScreencastWindowEvent_s(uint32_t ssrc, rtc::WindowEvent we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000495 bool IsScreencasting_w() const;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000496 void GetScreencastDetails_w(ScreencastDetailsData* d) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497 bool GetStats_w(VideoMediaInfo* stats);
498
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000499 virtual void OnMessage(rtc::Message* pmsg);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800500 virtual void GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000501 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000502 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000503 virtual void OnMediaMonitorUpdate(
504 VideoMediaChannel* media_channel, const VideoMediaInfo& info);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200505 virtual void OnScreencastWindowEvent(uint32_t ssrc, rtc::WindowEvent event);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000506 virtual void OnStateChange(VideoCapturer* capturer, CaptureState ev);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200507 bool GetLocalSsrc(const VideoCapturer* capturer, uint32_t* ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000508
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000509 VideoRenderer* renderer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000510 ScreencastMap screencast_capturers_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000511 rtc::scoped_ptr<VideoMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000512
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000513 rtc::WindowEvent previous_we_;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700514
515 // Last VideoSendParameters sent down to the media_channel() via
516 // SetSendParameters.
517 VideoSendParameters last_send_params_;
518 // Last VideoRecvParameters sent down to the media_channel() via
519 // SetRecvParameters.
520 VideoRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000521};
522
523// DataChannel is a specialization for data.
524class DataChannel : public BaseChannel {
525 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000526 DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000527 DataMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -0700528 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529 const std::string& content_name,
530 bool rtcp);
531 ~DataChannel();
532 bool Init();
533
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000534 virtual bool SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000535 const rtc::Buffer& payload,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000536 SendDataResult* result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000537
538 void StartMediaMonitor(int cms);
539 void StopMediaMonitor();
540
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000541 // Should be called on the signaling thread only.
542 bool ready_to_send_data() const {
543 return ready_to_send_data_;
544 }
545
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546 sigslot::signal2<DataChannel*, const DataMediaInfo&> SignalMediaMonitor;
547 sigslot::signal2<DataChannel*, const std::vector<ConnectionInfo>&>
548 SignalConnectionMonitor;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200549 sigslot::signal3<DataChannel*, const ReceiveDataParams&, const rtc::Buffer&>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000550 SignalDataReceived;
551 // Signal for notifying when the channel becomes ready to send data.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000552 // That occurs when the channel is enabled, the transport is writable,
553 // both local and remote descriptions are set, and the channel is unblocked.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000554 sigslot::signal1<bool> SignalReadyToSendData;
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +0000555 // Signal for notifying that the remote side has closed the DataChannel.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200556 sigslot::signal1<uint32_t> SignalStreamClosedRemotely;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000558 protected:
559 // downcasts a MediaChannel.
560 virtual DataMediaChannel* media_channel() const {
561 return static_cast<DataMediaChannel*>(BaseChannel::media_channel());
562 }
563
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000564 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000565 struct SendDataMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000566 SendDataMessageData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000567 const rtc::Buffer* payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000568 SendDataResult* result)
569 : params(params),
570 payload(payload),
571 result(result),
572 succeeded(false) {
573 }
574
575 const SendDataParams& params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000576 const rtc::Buffer* payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000577 SendDataResult* result;
578 bool succeeded;
579 };
580
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000581 struct DataReceivedMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000582 // We copy the data because the data will become invalid after we
583 // handle DataMediaChannel::SignalDataReceived but before we fire
584 // SignalDataReceived.
585 DataReceivedMessageData(
586 const ReceiveDataParams& params, const char* data, size_t len)
587 : params(params),
588 payload(data, len) {
589 }
590 const ReceiveDataParams params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000591 const rtc::Buffer payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000592 };
593
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000594 typedef rtc::TypedMessageData<bool> DataChannelReadyToSendMessageData;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000595
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000596 // overrides from BaseChannel
597 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
598 // If data_channel_type_ is DCT_NONE, set it. Otherwise, check that
599 // it's the same as what was set previously. Returns false if it's
600 // set to one type one type and changed to another type later.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000601 bool SetDataChannelType(DataChannelType new_data_channel_type,
602 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000603 // Same as SetDataChannelType, but extracts the type from the
604 // DataContentDescription.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000605 bool SetDataChannelTypeFromContent(const DataContentDescription* content,
606 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000607 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000608 ContentAction action,
609 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000610 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000611 ContentAction action,
612 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000613 virtual void ChangeState();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000614 virtual bool WantsPacket(bool rtcp, rtc::Buffer* packet);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000615
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000616 virtual void OnMessage(rtc::Message* pmsg);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800617 virtual void GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000618 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000619 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000620 virtual void OnMediaMonitorUpdate(
621 DataMediaChannel* media_channel, const DataMediaInfo& info);
622 virtual bool ShouldSetupDtlsSrtp() const;
623 void OnDataReceived(
624 const ReceiveDataParams& params, const char* data, size_t len);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200625 void OnDataChannelError(uint32_t ssrc, DataMediaChannel::Error error);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000626 void OnDataChannelReadyToSend(bool writable);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200627 void OnStreamClosedRemotely(uint32_t sid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000628
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000629 rtc::scoped_ptr<DataMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000630 // TODO(pthatcher): Make a separate SctpDataChannel and
631 // RtpDataChannel instead of using this.
632 DataChannelType data_channel_type_;
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000633 bool ready_to_send_data_;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700634
635 // Last DataSendParameters sent down to the media_channel() via
636 // SetSendParameters.
637 DataSendParameters last_send_params_;
638 // Last DataRecvParameters sent down to the media_channel() via
639 // SetRecvParameters.
640 DataRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000641};
642
643} // namespace cricket
644
645#endif // TALK_SESSION_MEDIA_CHANNEL_H_