blob: 4d3baf6134fdef00f30feb76b8d1a9669c8ff454 [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
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000034#include "webrtc/base/asyncudpsocket.h"
35#include "webrtc/base/criticalsection.h"
36#include "webrtc/base/network.h"
37#include "webrtc/base/sigslot.h"
38#include "webrtc/base/window.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039#include "talk/media/base/mediachannel.h"
40#include "talk/media/base/mediaengine.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041#include "talk/media/base/streamparams.h"
42#include "talk/media/base/videocapturer.h"
43#include "talk/p2p/base/session.h"
44#include "talk/p2p/client/socketmonitor.h"
45#include "talk/session/media/audiomonitor.h"
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +000046#include "talk/session/media/bundlefilter.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047#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"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051
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.
wu@webrtc.org78187522013-10-07 23:32:02 +000068//
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
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074class BaseChannel
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000075 : public rtc::MessageHandler, public sigslot::has_slots<>,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076 public MediaChannel::NetworkInterface {
77 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000078 BaseChannel(rtc::Thread* thread, MediaEngineInterface* media_engine,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079 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);
wu@webrtc.org78187522013-10-07 23:32:02 +000084 // Deinit may be called multiple times and is simply ignored if it's alreay
85 // done.
86 void Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000088 rtc::Thread* worker_thread() const { return worker_thread_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 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_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098
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,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000113 ContentAction action,
114 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115 bool SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000116 ContentAction action,
117 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118
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);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000127 bool AddSendStream(const StreamParams& sp);
128 bool RemoveSendStream(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129
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) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000153 rtc::CritScope cs(&signal_send_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154 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) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000165 rtc::CritScope cs(&signal_send_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166 if (SINK_POST_CRYPTO == type) {
167 SignalSendPacketPostCrypto.disconnect(sink);
168 } else {
169 SignalSendPacketPreCrypto.disconnect(sink);
170 }
171 }
172
173 bool HasSendSinks(SinkType type) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000174 rtc::CritScope cs(&signal_send_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 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) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000186 rtc::CritScope cs(&signal_recv_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187 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) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000198 rtc::CritScope cs(&signal_recv_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199 if (SINK_POST_CRYPTO == type) {
200 SignalRecvPacketPostCrypto.disconnect(sink);
201 } else {
202 SignalRecvPacketPreCrypto.disconnect(sink);
203 }
204 }
205
206 bool HasRecvSinks(SinkType type) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000207 rtc::CritScope cs(&signal_recv_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208 if (SINK_POST_CRYPTO == type) {
209 return !SignalRecvPacketPostCrypto.is_empty();
210 } else {
211 return !SignalRecvPacketPreCrypto.is_empty();
212 }
213 }
214
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000215 BundleFilter* bundle_filter() { return &bundle_filter_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000216
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
buildbot@webrtc.org6bfd6192014-05-15 16:15:59 +0000224 // Used for latency measurements.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000225 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;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000246 rtc::Thread* signaling_thread() { return session_->signaling_thread(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000247 SrtpFilter* srtp_filter() { return &srtp_filter_; }
248 bool rtcp() const { return rtcp_; }
249
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000250 void FlushRtcpMessages();
251
252 // NetworkInterface implementation, called by MediaEngine
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000253 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);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000258
259 // From TransportChannel
260 void OnWritableState(TransportChannel* channel);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000261 virtual void OnChannelRead(TransportChannel* channel,
262 const char* data,
263 size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000264 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000265 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266 void OnReadyToSend(TransportChannel* channel);
267
268 bool PacketIsRtcp(const TransportChannel* channel, const char* data,
269 size_t len);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000270 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);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275
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);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000288 bool AddSendStream_w(const StreamParams& sp);
289 bool RemoveSendStream_w(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000290 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,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000303 ContentAction action,
304 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000305 bool UpdateRemoteStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000306 ContentAction action,
307 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000308 bool SetBaseLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000309 ContentAction action,
310 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000311 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000312 ContentAction action,
313 std::string* error_desc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000314 bool SetBaseRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000315 ContentAction action,
316 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000317 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000318 ContentAction action,
319 std::string* error_desc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000320
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000321 // 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
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +0000326 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
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000333 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);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000344
345 // From MessageHandler
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000346 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000347
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
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000354 // 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
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000360 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;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000365 rtc::CriticalSection signal_send_packet_cs_;
366 rtc::CriticalSection signal_recv_packet_cs_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000367
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000368 rtc::Thread* worker_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000369 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_;
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000381 BundleFilter bundle_filter_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000382 rtc::scoped_ptr<SocketMonitor> socket_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000383 bool enabled_;
384 bool writable_;
385 bool rtp_ready_to_send_;
386 bool rtcp_ready_to_send_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000387 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_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000394 int rtp_abs_sendtime_extn_id_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000395};
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:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000401 VoiceChannel(rtc::Thread* thread, MediaEngineInterface* media_engine,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000402 VoiceMediaChannel* channel, BaseSession* session,
403 const std::string& content_name, bool rtcp);
404 ~VoiceChannel();
405 bool Init();
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000406 bool SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer);
407 bool SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000408
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.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000429 // The valid value for the |event| are 0 which corresponding to DTMF
430 // event 0-9, *, #, A-D.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000431 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,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000471 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000472 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000473 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000474 virtual void ChangeState();
475 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
476 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000477 ContentAction action,
478 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000479 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000480 ContentAction action,
481 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000482 bool SetRingbackTone_w(const void* buf, int len);
483 bool PlayRingbackTone_w(uint32 ssrc, bool play, bool loop);
484 void HandleEarlyMediaTimeout();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000485 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
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000489 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490 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);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499
500 static const int kEarlyMediaTimeout = 1000;
501 bool received_media_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000502 rtc::scoped_ptr<VoiceMediaMonitor> media_monitor_;
503 rtc::scoped_ptr<AudioMonitor> audio_monitor_;
504 rtc::scoped_ptr<TypingMonitor> typing_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000505};
506
507// VideoChannel is a specialization for video.
508class VideoChannel : public BaseChannel {
509 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000510 VideoChannel(rtc::Thread* thread, MediaEngineInterface* media_engine,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511 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".
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000522 // Passes ownership of the capturer to the channel.
523 bool AddScreencast(uint32 ssrc, VideoCapturer* capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524 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();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000529 int GetScreencastFps(uint32 ssrc);
530 int GetScreencastMaxPixels(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531 // Get statistics about the current media session.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000532 bool GetStats(const StatsOptions& options, VideoMediaInfo* stats);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000533
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;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000540 sigslot::signal2<uint32, rtc::WindowEvent> SignalScreencastWindowEvent;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000541
542 bool SendIntraFrame();
543 bool RequestIntraFrame();
544 sigslot::signal3<VideoChannel*, uint32, VideoMediaChannel::Error>
545 SignalMediaError;
546
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000547 // 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;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000558 struct ScreencastDetailsData;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000559
560 // overrides from BaseChannel
561 virtual void ChangeState();
562 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
563 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000564 ContentAction action,
565 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000566 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000567 ContentAction action,
568 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000569 bool ApplyViewRequest_w(const ViewRequest& request);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000570
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000571 bool AddScreencast_w(uint32 ssrc, VideoCapturer* capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000572 bool RemoveScreencast_w(uint32 ssrc);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000573 void OnScreencastWindowEvent_s(uint32 ssrc, rtc::WindowEvent we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000574 bool IsScreencasting_w() const;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000575 void GetScreencastDetails_w(ScreencastDetailsData* d) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000576 bool GetStats_w(VideoMediaInfo* stats);
577
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000578 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000579 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,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000585 rtc::WindowEvent event);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000586 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);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591
592 VoiceChannel* voice_channel_;
593 VideoRenderer* renderer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000594 ScreencastMap screencast_capturers_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000595 rtc::scoped_ptr<VideoMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000596
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000597 rtc::WindowEvent previous_we_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000598};
599
600// DataChannel is a specialization for data.
601class DataChannel : public BaseChannel {
602 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000603 DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000604 DataMediaChannel* media_channel,
605 BaseSession* session,
606 const std::string& content_name,
607 bool rtcp);
608 ~DataChannel();
609 bool Init();
610
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000611 virtual bool SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000612 const rtc::Buffer& payload,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000613 SendDataResult* result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000614
615 void StartMediaMonitor(int cms);
616 void StopMediaMonitor();
617
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000618 // Should be called on the signaling thread only.
619 bool ready_to_send_data() const {
620 return ready_to_send_data_;
621 }
622
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000623 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&,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000630 const rtc::Buffer&>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000631 SignalDataReceived;
632 // Signal for notifying when the channel becomes ready to send data.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000633 // That occurs when the channel is enabled, the transport is writable,
634 // both local and remote descriptions are set, and the channel is unblocked.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000635 sigslot::signal1<bool> SignalReadyToSendData;
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +0000636 // Signal for notifying that the remote side has closed the DataChannel.
637 sigslot::signal1<uint32> SignalStreamClosedRemotely;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000638
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000639 protected:
640 // downcasts a MediaChannel.
641 virtual DataMediaChannel* media_channel() const {
642 return static_cast<DataMediaChannel*>(BaseChannel::media_channel());
643 }
644
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000645 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000646 struct SendDataMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000647 SendDataMessageData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000648 const rtc::Buffer* payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000649 SendDataResult* result)
650 : params(params),
651 payload(payload),
652 result(result),
653 succeeded(false) {
654 }
655
656 const SendDataParams& params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000657 const rtc::Buffer* payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000658 SendDataResult* result;
659 bool succeeded;
660 };
661
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000662 struct DataReceivedMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000663 // 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;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000672 const rtc::Buffer payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000673 };
674
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000675 typedef rtc::TypedMessageData<bool> DataChannelReadyToSendMessageData;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000676
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000677 // 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.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000682 bool SetDataChannelType(DataChannelType new_data_channel_type,
683 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000684 // Same as SetDataChannelType, but extracts the type from the
685 // DataContentDescription.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000686 bool SetDataChannelTypeFromContent(const DataContentDescription* content,
687 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000688 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000689 ContentAction action,
690 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000691 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000692 ContentAction action,
693 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000694 virtual void ChangeState();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000695 virtual bool WantsPacket(bool rtcp, rtc::Buffer* packet);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000697 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000698 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);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000707 void OnDataChannelReadyToSend(bool writable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +0000709 void OnStreamClosedRemotely(uint32 sid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000710
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000711 rtc::scoped_ptr<DataMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000712 // TODO(pthatcher): Make a separate SctpDataChannel and
713 // RtpDataChannel instead of using this.
714 DataChannelType data_channel_type_;
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000715 bool ready_to_send_data_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000716};
717
718} // namespace cricket
719
720#endif // TALK_SESSION_MEDIA_CHANNEL_H_