blob: 91ea6222d480a86ecc39b0f1babafe037cd435fc [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
34#include "talk/base/asyncudpsocket.h"
35#include "talk/base/criticalsection.h"
36#include "talk/base/network.h"
37#include "talk/base/sigslot.h"
38#include "talk/base/window.h"
39#include "talk/media/base/mediachannel.h"
40#include "talk/media/base/mediaengine.h"
41#include "talk/media/base/screencastid.h"
42#include "talk/media/base/streamparams.h"
43#include "talk/media/base/videocapturer.h"
44#include "talk/p2p/base/session.h"
45#include "talk/p2p/client/socketmonitor.h"
46#include "talk/session/media/audiomonitor.h"
47#include "talk/session/media/mediamonitor.h"
48#include "talk/session/media/mediasession.h"
49#include "talk/session/media/rtcpmuxfilter.h"
50#include "talk/session/media/srtpfilter.h"
51#include "talk/session/media/ssrcmuxfilter.h"
52
53namespace cricket {
54
55struct CryptoParams;
56class MediaContentDescription;
57struct TypingMonitorOptions;
58class TypingMonitor;
59struct 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
67// enable/mute, marshaling calls to a worker thread, and
68// 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
76 : public talk_base::MessageHandler, public sigslot::has_slots<>,
77 public MediaChannel::NetworkInterface {
78 public:
79 BaseChannel(talk_base::Thread* thread, MediaEngineInterface* media_engine,
80 MediaChannel* channel, BaseSession* session,
81 const std::string& content_name, bool rtcp);
82 virtual ~BaseChannel();
83 bool Init(TransportChannel* transport_channel,
84 TransportChannel* rtcp_transport_channel);
wu@webrtc.org78187522013-10-07 23:32:02 +000085 // Deinit may be called multiple times and is simply ignored if it's alreay
86 // done.
87 void Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088
89 talk_base::Thread* worker_thread() const { return worker_thread_; }
90 BaseSession* session() const { return session_; }
91 const std::string& content_name() { return content_name_; }
92 TransportChannel* transport_channel() const {
93 return transport_channel_;
94 }
95 TransportChannel* rtcp_transport_channel() const {
96 return rtcp_transport_channel_;
97 }
98 bool enabled() const { return enabled_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099
100 // This function returns true if we are using SRTP.
101 bool secure() const { return srtp_filter_.IsActive(); }
102 // The following function returns true if we are using
103 // DTLS-based keying. If you turned off SRTP later, however
104 // you could have secure() == false and dtls_secure() == true.
105 bool secure_dtls() const { return dtls_keyed_; }
106 // This function returns true if we require secure channel for call setup.
107 bool secure_required() const { return secure_required_; }
108
109 bool writable() const { return writable_; }
110 bool IsStreamMuted(uint32 ssrc);
111
112 // Channel control
113 bool SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000114 ContentAction action,
115 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116 bool SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000117 ContentAction action,
118 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119
120 bool Enable(bool enable);
121 // Mute sending media on the stream with SSRC |ssrc|
122 // If there is only one sending stream SSRC 0 can be used.
123 bool MuteStream(uint32 ssrc, bool mute);
124
125 // Multiplexing
126 bool AddRecvStream(const StreamParams& sp);
127 bool RemoveRecvStream(uint32 ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000128 bool AddSendStream(const StreamParams& sp);
129 bool RemoveSendStream(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130
131 // Monitoring
132 void StartConnectionMonitor(int cms);
133 void StopConnectionMonitor();
134
135 void set_srtp_signal_silent_time(uint32 silent_time) {
136 srtp_filter_.set_signal_silent_time(silent_time);
137 }
138
139 void set_content_name(const std::string& content_name) {
140 ASSERT(signaling_thread()->IsCurrent());
141 ASSERT(!writable_);
142 if (session_->state() != BaseSession::STATE_INIT) {
143 LOG(LS_ERROR) << "Content name for a channel can be changed only "
144 << "when BaseSession is in STATE_INIT state.";
145 return;
146 }
147 content_name_ = content_name;
148 }
149
150 template <class T>
151 void RegisterSendSink(T* sink,
152 void (T::*OnPacket)(const void*, size_t, bool),
153 SinkType type) {
154 talk_base::CritScope cs(&signal_send_packet_cs_);
155 if (SINK_POST_CRYPTO == type) {
156 SignalSendPacketPostCrypto.disconnect(sink);
157 SignalSendPacketPostCrypto.connect(sink, OnPacket);
158 } else {
159 SignalSendPacketPreCrypto.disconnect(sink);
160 SignalSendPacketPreCrypto.connect(sink, OnPacket);
161 }
162 }
163
164 void UnregisterSendSink(sigslot::has_slots<>* sink,
165 SinkType type) {
166 talk_base::CritScope cs(&signal_send_packet_cs_);
167 if (SINK_POST_CRYPTO == type) {
168 SignalSendPacketPostCrypto.disconnect(sink);
169 } else {
170 SignalSendPacketPreCrypto.disconnect(sink);
171 }
172 }
173
174 bool HasSendSinks(SinkType type) {
175 talk_base::CritScope cs(&signal_send_packet_cs_);
176 if (SINK_POST_CRYPTO == type) {
177 return !SignalSendPacketPostCrypto.is_empty();
178 } else {
179 return !SignalSendPacketPreCrypto.is_empty();
180 }
181 }
182
183 template <class T>
184 void RegisterRecvSink(T* sink,
185 void (T::*OnPacket)(const void*, size_t, bool),
186 SinkType type) {
187 talk_base::CritScope cs(&signal_recv_packet_cs_);
188 if (SINK_POST_CRYPTO == type) {
189 SignalRecvPacketPostCrypto.disconnect(sink);
190 SignalRecvPacketPostCrypto.connect(sink, OnPacket);
191 } else {
192 SignalRecvPacketPreCrypto.disconnect(sink);
193 SignalRecvPacketPreCrypto.connect(sink, OnPacket);
194 }
195 }
196
197 void UnregisterRecvSink(sigslot::has_slots<>* sink,
198 SinkType type) {
199 talk_base::CritScope cs(&signal_recv_packet_cs_);
200 if (SINK_POST_CRYPTO == type) {
201 SignalRecvPacketPostCrypto.disconnect(sink);
202 } else {
203 SignalRecvPacketPreCrypto.disconnect(sink);
204 }
205 }
206
207 bool HasRecvSinks(SinkType type) {
208 talk_base::CritScope cs(&signal_recv_packet_cs_);
209 if (SINK_POST_CRYPTO == type) {
210 return !SignalRecvPacketPostCrypto.is_empty();
211 } else {
212 return !SignalRecvPacketPreCrypto.is_empty();
213 }
214 }
215
216 SsrcMuxFilter* ssrc_filter() { return &ssrc_filter_; }
217
218 const std::vector<StreamParams>& local_streams() const {
219 return local_streams_;
220 }
221 const std::vector<StreamParams>& remote_streams() const {
222 return remote_streams_;
223 }
224
225 // Used for latency measurements.
226 sigslot::signal1<BaseChannel*> SignalFirstPacketReceived;
227
228 // Used to alert UI when the muted status changes, perhaps autonomously.
229 sigslot::repeater2<BaseChannel*, bool> SignalAutoMuted;
230
231 // Made public for easier testing.
232 void SetReadyToSend(TransportChannel* channel, bool ready);
233
234 protected:
235 MediaEngineInterface* media_engine() const { return media_engine_; }
236 virtual MediaChannel* media_channel() const { return media_channel_; }
237 void set_rtcp_transport_channel(TransportChannel* transport);
238 bool was_ever_writable() const { return was_ever_writable_; }
239 void set_local_content_direction(MediaContentDirection direction) {
240 local_content_direction_ = direction;
241 }
242 void set_remote_content_direction(MediaContentDirection direction) {
243 remote_content_direction_ = direction;
244 }
245 bool IsReadyToReceive() const;
246 bool IsReadyToSend() const;
247 talk_base::Thread* signaling_thread() { return session_->signaling_thread(); }
248 SrtpFilter* srtp_filter() { return &srtp_filter_; }
249 bool rtcp() const { return rtcp_; }
250
251 void Send(uint32 id, talk_base::MessageData* pdata = NULL);
252 void Post(uint32 id, talk_base::MessageData* pdata = NULL);
253 void PostDelayed(int cmsDelay, uint32 id = 0,
254 talk_base::MessageData* pdata = NULL);
255 void Clear(uint32 id = talk_base::MQID_ANY,
256 talk_base::MessageList* removed = NULL);
257 void FlushRtcpMessages();
258
259 // NetworkInterface implementation, called by MediaEngine
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000260 virtual bool SendPacket(talk_base::Buffer* packet,
261 talk_base::DiffServCodePoint dscp);
262 virtual bool SendRtcp(talk_base::Buffer* packet,
263 talk_base::DiffServCodePoint dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000264 virtual int SetOption(SocketType type, talk_base::Socket::Option o, int val);
265
266 // From TransportChannel
267 void OnWritableState(TransportChannel* channel);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000268 virtual void OnChannelRead(TransportChannel* channel,
269 const char* data,
270 size_t len,
271 const talk_base::PacketTime& packet_time,
272 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273 void OnReadyToSend(TransportChannel* channel);
274
275 bool PacketIsRtcp(const TransportChannel* channel, const char* data,
276 size_t len);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000277 bool SendPacket(bool rtcp, talk_base::Buffer* packet,
278 talk_base::DiffServCodePoint dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000279 virtual bool WantsPacket(bool rtcp, talk_base::Buffer* packet);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000280 void HandlePacket(bool rtcp, talk_base::Buffer* packet,
281 const talk_base::PacketTime& packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000282
283 // Apply the new local/remote session description.
284 void OnNewLocalDescription(BaseSession* session, ContentAction action);
285 void OnNewRemoteDescription(BaseSession* session, ContentAction action);
286
287 void EnableMedia_w();
288 void DisableMedia_w();
289 virtual bool MuteStream_w(uint32 ssrc, bool mute);
290 bool IsStreamMuted_w(uint32 ssrc);
291 void ChannelWritable_w();
292 void ChannelNotWritable_w();
293 bool AddRecvStream_w(const StreamParams& sp);
294 bool RemoveRecvStream_w(uint32 ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000295 bool AddSendStream_w(const StreamParams& sp);
296 bool RemoveSendStream_w(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000297 virtual bool ShouldSetupDtlsSrtp() const;
298 // Do the DTLS key expansion and impose it on the SRTP/SRTCP filters.
299 // |rtcp_channel| indicates whether to set up the RTP or RTCP filter.
300 bool SetupDtlsSrtp(bool rtcp_channel);
301 // Set the DTLS-SRTP cipher policy on this channel as appropriate.
302 bool SetDtlsSrtpCiphers(TransportChannel *tc, bool rtcp);
303
304 virtual void ChangeState() = 0;
305
306 // Gets the content info appropriate to the channel (audio or video).
307 virtual const ContentInfo* GetFirstContent(
308 const SessionDescription* sdesc) = 0;
309 bool UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000310 ContentAction action,
311 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000312 bool UpdateRemoteStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000313 ContentAction action,
314 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000315 bool SetBaseLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000316 ContentAction action,
317 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000318 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000319 ContentAction action,
320 std::string* error_desc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000321 bool SetBaseRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000322 ContentAction action,
323 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000324 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000325 ContentAction action,
326 std::string* error_desc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000328 bool CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
329 bool* dtls,
330 std::string* error_desc);
331 bool SetSrtp_w(const std::vector<CryptoParams>& params,
332 ContentAction action,
333 ContentSource src,
334 std::string* error_desc);
335 bool SetRtcpMux_w(bool enable,
336 ContentAction action,
337 ContentSource src,
338 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000339
340 // From MessageHandler
341 virtual void OnMessage(talk_base::Message* pmsg);
342
343 // Handled in derived classes
344 // Get the SRTP ciphers to use for RTP media
345 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const = 0;
346 virtual void OnConnectionMonitorUpdate(SocketMonitor* monitor,
347 const std::vector<ConnectionInfo>& infos) = 0;
348
349 private:
350 sigslot::signal3<const void*, size_t, bool> SignalSendPacketPreCrypto;
351 sigslot::signal3<const void*, size_t, bool> SignalSendPacketPostCrypto;
352 sigslot::signal3<const void*, size_t, bool> SignalRecvPacketPreCrypto;
353 sigslot::signal3<const void*, size_t, bool> SignalRecvPacketPostCrypto;
354 talk_base::CriticalSection signal_send_packet_cs_;
355 talk_base::CriticalSection signal_recv_packet_cs_;
356
357 talk_base::Thread* worker_thread_;
358 MediaEngineInterface* media_engine_;
359 BaseSession* session_;
360 MediaChannel* media_channel_;
361 std::vector<StreamParams> local_streams_;
362 std::vector<StreamParams> remote_streams_;
363
364 std::string content_name_;
365 bool rtcp_;
366 TransportChannel* transport_channel_;
367 TransportChannel* rtcp_transport_channel_;
368 SrtpFilter srtp_filter_;
369 RtcpMuxFilter rtcp_mux_filter_;
370 SsrcMuxFilter ssrc_filter_;
371 talk_base::scoped_ptr<SocketMonitor> socket_monitor_;
372 bool enabled_;
373 bool writable_;
374 bool rtp_ready_to_send_;
375 bool rtcp_ready_to_send_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000376 bool was_ever_writable_;
377 MediaContentDirection local_content_direction_;
378 MediaContentDirection remote_content_direction_;
379 std::set<uint32> muted_streams_;
380 bool has_received_packet_;
381 bool dtls_keyed_;
382 bool secure_required_;
383};
384
385// VoiceChannel is a specialization that adds support for early media, DTMF,
386// and input/output level monitoring.
387class VoiceChannel : public BaseChannel {
388 public:
389 VoiceChannel(talk_base::Thread* thread, MediaEngineInterface* media_engine,
390 VoiceMediaChannel* channel, BaseSession* session,
391 const std::string& content_name, bool rtcp);
392 ~VoiceChannel();
393 bool Init();
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000394 bool SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer);
395 bool SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000396
397 // downcasts a MediaChannel
398 virtual VoiceMediaChannel* media_channel() const {
399 return static_cast<VoiceMediaChannel*>(BaseChannel::media_channel());
400 }
401
402 bool SetRingbackTone(const void* buf, int len);
403 void SetEarlyMedia(bool enable);
404 // This signal is emitted when we have gone a period of time without
405 // receiving early media. When received, a UI should start playing its
406 // own ringing sound
407 sigslot::signal1<VoiceChannel*> SignalEarlyMediaTimeout;
408
409 bool PlayRingbackTone(uint32 ssrc, bool play, bool loop);
410 // TODO(ronghuawu): Replace PressDTMF with InsertDtmf.
411 bool PressDTMF(int digit, bool playout);
412 // Returns if the telephone-event has been negotiated.
413 bool CanInsertDtmf();
414 // Send and/or play a DTMF |event| according to the |flags|.
415 // The DTMF out-of-band signal will be used on sending.
416 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000417 // The valid value for the |event| are 0 which corresponding to DTMF
418 // event 0-9, *, #, A-D.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000419 bool InsertDtmf(uint32 ssrc, int event_code, int duration, int flags);
420 bool SetOutputScaling(uint32 ssrc, double left, double right);
421 // Get statistics about the current media session.
422 bool GetStats(VoiceMediaInfo* stats);
423
424 // Monitoring functions
425 sigslot::signal2<VoiceChannel*, const std::vector<ConnectionInfo>&>
426 SignalConnectionMonitor;
427
428 void StartMediaMonitor(int cms);
429 void StopMediaMonitor();
430 sigslot::signal2<VoiceChannel*, const VoiceMediaInfo&> SignalMediaMonitor;
431
432 void StartAudioMonitor(int cms);
433 void StopAudioMonitor();
434 bool IsAudioMonitorRunning() const;
435 sigslot::signal2<VoiceChannel*, const AudioInfo&> SignalAudioMonitor;
436
437 void StartTypingMonitor(const TypingMonitorOptions& settings);
438 void StopTypingMonitor();
439 bool IsTypingMonitorRunning() const;
440
441 // Overrides BaseChannel::MuteStream_w.
442 virtual bool MuteStream_w(uint32 ssrc, bool mute);
443
444 int GetInputLevel_w();
445 int GetOutputLevel_w();
446 void GetActiveStreams_w(AudioInfo::StreamList* actives);
447
448 // Signal errors from VoiceMediaChannel. Arguments are:
449 // ssrc(uint32), and error(VoiceMediaChannel::Error).
450 sigslot::signal3<VoiceChannel*, uint32, VoiceMediaChannel::Error>
451 SignalMediaError;
452
453 // Configuration and setting.
454 bool SetChannelOptions(const AudioOptions& options);
455
456 private:
457 // overrides from BaseChannel
458 virtual void OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000459 const char* data, size_t len,
460 const talk_base::PacketTime& packet_time,
461 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000462 virtual void ChangeState();
463 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
464 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000465 ContentAction action,
466 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000467 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000468 ContentAction action,
469 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000470 bool SetRingbackTone_w(const void* buf, int len);
471 bool PlayRingbackTone_w(uint32 ssrc, bool play, bool loop);
472 void HandleEarlyMediaTimeout();
473 bool CanInsertDtmf_w();
474 bool InsertDtmf_w(uint32 ssrc, int event, int duration, int flags);
475 bool SetOutputScaling_w(uint32 ssrc, double left, double right);
476 bool GetStats_w(VoiceMediaInfo* stats);
477
478 virtual void OnMessage(talk_base::Message* pmsg);
479 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
480 virtual void OnConnectionMonitorUpdate(
481 SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos);
482 virtual void OnMediaMonitorUpdate(
483 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info);
484 void OnAudioMonitorUpdate(AudioMonitor* monitor, const AudioInfo& info);
485 void OnVoiceChannelError(uint32 ssrc, VoiceMediaChannel::Error error);
486 void SendLastMediaError();
487 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
488 // Configuration and setting.
489 bool SetChannelOptions_w(const AudioOptions& options);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000490 bool SetRenderer_w(uint32 ssrc, AudioRenderer* renderer, bool is_local);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491
492 static const int kEarlyMediaTimeout = 1000;
493 bool received_media_;
494 talk_base::scoped_ptr<VoiceMediaMonitor> media_monitor_;
495 talk_base::scoped_ptr<AudioMonitor> audio_monitor_;
496 talk_base::scoped_ptr<TypingMonitor> typing_monitor_;
497};
498
499// VideoChannel is a specialization for video.
500class VideoChannel : public BaseChannel {
501 public:
502 // Make screen capturer virtual so that it can be overriden in testing.
503 // E.g. used to test that window events are triggered correctly.
504 class ScreenCapturerFactory {
505 public:
506 virtual VideoCapturer* CreateScreenCapturer(const ScreencastId& window) = 0;
507 virtual ~ScreenCapturerFactory() {}
508 };
509
510 VideoChannel(talk_base::Thread* thread, MediaEngineInterface* media_engine,
511 VideoMediaChannel* channel, BaseSession* session,
512 const std::string& content_name, bool rtcp,
513 VoiceChannel* voice_channel);
514 ~VideoChannel();
515 bool Init();
516
517 bool SetRenderer(uint32 ssrc, VideoRenderer* renderer);
518 bool ApplyViewRequest(const ViewRequest& request);
519
520 // TODO(pthatcher): Refactor to use a "capture id" instead of an
521 // ssrc here as the "key".
522 VideoCapturer* AddScreencast(uint32 ssrc, const ScreencastId& id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000523 bool SetCapturer(uint32 ssrc, VideoCapturer* capturer);
524 bool RemoveScreencast(uint32 ssrc);
525 // True if we've added a screencast. Doesn't matter if the capturer
526 // has been started or not.
527 bool IsScreencasting();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000528 int GetScreencastFps(uint32 ssrc);
529 int GetScreencastMaxPixels(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000530 // Get statistics about the current media session.
531 bool GetStats(VideoMediaInfo* stats);
532
533 sigslot::signal2<VideoChannel*, const std::vector<ConnectionInfo>&>
534 SignalConnectionMonitor;
535
536 void StartMediaMonitor(int cms);
537 void StopMediaMonitor();
538 sigslot::signal2<VideoChannel*, const VideoMediaInfo&> SignalMediaMonitor;
539 sigslot::signal2<uint32, talk_base::WindowEvent> SignalScreencastWindowEvent;
540
541 bool SendIntraFrame();
542 bool RequestIntraFrame();
543 sigslot::signal3<VideoChannel*, uint32, VideoMediaChannel::Error>
544 SignalMediaError;
545
546 void SetScreenCaptureFactory(
547 ScreenCapturerFactory* screencapture_factory);
548
549 // 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;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000560 struct ScreencastDetailsMessageData;
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 void SendIntraFrame_w() {
572 media_channel()->SendIntraFrame();
573 }
574 void RequestIntraFrame_w() {
575 media_channel()->RequestIntraFrame();
576 }
577
578 bool ApplyViewRequest_w(const ViewRequest& request);
579 void SetRenderer_w(uint32 ssrc, VideoRenderer* renderer);
580
581 VideoCapturer* AddScreencast_w(uint32 ssrc, const ScreencastId& id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000582 bool SetCapturer_w(uint32 ssrc, VideoCapturer* capturer);
583 bool RemoveScreencast_w(uint32 ssrc);
584 void OnScreencastWindowEvent_s(uint32 ssrc, talk_base::WindowEvent we);
585 bool IsScreencasting_w() const;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000586 void ScreencastDetails_w(ScreencastDetailsMessageData* d) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000587 void SetScreenCaptureFactory_w(
588 ScreenCapturerFactory* screencapture_factory);
589 bool GetStats_w(VideoMediaInfo* stats);
590
591 virtual void OnMessage(talk_base::Message* pmsg);
592 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
593 virtual void OnConnectionMonitorUpdate(
594 SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos);
595 virtual void OnMediaMonitorUpdate(
596 VideoMediaChannel* media_channel, const VideoMediaInfo& info);
597 virtual void OnScreencastWindowEvent(uint32 ssrc,
598 talk_base::WindowEvent event);
599 virtual void OnStateChange(VideoCapturer* capturer, CaptureState ev);
600 bool GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc);
601
602 void OnVideoChannelError(uint32 ssrc, VideoMediaChannel::Error error);
603 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
604 // Configuration and setting.
605 bool SetChannelOptions_w(const VideoOptions& options);
606
607 VoiceChannel* voice_channel_;
608 VideoRenderer* renderer_;
609 talk_base::scoped_ptr<ScreenCapturerFactory> screencapture_factory_;
610 ScreencastMap screencast_capturers_;
611 talk_base::scoped_ptr<VideoMediaMonitor> media_monitor_;
612
613 talk_base::WindowEvent previous_we_;
614};
615
616// DataChannel is a specialization for data.
617class DataChannel : public BaseChannel {
618 public:
619 DataChannel(talk_base::Thread* thread,
620 DataMediaChannel* media_channel,
621 BaseSession* session,
622 const std::string& content_name,
623 bool rtcp);
624 ~DataChannel();
625 bool Init();
626
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000627 virtual bool SendData(const SendDataParams& params,
628 const talk_base::Buffer& payload,
629 SendDataResult* result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000630
631 void StartMediaMonitor(int cms);
632 void StopMediaMonitor();
633
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000634 // Should be called on the signaling thread only.
635 bool ready_to_send_data() const {
636 return ready_to_send_data_;
637 }
638
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639 sigslot::signal2<DataChannel*, const DataMediaInfo&> SignalMediaMonitor;
640 sigslot::signal2<DataChannel*, const std::vector<ConnectionInfo>&>
641 SignalConnectionMonitor;
642 sigslot::signal3<DataChannel*, uint32, DataMediaChannel::Error>
643 SignalMediaError;
644 sigslot::signal3<DataChannel*,
645 const ReceiveDataParams&,
646 const talk_base::Buffer&>
647 SignalDataReceived;
648 // Signal for notifying when the channel becomes ready to send data.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000649 // That occurs when the channel is enabled, the transport is writable,
650 // both local and remote descriptions are set, and the channel is unblocked.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000651 sigslot::signal1<bool> SignalReadyToSendData;
652
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000653 protected:
654 // downcasts a MediaChannel.
655 virtual DataMediaChannel* media_channel() const {
656 return static_cast<DataMediaChannel*>(BaseChannel::media_channel());
657 }
658
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000659 private:
660 struct SendDataMessageData : public talk_base::MessageData {
661 SendDataMessageData(const SendDataParams& params,
662 const talk_base::Buffer* payload,
663 SendDataResult* result)
664 : params(params),
665 payload(payload),
666 result(result),
667 succeeded(false) {
668 }
669
670 const SendDataParams& params;
671 const talk_base::Buffer* payload;
672 SendDataResult* result;
673 bool succeeded;
674 };
675
676 struct DataReceivedMessageData : public talk_base::MessageData {
677 // We copy the data because the data will become invalid after we
678 // handle DataMediaChannel::SignalDataReceived but before we fire
679 // SignalDataReceived.
680 DataReceivedMessageData(
681 const ReceiveDataParams& params, const char* data, size_t len)
682 : params(params),
683 payload(data, len) {
684 }
685 const ReceiveDataParams params;
686 const talk_base::Buffer payload;
687 };
688
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000689 typedef talk_base::TypedMessageData<bool> DataChannelReadyToSendMessageData;
690
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000691 // overrides from BaseChannel
692 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
693 // If data_channel_type_ is DCT_NONE, set it. Otherwise, check that
694 // it's the same as what was set previously. Returns false if it's
695 // set to one type one type and changed to another type later.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000696 bool SetDataChannelType(DataChannelType new_data_channel_type,
697 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000698 // Same as SetDataChannelType, but extracts the type from the
699 // DataContentDescription.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000700 bool SetDataChannelTypeFromContent(const DataContentDescription* content,
701 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000702 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000703 ContentAction action,
704 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000705 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000706 ContentAction action,
707 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708 virtual void ChangeState();
709 virtual bool WantsPacket(bool rtcp, talk_base::Buffer* packet);
710
711 virtual void OnMessage(talk_base::Message* pmsg);
712 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
713 virtual void OnConnectionMonitorUpdate(
714 SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos);
715 virtual void OnMediaMonitorUpdate(
716 DataMediaChannel* media_channel, const DataMediaInfo& info);
717 virtual bool ShouldSetupDtlsSrtp() const;
718 void OnDataReceived(
719 const ReceiveDataParams& params, const char* data, size_t len);
720 void OnDataChannelError(uint32 ssrc, DataMediaChannel::Error error);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000721 void OnDataChannelReadyToSend(bool writable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000722 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
723
724 talk_base::scoped_ptr<DataMediaMonitor> media_monitor_;
725 // TODO(pthatcher): Make a separate SctpDataChannel and
726 // RtpDataChannel instead of using this.
727 DataChannelType data_channel_type_;
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000728 bool ready_to_send_data_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000729};
730
731} // namespace cricket
732
733#endif // TALK_SESSION_MEDIA_CHANNEL_H_