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