blob: 5f51c07a287cf79aad3c2c82ab4aaa8fafc76834 [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 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700192 void set_secure_required(bool secure_required) {
193 secure_required_ = secure_required;
194 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195 bool IsReadyToReceive() const;
196 bool IsReadyToSend() const;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000197 rtc::Thread* signaling_thread() { return session_->signaling_thread(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198 SrtpFilter* srtp_filter() { return &srtp_filter_; }
199 bool rtcp() const { return rtcp_; }
200
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000201 void ConnectToTransportChannel(TransportChannel* tc);
202 void DisconnectFromTransportChannel(TransportChannel* tc);
203
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000204 void FlushRtcpMessages();
205
206 // NetworkInterface implementation, called by MediaEngine
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000207 virtual bool SendPacket(rtc::Buffer* packet,
208 rtc::DiffServCodePoint dscp);
209 virtual bool SendRtcp(rtc::Buffer* packet,
210 rtc::DiffServCodePoint dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211
212 // From TransportChannel
213 void OnWritableState(TransportChannel* channel);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000214 virtual void OnChannelRead(TransportChannel* channel,
215 const char* data,
216 size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000217 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000218 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000219 void OnReadyToSend(TransportChannel* channel);
220
221 bool PacketIsRtcp(const TransportChannel* channel, const char* data,
222 size_t len);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000223 bool SendPacket(bool rtcp, rtc::Buffer* packet,
224 rtc::DiffServCodePoint dscp);
225 virtual bool WantsPacket(bool rtcp, rtc::Buffer* packet);
226 void HandlePacket(bool rtcp, rtc::Buffer* packet,
227 const rtc::PacketTime& packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228
229 // Apply the new local/remote session description.
230 void OnNewLocalDescription(BaseSession* session, ContentAction action);
231 void OnNewRemoteDescription(BaseSession* session, ContentAction action);
232
233 void EnableMedia_w();
234 void DisableMedia_w();
235 virtual bool MuteStream_w(uint32 ssrc, bool mute);
236 bool IsStreamMuted_w(uint32 ssrc);
237 void ChannelWritable_w();
238 void ChannelNotWritable_w();
239 bool AddRecvStream_w(const StreamParams& sp);
240 bool RemoveRecvStream_w(uint32 ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000241 bool AddSendStream_w(const StreamParams& sp);
242 bool RemoveSendStream_w(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000243 virtual bool ShouldSetupDtlsSrtp() const;
244 // Do the DTLS key expansion and impose it on the SRTP/SRTCP filters.
245 // |rtcp_channel| indicates whether to set up the RTP or RTCP filter.
246 bool SetupDtlsSrtp(bool rtcp_channel);
247 // Set the DTLS-SRTP cipher policy on this channel as appropriate.
248 bool SetDtlsSrtpCiphers(TransportChannel *tc, bool rtcp);
249
250 virtual void ChangeState() = 0;
251
252 // Gets the content info appropriate to the channel (audio or video).
253 virtual const ContentInfo* GetFirstContent(
254 const SessionDescription* sdesc) = 0;
255 bool UpdateLocalStreams_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 UpdateRemoteStreams_w(const std::vector<StreamParams>& streams,
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 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000265 ContentAction action,
266 std::string* error_desc) = 0;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700267 bool SetRtpTransportParameters_w(const MediaContentDescription* content,
268 ContentAction action,
269 ContentSource src,
270 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000272 // Helper method to get RTP Absoulute SendTime extension header id if
273 // present in remote supported extensions list.
274 void MaybeCacheRtpAbsSendTimeHeaderExtension(
275 const std::vector<RtpHeaderExtension>& extensions);
276
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000277 bool CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
278 bool* dtls,
279 std::string* error_desc);
280 bool SetSrtp_w(const std::vector<CryptoParams>& params,
281 ContentAction action,
282 ContentSource src,
283 std::string* error_desc);
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700284 void ActivateRtcpMux_w();
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000285 bool SetRtcpMux_w(bool enable,
286 ContentAction action,
287 ContentSource src,
288 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000289
290 // From MessageHandler
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000291 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000292
293 // Handled in derived classes
294 // Get the SRTP ciphers to use for RTP media
295 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const = 0;
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000296 virtual void OnConnectionMonitorUpdate(ConnectionMonitor* monitor,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000297 const std::vector<ConnectionInfo>& infos) = 0;
298
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000299 // Helper function for invoking bool-returning methods on the worker thread.
300 template <class FunctorT>
301 bool InvokeOnWorker(const FunctorT& functor) {
302 return worker_thread_->Invoke<bool>(functor);
303 }
304
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000305 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000306 rtc::Thread* worker_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000307 BaseSession* session_;
308 MediaChannel* media_channel_;
309 std::vector<StreamParams> local_streams_;
310 std::vector<StreamParams> remote_streams_;
311
pthatcher@webrtc.org990a00c2015-03-13 18:20:33 +0000312 const std::string content_name_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000313 bool rtcp_;
314 TransportChannel* transport_channel_;
315 TransportChannel* rtcp_transport_channel_;
316 SrtpFilter srtp_filter_;
317 RtcpMuxFilter rtcp_mux_filter_;
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000318 BundleFilter bundle_filter_;
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000319 rtc::scoped_ptr<ConnectionMonitor> connection_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000320 bool enabled_;
321 bool writable_;
322 bool rtp_ready_to_send_;
323 bool rtcp_ready_to_send_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000324 bool was_ever_writable_;
325 MediaContentDirection local_content_direction_;
326 MediaContentDirection remote_content_direction_;
327 std::set<uint32> muted_streams_;
328 bool has_received_packet_;
329 bool dtls_keyed_;
330 bool secure_required_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000331 int rtp_abs_sendtime_extn_id_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000332};
333
334// VoiceChannel is a specialization that adds support for early media, DTMF,
335// and input/output level monitoring.
336class VoiceChannel : public BaseChannel {
337 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000338 VoiceChannel(rtc::Thread* thread, MediaEngineInterface* media_engine,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000339 VoiceMediaChannel* channel, BaseSession* session,
340 const std::string& content_name, bool rtcp);
341 ~VoiceChannel();
342 bool Init();
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000343 bool SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer);
344 bool SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000345
346 // downcasts a MediaChannel
347 virtual VoiceMediaChannel* media_channel() const {
348 return static_cast<VoiceMediaChannel*>(BaseChannel::media_channel());
349 }
350
351 bool SetRingbackTone(const void* buf, int len);
352 void SetEarlyMedia(bool enable);
353 // This signal is emitted when we have gone a period of time without
354 // receiving early media. When received, a UI should start playing its
355 // own ringing sound
356 sigslot::signal1<VoiceChannel*> SignalEarlyMediaTimeout;
357
358 bool PlayRingbackTone(uint32 ssrc, bool play, bool loop);
359 // TODO(ronghuawu): Replace PressDTMF with InsertDtmf.
360 bool PressDTMF(int digit, bool playout);
361 // Returns if the telephone-event has been negotiated.
362 bool CanInsertDtmf();
363 // Send and/or play a DTMF |event| according to the |flags|.
364 // The DTMF out-of-band signal will be used on sending.
365 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000366 // The valid value for the |event| are 0 which corresponding to DTMF
367 // event 0-9, *, #, A-D.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000368 bool InsertDtmf(uint32 ssrc, int event_code, int duration, int flags);
369 bool SetOutputScaling(uint32 ssrc, double left, double right);
370 // Get statistics about the current media session.
371 bool GetStats(VoiceMediaInfo* stats);
372
373 // Monitoring functions
374 sigslot::signal2<VoiceChannel*, const std::vector<ConnectionInfo>&>
375 SignalConnectionMonitor;
376
377 void StartMediaMonitor(int cms);
378 void StopMediaMonitor();
379 sigslot::signal2<VoiceChannel*, const VoiceMediaInfo&> SignalMediaMonitor;
380
381 void StartAudioMonitor(int cms);
382 void StopAudioMonitor();
383 bool IsAudioMonitorRunning() const;
384 sigslot::signal2<VoiceChannel*, const AudioInfo&> SignalAudioMonitor;
385
386 void StartTypingMonitor(const TypingMonitorOptions& settings);
387 void StopTypingMonitor();
388 bool IsTypingMonitorRunning() const;
389
390 // Overrides BaseChannel::MuteStream_w.
391 virtual bool MuteStream_w(uint32 ssrc, bool mute);
392
393 int GetInputLevel_w();
394 int GetOutputLevel_w();
395 void GetActiveStreams_w(AudioInfo::StreamList* actives);
396
397 // Signal errors from VoiceMediaChannel. Arguments are:
398 // ssrc(uint32), and error(VoiceMediaChannel::Error).
399 sigslot::signal3<VoiceChannel*, uint32, VoiceMediaChannel::Error>
400 SignalMediaError;
401
402 // Configuration and setting.
403 bool SetChannelOptions(const AudioOptions& options);
404
405 private:
406 // overrides from BaseChannel
407 virtual void OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000408 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000409 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000410 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000411 virtual void ChangeState();
412 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
413 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000414 ContentAction action,
415 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000416 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000417 ContentAction action,
418 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000419 bool SetRingbackTone_w(const void* buf, int len);
420 bool PlayRingbackTone_w(uint32 ssrc, bool play, bool loop);
421 void HandleEarlyMediaTimeout();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000422 bool InsertDtmf_w(uint32 ssrc, int event, int duration, int flags);
423 bool SetOutputScaling_w(uint32 ssrc, double left, double right);
424 bool GetStats_w(VoiceMediaInfo* stats);
425
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000426 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000427 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
428 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000429 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000430 virtual void OnMediaMonitorUpdate(
431 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info);
432 void OnAudioMonitorUpdate(AudioMonitor* monitor, const AudioInfo& info);
433 void OnVoiceChannelError(uint32 ssrc, VoiceMediaChannel::Error error);
434 void SendLastMediaError();
435 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000436
437 static const int kEarlyMediaTimeout = 1000;
Fredrik Solenberg0c022642015-08-05 12:25:22 +0200438 MediaEngineInterface* media_engine_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000439 bool received_media_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000440 rtc::scoped_ptr<VoiceMediaMonitor> media_monitor_;
441 rtc::scoped_ptr<AudioMonitor> audio_monitor_;
442 rtc::scoped_ptr<TypingMonitor> typing_monitor_;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700443
444 // Last AudioSendParameters sent down to the media_channel() via
445 // SetSendParameters.
446 AudioSendParameters last_send_params_;
447 // Last AudioRecvParameters sent down to the media_channel() via
448 // SetRecvParameters.
449 AudioRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000450};
451
452// VideoChannel is a specialization for video.
453class VideoChannel : public BaseChannel {
454 public:
Fredrik Solenberg0c022642015-08-05 12:25:22 +0200455 VideoChannel(rtc::Thread* thread, VideoMediaChannel* channel,
456 BaseSession* session, const std::string& content_name,
457 bool rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000458 ~VideoChannel();
459 bool Init();
460
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200461 // downcasts a MediaChannel
462 virtual VideoMediaChannel* media_channel() const {
463 return static_cast<VideoMediaChannel*>(BaseChannel::media_channel());
464 }
465
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000466 bool SetRenderer(uint32 ssrc, VideoRenderer* renderer);
467 bool ApplyViewRequest(const ViewRequest& request);
468
469 // TODO(pthatcher): Refactor to use a "capture id" instead of an
470 // ssrc here as the "key".
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000471 // Passes ownership of the capturer to the channel.
472 bool AddScreencast(uint32 ssrc, VideoCapturer* capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000473 bool SetCapturer(uint32 ssrc, VideoCapturer* capturer);
474 bool RemoveScreencast(uint32 ssrc);
475 // True if we've added a screencast. Doesn't matter if the capturer
476 // has been started or not.
477 bool IsScreencasting();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000478 int GetScreencastFps(uint32 ssrc);
479 int GetScreencastMaxPixels(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480 // Get statistics about the current media session.
pbos@webrtc.org058b1f12015-03-04 08:54:32 +0000481 bool GetStats(VideoMediaInfo* stats);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000482
483 sigslot::signal2<VideoChannel*, const std::vector<ConnectionInfo>&>
484 SignalConnectionMonitor;
485
486 void StartMediaMonitor(int cms);
487 void StopMediaMonitor();
488 sigslot::signal2<VideoChannel*, const VideoMediaInfo&> SignalMediaMonitor;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000489 sigslot::signal2<uint32, rtc::WindowEvent> SignalScreencastWindowEvent;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490
491 bool SendIntraFrame();
492 bool RequestIntraFrame();
493 sigslot::signal3<VideoChannel*, uint32, VideoMediaChannel::Error>
494 SignalMediaError;
495
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000496 // Configuration and setting.
497 bool SetChannelOptions(const VideoOptions& options);
498
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499 private:
500 typedef std::map<uint32, VideoCapturer*> ScreencastMap;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000501 struct ScreencastDetailsData;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502
503 // overrides from BaseChannel
504 virtual void ChangeState();
505 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
506 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000507 ContentAction action,
508 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000509 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000510 ContentAction action,
511 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000512 bool ApplyViewRequest_w(const ViewRequest& request);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000513
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000514 bool AddScreencast_w(uint32 ssrc, VideoCapturer* capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000515 bool RemoveScreencast_w(uint32 ssrc);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000516 void OnScreencastWindowEvent_s(uint32 ssrc, rtc::WindowEvent we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000517 bool IsScreencasting_w() const;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000518 void GetScreencastDetails_w(ScreencastDetailsData* d) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000519 bool GetStats_w(VideoMediaInfo* stats);
520
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000521 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000522 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
523 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000524 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000525 virtual void OnMediaMonitorUpdate(
526 VideoMediaChannel* media_channel, const VideoMediaInfo& info);
527 virtual void OnScreencastWindowEvent(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000528 rtc::WindowEvent event);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529 virtual void OnStateChange(VideoCapturer* capturer, CaptureState ev);
530 bool GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc);
531
532 void OnVideoChannelError(uint32 ssrc, VideoMediaChannel::Error error);
533 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000534
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535 VideoRenderer* renderer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000536 ScreencastMap screencast_capturers_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000537 rtc::scoped_ptr<VideoMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000538
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000539 rtc::WindowEvent previous_we_;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700540
541 // Last VideoSendParameters sent down to the media_channel() via
542 // SetSendParameters.
543 VideoSendParameters last_send_params_;
544 // Last VideoRecvParameters sent down to the media_channel() via
545 // SetRecvParameters.
546 VideoRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000547};
548
549// DataChannel is a specialization for data.
550class DataChannel : public BaseChannel {
551 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000552 DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553 DataMediaChannel* media_channel,
554 BaseSession* session,
555 const std::string& content_name,
556 bool rtcp);
557 ~DataChannel();
558 bool Init();
559
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000560 virtual bool SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000561 const rtc::Buffer& payload,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000562 SendDataResult* result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000563
564 void StartMediaMonitor(int cms);
565 void StopMediaMonitor();
566
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000567 // Should be called on the signaling thread only.
568 bool ready_to_send_data() const {
569 return ready_to_send_data_;
570 }
571
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000572 sigslot::signal2<DataChannel*, const DataMediaInfo&> SignalMediaMonitor;
573 sigslot::signal2<DataChannel*, const std::vector<ConnectionInfo>&>
574 SignalConnectionMonitor;
575 sigslot::signal3<DataChannel*, uint32, DataMediaChannel::Error>
576 SignalMediaError;
577 sigslot::signal3<DataChannel*,
578 const ReceiveDataParams&,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000579 const rtc::Buffer&>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000580 SignalDataReceived;
581 // Signal for notifying when the channel becomes ready to send data.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000582 // That occurs when the channel is enabled, the transport is writable,
583 // both local and remote descriptions are set, and the channel is unblocked.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000584 sigslot::signal1<bool> SignalReadyToSendData;
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +0000585 // Signal for notifying that the remote side has closed the DataChannel.
586 sigslot::signal1<uint32> SignalStreamClosedRemotely;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000587
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000588 protected:
589 // downcasts a MediaChannel.
590 virtual DataMediaChannel* media_channel() const {
591 return static_cast<DataMediaChannel*>(BaseChannel::media_channel());
592 }
593
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000594 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000595 struct SendDataMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000596 SendDataMessageData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000597 const rtc::Buffer* payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000598 SendDataResult* result)
599 : params(params),
600 payload(payload),
601 result(result),
602 succeeded(false) {
603 }
604
605 const SendDataParams& params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000606 const rtc::Buffer* payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000607 SendDataResult* result;
608 bool succeeded;
609 };
610
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000611 struct DataReceivedMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000612 // We copy the data because the data will become invalid after we
613 // handle DataMediaChannel::SignalDataReceived but before we fire
614 // SignalDataReceived.
615 DataReceivedMessageData(
616 const ReceiveDataParams& params, const char* data, size_t len)
617 : params(params),
618 payload(data, len) {
619 }
620 const ReceiveDataParams params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000621 const rtc::Buffer payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000622 };
623
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000624 typedef rtc::TypedMessageData<bool> DataChannelReadyToSendMessageData;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000625
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626 // overrides from BaseChannel
627 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
628 // If data_channel_type_ is DCT_NONE, set it. Otherwise, check that
629 // it's the same as what was set previously. Returns false if it's
630 // set to one type one type and changed to another type later.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000631 bool SetDataChannelType(DataChannelType new_data_channel_type,
632 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000633 // Same as SetDataChannelType, but extracts the type from the
634 // DataContentDescription.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000635 bool SetDataChannelTypeFromContent(const DataContentDescription* content,
636 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000638 ContentAction action,
639 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000640 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000641 ContentAction action,
642 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000643 virtual void ChangeState();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000644 virtual bool WantsPacket(bool rtcp, rtc::Buffer* packet);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000645
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000646 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000647 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
648 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000649 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000650 virtual void OnMediaMonitorUpdate(
651 DataMediaChannel* media_channel, const DataMediaInfo& info);
652 virtual bool ShouldSetupDtlsSrtp() const;
653 void OnDataReceived(
654 const ReceiveDataParams& params, const char* data, size_t len);
655 void OnDataChannelError(uint32 ssrc, DataMediaChannel::Error error);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000656 void OnDataChannelReadyToSend(bool writable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000657 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +0000658 void OnStreamClosedRemotely(uint32 sid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000659
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000660 rtc::scoped_ptr<DataMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000661 // TODO(pthatcher): Make a separate SctpDataChannel and
662 // RtpDataChannel instead of using this.
663 DataChannelType data_channel_type_;
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000664 bool ready_to_send_data_;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700665
666 // Last DataSendParameters sent down to the media_channel() via
667 // SetSendParameters.
668 DataSendParameters last_send_params_;
669 // Last DataRecvParameters sent down to the media_channel() via
670 // SetRecvParameters.
671 DataRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000672};
673
674} // namespace cricket
675
676#endif // TALK_SESSION_MEDIA_CHANNEL_H_