blob: b49d5a05354671bdf8a9848c6eac81667ce7ceca [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.
69class BaseChannel
70 : public talk_base::MessageHandler, public sigslot::has_slots<>,
71 public MediaChannel::NetworkInterface {
72 public:
73 BaseChannel(talk_base::Thread* thread, MediaEngineInterface* media_engine,
74 MediaChannel* channel, BaseSession* session,
75 const std::string& content_name, bool rtcp);
76 virtual ~BaseChannel();
77 bool Init(TransportChannel* transport_channel,
78 TransportChannel* rtcp_transport_channel);
79
80 talk_base::Thread* worker_thread() const { return worker_thread_; }
81 BaseSession* session() const { return session_; }
82 const std::string& content_name() { return content_name_; }
83 TransportChannel* transport_channel() const {
84 return transport_channel_;
85 }
86 TransportChannel* rtcp_transport_channel() const {
87 return rtcp_transport_channel_;
88 }
89 bool enabled() const { return enabled_; }
90 // Set to true to have the channel optimistically allow data to be sent even
91 // when the channel isn't fully writable.
92 void set_optimistic_data_send(bool value) { optimistic_data_send_ = value; }
93 bool optimistic_data_send() const { return optimistic_data_send_; }
94
95 // This function returns true if we are using SRTP.
96 bool secure() const { return srtp_filter_.IsActive(); }
97 // The following function returns true if we are using
98 // DTLS-based keying. If you turned off SRTP later, however
99 // you could have secure() == false and dtls_secure() == true.
100 bool secure_dtls() const { return dtls_keyed_; }
101 // This function returns true if we require secure channel for call setup.
102 bool secure_required() const { return secure_required_; }
103
104 bool writable() const { return writable_; }
105 bool IsStreamMuted(uint32 ssrc);
106
107 // Channel control
108 bool SetLocalContent(const MediaContentDescription* content,
109 ContentAction action);
110 bool SetRemoteContent(const MediaContentDescription* content,
111 ContentAction action);
112 bool SetMaxSendBandwidth(int max_bandwidth);
113
114 bool Enable(bool enable);
115 // Mute sending media on the stream with SSRC |ssrc|
116 // If there is only one sending stream SSRC 0 can be used.
117 bool MuteStream(uint32 ssrc, bool mute);
118
119 // Multiplexing
120 bool AddRecvStream(const StreamParams& sp);
121 bool RemoveRecvStream(uint32 ssrc);
122
123 // Monitoring
124 void StartConnectionMonitor(int cms);
125 void StopConnectionMonitor();
126
127 void set_srtp_signal_silent_time(uint32 silent_time) {
128 srtp_filter_.set_signal_silent_time(silent_time);
129 }
130
131 void set_content_name(const std::string& content_name) {
132 ASSERT(signaling_thread()->IsCurrent());
133 ASSERT(!writable_);
134 if (session_->state() != BaseSession::STATE_INIT) {
135 LOG(LS_ERROR) << "Content name for a channel can be changed only "
136 << "when BaseSession is in STATE_INIT state.";
137 return;
138 }
139 content_name_ = content_name;
140 }
141
142 template <class T>
143 void RegisterSendSink(T* sink,
144 void (T::*OnPacket)(const void*, size_t, bool),
145 SinkType type) {
146 talk_base::CritScope cs(&signal_send_packet_cs_);
147 if (SINK_POST_CRYPTO == type) {
148 SignalSendPacketPostCrypto.disconnect(sink);
149 SignalSendPacketPostCrypto.connect(sink, OnPacket);
150 } else {
151 SignalSendPacketPreCrypto.disconnect(sink);
152 SignalSendPacketPreCrypto.connect(sink, OnPacket);
153 }
154 }
155
156 void UnregisterSendSink(sigslot::has_slots<>* sink,
157 SinkType type) {
158 talk_base::CritScope cs(&signal_send_packet_cs_);
159 if (SINK_POST_CRYPTO == type) {
160 SignalSendPacketPostCrypto.disconnect(sink);
161 } else {
162 SignalSendPacketPreCrypto.disconnect(sink);
163 }
164 }
165
166 bool HasSendSinks(SinkType type) {
167 talk_base::CritScope cs(&signal_send_packet_cs_);
168 if (SINK_POST_CRYPTO == type) {
169 return !SignalSendPacketPostCrypto.is_empty();
170 } else {
171 return !SignalSendPacketPreCrypto.is_empty();
172 }
173 }
174
175 template <class T>
176 void RegisterRecvSink(T* sink,
177 void (T::*OnPacket)(const void*, size_t, bool),
178 SinkType type) {
179 talk_base::CritScope cs(&signal_recv_packet_cs_);
180 if (SINK_POST_CRYPTO == type) {
181 SignalRecvPacketPostCrypto.disconnect(sink);
182 SignalRecvPacketPostCrypto.connect(sink, OnPacket);
183 } else {
184 SignalRecvPacketPreCrypto.disconnect(sink);
185 SignalRecvPacketPreCrypto.connect(sink, OnPacket);
186 }
187 }
188
189 void UnregisterRecvSink(sigslot::has_slots<>* sink,
190 SinkType type) {
191 talk_base::CritScope cs(&signal_recv_packet_cs_);
192 if (SINK_POST_CRYPTO == type) {
193 SignalRecvPacketPostCrypto.disconnect(sink);
194 } else {
195 SignalRecvPacketPreCrypto.disconnect(sink);
196 }
197 }
198
199 bool HasRecvSinks(SinkType type) {
200 talk_base::CritScope cs(&signal_recv_packet_cs_);
201 if (SINK_POST_CRYPTO == type) {
202 return !SignalRecvPacketPostCrypto.is_empty();
203 } else {
204 return !SignalRecvPacketPreCrypto.is_empty();
205 }
206 }
207
208 SsrcMuxFilter* ssrc_filter() { return &ssrc_filter_; }
209
210 const std::vector<StreamParams>& local_streams() const {
211 return local_streams_;
212 }
213 const std::vector<StreamParams>& remote_streams() const {
214 return remote_streams_;
215 }
216
217 // Used for latency measurements.
218 sigslot::signal1<BaseChannel*> SignalFirstPacketReceived;
219
220 // Used to alert UI when the muted status changes, perhaps autonomously.
221 sigslot::repeater2<BaseChannel*, bool> SignalAutoMuted;
222
223 // Made public for easier testing.
224 void SetReadyToSend(TransportChannel* channel, bool ready);
225
226 protected:
227 MediaEngineInterface* media_engine() const { return media_engine_; }
228 virtual MediaChannel* media_channel() const { return media_channel_; }
229 void set_rtcp_transport_channel(TransportChannel* transport);
230 bool was_ever_writable() const { return was_ever_writable_; }
231 void set_local_content_direction(MediaContentDirection direction) {
232 local_content_direction_ = direction;
233 }
234 void set_remote_content_direction(MediaContentDirection direction) {
235 remote_content_direction_ = direction;
236 }
237 bool IsReadyToReceive() const;
238 bool IsReadyToSend() const;
239 talk_base::Thread* signaling_thread() { return session_->signaling_thread(); }
240 SrtpFilter* srtp_filter() { return &srtp_filter_; }
241 bool rtcp() const { return rtcp_; }
242
243 void Send(uint32 id, talk_base::MessageData* pdata = NULL);
244 void Post(uint32 id, talk_base::MessageData* pdata = NULL);
245 void PostDelayed(int cmsDelay, uint32 id = 0,
246 talk_base::MessageData* pdata = NULL);
247 void Clear(uint32 id = talk_base::MQID_ANY,
248 talk_base::MessageList* removed = NULL);
249 void FlushRtcpMessages();
250
251 // NetworkInterface implementation, called by MediaEngine
252 virtual bool SendPacket(talk_base::Buffer* packet);
253 virtual bool SendRtcp(talk_base::Buffer* packet);
254 virtual int SetOption(SocketType type, talk_base::Socket::Option o, int val);
255
256 // From TransportChannel
257 void OnWritableState(TransportChannel* channel);
258 virtual void OnChannelRead(TransportChannel* channel, const char* data,
259 size_t len, int flags);
260 void OnReadyToSend(TransportChannel* channel);
261
262 bool PacketIsRtcp(const TransportChannel* channel, const char* data,
263 size_t len);
264 bool SendPacket(bool rtcp, talk_base::Buffer* packet);
265 virtual bool WantsPacket(bool rtcp, talk_base::Buffer* packet);
266 void HandlePacket(bool rtcp, talk_base::Buffer* packet);
267
268 // Apply the new local/remote session description.
269 void OnNewLocalDescription(BaseSession* session, ContentAction action);
270 void OnNewRemoteDescription(BaseSession* session, ContentAction action);
271
272 void EnableMedia_w();
273 void DisableMedia_w();
274 virtual bool MuteStream_w(uint32 ssrc, bool mute);
275 bool IsStreamMuted_w(uint32 ssrc);
276 void ChannelWritable_w();
277 void ChannelNotWritable_w();
278 bool AddRecvStream_w(const StreamParams& sp);
279 bool RemoveRecvStream_w(uint32 ssrc);
280 virtual bool ShouldSetupDtlsSrtp() const;
281 // Do the DTLS key expansion and impose it on the SRTP/SRTCP filters.
282 // |rtcp_channel| indicates whether to set up the RTP or RTCP filter.
283 bool SetupDtlsSrtp(bool rtcp_channel);
284 // Set the DTLS-SRTP cipher policy on this channel as appropriate.
285 bool SetDtlsSrtpCiphers(TransportChannel *tc, bool rtcp);
286
287 virtual void ChangeState() = 0;
288
289 // Gets the content info appropriate to the channel (audio or video).
290 virtual const ContentInfo* GetFirstContent(
291 const SessionDescription* sdesc) = 0;
292 bool UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
293 ContentAction action);
294 bool UpdateRemoteStreams_w(const std::vector<StreamParams>& streams,
295 ContentAction action);
296 bool SetBaseLocalContent_w(const MediaContentDescription* content,
297 ContentAction action);
298 virtual bool SetLocalContent_w(const MediaContentDescription* content,
299 ContentAction action) = 0;
300 bool SetBaseRemoteContent_w(const MediaContentDescription* content,
301 ContentAction action);
302 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
303 ContentAction action) = 0;
304
305 bool SetSrtp_w(const std::vector<CryptoParams>& params, ContentAction action,
306 ContentSource src);
307 bool SetRtcpMux_w(bool enable, ContentAction action, ContentSource src);
308
309 virtual bool SetMaxSendBandwidth_w(int max_bandwidth);
310
311 // From MessageHandler
312 virtual void OnMessage(talk_base::Message* pmsg);
313
314 // Handled in derived classes
315 // Get the SRTP ciphers to use for RTP media
316 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const = 0;
317 virtual void OnConnectionMonitorUpdate(SocketMonitor* monitor,
318 const std::vector<ConnectionInfo>& infos) = 0;
319
320 private:
321 sigslot::signal3<const void*, size_t, bool> SignalSendPacketPreCrypto;
322 sigslot::signal3<const void*, size_t, bool> SignalSendPacketPostCrypto;
323 sigslot::signal3<const void*, size_t, bool> SignalRecvPacketPreCrypto;
324 sigslot::signal3<const void*, size_t, bool> SignalRecvPacketPostCrypto;
325 talk_base::CriticalSection signal_send_packet_cs_;
326 talk_base::CriticalSection signal_recv_packet_cs_;
327
328 talk_base::Thread* worker_thread_;
329 MediaEngineInterface* media_engine_;
330 BaseSession* session_;
331 MediaChannel* media_channel_;
332 std::vector<StreamParams> local_streams_;
333 std::vector<StreamParams> remote_streams_;
334
335 std::string content_name_;
336 bool rtcp_;
337 TransportChannel* transport_channel_;
338 TransportChannel* rtcp_transport_channel_;
339 SrtpFilter srtp_filter_;
340 RtcpMuxFilter rtcp_mux_filter_;
341 SsrcMuxFilter ssrc_filter_;
342 talk_base::scoped_ptr<SocketMonitor> socket_monitor_;
343 bool enabled_;
344 bool writable_;
345 bool rtp_ready_to_send_;
346 bool rtcp_ready_to_send_;
347 bool optimistic_data_send_;
348 bool was_ever_writable_;
349 MediaContentDirection local_content_direction_;
350 MediaContentDirection remote_content_direction_;
351 std::set<uint32> muted_streams_;
352 bool has_received_packet_;
353 bool dtls_keyed_;
354 bool secure_required_;
355};
356
357// VoiceChannel is a specialization that adds support for early media, DTMF,
358// and input/output level monitoring.
359class VoiceChannel : public BaseChannel {
360 public:
361 VoiceChannel(talk_base::Thread* thread, MediaEngineInterface* media_engine,
362 VoiceMediaChannel* channel, BaseSession* session,
363 const std::string& content_name, bool rtcp);
364 ~VoiceChannel();
365 bool Init();
366 bool SetRenderer(uint32 ssrc, AudioRenderer* renderer);
367
368 // downcasts a MediaChannel
369 virtual VoiceMediaChannel* media_channel() const {
370 return static_cast<VoiceMediaChannel*>(BaseChannel::media_channel());
371 }
372
373 bool SetRingbackTone(const void* buf, int len);
374 void SetEarlyMedia(bool enable);
375 // This signal is emitted when we have gone a period of time without
376 // receiving early media. When received, a UI should start playing its
377 // own ringing sound
378 sigslot::signal1<VoiceChannel*> SignalEarlyMediaTimeout;
379
380 bool PlayRingbackTone(uint32 ssrc, bool play, bool loop);
381 // TODO(ronghuawu): Replace PressDTMF with InsertDtmf.
382 bool PressDTMF(int digit, bool playout);
383 // Returns if the telephone-event has been negotiated.
384 bool CanInsertDtmf();
385 // Send and/or play a DTMF |event| according to the |flags|.
386 // The DTMF out-of-band signal will be used on sending.
387 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000388 // The valid value for the |event| are 0 which corresponding to DTMF
389 // event 0-9, *, #, A-D.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000390 bool InsertDtmf(uint32 ssrc, int event_code, int duration, int flags);
391 bool SetOutputScaling(uint32 ssrc, double left, double right);
392 // Get statistics about the current media session.
393 bool GetStats(VoiceMediaInfo* stats);
394
395 // Monitoring functions
396 sigslot::signal2<VoiceChannel*, const std::vector<ConnectionInfo>&>
397 SignalConnectionMonitor;
398
399 void StartMediaMonitor(int cms);
400 void StopMediaMonitor();
401 sigslot::signal2<VoiceChannel*, const VoiceMediaInfo&> SignalMediaMonitor;
402
403 void StartAudioMonitor(int cms);
404 void StopAudioMonitor();
405 bool IsAudioMonitorRunning() const;
406 sigslot::signal2<VoiceChannel*, const AudioInfo&> SignalAudioMonitor;
407
408 void StartTypingMonitor(const TypingMonitorOptions& settings);
409 void StopTypingMonitor();
410 bool IsTypingMonitorRunning() const;
411
412 // Overrides BaseChannel::MuteStream_w.
413 virtual bool MuteStream_w(uint32 ssrc, bool mute);
414
415 int GetInputLevel_w();
416 int GetOutputLevel_w();
417 void GetActiveStreams_w(AudioInfo::StreamList* actives);
418
419 // Signal errors from VoiceMediaChannel. Arguments are:
420 // ssrc(uint32), and error(VoiceMediaChannel::Error).
421 sigslot::signal3<VoiceChannel*, uint32, VoiceMediaChannel::Error>
422 SignalMediaError;
423
424 // Configuration and setting.
425 bool SetChannelOptions(const AudioOptions& options);
426
427 private:
428 // overrides from BaseChannel
429 virtual void OnChannelRead(TransportChannel* channel,
430 const char* data, size_t len, int flags);
431 virtual void ChangeState();
432 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
433 virtual bool SetLocalContent_w(const MediaContentDescription* content,
434 ContentAction action);
435 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
436 ContentAction action);
437 bool SetRingbackTone_w(const void* buf, int len);
438 bool PlayRingbackTone_w(uint32 ssrc, bool play, bool loop);
439 void HandleEarlyMediaTimeout();
440 bool CanInsertDtmf_w();
441 bool InsertDtmf_w(uint32 ssrc, int event, int duration, int flags);
442 bool SetOutputScaling_w(uint32 ssrc, double left, double right);
443 bool GetStats_w(VoiceMediaInfo* stats);
444
445 virtual void OnMessage(talk_base::Message* pmsg);
446 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
447 virtual void OnConnectionMonitorUpdate(
448 SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos);
449 virtual void OnMediaMonitorUpdate(
450 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info);
451 void OnAudioMonitorUpdate(AudioMonitor* monitor, const AudioInfo& info);
452 void OnVoiceChannelError(uint32 ssrc, VoiceMediaChannel::Error error);
453 void SendLastMediaError();
454 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
455 // Configuration and setting.
456 bool SetChannelOptions_w(const AudioOptions& options);
457 bool SetRenderer_w(uint32 ssrc, AudioRenderer* renderer);
458
459 static const int kEarlyMediaTimeout = 1000;
460 bool received_media_;
461 talk_base::scoped_ptr<VoiceMediaMonitor> media_monitor_;
462 talk_base::scoped_ptr<AudioMonitor> audio_monitor_;
463 talk_base::scoped_ptr<TypingMonitor> typing_monitor_;
464};
465
466// VideoChannel is a specialization for video.
467class VideoChannel : public BaseChannel {
468 public:
469 // Make screen capturer virtual so that it can be overriden in testing.
470 // E.g. used to test that window events are triggered correctly.
471 class ScreenCapturerFactory {
472 public:
473 virtual VideoCapturer* CreateScreenCapturer(const ScreencastId& window) = 0;
474 virtual ~ScreenCapturerFactory() {}
475 };
476
477 VideoChannel(talk_base::Thread* thread, MediaEngineInterface* media_engine,
478 VideoMediaChannel* channel, BaseSession* session,
479 const std::string& content_name, bool rtcp,
480 VoiceChannel* voice_channel);
481 ~VideoChannel();
482 bool Init();
483
484 bool SetRenderer(uint32 ssrc, VideoRenderer* renderer);
485 bool ApplyViewRequest(const ViewRequest& request);
486
487 // TODO(pthatcher): Refactor to use a "capture id" instead of an
488 // ssrc here as the "key".
489 VideoCapturer* AddScreencast(uint32 ssrc, const ScreencastId& id);
490 VideoCapturer* GetScreencastCapturer(uint32 ssrc);
491 bool SetCapturer(uint32 ssrc, VideoCapturer* capturer);
492 bool RemoveScreencast(uint32 ssrc);
493 // True if we've added a screencast. Doesn't matter if the capturer
494 // has been started or not.
495 bool IsScreencasting();
496 int ScreencastFps(uint32 ssrc);
497 // Get statistics about the current media session.
498 bool GetStats(VideoMediaInfo* stats);
499
500 sigslot::signal2<VideoChannel*, const std::vector<ConnectionInfo>&>
501 SignalConnectionMonitor;
502
503 void StartMediaMonitor(int cms);
504 void StopMediaMonitor();
505 sigslot::signal2<VideoChannel*, const VideoMediaInfo&> SignalMediaMonitor;
506 sigslot::signal2<uint32, talk_base::WindowEvent> SignalScreencastWindowEvent;
507
508 bool SendIntraFrame();
509 bool RequestIntraFrame();
510 sigslot::signal3<VideoChannel*, uint32, VideoMediaChannel::Error>
511 SignalMediaError;
512
513 void SetScreenCaptureFactory(
514 ScreenCapturerFactory* screencapture_factory);
515
516 // Configuration and setting.
517 bool SetChannelOptions(const VideoOptions& options);
518
519 protected:
520 // downcasts a MediaChannel
521 virtual VideoMediaChannel* media_channel() const {
522 return static_cast<VideoMediaChannel*>(BaseChannel::media_channel());
523 }
524
525 private:
526 typedef std::map<uint32, VideoCapturer*> ScreencastMap;
527
528 // overrides from BaseChannel
529 virtual void ChangeState();
530 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
531 virtual bool SetLocalContent_w(const MediaContentDescription* content,
532 ContentAction action);
533 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
534 ContentAction action);
535 void SendIntraFrame_w() {
536 media_channel()->SendIntraFrame();
537 }
538 void RequestIntraFrame_w() {
539 media_channel()->RequestIntraFrame();
540 }
541
542 bool ApplyViewRequest_w(const ViewRequest& request);
543 void SetRenderer_w(uint32 ssrc, VideoRenderer* renderer);
544
545 VideoCapturer* AddScreencast_w(uint32 ssrc, const ScreencastId& id);
546 VideoCapturer* GetScreencastCapturer_w(uint32 ssrc);
547 bool SetCapturer_w(uint32 ssrc, VideoCapturer* capturer);
548 bool RemoveScreencast_w(uint32 ssrc);
549 void OnScreencastWindowEvent_s(uint32 ssrc, talk_base::WindowEvent we);
550 bool IsScreencasting_w() const;
551 int ScreencastFps_w(uint32 ssrc) const;
552 void SetScreenCaptureFactory_w(
553 ScreenCapturerFactory* screencapture_factory);
554 bool GetStats_w(VideoMediaInfo* stats);
555
556 virtual void OnMessage(talk_base::Message* pmsg);
557 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
558 virtual void OnConnectionMonitorUpdate(
559 SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos);
560 virtual void OnMediaMonitorUpdate(
561 VideoMediaChannel* media_channel, const VideoMediaInfo& info);
562 virtual void OnScreencastWindowEvent(uint32 ssrc,
563 talk_base::WindowEvent event);
564 virtual void OnStateChange(VideoCapturer* capturer, CaptureState ev);
565 bool GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc);
566
567 void OnVideoChannelError(uint32 ssrc, VideoMediaChannel::Error error);
568 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
569 // Configuration and setting.
570 bool SetChannelOptions_w(const VideoOptions& options);
571
572 VoiceChannel* voice_channel_;
573 VideoRenderer* renderer_;
574 talk_base::scoped_ptr<ScreenCapturerFactory> screencapture_factory_;
575 ScreencastMap screencast_capturers_;
576 talk_base::scoped_ptr<VideoMediaMonitor> media_monitor_;
577
578 talk_base::WindowEvent previous_we_;
579};
580
581// DataChannel is a specialization for data.
582class DataChannel : public BaseChannel {
583 public:
584 DataChannel(talk_base::Thread* thread,
585 DataMediaChannel* media_channel,
586 BaseSession* session,
587 const std::string& content_name,
588 bool rtcp);
589 ~DataChannel();
590 bool Init();
591
592 // downcasts a MediaChannel
593 virtual DataMediaChannel* media_channel() const {
594 return static_cast<DataMediaChannel*>(BaseChannel::media_channel());
595 }
596
597 bool SendData(const SendDataParams& params,
598 const talk_base::Buffer& payload,
599 SendDataResult* result);
600
601 void StartMediaMonitor(int cms);
602 void StopMediaMonitor();
603
604 sigslot::signal2<DataChannel*, const DataMediaInfo&> SignalMediaMonitor;
605 sigslot::signal2<DataChannel*, const std::vector<ConnectionInfo>&>
606 SignalConnectionMonitor;
607 sigslot::signal3<DataChannel*, uint32, DataMediaChannel::Error>
608 SignalMediaError;
609 sigslot::signal3<DataChannel*,
610 const ReceiveDataParams&,
611 const talk_base::Buffer&>
612 SignalDataReceived;
613 // Signal for notifying when the channel becomes ready to send data.
614 // That occurs when the channel is enabled, the transport is writable and
615 // both local and remote descriptions are set.
616 // TODO(perkj): Signal this per SSRC stream.
617 sigslot::signal1<bool> SignalReadyToSendData;
618
619 private:
620 struct SendDataMessageData : public talk_base::MessageData {
621 SendDataMessageData(const SendDataParams& params,
622 const talk_base::Buffer* payload,
623 SendDataResult* result)
624 : params(params),
625 payload(payload),
626 result(result),
627 succeeded(false) {
628 }
629
630 const SendDataParams& params;
631 const talk_base::Buffer* payload;
632 SendDataResult* result;
633 bool succeeded;
634 };
635
636 struct DataReceivedMessageData : public talk_base::MessageData {
637 // We copy the data because the data will become invalid after we
638 // handle DataMediaChannel::SignalDataReceived but before we fire
639 // SignalDataReceived.
640 DataReceivedMessageData(
641 const ReceiveDataParams& params, const char* data, size_t len)
642 : params(params),
643 payload(data, len) {
644 }
645 const ReceiveDataParams params;
646 const talk_base::Buffer payload;
647 };
648
649 // overrides from BaseChannel
650 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
651 // If data_channel_type_ is DCT_NONE, set it. Otherwise, check that
652 // it's the same as what was set previously. Returns false if it's
653 // set to one type one type and changed to another type later.
654 bool SetDataChannelType(DataChannelType new_data_channel_type);
655 // Same as SetDataChannelType, but extracts the type from the
656 // DataContentDescription.
657 bool SetDataChannelTypeFromContent(const DataContentDescription* content);
658 virtual bool SetMaxSendBandwidth_w(int max_bandwidth);
659 virtual bool SetLocalContent_w(const MediaContentDescription* content,
660 ContentAction action);
661 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
662 ContentAction action);
663 virtual void ChangeState();
664 virtual bool WantsPacket(bool rtcp, talk_base::Buffer* packet);
665
666 virtual void OnMessage(talk_base::Message* pmsg);
667 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
668 virtual void OnConnectionMonitorUpdate(
669 SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos);
670 virtual void OnMediaMonitorUpdate(
671 DataMediaChannel* media_channel, const DataMediaInfo& info);
672 virtual bool ShouldSetupDtlsSrtp() const;
673 void OnDataReceived(
674 const ReceiveDataParams& params, const char* data, size_t len);
675 void OnDataChannelError(uint32 ssrc, DataMediaChannel::Error error);
676 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
677
678 talk_base::scoped_ptr<DataMediaMonitor> media_monitor_;
679 // TODO(pthatcher): Make a separate SctpDataChannel and
680 // RtpDataChannel instead of using this.
681 DataChannelType data_channel_type_;
682};
683
684} // namespace cricket
685
686#endif // TALK_SESSION_MEDIA_CHANNEL_H_