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