blob: d297ee4b8fd868296eb74024c44467540d17aaa7 [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
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000034#include "talk/app/webrtc/datachannelinterface.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035#include "talk/base/asyncudpsocket.h"
36#include "talk/base/criticalsection.h"
37#include "talk/base/network.h"
38#include "talk/base/sigslot.h"
39#include "talk/base/window.h"
40#include "talk/media/base/mediachannel.h"
41#include "talk/media/base/mediaengine.h"
42#include "talk/media/base/screencastid.h"
43#include "talk/media/base/streamparams.h"
44#include "talk/media/base/videocapturer.h"
45#include "talk/p2p/base/session.h"
46#include "talk/p2p/client/socketmonitor.h"
47#include "talk/session/media/audiomonitor.h"
48#include "talk/session/media/mediamonitor.h"
49#include "talk/session/media/mediasession.h"
50#include "talk/session/media/rtcpmuxfilter.h"
51#include "talk/session/media/srtpfilter.h"
52#include "talk/session/media/ssrcmuxfilter.h"
53
54namespace cricket {
55
56struct CryptoParams;
57class MediaContentDescription;
58struct TypingMonitorOptions;
59class TypingMonitor;
60struct ViewRequest;
61
62enum SinkType {
63 SINK_PRE_CRYPTO, // Sink packets before encryption or after decryption.
64 SINK_POST_CRYPTO // Sink packets after encryption or before decryption.
65};
66
67// BaseChannel contains logic common to voice and video, including
68// enable/mute, marshaling calls to a worker thread, and
69// connection and media monitors.
wu@webrtc.org78187522013-10-07 23:32:02 +000070//
71// WARNING! SUBCLASSES MUST CALL Deinit() IN THEIR DESTRUCTORS!
72// This is required to avoid a data race between the destructor modifying the
73// vtable, and the media channel's thread using BaseChannel as the
74// NetworkInterface.
75
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076class BaseChannel
77 : public talk_base::MessageHandler, public sigslot::has_slots<>,
78 public MediaChannel::NetworkInterface {
79 public:
80 BaseChannel(talk_base::Thread* thread, MediaEngineInterface* media_engine,
81 MediaChannel* channel, BaseSession* session,
82 const std::string& content_name, bool rtcp);
83 virtual ~BaseChannel();
84 bool Init(TransportChannel* transport_channel,
85 TransportChannel* rtcp_transport_channel);
wu@webrtc.org78187522013-10-07 23:32:02 +000086 // Deinit may be called multiple times and is simply ignored if it's alreay
87 // done.
88 void Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089
90 talk_base::Thread* worker_thread() const { return worker_thread_; }
91 BaseSession* session() const { return session_; }
92 const std::string& content_name() { return content_name_; }
93 TransportChannel* transport_channel() const {
94 return transport_channel_;
95 }
96 TransportChannel* rtcp_transport_channel() const {
97 return rtcp_transport_channel_;
98 }
99 bool enabled() const { return enabled_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100
101 // This function returns true if we are using SRTP.
102 bool secure() const { return srtp_filter_.IsActive(); }
103 // The following function returns true if we are using
104 // DTLS-based keying. If you turned off SRTP later, however
105 // you could have secure() == false and dtls_secure() == true.
106 bool secure_dtls() const { return dtls_keyed_; }
107 // This function returns true if we require secure channel for call setup.
108 bool secure_required() const { return secure_required_; }
109
110 bool writable() const { return writable_; }
111 bool IsStreamMuted(uint32 ssrc);
112
113 // Channel control
114 bool SetLocalContent(const MediaContentDescription* content,
115 ContentAction action);
116 bool SetRemoteContent(const MediaContentDescription* content,
117 ContentAction action);
118 bool SetMaxSendBandwidth(int max_bandwidth);
119
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,
310 ContentAction action);
311 bool UpdateRemoteStreams_w(const std::vector<StreamParams>& streams,
312 ContentAction action);
313 bool SetBaseLocalContent_w(const MediaContentDescription* content,
314 ContentAction action);
315 virtual bool SetLocalContent_w(const MediaContentDescription* content,
316 ContentAction action) = 0;
317 bool SetBaseRemoteContent_w(const MediaContentDescription* content,
318 ContentAction action);
319 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
320 ContentAction action) = 0;
321
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000322 bool CheckSrtpConfig(const std::vector<CryptoParams>& cryptos, bool* dtls);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000323 bool SetSrtp_w(const std::vector<CryptoParams>& params, ContentAction action,
324 ContentSource src);
325 bool SetRtcpMux_w(bool enable, ContentAction action, ContentSource src);
326
327 virtual bool SetMaxSendBandwidth_w(int max_bandwidth);
328
329 // From MessageHandler
330 virtual void OnMessage(talk_base::Message* pmsg);
331
332 // Handled in derived classes
333 // Get the SRTP ciphers to use for RTP media
334 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const = 0;
335 virtual void OnConnectionMonitorUpdate(SocketMonitor* monitor,
336 const std::vector<ConnectionInfo>& infos) = 0;
337
338 private:
339 sigslot::signal3<const void*, size_t, bool> SignalSendPacketPreCrypto;
340 sigslot::signal3<const void*, size_t, bool> SignalSendPacketPostCrypto;
341 sigslot::signal3<const void*, size_t, bool> SignalRecvPacketPreCrypto;
342 sigslot::signal3<const void*, size_t, bool> SignalRecvPacketPostCrypto;
343 talk_base::CriticalSection signal_send_packet_cs_;
344 talk_base::CriticalSection signal_recv_packet_cs_;
345
346 talk_base::Thread* worker_thread_;
347 MediaEngineInterface* media_engine_;
348 BaseSession* session_;
349 MediaChannel* media_channel_;
350 std::vector<StreamParams> local_streams_;
351 std::vector<StreamParams> remote_streams_;
352
353 std::string content_name_;
354 bool rtcp_;
355 TransportChannel* transport_channel_;
356 TransportChannel* rtcp_transport_channel_;
357 SrtpFilter srtp_filter_;
358 RtcpMuxFilter rtcp_mux_filter_;
359 SsrcMuxFilter ssrc_filter_;
360 talk_base::scoped_ptr<SocketMonitor> socket_monitor_;
361 bool enabled_;
362 bool writable_;
363 bool rtp_ready_to_send_;
364 bool rtcp_ready_to_send_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000365 bool was_ever_writable_;
366 MediaContentDirection local_content_direction_;
367 MediaContentDirection remote_content_direction_;
368 std::set<uint32> muted_streams_;
369 bool has_received_packet_;
370 bool dtls_keyed_;
371 bool secure_required_;
372};
373
374// VoiceChannel is a specialization that adds support for early media, DTMF,
375// and input/output level monitoring.
376class VoiceChannel : public BaseChannel {
377 public:
378 VoiceChannel(talk_base::Thread* thread, MediaEngineInterface* media_engine,
379 VoiceMediaChannel* channel, BaseSession* session,
380 const std::string& content_name, bool rtcp);
381 ~VoiceChannel();
382 bool Init();
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000383 bool SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer);
384 bool SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000385
386 // downcasts a MediaChannel
387 virtual VoiceMediaChannel* media_channel() const {
388 return static_cast<VoiceMediaChannel*>(BaseChannel::media_channel());
389 }
390
391 bool SetRingbackTone(const void* buf, int len);
392 void SetEarlyMedia(bool enable);
393 // This signal is emitted when we have gone a period of time without
394 // receiving early media. When received, a UI should start playing its
395 // own ringing sound
396 sigslot::signal1<VoiceChannel*> SignalEarlyMediaTimeout;
397
398 bool PlayRingbackTone(uint32 ssrc, bool play, bool loop);
399 // TODO(ronghuawu): Replace PressDTMF with InsertDtmf.
400 bool PressDTMF(int digit, bool playout);
401 // Returns if the telephone-event has been negotiated.
402 bool CanInsertDtmf();
403 // Send and/or play a DTMF |event| according to the |flags|.
404 // The DTMF out-of-band signal will be used on sending.
405 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000406 // The valid value for the |event| are 0 which corresponding to DTMF
407 // event 0-9, *, #, A-D.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000408 bool InsertDtmf(uint32 ssrc, int event_code, int duration, int flags);
409 bool SetOutputScaling(uint32 ssrc, double left, double right);
410 // Get statistics about the current media session.
411 bool GetStats(VoiceMediaInfo* stats);
412
413 // Monitoring functions
414 sigslot::signal2<VoiceChannel*, const std::vector<ConnectionInfo>&>
415 SignalConnectionMonitor;
416
417 void StartMediaMonitor(int cms);
418 void StopMediaMonitor();
419 sigslot::signal2<VoiceChannel*, const VoiceMediaInfo&> SignalMediaMonitor;
420
421 void StartAudioMonitor(int cms);
422 void StopAudioMonitor();
423 bool IsAudioMonitorRunning() const;
424 sigslot::signal2<VoiceChannel*, const AudioInfo&> SignalAudioMonitor;
425
426 void StartTypingMonitor(const TypingMonitorOptions& settings);
427 void StopTypingMonitor();
428 bool IsTypingMonitorRunning() const;
429
430 // Overrides BaseChannel::MuteStream_w.
431 virtual bool MuteStream_w(uint32 ssrc, bool mute);
432
433 int GetInputLevel_w();
434 int GetOutputLevel_w();
435 void GetActiveStreams_w(AudioInfo::StreamList* actives);
436
437 // Signal errors from VoiceMediaChannel. Arguments are:
438 // ssrc(uint32), and error(VoiceMediaChannel::Error).
439 sigslot::signal3<VoiceChannel*, uint32, VoiceMediaChannel::Error>
440 SignalMediaError;
441
442 // Configuration and setting.
443 bool SetChannelOptions(const AudioOptions& options);
444
445 private:
446 // overrides from BaseChannel
447 virtual void OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000448 const char* data, size_t len,
449 const talk_base::PacketTime& packet_time,
450 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000451 virtual void ChangeState();
452 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
453 virtual bool SetLocalContent_w(const MediaContentDescription* content,
454 ContentAction action);
455 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
456 ContentAction action);
457 bool SetRingbackTone_w(const void* buf, int len);
458 bool PlayRingbackTone_w(uint32 ssrc, bool play, bool loop);
459 void HandleEarlyMediaTimeout();
460 bool CanInsertDtmf_w();
461 bool InsertDtmf_w(uint32 ssrc, int event, int duration, int flags);
462 bool SetOutputScaling_w(uint32 ssrc, double left, double right);
463 bool GetStats_w(VoiceMediaInfo* stats);
464
465 virtual void OnMessage(talk_base::Message* pmsg);
466 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
467 virtual void OnConnectionMonitorUpdate(
468 SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos);
469 virtual void OnMediaMonitorUpdate(
470 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info);
471 void OnAudioMonitorUpdate(AudioMonitor* monitor, const AudioInfo& info);
472 void OnVoiceChannelError(uint32 ssrc, VoiceMediaChannel::Error error);
473 void SendLastMediaError();
474 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
475 // Configuration and setting.
476 bool SetChannelOptions_w(const AudioOptions& options);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000477 bool SetRenderer_w(uint32 ssrc, AudioRenderer* renderer, bool is_local);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000478
479 static const int kEarlyMediaTimeout = 1000;
480 bool received_media_;
481 talk_base::scoped_ptr<VoiceMediaMonitor> media_monitor_;
482 talk_base::scoped_ptr<AudioMonitor> audio_monitor_;
483 talk_base::scoped_ptr<TypingMonitor> typing_monitor_;
484};
485
486// VideoChannel is a specialization for video.
487class VideoChannel : public BaseChannel {
488 public:
489 // Make screen capturer virtual so that it can be overriden in testing.
490 // E.g. used to test that window events are triggered correctly.
491 class ScreenCapturerFactory {
492 public:
493 virtual VideoCapturer* CreateScreenCapturer(const ScreencastId& window) = 0;
494 virtual ~ScreenCapturerFactory() {}
495 };
496
497 VideoChannel(talk_base::Thread* thread, MediaEngineInterface* media_engine,
498 VideoMediaChannel* channel, BaseSession* session,
499 const std::string& content_name, bool rtcp,
500 VoiceChannel* voice_channel);
501 ~VideoChannel();
502 bool Init();
503
504 bool SetRenderer(uint32 ssrc, VideoRenderer* renderer);
505 bool ApplyViewRequest(const ViewRequest& request);
506
507 // TODO(pthatcher): Refactor to use a "capture id" instead of an
508 // ssrc here as the "key".
509 VideoCapturer* AddScreencast(uint32 ssrc, const ScreencastId& id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000510 bool SetCapturer(uint32 ssrc, VideoCapturer* capturer);
511 bool RemoveScreencast(uint32 ssrc);
512 // True if we've added a screencast. Doesn't matter if the capturer
513 // has been started or not.
514 bool IsScreencasting();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000515 int GetScreencastFps(uint32 ssrc);
516 int GetScreencastMaxPixels(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000517 // Get statistics about the current media session.
518 bool GetStats(VideoMediaInfo* stats);
519
520 sigslot::signal2<VideoChannel*, const std::vector<ConnectionInfo>&>
521 SignalConnectionMonitor;
522
523 void StartMediaMonitor(int cms);
524 void StopMediaMonitor();
525 sigslot::signal2<VideoChannel*, const VideoMediaInfo&> SignalMediaMonitor;
526 sigslot::signal2<uint32, talk_base::WindowEvent> SignalScreencastWindowEvent;
527
528 bool SendIntraFrame();
529 bool RequestIntraFrame();
530 sigslot::signal3<VideoChannel*, uint32, VideoMediaChannel::Error>
531 SignalMediaError;
532
533 void SetScreenCaptureFactory(
534 ScreenCapturerFactory* screencapture_factory);
535
536 // Configuration and setting.
537 bool SetChannelOptions(const VideoOptions& options);
538
539 protected:
540 // downcasts a MediaChannel
541 virtual VideoMediaChannel* media_channel() const {
542 return static_cast<VideoMediaChannel*>(BaseChannel::media_channel());
543 }
544
545 private:
546 typedef std::map<uint32, VideoCapturer*> ScreencastMap;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000547 struct ScreencastDetailsMessageData;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000548
549 // overrides from BaseChannel
550 virtual void ChangeState();
551 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
552 virtual bool SetLocalContent_w(const MediaContentDescription* content,
553 ContentAction action);
554 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
555 ContentAction action);
556 void SendIntraFrame_w() {
557 media_channel()->SendIntraFrame();
558 }
559 void RequestIntraFrame_w() {
560 media_channel()->RequestIntraFrame();
561 }
562
563 bool ApplyViewRequest_w(const ViewRequest& request);
564 void SetRenderer_w(uint32 ssrc, VideoRenderer* renderer);
565
566 VideoCapturer* AddScreencast_w(uint32 ssrc, const ScreencastId& id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000567 bool SetCapturer_w(uint32 ssrc, VideoCapturer* capturer);
568 bool RemoveScreencast_w(uint32 ssrc);
569 void OnScreencastWindowEvent_s(uint32 ssrc, talk_base::WindowEvent we);
570 bool IsScreencasting_w() const;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000571 void ScreencastDetails_w(ScreencastDetailsMessageData* d) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000572 void SetScreenCaptureFactory_w(
573 ScreenCapturerFactory* screencapture_factory);
574 bool GetStats_w(VideoMediaInfo* stats);
575
576 virtual void OnMessage(talk_base::Message* pmsg);
577 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
578 virtual void OnConnectionMonitorUpdate(
579 SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos);
580 virtual void OnMediaMonitorUpdate(
581 VideoMediaChannel* media_channel, const VideoMediaInfo& info);
582 virtual void OnScreencastWindowEvent(uint32 ssrc,
583 talk_base::WindowEvent event);
584 virtual void OnStateChange(VideoCapturer* capturer, CaptureState ev);
585 bool GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc);
586
587 void OnVideoChannelError(uint32 ssrc, VideoMediaChannel::Error error);
588 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
589 // Configuration and setting.
590 bool SetChannelOptions_w(const VideoOptions& options);
591
592 VoiceChannel* voice_channel_;
593 VideoRenderer* renderer_;
594 talk_base::scoped_ptr<ScreenCapturerFactory> screencapture_factory_;
595 ScreencastMap screencast_capturers_;
596 talk_base::scoped_ptr<VideoMediaMonitor> media_monitor_;
597
598 talk_base::WindowEvent previous_we_;
599};
600
601// DataChannel is a specialization for data.
602class DataChannel : public BaseChannel {
603 public:
604 DataChannel(talk_base::Thread* thread,
605 DataMediaChannel* media_channel,
606 BaseSession* session,
607 const std::string& content_name,
608 bool rtcp);
609 ~DataChannel();
610 bool Init();
611
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000612 virtual bool SendData(const SendDataParams& params,
613 const talk_base::Buffer& payload,
614 SendDataResult* result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000615
616 void StartMediaMonitor(int cms);
617 void StopMediaMonitor();
618
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000619 // Should be called on the signaling thread only.
620 bool ready_to_send_data() const {
621 return ready_to_send_data_;
622 }
623
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000624 sigslot::signal2<DataChannel*, const DataMediaInfo&> SignalMediaMonitor;
625 sigslot::signal2<DataChannel*, const std::vector<ConnectionInfo>&>
626 SignalConnectionMonitor;
627 sigslot::signal3<DataChannel*, uint32, DataMediaChannel::Error>
628 SignalMediaError;
629 sigslot::signal3<DataChannel*,
630 const ReceiveDataParams&,
631 const talk_base::Buffer&>
632 SignalDataReceived;
633 // Signal for notifying when the channel becomes ready to send data.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000634 // That occurs when the channel is enabled, the transport is writable,
635 // both local and remote descriptions are set, and the channel is unblocked.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000636 sigslot::signal1<bool> SignalReadyToSendData;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000637 // Signal for notifying when a new stream is added from the remote side. Used
638 // for the in-band negotioation through the OPEN message for SCTP data
639 // channel.
640 sigslot::signal2<const std::string&, const webrtc::DataChannelInit&>
641 SignalNewStreamReceived;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000642
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000643 protected:
644 // downcasts a MediaChannel.
645 virtual DataMediaChannel* media_channel() const {
646 return static_cast<DataMediaChannel*>(BaseChannel::media_channel());
647 }
648
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000649 private:
650 struct SendDataMessageData : public talk_base::MessageData {
651 SendDataMessageData(const SendDataParams& params,
652 const talk_base::Buffer* payload,
653 SendDataResult* result)
654 : params(params),
655 payload(payload),
656 result(result),
657 succeeded(false) {
658 }
659
660 const SendDataParams& params;
661 const talk_base::Buffer* payload;
662 SendDataResult* result;
663 bool succeeded;
664 };
665
666 struct DataReceivedMessageData : public talk_base::MessageData {
667 // We copy the data because the data will become invalid after we
668 // handle DataMediaChannel::SignalDataReceived but before we fire
669 // SignalDataReceived.
670 DataReceivedMessageData(
671 const ReceiveDataParams& params, const char* data, size_t len)
672 : params(params),
673 payload(data, len) {
674 }
675 const ReceiveDataParams params;
676 const talk_base::Buffer payload;
677 };
678
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000679 typedef talk_base::TypedMessageData<bool> DataChannelReadyToSendMessageData;
680
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000681 struct DataChannelNewStreamReceivedMessageData
682 : public talk_base::MessageData {
683 DataChannelNewStreamReceivedMessageData(
684 const std::string& label, const webrtc::DataChannelInit& init)
685 : label(label),
686 init(init) {
687 }
688 const std::string label;
689 const webrtc::DataChannelInit init;
690 };
691
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000692 // overrides from BaseChannel
693 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
694 // If data_channel_type_ is DCT_NONE, set it. Otherwise, check that
695 // it's the same as what was set previously. Returns false if it's
696 // set to one type one type and changed to another type later.
697 bool SetDataChannelType(DataChannelType new_data_channel_type);
698 // Same as SetDataChannelType, but extracts the type from the
699 // DataContentDescription.
700 bool SetDataChannelTypeFromContent(const DataContentDescription* content);
701 virtual bool SetMaxSendBandwidth_w(int max_bandwidth);
702 virtual bool SetLocalContent_w(const MediaContentDescription* content,
703 ContentAction action);
704 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
705 ContentAction action);
706 virtual void ChangeState();
707 virtual bool WantsPacket(bool rtcp, talk_base::Buffer* packet);
708
709 virtual void OnMessage(talk_base::Message* pmsg);
710 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
711 virtual void OnConnectionMonitorUpdate(
712 SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos);
713 virtual void OnMediaMonitorUpdate(
714 DataMediaChannel* media_channel, const DataMediaInfo& info);
715 virtual bool ShouldSetupDtlsSrtp() const;
716 void OnDataReceived(
717 const ReceiveDataParams& params, const char* data, size_t len);
718 void OnDataChannelError(uint32 ssrc, DataMediaChannel::Error error);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000719 void OnDataChannelReadyToSend(bool writable);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000720 void OnDataChannelNewStreamReceived(const std::string& label,
721 const webrtc::DataChannelInit& init);
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_