blob: 420141c0003a32133d2e504bc32905542dfc1588 [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
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034#include "talk/media/base/mediachannel.h"
35#include "talk/media/base/mediaengine.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036#include "talk/media/base/streamparams.h"
37#include "talk/media/base/videocapturer.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000038#include "webrtc/p2p/base/session.h"
39#include "webrtc/p2p/client/socketmonitor.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040#include "talk/session/media/audiomonitor.h"
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +000041#include "talk/session/media/bundlefilter.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042#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"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000046#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"
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<>,
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +000076 public MediaChannel::NetworkInterface,
77 public ConnectionStatsGetter {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078 public:
Fredrik Solenberg0c022642015-08-05 12:25:22 +020079 BaseChannel(rtc::Thread* thread, MediaChannel* channel, BaseSession* session,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080 const std::string& content_name, bool rtcp);
81 virtual ~BaseChannel();
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +000082 bool Init();
wu@webrtc.org78187522013-10-07 23:32:02 +000083 // Deinit may be called multiple times and is simply ignored if it's alreay
84 // done.
85 void Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000087 rtc::Thread* worker_thread() const { return worker_thread_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088 BaseSession* session() const { return session_; }
89 const std::string& content_name() { return content_name_; }
90 TransportChannel* transport_channel() const {
91 return transport_channel_;
92 }
93 TransportChannel* rtcp_transport_channel() const {
94 return rtcp_transport_channel_;
95 }
96 bool enabled() const { return enabled_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097
98 // This function returns true if we are using SRTP.
99 bool secure() const { return srtp_filter_.IsActive(); }
100 // The following function returns true if we are using
101 // DTLS-based keying. If you turned off SRTP later, however
102 // you could have secure() == false and dtls_secure() == true.
103 bool secure_dtls() const { return dtls_keyed_; }
104 // This function returns true if we require secure channel for call setup.
105 bool secure_required() const { return secure_required_; }
106
107 bool writable() const { return writable_; }
108 bool IsStreamMuted(uint32 ssrc);
109
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700110 // Activate RTCP mux, regardless of the state so far. Once
111 // activated, it can not be deactivated, and if the remote
112 // description doesn't support RTCP mux, setting the remote
113 // description will fail.
114 void ActivateRtcpMux();
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000115 bool PushdownLocalDescription(const SessionDescription* local_desc,
116 ContentAction action,
117 std::string* error_desc);
118 bool PushdownRemoteDescription(const SessionDescription* remote_desc,
119 ContentAction action,
120 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000121 // Channel control
122 bool SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000123 ContentAction action,
124 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125 bool SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000126 ContentAction action,
127 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128
129 bool Enable(bool enable);
130 // Mute sending media on the stream with SSRC |ssrc|
131 // If there is only one sending stream SSRC 0 can be used.
132 bool MuteStream(uint32 ssrc, bool mute);
133
134 // Multiplexing
135 bool AddRecvStream(const StreamParams& sp);
136 bool RemoveRecvStream(uint32 ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000137 bool AddSendStream(const StreamParams& sp);
138 bool RemoveSendStream(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139
140 // Monitoring
141 void StartConnectionMonitor(int cms);
142 void StopConnectionMonitor();
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000143 // For ConnectionStatsGetter, used by ConnectionMonitor
144 virtual bool GetConnectionStats(ConnectionInfos* infos) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000145
146 void set_srtp_signal_silent_time(uint32 silent_time) {
147 srtp_filter_.set_signal_silent_time(silent_time);
148 }
149
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000150 BundleFilter* bundle_filter() { return &bundle_filter_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151
152 const std::vector<StreamParams>& local_streams() const {
153 return local_streams_;
154 }
155 const std::vector<StreamParams>& remote_streams() const {
156 return remote_streams_;
157 }
158
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000159 sigslot::signal2<BaseChannel*, bool> SignalDtlsSetupFailure;
160 void SignalDtlsSetupFailure_w(bool rtcp);
161 void SignalDtlsSetupFailure_s(bool rtcp);
162
buildbot@webrtc.org6bfd6192014-05-15 16:15:59 +0000163 // Used for latency measurements.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164 sigslot::signal1<BaseChannel*> SignalFirstPacketReceived;
165
166 // Used to alert UI when the muted status changes, perhaps autonomously.
167 sigslot::repeater2<BaseChannel*, bool> SignalAutoMuted;
168
169 // Made public for easier testing.
170 void SetReadyToSend(TransportChannel* channel, bool ready);
171
guoweis@webrtc.org4f852882015-03-12 20:09:44 +0000172 // Only public for unit tests. Otherwise, consider protected.
173 virtual int SetOption(SocketType type, rtc::Socket::Option o, int val);
174
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 protected:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176 virtual MediaChannel* media_channel() const { return media_channel_; }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000177 // Sets the transport_channel_ and rtcp_transport_channel_. If
178 // |rtcp| is false, set rtcp_transport_channel_ is set to NULL. Get
179 // the transport channels from |session|.
180 // TODO(pthatcher): Pass in a Transport instead of a BaseSession.
181 bool SetTransportChannels(BaseSession* session, bool rtcp);
182 bool SetTransportChannels_w(BaseSession* session, bool rtcp);
183 void set_transport_channel(TransportChannel* transport);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000184 void set_rtcp_transport_channel(TransportChannel* transport);
185 bool was_ever_writable() const { return was_ever_writable_; }
186 void set_local_content_direction(MediaContentDirection direction) {
187 local_content_direction_ = direction;
188 }
189 void set_remote_content_direction(MediaContentDirection direction) {
190 remote_content_direction_ = direction;
191 }
192 bool IsReadyToReceive() const;
193 bool IsReadyToSend() const;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000194 rtc::Thread* signaling_thread() { return session_->signaling_thread(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195 SrtpFilter* srtp_filter() { return &srtp_filter_; }
196 bool rtcp() const { return rtcp_; }
197
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000198 void ConnectToTransportChannel(TransportChannel* tc);
199 void DisconnectFromTransportChannel(TransportChannel* tc);
200
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201 void FlushRtcpMessages();
202
203 // NetworkInterface implementation, called by MediaEngine
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000204 virtual bool SendPacket(rtc::Buffer* packet,
205 rtc::DiffServCodePoint dscp);
206 virtual bool SendRtcp(rtc::Buffer* packet,
207 rtc::DiffServCodePoint dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208
209 // From TransportChannel
210 void OnWritableState(TransportChannel* channel);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000211 virtual void OnChannelRead(TransportChannel* channel,
212 const char* data,
213 size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000214 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000215 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000216 void OnReadyToSend(TransportChannel* channel);
217
218 bool PacketIsRtcp(const TransportChannel* channel, const char* data,
219 size_t len);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000220 bool SendPacket(bool rtcp, rtc::Buffer* packet,
221 rtc::DiffServCodePoint dscp);
222 virtual bool WantsPacket(bool rtcp, rtc::Buffer* packet);
223 void HandlePacket(bool rtcp, rtc::Buffer* packet,
224 const rtc::PacketTime& packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000225
226 // Apply the new local/remote session description.
227 void OnNewLocalDescription(BaseSession* session, ContentAction action);
228 void OnNewRemoteDescription(BaseSession* session, ContentAction action);
229
230 void EnableMedia_w();
231 void DisableMedia_w();
232 virtual bool MuteStream_w(uint32 ssrc, bool mute);
233 bool IsStreamMuted_w(uint32 ssrc);
234 void ChannelWritable_w();
235 void ChannelNotWritable_w();
236 bool AddRecvStream_w(const StreamParams& sp);
237 bool RemoveRecvStream_w(uint32 ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000238 bool AddSendStream_w(const StreamParams& sp);
239 bool RemoveSendStream_w(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000240 virtual bool ShouldSetupDtlsSrtp() const;
241 // Do the DTLS key expansion and impose it on the SRTP/SRTCP filters.
242 // |rtcp_channel| indicates whether to set up the RTP or RTCP filter.
243 bool SetupDtlsSrtp(bool rtcp_channel);
244 // Set the DTLS-SRTP cipher policy on this channel as appropriate.
245 bool SetDtlsSrtpCiphers(TransportChannel *tc, bool rtcp);
246
247 virtual void ChangeState() = 0;
248
249 // Gets the content info appropriate to the channel (audio or video).
250 virtual const ContentInfo* GetFirstContent(
251 const SessionDescription* sdesc) = 0;
252 bool UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000253 ContentAction action,
254 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255 bool UpdateRemoteStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000256 ContentAction action,
257 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000258 bool SetBaseLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000259 ContentAction action,
260 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000261 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000262 ContentAction action,
263 std::string* error_desc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000264 bool SetBaseRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000265 ContentAction action,
266 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000268 ContentAction action,
269 std::string* error_desc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000270
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000271 // Helper method to get RTP Absoulute SendTime extension header id if
272 // present in remote supported extensions list.
273 void MaybeCacheRtpAbsSendTimeHeaderExtension(
274 const std::vector<RtpHeaderExtension>& extensions);
275
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +0000276 bool SetRecvRtpHeaderExtensions_w(const MediaContentDescription* content,
277 MediaChannel* media_channel,
278 std::string* error_desc);
279 bool SetSendRtpHeaderExtensions_w(const MediaContentDescription* content,
280 MediaChannel* media_channel,
281 std::string* error_desc);
282
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000283 bool CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
284 bool* dtls,
285 std::string* error_desc);
286 bool SetSrtp_w(const std::vector<CryptoParams>& params,
287 ContentAction action,
288 ContentSource src,
289 std::string* error_desc);
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700290 void ActivateRtcpMux_w();
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000291 bool SetRtcpMux_w(bool enable,
292 ContentAction action,
293 ContentSource src,
294 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295
296 // From MessageHandler
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000297 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000298
299 // Handled in derived classes
300 // Get the SRTP ciphers to use for RTP media
301 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const = 0;
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000302 virtual void OnConnectionMonitorUpdate(ConnectionMonitor* monitor,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303 const std::vector<ConnectionInfo>& infos) = 0;
304
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000305 // Helper function for invoking bool-returning methods on the worker thread.
306 template <class FunctorT>
307 bool InvokeOnWorker(const FunctorT& functor) {
308 return worker_thread_->Invoke<bool>(functor);
309 }
310
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000311 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000312 rtc::Thread* worker_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000313 BaseSession* session_;
314 MediaChannel* media_channel_;
315 std::vector<StreamParams> local_streams_;
316 std::vector<StreamParams> remote_streams_;
317
pthatcher@webrtc.org990a00c2015-03-13 18:20:33 +0000318 const std::string content_name_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000319 bool rtcp_;
320 TransportChannel* transport_channel_;
321 TransportChannel* rtcp_transport_channel_;
322 SrtpFilter srtp_filter_;
323 RtcpMuxFilter rtcp_mux_filter_;
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000324 BundleFilter bundle_filter_;
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000325 rtc::scoped_ptr<ConnectionMonitor> connection_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000326 bool enabled_;
327 bool writable_;
328 bool rtp_ready_to_send_;
329 bool rtcp_ready_to_send_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000330 bool was_ever_writable_;
331 MediaContentDirection local_content_direction_;
332 MediaContentDirection remote_content_direction_;
333 std::set<uint32> muted_streams_;
334 bool has_received_packet_;
335 bool dtls_keyed_;
336 bool secure_required_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000337 int rtp_abs_sendtime_extn_id_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338};
339
340// VoiceChannel is a specialization that adds support for early media, DTMF,
341// and input/output level monitoring.
342class VoiceChannel : public BaseChannel {
343 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000344 VoiceChannel(rtc::Thread* thread, MediaEngineInterface* media_engine,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000345 VoiceMediaChannel* channel, BaseSession* session,
346 const std::string& content_name, bool rtcp);
347 ~VoiceChannel();
348 bool Init();
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000349 bool SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer);
350 bool SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000351
352 // downcasts a MediaChannel
353 virtual VoiceMediaChannel* media_channel() const {
354 return static_cast<VoiceMediaChannel*>(BaseChannel::media_channel());
355 }
356
357 bool SetRingbackTone(const void* buf, int len);
358 void SetEarlyMedia(bool enable);
359 // This signal is emitted when we have gone a period of time without
360 // receiving early media. When received, a UI should start playing its
361 // own ringing sound
362 sigslot::signal1<VoiceChannel*> SignalEarlyMediaTimeout;
363
364 bool PlayRingbackTone(uint32 ssrc, bool play, bool loop);
365 // TODO(ronghuawu): Replace PressDTMF with InsertDtmf.
366 bool PressDTMF(int digit, bool playout);
367 // Returns if the telephone-event has been negotiated.
368 bool CanInsertDtmf();
369 // Send and/or play a DTMF |event| according to the |flags|.
370 // The DTMF out-of-band signal will be used on sending.
371 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000372 // The valid value for the |event| are 0 which corresponding to DTMF
373 // event 0-9, *, #, A-D.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000374 bool InsertDtmf(uint32 ssrc, int event_code, int duration, int flags);
375 bool SetOutputScaling(uint32 ssrc, double left, double right);
376 // Get statistics about the current media session.
377 bool GetStats(VoiceMediaInfo* stats);
378
379 // Monitoring functions
380 sigslot::signal2<VoiceChannel*, const std::vector<ConnectionInfo>&>
381 SignalConnectionMonitor;
382
383 void StartMediaMonitor(int cms);
384 void StopMediaMonitor();
385 sigslot::signal2<VoiceChannel*, const VoiceMediaInfo&> SignalMediaMonitor;
386
387 void StartAudioMonitor(int cms);
388 void StopAudioMonitor();
389 bool IsAudioMonitorRunning() const;
390 sigslot::signal2<VoiceChannel*, const AudioInfo&> SignalAudioMonitor;
391
392 void StartTypingMonitor(const TypingMonitorOptions& settings);
393 void StopTypingMonitor();
394 bool IsTypingMonitorRunning() const;
395
396 // Overrides BaseChannel::MuteStream_w.
397 virtual bool MuteStream_w(uint32 ssrc, bool mute);
398
399 int GetInputLevel_w();
400 int GetOutputLevel_w();
401 void GetActiveStreams_w(AudioInfo::StreamList* actives);
402
403 // Signal errors from VoiceMediaChannel. Arguments are:
404 // ssrc(uint32), and error(VoiceMediaChannel::Error).
405 sigslot::signal3<VoiceChannel*, uint32, VoiceMediaChannel::Error>
406 SignalMediaError;
407
408 // Configuration and setting.
409 bool SetChannelOptions(const AudioOptions& options);
410
411 private:
412 // overrides from BaseChannel
413 virtual void OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000414 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000415 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000416 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000417 virtual void ChangeState();
418 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
419 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000420 ContentAction action,
421 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000422 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000423 ContentAction action,
424 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000425 bool SetRingbackTone_w(const void* buf, int len);
426 bool PlayRingbackTone_w(uint32 ssrc, bool play, bool loop);
427 void HandleEarlyMediaTimeout();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000428 bool InsertDtmf_w(uint32 ssrc, int event, int duration, int flags);
429 bool SetOutputScaling_w(uint32 ssrc, double left, double right);
430 bool GetStats_w(VoiceMediaInfo* stats);
431
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000432 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000433 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
434 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000435 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000436 virtual void OnMediaMonitorUpdate(
437 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info);
438 void OnAudioMonitorUpdate(AudioMonitor* monitor, const AudioInfo& info);
439 void OnVoiceChannelError(uint32 ssrc, VoiceMediaChannel::Error error);
440 void SendLastMediaError();
441 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000442
443 static const int kEarlyMediaTimeout = 1000;
Fredrik Solenberg0c022642015-08-05 12:25:22 +0200444 MediaEngineInterface* media_engine_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000445 bool received_media_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000446 rtc::scoped_ptr<VoiceMediaMonitor> media_monitor_;
447 rtc::scoped_ptr<AudioMonitor> audio_monitor_;
448 rtc::scoped_ptr<TypingMonitor> typing_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000449};
450
451// VideoChannel is a specialization for video.
452class VideoChannel : public BaseChannel {
453 public:
Fredrik Solenberg0c022642015-08-05 12:25:22 +0200454 VideoChannel(rtc::Thread* thread, VideoMediaChannel* channel,
455 BaseSession* session, const std::string& content_name,
456 bool rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000457 ~VideoChannel();
458 bool Init();
459
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200460 // downcasts a MediaChannel
461 virtual VideoMediaChannel* media_channel() const {
462 return static_cast<VideoMediaChannel*>(BaseChannel::media_channel());
463 }
464
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000465 bool SetRenderer(uint32 ssrc, VideoRenderer* renderer);
466 bool ApplyViewRequest(const ViewRequest& request);
467
468 // TODO(pthatcher): Refactor to use a "capture id" instead of an
469 // ssrc here as the "key".
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000470 // Passes ownership of the capturer to the channel.
471 bool AddScreencast(uint32 ssrc, VideoCapturer* capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000472 bool SetCapturer(uint32 ssrc, VideoCapturer* capturer);
473 bool RemoveScreencast(uint32 ssrc);
474 // True if we've added a screencast. Doesn't matter if the capturer
475 // has been started or not.
476 bool IsScreencasting();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000477 int GetScreencastFps(uint32 ssrc);
478 int GetScreencastMaxPixels(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000479 // Get statistics about the current media session.
pbos@webrtc.org058b1f12015-03-04 08:54:32 +0000480 bool GetStats(VideoMediaInfo* stats);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000481
482 sigslot::signal2<VideoChannel*, const std::vector<ConnectionInfo>&>
483 SignalConnectionMonitor;
484
485 void StartMediaMonitor(int cms);
486 void StopMediaMonitor();
487 sigslot::signal2<VideoChannel*, const VideoMediaInfo&> SignalMediaMonitor;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000488 sigslot::signal2<uint32, rtc::WindowEvent> SignalScreencastWindowEvent;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489
490 bool SendIntraFrame();
491 bool RequestIntraFrame();
492 sigslot::signal3<VideoChannel*, uint32, VideoMediaChannel::Error>
493 SignalMediaError;
494
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000495 // Configuration and setting.
496 bool SetChannelOptions(const VideoOptions& options);
497
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000498 private:
499 typedef std::map<uint32, VideoCapturer*> ScreencastMap;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000500 struct ScreencastDetailsData;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000501
502 // overrides from BaseChannel
503 virtual void ChangeState();
504 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
505 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000506 ContentAction action,
507 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000508 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000509 ContentAction action,
510 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511 bool ApplyViewRequest_w(const ViewRequest& request);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000512
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000513 bool AddScreencast_w(uint32 ssrc, VideoCapturer* capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000514 bool RemoveScreencast_w(uint32 ssrc);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000515 void OnScreencastWindowEvent_s(uint32 ssrc, rtc::WindowEvent we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000516 bool IsScreencasting_w() const;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000517 void GetScreencastDetails_w(ScreencastDetailsData* d) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000518 bool GetStats_w(VideoMediaInfo* stats);
519
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000520 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000521 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
522 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000523 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524 virtual void OnMediaMonitorUpdate(
525 VideoMediaChannel* media_channel, const VideoMediaInfo& info);
526 virtual void OnScreencastWindowEvent(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000527 rtc::WindowEvent event);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528 virtual void OnStateChange(VideoCapturer* capturer, CaptureState ev);
529 bool GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc);
530
531 void OnVideoChannelError(uint32 ssrc, VideoMediaChannel::Error error);
532 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000533
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000534 VideoRenderer* renderer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535 ScreencastMap screencast_capturers_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000536 rtc::scoped_ptr<VideoMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000537
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000538 rtc::WindowEvent previous_we_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000539};
540
541// DataChannel is a specialization for data.
542class DataChannel : public BaseChannel {
543 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000544 DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545 DataMediaChannel* media_channel,
546 BaseSession* session,
547 const std::string& content_name,
548 bool rtcp);
549 ~DataChannel();
550 bool Init();
551
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000552 virtual bool SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000553 const rtc::Buffer& payload,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000554 SendDataResult* result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000555
556 void StartMediaMonitor(int cms);
557 void StopMediaMonitor();
558
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000559 // Should be called on the signaling thread only.
560 bool ready_to_send_data() const {
561 return ready_to_send_data_;
562 }
563
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000564 sigslot::signal2<DataChannel*, const DataMediaInfo&> SignalMediaMonitor;
565 sigslot::signal2<DataChannel*, const std::vector<ConnectionInfo>&>
566 SignalConnectionMonitor;
567 sigslot::signal3<DataChannel*, uint32, DataMediaChannel::Error>
568 SignalMediaError;
569 sigslot::signal3<DataChannel*,
570 const ReceiveDataParams&,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000571 const rtc::Buffer&>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000572 SignalDataReceived;
573 // Signal for notifying when the channel becomes ready to send data.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000574 // That occurs when the channel is enabled, the transport is writable,
575 // both local and remote descriptions are set, and the channel is unblocked.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000576 sigslot::signal1<bool> SignalReadyToSendData;
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +0000577 // Signal for notifying that the remote side has closed the DataChannel.
578 sigslot::signal1<uint32> SignalStreamClosedRemotely;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000579
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000580 protected:
581 // downcasts a MediaChannel.
582 virtual DataMediaChannel* media_channel() const {
583 return static_cast<DataMediaChannel*>(BaseChannel::media_channel());
584 }
585
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000586 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000587 struct SendDataMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000588 SendDataMessageData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000589 const rtc::Buffer* payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000590 SendDataResult* result)
591 : params(params),
592 payload(payload),
593 result(result),
594 succeeded(false) {
595 }
596
597 const SendDataParams& params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000598 const rtc::Buffer* payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000599 SendDataResult* result;
600 bool succeeded;
601 };
602
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000603 struct DataReceivedMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000604 // We copy the data because the data will become invalid after we
605 // handle DataMediaChannel::SignalDataReceived but before we fire
606 // SignalDataReceived.
607 DataReceivedMessageData(
608 const ReceiveDataParams& params, const char* data, size_t len)
609 : params(params),
610 payload(data, len) {
611 }
612 const ReceiveDataParams params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000613 const rtc::Buffer payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000614 };
615
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000616 typedef rtc::TypedMessageData<bool> DataChannelReadyToSendMessageData;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000617
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000618 // overrides from BaseChannel
619 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
620 // If data_channel_type_ is DCT_NONE, set it. Otherwise, check that
621 // it's the same as what was set previously. Returns false if it's
622 // set to one type one type and changed to another type later.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000623 bool SetDataChannelType(DataChannelType new_data_channel_type,
624 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000625 // Same as SetDataChannelType, but extracts the type from the
626 // DataContentDescription.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000627 bool SetDataChannelTypeFromContent(const DataContentDescription* content,
628 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000630 ContentAction action,
631 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000632 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000633 ContentAction action,
634 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000635 virtual void ChangeState();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000636 virtual bool WantsPacket(bool rtcp, rtc::Buffer* packet);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000638 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
640 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000641 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000642 virtual void OnMediaMonitorUpdate(
643 DataMediaChannel* media_channel, const DataMediaInfo& info);
644 virtual bool ShouldSetupDtlsSrtp() const;
645 void OnDataReceived(
646 const ReceiveDataParams& params, const char* data, size_t len);
647 void OnDataChannelError(uint32 ssrc, DataMediaChannel::Error error);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000648 void OnDataChannelReadyToSend(bool writable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000649 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +0000650 void OnStreamClosedRemotely(uint32 sid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000651
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000652 rtc::scoped_ptr<DataMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000653 // TODO(pthatcher): Make a separate SctpDataChannel and
654 // RtpDataChannel instead of using this.
655 DataChannelType data_channel_type_;
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000656 bool ready_to_send_data_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000657};
658
659} // namespace cricket
660
661#endif // TALK_SESSION_MEDIA_CHANNEL_H_