blob: 51fb575dd94cfc37e8827c48614f44b45e6a0b6e [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);
268 virtual void OnChannelRead(TransportChannel* channel, const char* data,
269 size_t len, int flags);
270 void OnReadyToSend(TransportChannel* channel);
271
272 bool PacketIsRtcp(const TransportChannel* channel, const char* data,
273 size_t len);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000274 bool SendPacket(bool rtcp, talk_base::Buffer* packet,
275 talk_base::DiffServCodePoint dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000276 virtual bool WantsPacket(bool rtcp, talk_base::Buffer* packet);
277 void HandlePacket(bool rtcp, talk_base::Buffer* packet);
278
279 // Apply the new local/remote session description.
280 void OnNewLocalDescription(BaseSession* session, ContentAction action);
281 void OnNewRemoteDescription(BaseSession* session, ContentAction action);
282
283 void EnableMedia_w();
284 void DisableMedia_w();
285 virtual bool MuteStream_w(uint32 ssrc, bool mute);
286 bool IsStreamMuted_w(uint32 ssrc);
287 void ChannelWritable_w();
288 void ChannelNotWritable_w();
289 bool AddRecvStream_w(const StreamParams& sp);
290 bool RemoveRecvStream_w(uint32 ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000291 bool AddSendStream_w(const StreamParams& sp);
292 bool RemoveSendStream_w(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000293 virtual bool ShouldSetupDtlsSrtp() const;
294 // Do the DTLS key expansion and impose it on the SRTP/SRTCP filters.
295 // |rtcp_channel| indicates whether to set up the RTP or RTCP filter.
296 bool SetupDtlsSrtp(bool rtcp_channel);
297 // Set the DTLS-SRTP cipher policy on this channel as appropriate.
298 bool SetDtlsSrtpCiphers(TransportChannel *tc, bool rtcp);
299
300 virtual void ChangeState() = 0;
301
302 // Gets the content info appropriate to the channel (audio or video).
303 virtual const ContentInfo* GetFirstContent(
304 const SessionDescription* sdesc) = 0;
305 bool UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
306 ContentAction action);
307 bool UpdateRemoteStreams_w(const std::vector<StreamParams>& streams,
308 ContentAction action);
309 bool SetBaseLocalContent_w(const MediaContentDescription* content,
310 ContentAction action);
311 virtual bool SetLocalContent_w(const MediaContentDescription* content,
312 ContentAction action) = 0;
313 bool SetBaseRemoteContent_w(const MediaContentDescription* content,
314 ContentAction action);
315 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
316 ContentAction action) = 0;
317
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000318 bool CheckSrtpConfig(const std::vector<CryptoParams>& cryptos, bool* dtls);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000319 bool SetSrtp_w(const std::vector<CryptoParams>& params, ContentAction action,
320 ContentSource src);
321 bool SetRtcpMux_w(bool enable, ContentAction action, ContentSource src);
322
323 virtual bool SetMaxSendBandwidth_w(int max_bandwidth);
324
325 // From MessageHandler
326 virtual void OnMessage(talk_base::Message* pmsg);
327
328 // Handled in derived classes
329 // Get the SRTP ciphers to use for RTP media
330 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const = 0;
331 virtual void OnConnectionMonitorUpdate(SocketMonitor* monitor,
332 const std::vector<ConnectionInfo>& infos) = 0;
333
334 private:
335 sigslot::signal3<const void*, size_t, bool> SignalSendPacketPreCrypto;
336 sigslot::signal3<const void*, size_t, bool> SignalSendPacketPostCrypto;
337 sigslot::signal3<const void*, size_t, bool> SignalRecvPacketPreCrypto;
338 sigslot::signal3<const void*, size_t, bool> SignalRecvPacketPostCrypto;
339 talk_base::CriticalSection signal_send_packet_cs_;
340 talk_base::CriticalSection signal_recv_packet_cs_;
341
342 talk_base::Thread* worker_thread_;
343 MediaEngineInterface* media_engine_;
344 BaseSession* session_;
345 MediaChannel* media_channel_;
346 std::vector<StreamParams> local_streams_;
347 std::vector<StreamParams> remote_streams_;
348
349 std::string content_name_;
350 bool rtcp_;
351 TransportChannel* transport_channel_;
352 TransportChannel* rtcp_transport_channel_;
353 SrtpFilter srtp_filter_;
354 RtcpMuxFilter rtcp_mux_filter_;
355 SsrcMuxFilter ssrc_filter_;
356 talk_base::scoped_ptr<SocketMonitor> socket_monitor_;
357 bool enabled_;
358 bool writable_;
359 bool rtp_ready_to_send_;
360 bool rtcp_ready_to_send_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000361 bool was_ever_writable_;
362 MediaContentDirection local_content_direction_;
363 MediaContentDirection remote_content_direction_;
364 std::set<uint32> muted_streams_;
365 bool has_received_packet_;
366 bool dtls_keyed_;
367 bool secure_required_;
368};
369
370// VoiceChannel is a specialization that adds support for early media, DTMF,
371// and input/output level monitoring.
372class VoiceChannel : public BaseChannel {
373 public:
374 VoiceChannel(talk_base::Thread* thread, MediaEngineInterface* media_engine,
375 VoiceMediaChannel* channel, BaseSession* session,
376 const std::string& content_name, bool rtcp);
377 ~VoiceChannel();
378 bool Init();
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000379 bool SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer);
380 bool SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000381
382 // downcasts a MediaChannel
383 virtual VoiceMediaChannel* media_channel() const {
384 return static_cast<VoiceMediaChannel*>(BaseChannel::media_channel());
385 }
386
387 bool SetRingbackTone(const void* buf, int len);
388 void SetEarlyMedia(bool enable);
389 // This signal is emitted when we have gone a period of time without
390 // receiving early media. When received, a UI should start playing its
391 // own ringing sound
392 sigslot::signal1<VoiceChannel*> SignalEarlyMediaTimeout;
393
394 bool PlayRingbackTone(uint32 ssrc, bool play, bool loop);
395 // TODO(ronghuawu): Replace PressDTMF with InsertDtmf.
396 bool PressDTMF(int digit, bool playout);
397 // Returns if the telephone-event has been negotiated.
398 bool CanInsertDtmf();
399 // Send and/or play a DTMF |event| according to the |flags|.
400 // The DTMF out-of-band signal will be used on sending.
401 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000402 // The valid value for the |event| are 0 which corresponding to DTMF
403 // event 0-9, *, #, A-D.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000404 bool InsertDtmf(uint32 ssrc, int event_code, int duration, int flags);
405 bool SetOutputScaling(uint32 ssrc, double left, double right);
406 // Get statistics about the current media session.
407 bool GetStats(VoiceMediaInfo* stats);
408
409 // Monitoring functions
410 sigslot::signal2<VoiceChannel*, const std::vector<ConnectionInfo>&>
411 SignalConnectionMonitor;
412
413 void StartMediaMonitor(int cms);
414 void StopMediaMonitor();
415 sigslot::signal2<VoiceChannel*, const VoiceMediaInfo&> SignalMediaMonitor;
416
417 void StartAudioMonitor(int cms);
418 void StopAudioMonitor();
419 bool IsAudioMonitorRunning() const;
420 sigslot::signal2<VoiceChannel*, const AudioInfo&> SignalAudioMonitor;
421
422 void StartTypingMonitor(const TypingMonitorOptions& settings);
423 void StopTypingMonitor();
424 bool IsTypingMonitorRunning() const;
425
426 // Overrides BaseChannel::MuteStream_w.
427 virtual bool MuteStream_w(uint32 ssrc, bool mute);
428
429 int GetInputLevel_w();
430 int GetOutputLevel_w();
431 void GetActiveStreams_w(AudioInfo::StreamList* actives);
432
433 // Signal errors from VoiceMediaChannel. Arguments are:
434 // ssrc(uint32), and error(VoiceMediaChannel::Error).
435 sigslot::signal3<VoiceChannel*, uint32, VoiceMediaChannel::Error>
436 SignalMediaError;
437
438 // Configuration and setting.
439 bool SetChannelOptions(const AudioOptions& options);
440
441 private:
442 // overrides from BaseChannel
443 virtual void OnChannelRead(TransportChannel* channel,
444 const char* data, size_t len, int flags);
445 virtual void ChangeState();
446 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
447 virtual bool SetLocalContent_w(const MediaContentDescription* content,
448 ContentAction action);
449 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
450 ContentAction action);
451 bool SetRingbackTone_w(const void* buf, int len);
452 bool PlayRingbackTone_w(uint32 ssrc, bool play, bool loop);
453 void HandleEarlyMediaTimeout();
454 bool CanInsertDtmf_w();
455 bool InsertDtmf_w(uint32 ssrc, int event, int duration, int flags);
456 bool SetOutputScaling_w(uint32 ssrc, double left, double right);
457 bool GetStats_w(VoiceMediaInfo* stats);
458
459 virtual void OnMessage(talk_base::Message* pmsg);
460 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
461 virtual void OnConnectionMonitorUpdate(
462 SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos);
463 virtual void OnMediaMonitorUpdate(
464 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info);
465 void OnAudioMonitorUpdate(AudioMonitor* monitor, const AudioInfo& info);
466 void OnVoiceChannelError(uint32 ssrc, VoiceMediaChannel::Error error);
467 void SendLastMediaError();
468 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
469 // Configuration and setting.
470 bool SetChannelOptions_w(const AudioOptions& options);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000471 bool SetRenderer_w(uint32 ssrc, AudioRenderer* renderer, bool is_local);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000472
473 static const int kEarlyMediaTimeout = 1000;
474 bool received_media_;
475 talk_base::scoped_ptr<VoiceMediaMonitor> media_monitor_;
476 talk_base::scoped_ptr<AudioMonitor> audio_monitor_;
477 talk_base::scoped_ptr<TypingMonitor> typing_monitor_;
478};
479
480// VideoChannel is a specialization for video.
481class VideoChannel : public BaseChannel {
482 public:
483 // Make screen capturer virtual so that it can be overriden in testing.
484 // E.g. used to test that window events are triggered correctly.
485 class ScreenCapturerFactory {
486 public:
487 virtual VideoCapturer* CreateScreenCapturer(const ScreencastId& window) = 0;
488 virtual ~ScreenCapturerFactory() {}
489 };
490
491 VideoChannel(talk_base::Thread* thread, MediaEngineInterface* media_engine,
492 VideoMediaChannel* channel, BaseSession* session,
493 const std::string& content_name, bool rtcp,
494 VoiceChannel* voice_channel);
495 ~VideoChannel();
496 bool Init();
497
498 bool SetRenderer(uint32 ssrc, VideoRenderer* renderer);
499 bool ApplyViewRequest(const ViewRequest& request);
500
501 // TODO(pthatcher): Refactor to use a "capture id" instead of an
502 // ssrc here as the "key".
503 VideoCapturer* AddScreencast(uint32 ssrc, const ScreencastId& id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504 bool SetCapturer(uint32 ssrc, VideoCapturer* capturer);
505 bool RemoveScreencast(uint32 ssrc);
506 // True if we've added a screencast. Doesn't matter if the capturer
507 // has been started or not.
508 bool IsScreencasting();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000509 int GetScreencastFps(uint32 ssrc);
510 int GetScreencastMaxPixels(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511 // Get statistics about the current media session.
512 bool GetStats(VideoMediaInfo* stats);
513
514 sigslot::signal2<VideoChannel*, const std::vector<ConnectionInfo>&>
515 SignalConnectionMonitor;
516
517 void StartMediaMonitor(int cms);
518 void StopMediaMonitor();
519 sigslot::signal2<VideoChannel*, const VideoMediaInfo&> SignalMediaMonitor;
520 sigslot::signal2<uint32, talk_base::WindowEvent> SignalScreencastWindowEvent;
521
522 bool SendIntraFrame();
523 bool RequestIntraFrame();
524 sigslot::signal3<VideoChannel*, uint32, VideoMediaChannel::Error>
525 SignalMediaError;
526
527 void SetScreenCaptureFactory(
528 ScreenCapturerFactory* screencapture_factory);
529
530 // Configuration and setting.
531 bool SetChannelOptions(const VideoOptions& options);
532
533 protected:
534 // downcasts a MediaChannel
535 virtual VideoMediaChannel* media_channel() const {
536 return static_cast<VideoMediaChannel*>(BaseChannel::media_channel());
537 }
538
539 private:
540 typedef std::map<uint32, VideoCapturer*> ScreencastMap;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000541 struct ScreencastDetailsMessageData;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000542
543 // overrides from BaseChannel
544 virtual void ChangeState();
545 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
546 virtual bool SetLocalContent_w(const MediaContentDescription* content,
547 ContentAction action);
548 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
549 ContentAction action);
550 void SendIntraFrame_w() {
551 media_channel()->SendIntraFrame();
552 }
553 void RequestIntraFrame_w() {
554 media_channel()->RequestIntraFrame();
555 }
556
557 bool ApplyViewRequest_w(const ViewRequest& request);
558 void SetRenderer_w(uint32 ssrc, VideoRenderer* renderer);
559
560 VideoCapturer* AddScreencast_w(uint32 ssrc, const ScreencastId& id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000561 bool SetCapturer_w(uint32 ssrc, VideoCapturer* capturer);
562 bool RemoveScreencast_w(uint32 ssrc);
563 void OnScreencastWindowEvent_s(uint32 ssrc, talk_base::WindowEvent we);
564 bool IsScreencasting_w() const;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000565 void ScreencastDetails_w(ScreencastDetailsMessageData* d) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000566 void SetScreenCaptureFactory_w(
567 ScreenCapturerFactory* screencapture_factory);
568 bool GetStats_w(VideoMediaInfo* stats);
569
570 virtual void OnMessage(talk_base::Message* pmsg);
571 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
572 virtual void OnConnectionMonitorUpdate(
573 SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos);
574 virtual void OnMediaMonitorUpdate(
575 VideoMediaChannel* media_channel, const VideoMediaInfo& info);
576 virtual void OnScreencastWindowEvent(uint32 ssrc,
577 talk_base::WindowEvent event);
578 virtual void OnStateChange(VideoCapturer* capturer, CaptureState ev);
579 bool GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc);
580
581 void OnVideoChannelError(uint32 ssrc, VideoMediaChannel::Error error);
582 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
583 // Configuration and setting.
584 bool SetChannelOptions_w(const VideoOptions& options);
585
586 VoiceChannel* voice_channel_;
587 VideoRenderer* renderer_;
588 talk_base::scoped_ptr<ScreenCapturerFactory> screencapture_factory_;
589 ScreencastMap screencast_capturers_;
590 talk_base::scoped_ptr<VideoMediaMonitor> media_monitor_;
591
592 talk_base::WindowEvent previous_we_;
593};
594
595// DataChannel is a specialization for data.
596class DataChannel : public BaseChannel {
597 public:
598 DataChannel(talk_base::Thread* thread,
599 DataMediaChannel* media_channel,
600 BaseSession* session,
601 const std::string& content_name,
602 bool rtcp);
603 ~DataChannel();
604 bool Init();
605
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000606 virtual bool SendData(const SendDataParams& params,
607 const talk_base::Buffer& payload,
608 SendDataResult* result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609
610 void StartMediaMonitor(int cms);
611 void StopMediaMonitor();
612
613 sigslot::signal2<DataChannel*, const DataMediaInfo&> SignalMediaMonitor;
614 sigslot::signal2<DataChannel*, const std::vector<ConnectionInfo>&>
615 SignalConnectionMonitor;
616 sigslot::signal3<DataChannel*, uint32, DataMediaChannel::Error>
617 SignalMediaError;
618 sigslot::signal3<DataChannel*,
619 const ReceiveDataParams&,
620 const talk_base::Buffer&>
621 SignalDataReceived;
622 // Signal for notifying when the channel becomes ready to send data.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000623 // That occurs when the channel is enabled, the transport is writable,
624 // both local and remote descriptions are set, and the channel is unblocked.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000625 sigslot::signal1<bool> SignalReadyToSendData;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000626 // Signal for notifying when a new stream is added from the remote side. Used
627 // for the in-band negotioation through the OPEN message for SCTP data
628 // channel.
629 sigslot::signal2<const std::string&, const webrtc::DataChannelInit&>
630 SignalNewStreamReceived;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000631
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000632 protected:
633 // downcasts a MediaChannel.
634 virtual DataMediaChannel* media_channel() const {
635 return static_cast<DataMediaChannel*>(BaseChannel::media_channel());
636 }
637
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000638 private:
639 struct SendDataMessageData : public talk_base::MessageData {
640 SendDataMessageData(const SendDataParams& params,
641 const talk_base::Buffer* payload,
642 SendDataResult* result)
643 : params(params),
644 payload(payload),
645 result(result),
646 succeeded(false) {
647 }
648
649 const SendDataParams& params;
650 const talk_base::Buffer* payload;
651 SendDataResult* result;
652 bool succeeded;
653 };
654
655 struct DataReceivedMessageData : public talk_base::MessageData {
656 // We copy the data because the data will become invalid after we
657 // handle DataMediaChannel::SignalDataReceived but before we fire
658 // SignalDataReceived.
659 DataReceivedMessageData(
660 const ReceiveDataParams& params, const char* data, size_t len)
661 : params(params),
662 payload(data, len) {
663 }
664 const ReceiveDataParams params;
665 const talk_base::Buffer payload;
666 };
667
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000668 typedef talk_base::TypedMessageData<bool> DataChannelReadyToSendMessageData;
669
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000670 struct DataChannelNewStreamReceivedMessageData
671 : public talk_base::MessageData {
672 DataChannelNewStreamReceivedMessageData(
673 const std::string& label, const webrtc::DataChannelInit& init)
674 : label(label),
675 init(init) {
676 }
677 const std::string label;
678 const webrtc::DataChannelInit init;
679 };
680
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681 // overrides from BaseChannel
682 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
683 // If data_channel_type_ is DCT_NONE, set it. Otherwise, check that
684 // it's the same as what was set previously. Returns false if it's
685 // set to one type one type and changed to another type later.
686 bool SetDataChannelType(DataChannelType new_data_channel_type);
687 // Same as SetDataChannelType, but extracts the type from the
688 // DataContentDescription.
689 bool SetDataChannelTypeFromContent(const DataContentDescription* content);
690 virtual bool SetMaxSendBandwidth_w(int max_bandwidth);
691 virtual bool SetLocalContent_w(const MediaContentDescription* content,
692 ContentAction action);
693 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
694 ContentAction action);
695 virtual void ChangeState();
696 virtual bool WantsPacket(bool rtcp, talk_base::Buffer* packet);
697
698 virtual void OnMessage(talk_base::Message* pmsg);
699 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
700 virtual void OnConnectionMonitorUpdate(
701 SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos);
702 virtual void OnMediaMonitorUpdate(
703 DataMediaChannel* media_channel, const DataMediaInfo& info);
704 virtual bool ShouldSetupDtlsSrtp() const;
705 void OnDataReceived(
706 const ReceiveDataParams& params, const char* data, size_t len);
707 void OnDataChannelError(uint32 ssrc, DataMediaChannel::Error error);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000708 void OnDataChannelReadyToSend(bool writable);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000709 void OnDataChannelNewStreamReceived(const std::string& label,
710 const webrtc::DataChannelInit& init);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
712
713 talk_base::scoped_ptr<DataMediaMonitor> media_monitor_;
714 // TODO(pthatcher): Make a separate SctpDataChannel and
715 // RtpDataChannel instead of using this.
716 DataChannelType data_channel_type_;
717};
718
719} // namespace cricket
720
721#endif // TALK_SESSION_MEDIA_CHANNEL_H_