blob: 1d0a887ff975ffad24d686f2f87ede83dcb0b6a7 [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>
33
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034#include "talk/media/base/mediachannel.h"
35#include "talk/media/base/mediaengine.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036#include "talk/media/base/streamparams.h"
37#include "talk/media/base/videocapturer.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000038#include "webrtc/p2p/base/session.h"
39#include "webrtc/p2p/client/socketmonitor.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040#include "talk/session/media/audiomonitor.h"
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +000041#include "talk/session/media/bundlefilter.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042#include "talk/session/media/mediamonitor.h"
43#include "talk/session/media/mediasession.h"
44#include "talk/session/media/rtcpmuxfilter.h"
45#include "talk/session/media/srtpfilter.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000046#include "webrtc/base/asyncudpsocket.h"
47#include "webrtc/base/criticalsection.h"
48#include "webrtc/base/network.h"
49#include "webrtc/base/sigslot.h"
50#include "webrtc/base/window.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051
52namespace cricket {
53
54struct CryptoParams;
55class MediaContentDescription;
56struct TypingMonitorOptions;
57class TypingMonitor;
58struct ViewRequest;
59
60enum SinkType {
61 SINK_PRE_CRYPTO, // Sink packets before encryption or after decryption.
62 SINK_POST_CRYPTO // Sink packets after encryption or before decryption.
63};
64
65// BaseChannel contains logic common to voice and video, including
66// enable/mute, marshaling calls to a worker thread, and
67// connection and media monitors.
wu@webrtc.org78187522013-10-07 23:32:02 +000068//
69// WARNING! SUBCLASSES MUST CALL Deinit() IN THEIR DESTRUCTORS!
70// This is required to avoid a data race between the destructor modifying the
71// vtable, and the media channel's thread using BaseChannel as the
72// NetworkInterface.
73
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074class BaseChannel
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000075 : public rtc::MessageHandler, public sigslot::has_slots<>,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076 public MediaChannel::NetworkInterface {
77 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000078 BaseChannel(rtc::Thread* thread, MediaEngineInterface* media_engine,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079 MediaChannel* channel, BaseSession* session,
80 const std::string& content_name, bool rtcp);
81 virtual ~BaseChannel();
82 bool Init(TransportChannel* transport_channel,
83 TransportChannel* rtcp_transport_channel);
wu@webrtc.org78187522013-10-07 23:32:02 +000084 // Deinit may be called multiple times and is simply ignored if it's alreay
85 // done.
86 void Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000088 rtc::Thread* worker_thread() const { return worker_thread_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 BaseSession* session() const { return session_; }
90 const std::string& content_name() { return content_name_; }
91 TransportChannel* transport_channel() const {
92 return transport_channel_;
93 }
94 TransportChannel* rtcp_transport_channel() const {
95 return rtcp_transport_channel_;
96 }
97 bool enabled() const { return enabled_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098
99 // This function returns true if we are using SRTP.
100 bool secure() const { return srtp_filter_.IsActive(); }
101 // The following function returns true if we are using
102 // DTLS-based keying. If you turned off SRTP later, however
103 // you could have secure() == false and dtls_secure() == true.
104 bool secure_dtls() const { return dtls_keyed_; }
105 // This function returns true if we require secure channel for call setup.
106 bool secure_required() const { return secure_required_; }
107
108 bool writable() const { return writable_; }
109 bool IsStreamMuted(uint32 ssrc);
110
111 // Channel control
112 bool SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000113 ContentAction action,
114 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115 bool SetRemoteContent(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
119 bool Enable(bool enable);
120 // Mute sending media on the stream with SSRC |ssrc|
121 // If there is only one sending stream SSRC 0 can be used.
122 bool MuteStream(uint32 ssrc, bool mute);
123
124 // Multiplexing
125 bool AddRecvStream(const StreamParams& sp);
126 bool RemoveRecvStream(uint32 ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000127 bool AddSendStream(const StreamParams& sp);
128 bool RemoveSendStream(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129
130 // Monitoring
131 void StartConnectionMonitor(int cms);
132 void StopConnectionMonitor();
133
134 void set_srtp_signal_silent_time(uint32 silent_time) {
135 srtp_filter_.set_signal_silent_time(silent_time);
136 }
137
138 void set_content_name(const std::string& content_name) {
139 ASSERT(signaling_thread()->IsCurrent());
140 ASSERT(!writable_);
141 if (session_->state() != BaseSession::STATE_INIT) {
142 LOG(LS_ERROR) << "Content name for a channel can be changed only "
143 << "when BaseSession is in STATE_INIT state.";
144 return;
145 }
146 content_name_ = content_name;
147 }
148
149 template <class T>
150 void RegisterSendSink(T* sink,
151 void (T::*OnPacket)(const void*, size_t, bool),
152 SinkType type) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000153 rtc::CritScope cs(&signal_send_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154 if (SINK_POST_CRYPTO == type) {
155 SignalSendPacketPostCrypto.disconnect(sink);
156 SignalSendPacketPostCrypto.connect(sink, OnPacket);
157 } else {
158 SignalSendPacketPreCrypto.disconnect(sink);
159 SignalSendPacketPreCrypto.connect(sink, OnPacket);
160 }
161 }
162
163 void UnregisterSendSink(sigslot::has_slots<>* sink,
164 SinkType type) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000165 rtc::CritScope cs(&signal_send_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166 if (SINK_POST_CRYPTO == type) {
167 SignalSendPacketPostCrypto.disconnect(sink);
168 } else {
169 SignalSendPacketPreCrypto.disconnect(sink);
170 }
171 }
172
173 bool HasSendSinks(SinkType type) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000174 rtc::CritScope cs(&signal_send_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 if (SINK_POST_CRYPTO == type) {
176 return !SignalSendPacketPostCrypto.is_empty();
177 } else {
178 return !SignalSendPacketPreCrypto.is_empty();
179 }
180 }
181
182 template <class T>
183 void RegisterRecvSink(T* sink,
184 void (T::*OnPacket)(const void*, size_t, bool),
185 SinkType type) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000186 rtc::CritScope cs(&signal_recv_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187 if (SINK_POST_CRYPTO == type) {
188 SignalRecvPacketPostCrypto.disconnect(sink);
189 SignalRecvPacketPostCrypto.connect(sink, OnPacket);
190 } else {
191 SignalRecvPacketPreCrypto.disconnect(sink);
192 SignalRecvPacketPreCrypto.connect(sink, OnPacket);
193 }
194 }
195
196 void UnregisterRecvSink(sigslot::has_slots<>* sink,
197 SinkType type) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000198 rtc::CritScope cs(&signal_recv_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199 if (SINK_POST_CRYPTO == type) {
200 SignalRecvPacketPostCrypto.disconnect(sink);
201 } else {
202 SignalRecvPacketPreCrypto.disconnect(sink);
203 }
204 }
205
206 bool HasRecvSinks(SinkType type) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000207 rtc::CritScope cs(&signal_recv_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208 if (SINK_POST_CRYPTO == type) {
209 return !SignalRecvPacketPostCrypto.is_empty();
210 } else {
211 return !SignalRecvPacketPreCrypto.is_empty();
212 }
213 }
214
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000215 BundleFilter* bundle_filter() { return &bundle_filter_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000216
217 const std::vector<StreamParams>& local_streams() const {
218 return local_streams_;
219 }
220 const std::vector<StreamParams>& remote_streams() const {
221 return remote_streams_;
222 }
223
buildbot@webrtc.org6bfd6192014-05-15 16:15:59 +0000224 // Used for latency measurements.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000225 sigslot::signal1<BaseChannel*> SignalFirstPacketReceived;
226
227 // Used to alert UI when the muted status changes, perhaps autonomously.
228 sigslot::repeater2<BaseChannel*, bool> SignalAutoMuted;
229
230 // Made public for easier testing.
231 void SetReadyToSend(TransportChannel* channel, bool ready);
232
guoweis@webrtc.org4f852882015-03-12 20:09:44 +0000233 // Only public for unit tests. Otherwise, consider protected.
234 virtual int SetOption(SocketType type, rtc::Socket::Option o, int val);
235
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236 protected:
237 MediaEngineInterface* media_engine() const { return media_engine_; }
238 virtual MediaChannel* media_channel() const { return media_channel_; }
239 void set_rtcp_transport_channel(TransportChannel* transport);
240 bool was_ever_writable() const { return was_ever_writable_; }
241 void set_local_content_direction(MediaContentDirection direction) {
242 local_content_direction_ = direction;
243 }
244 void set_remote_content_direction(MediaContentDirection direction) {
245 remote_content_direction_ = direction;
246 }
247 bool IsReadyToReceive() const;
248 bool IsReadyToSend() const;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000249 rtc::Thread* signaling_thread() { return session_->signaling_thread(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000250 SrtpFilter* srtp_filter() { return &srtp_filter_; }
251 bool rtcp() const { return rtcp_; }
252
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000253 void FlushRtcpMessages();
254
255 // NetworkInterface implementation, called by MediaEngine
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000256 virtual bool SendPacket(rtc::Buffer* packet,
257 rtc::DiffServCodePoint dscp);
258 virtual bool SendRtcp(rtc::Buffer* packet,
259 rtc::DiffServCodePoint dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000260
261 // From TransportChannel
262 void OnWritableState(TransportChannel* channel);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000263 virtual void OnChannelRead(TransportChannel* channel,
264 const char* data,
265 size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000266 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000267 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000268 void OnReadyToSend(TransportChannel* channel);
269
270 bool PacketIsRtcp(const TransportChannel* channel, const char* data,
271 size_t len);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000272 bool SendPacket(bool rtcp, rtc::Buffer* packet,
273 rtc::DiffServCodePoint dscp);
274 virtual bool WantsPacket(bool rtcp, rtc::Buffer* packet);
275 void HandlePacket(bool rtcp, rtc::Buffer* packet,
276 const rtc::PacketTime& packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277
278 // Apply the new local/remote session description.
279 void OnNewLocalDescription(BaseSession* session, ContentAction action);
280 void OnNewRemoteDescription(BaseSession* session, ContentAction action);
281
282 void EnableMedia_w();
283 void DisableMedia_w();
284 virtual bool MuteStream_w(uint32 ssrc, bool mute);
285 bool IsStreamMuted_w(uint32 ssrc);
286 void ChannelWritable_w();
287 void ChannelNotWritable_w();
288 bool AddRecvStream_w(const StreamParams& sp);
289 bool RemoveRecvStream_w(uint32 ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000290 bool AddSendStream_w(const StreamParams& sp);
291 bool RemoveSendStream_w(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000292 virtual bool ShouldSetupDtlsSrtp() const;
293 // Do the DTLS key expansion and impose it on the SRTP/SRTCP filters.
294 // |rtcp_channel| indicates whether to set up the RTP or RTCP filter.
295 bool SetupDtlsSrtp(bool rtcp_channel);
296 // Set the DTLS-SRTP cipher policy on this channel as appropriate.
297 bool SetDtlsSrtpCiphers(TransportChannel *tc, bool rtcp);
298
299 virtual void ChangeState() = 0;
300
301 // Gets the content info appropriate to the channel (audio or video).
302 virtual const ContentInfo* GetFirstContent(
303 const SessionDescription* sdesc) = 0;
304 bool UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000305 ContentAction action,
306 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000307 bool UpdateRemoteStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000308 ContentAction action,
309 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000310 bool SetBaseLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000311 ContentAction action,
312 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000313 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000314 ContentAction action,
315 std::string* error_desc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000316 bool SetBaseRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000317 ContentAction action,
318 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000319 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000320 ContentAction action,
321 std::string* error_desc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000322
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000323 // Helper method to get RTP Absoulute SendTime extension header id if
324 // present in remote supported extensions list.
325 void MaybeCacheRtpAbsSendTimeHeaderExtension(
326 const std::vector<RtpHeaderExtension>& extensions);
327
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +0000328 bool SetRecvRtpHeaderExtensions_w(const MediaContentDescription* content,
329 MediaChannel* media_channel,
330 std::string* error_desc);
331 bool SetSendRtpHeaderExtensions_w(const MediaContentDescription* content,
332 MediaChannel* media_channel,
333 std::string* error_desc);
334
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000335 bool CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
336 bool* dtls,
337 std::string* error_desc);
338 bool SetSrtp_w(const std::vector<CryptoParams>& params,
339 ContentAction action,
340 ContentSource src,
341 std::string* error_desc);
342 bool SetRtcpMux_w(bool enable,
343 ContentAction action,
344 ContentSource src,
345 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346
347 // From MessageHandler
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000348 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000349
350 // Handled in derived classes
351 // Get the SRTP ciphers to use for RTP media
352 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const = 0;
353 virtual void OnConnectionMonitorUpdate(SocketMonitor* monitor,
354 const std::vector<ConnectionInfo>& infos) = 0;
355
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000356 // Helper function for invoking bool-returning methods on the worker thread.
357 template <class FunctorT>
358 bool InvokeOnWorker(const FunctorT& functor) {
359 return worker_thread_->Invoke<bool>(functor);
360 }
361
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000362 private:
363 sigslot::signal3<const void*, size_t, bool> SignalSendPacketPreCrypto;
364 sigslot::signal3<const void*, size_t, bool> SignalSendPacketPostCrypto;
365 sigslot::signal3<const void*, size_t, bool> SignalRecvPacketPreCrypto;
366 sigslot::signal3<const void*, size_t, bool> SignalRecvPacketPostCrypto;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000367 rtc::CriticalSection signal_send_packet_cs_;
368 rtc::CriticalSection signal_recv_packet_cs_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000369
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000370 rtc::Thread* worker_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000371 MediaEngineInterface* media_engine_;
372 BaseSession* session_;
373 MediaChannel* media_channel_;
374 std::vector<StreamParams> local_streams_;
375 std::vector<StreamParams> remote_streams_;
376
377 std::string content_name_;
378 bool rtcp_;
379 TransportChannel* transport_channel_;
380 TransportChannel* rtcp_transport_channel_;
381 SrtpFilter srtp_filter_;
382 RtcpMuxFilter rtcp_mux_filter_;
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000383 BundleFilter bundle_filter_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000384 rtc::scoped_ptr<SocketMonitor> socket_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000385 bool enabled_;
386 bool writable_;
387 bool rtp_ready_to_send_;
388 bool rtcp_ready_to_send_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000389 bool was_ever_writable_;
390 MediaContentDirection local_content_direction_;
391 MediaContentDirection remote_content_direction_;
392 std::set<uint32> muted_streams_;
393 bool has_received_packet_;
394 bool dtls_keyed_;
395 bool secure_required_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000396 int rtp_abs_sendtime_extn_id_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000397};
398
399// VoiceChannel is a specialization that adds support for early media, DTMF,
400// and input/output level monitoring.
401class VoiceChannel : public BaseChannel {
402 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000403 VoiceChannel(rtc::Thread* thread, MediaEngineInterface* media_engine,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000404 VoiceMediaChannel* channel, BaseSession* session,
405 const std::string& content_name, bool rtcp);
406 ~VoiceChannel();
407 bool Init();
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000408 bool SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer);
409 bool SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000410
411 // downcasts a MediaChannel
412 virtual VoiceMediaChannel* media_channel() const {
413 return static_cast<VoiceMediaChannel*>(BaseChannel::media_channel());
414 }
415
416 bool SetRingbackTone(const void* buf, int len);
417 void SetEarlyMedia(bool enable);
418 // This signal is emitted when we have gone a period of time without
419 // receiving early media. When received, a UI should start playing its
420 // own ringing sound
421 sigslot::signal1<VoiceChannel*> SignalEarlyMediaTimeout;
422
423 bool PlayRingbackTone(uint32 ssrc, bool play, bool loop);
424 // TODO(ronghuawu): Replace PressDTMF with InsertDtmf.
425 bool PressDTMF(int digit, bool playout);
426 // Returns if the telephone-event has been negotiated.
427 bool CanInsertDtmf();
428 // Send and/or play a DTMF |event| according to the |flags|.
429 // The DTMF out-of-band signal will be used on sending.
430 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000431 // The valid value for the |event| are 0 which corresponding to DTMF
432 // event 0-9, *, #, A-D.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000433 bool InsertDtmf(uint32 ssrc, int event_code, int duration, int flags);
434 bool SetOutputScaling(uint32 ssrc, double left, double right);
435 // Get statistics about the current media session.
436 bool GetStats(VoiceMediaInfo* stats);
437
438 // Monitoring functions
439 sigslot::signal2<VoiceChannel*, const std::vector<ConnectionInfo>&>
440 SignalConnectionMonitor;
441
442 void StartMediaMonitor(int cms);
443 void StopMediaMonitor();
444 sigslot::signal2<VoiceChannel*, const VoiceMediaInfo&> SignalMediaMonitor;
445
446 void StartAudioMonitor(int cms);
447 void StopAudioMonitor();
448 bool IsAudioMonitorRunning() const;
449 sigslot::signal2<VoiceChannel*, const AudioInfo&> SignalAudioMonitor;
450
451 void StartTypingMonitor(const TypingMonitorOptions& settings);
452 void StopTypingMonitor();
453 bool IsTypingMonitorRunning() const;
454
455 // Overrides BaseChannel::MuteStream_w.
456 virtual bool MuteStream_w(uint32 ssrc, bool mute);
457
458 int GetInputLevel_w();
459 int GetOutputLevel_w();
460 void GetActiveStreams_w(AudioInfo::StreamList* actives);
461
462 // Signal errors from VoiceMediaChannel. Arguments are:
463 // ssrc(uint32), and error(VoiceMediaChannel::Error).
464 sigslot::signal3<VoiceChannel*, uint32, VoiceMediaChannel::Error>
465 SignalMediaError;
466
467 // Configuration and setting.
468 bool SetChannelOptions(const AudioOptions& options);
469
470 private:
471 // overrides from BaseChannel
472 virtual void OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000473 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000474 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000475 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000476 virtual void ChangeState();
477 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
478 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000479 ContentAction action,
480 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000481 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000482 ContentAction action,
483 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000484 bool SetRingbackTone_w(const void* buf, int len);
485 bool PlayRingbackTone_w(uint32 ssrc, bool play, bool loop);
486 void HandleEarlyMediaTimeout();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000487 bool InsertDtmf_w(uint32 ssrc, int event, int duration, int flags);
488 bool SetOutputScaling_w(uint32 ssrc, double left, double right);
489 bool GetStats_w(VoiceMediaInfo* stats);
490
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000491 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000492 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
493 virtual void OnConnectionMonitorUpdate(
494 SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos);
495 virtual void OnMediaMonitorUpdate(
496 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info);
497 void OnAudioMonitorUpdate(AudioMonitor* monitor, const AudioInfo& info);
498 void OnVoiceChannelError(uint32 ssrc, VoiceMediaChannel::Error error);
499 void SendLastMediaError();
500 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000501
502 static const int kEarlyMediaTimeout = 1000;
503 bool received_media_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000504 rtc::scoped_ptr<VoiceMediaMonitor> media_monitor_;
505 rtc::scoped_ptr<AudioMonitor> audio_monitor_;
506 rtc::scoped_ptr<TypingMonitor> typing_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000507};
508
509// VideoChannel is a specialization for video.
510class VideoChannel : public BaseChannel {
511 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000512 VideoChannel(rtc::Thread* thread, MediaEngineInterface* media_engine,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000513 VideoMediaChannel* channel, BaseSession* session,
514 const std::string& content_name, bool rtcp,
515 VoiceChannel* voice_channel);
516 ~VideoChannel();
517 bool Init();
518
519 bool SetRenderer(uint32 ssrc, VideoRenderer* renderer);
520 bool ApplyViewRequest(const ViewRequest& request);
521
522 // TODO(pthatcher): Refactor to use a "capture id" instead of an
523 // ssrc here as the "key".
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000524 // Passes ownership of the capturer to the channel.
525 bool AddScreencast(uint32 ssrc, VideoCapturer* capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526 bool SetCapturer(uint32 ssrc, VideoCapturer* capturer);
527 bool RemoveScreencast(uint32 ssrc);
528 // True if we've added a screencast. Doesn't matter if the capturer
529 // has been started or not.
530 bool IsScreencasting();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000531 int GetScreencastFps(uint32 ssrc);
532 int GetScreencastMaxPixels(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000533 // Get statistics about the current media session.
pbos@webrtc.org058b1f12015-03-04 08:54:32 +0000534 bool GetStats(VideoMediaInfo* stats);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535
536 sigslot::signal2<VideoChannel*, const std::vector<ConnectionInfo>&>
537 SignalConnectionMonitor;
538
539 void StartMediaMonitor(int cms);
540 void StopMediaMonitor();
541 sigslot::signal2<VideoChannel*, const VideoMediaInfo&> SignalMediaMonitor;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000542 sigslot::signal2<uint32, rtc::WindowEvent> SignalScreencastWindowEvent;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000543
544 bool SendIntraFrame();
545 bool RequestIntraFrame();
546 sigslot::signal3<VideoChannel*, uint32, VideoMediaChannel::Error>
547 SignalMediaError;
548
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000549 // Configuration and setting.
550 bool SetChannelOptions(const VideoOptions& options);
551
552 protected:
553 // downcasts a MediaChannel
554 virtual VideoMediaChannel* media_channel() const {
555 return static_cast<VideoMediaChannel*>(BaseChannel::media_channel());
556 }
557
558 private:
559 typedef std::map<uint32, VideoCapturer*> ScreencastMap;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000560 struct ScreencastDetailsData;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000561
562 // overrides from BaseChannel
563 virtual void ChangeState();
564 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
565 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000566 ContentAction action,
567 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000568 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000569 ContentAction action,
570 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000571 bool ApplyViewRequest_w(const ViewRequest& request);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000572
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000573 bool AddScreencast_w(uint32 ssrc, VideoCapturer* capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000574 bool RemoveScreencast_w(uint32 ssrc);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000575 void OnScreencastWindowEvent_s(uint32 ssrc, rtc::WindowEvent we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000576 bool IsScreencasting_w() const;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000577 void GetScreencastDetails_w(ScreencastDetailsData* d) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000578 bool GetStats_w(VideoMediaInfo* stats);
579
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000580 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000581 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
582 virtual void OnConnectionMonitorUpdate(
583 SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos);
584 virtual void OnMediaMonitorUpdate(
585 VideoMediaChannel* media_channel, const VideoMediaInfo& info);
586 virtual void OnScreencastWindowEvent(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000587 rtc::WindowEvent event);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000588 virtual void OnStateChange(VideoCapturer* capturer, CaptureState ev);
589 bool GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc);
590
591 void OnVideoChannelError(uint32 ssrc, VideoMediaChannel::Error error);
592 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000593
594 VoiceChannel* voice_channel_;
595 VideoRenderer* renderer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000596 ScreencastMap screencast_capturers_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000597 rtc::scoped_ptr<VideoMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000598
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000599 rtc::WindowEvent previous_we_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000600};
601
602// DataChannel is a specialization for data.
603class DataChannel : public BaseChannel {
604 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000605 DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000606 DataMediaChannel* media_channel,
607 BaseSession* session,
608 const std::string& content_name,
609 bool rtcp);
610 ~DataChannel();
611 bool Init();
612
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000613 virtual bool SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000614 const rtc::Buffer& payload,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000615 SendDataResult* result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000616
617 void StartMediaMonitor(int cms);
618 void StopMediaMonitor();
619
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000620 // Should be called on the signaling thread only.
621 bool ready_to_send_data() const {
622 return ready_to_send_data_;
623 }
624
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000625 sigslot::signal2<DataChannel*, const DataMediaInfo&> SignalMediaMonitor;
626 sigslot::signal2<DataChannel*, const std::vector<ConnectionInfo>&>
627 SignalConnectionMonitor;
628 sigslot::signal3<DataChannel*, uint32, DataMediaChannel::Error>
629 SignalMediaError;
630 sigslot::signal3<DataChannel*,
631 const ReceiveDataParams&,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000632 const rtc::Buffer&>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000633 SignalDataReceived;
634 // Signal for notifying when the channel becomes ready to send data.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000635 // That occurs when the channel is enabled, the transport is writable,
636 // both local and remote descriptions are set, and the channel is unblocked.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637 sigslot::signal1<bool> SignalReadyToSendData;
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +0000638 // Signal for notifying that the remote side has closed the DataChannel.
639 sigslot::signal1<uint32> SignalStreamClosedRemotely;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000640
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000641 protected:
642 // downcasts a MediaChannel.
643 virtual DataMediaChannel* media_channel() const {
644 return static_cast<DataMediaChannel*>(BaseChannel::media_channel());
645 }
646
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000647 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000648 struct SendDataMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000649 SendDataMessageData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000650 const rtc::Buffer* payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000651 SendDataResult* result)
652 : params(params),
653 payload(payload),
654 result(result),
655 succeeded(false) {
656 }
657
658 const SendDataParams& params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000659 const rtc::Buffer* payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000660 SendDataResult* result;
661 bool succeeded;
662 };
663
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000664 struct DataReceivedMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000665 // We copy the data because the data will become invalid after we
666 // handle DataMediaChannel::SignalDataReceived but before we fire
667 // SignalDataReceived.
668 DataReceivedMessageData(
669 const ReceiveDataParams& params, const char* data, size_t len)
670 : params(params),
671 payload(data, len) {
672 }
673 const ReceiveDataParams params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000674 const rtc::Buffer payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000675 };
676
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000677 typedef rtc::TypedMessageData<bool> DataChannelReadyToSendMessageData;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000678
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000679 // overrides from BaseChannel
680 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
681 // If data_channel_type_ is DCT_NONE, set it. Otherwise, check that
682 // it's the same as what was set previously. Returns false if it's
683 // set to one type one type and changed to another type later.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000684 bool SetDataChannelType(DataChannelType new_data_channel_type,
685 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000686 // Same as SetDataChannelType, but extracts the type from the
687 // DataContentDescription.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000688 bool SetDataChannelTypeFromContent(const DataContentDescription* content,
689 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000690 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000691 ContentAction action,
692 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000693 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000694 ContentAction action,
695 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696 virtual void ChangeState();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000697 virtual bool WantsPacket(bool rtcp, rtc::Buffer* packet);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000698
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000699 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000700 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
701 virtual void OnConnectionMonitorUpdate(
702 SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos);
703 virtual void OnMediaMonitorUpdate(
704 DataMediaChannel* media_channel, const DataMediaInfo& info);
705 virtual bool ShouldSetupDtlsSrtp() const;
706 void OnDataReceived(
707 const ReceiveDataParams& params, const char* data, size_t len);
708 void OnDataChannelError(uint32 ssrc, DataMediaChannel::Error error);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000709 void OnDataChannelReadyToSend(bool writable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000710 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +0000711 void OnStreamClosedRemotely(uint32 sid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000712
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000713 rtc::scoped_ptr<DataMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000714 // TODO(pthatcher): Make a separate SctpDataChannel and
715 // RtpDataChannel instead of using this.
716 DataChannelType data_channel_type_;
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000717 bool ready_to_send_data_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000718};
719
720} // namespace cricket
721
722#endif // TALK_SESSION_MEDIA_CHANNEL_H_