blob: ec35f4cdb8c4c18f25d5da716e7a218beb6668b4 [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"
torbjornga81a42f2015-09-23 02:16:58 -070038#include "webrtc/p2p/base/session.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000039#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;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056struct ViewRequest;
57
58enum SinkType {
59 SINK_PRE_CRYPTO, // Sink packets before encryption or after decryption.
60 SINK_POST_CRYPTO // Sink packets after encryption or before decryption.
61};
62
63// BaseChannel contains logic common to voice and video, including
solenberg1dd98f32015-09-10 01:57:14 -070064// enable, marshaling calls to a worker thread, and
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065// connection and media monitors.
wu@webrtc.org78187522013-10-07 23:32:02 +000066//
67// WARNING! SUBCLASSES MUST CALL Deinit() IN THEIR DESTRUCTORS!
68// This is required to avoid a data race between the destructor modifying the
69// vtable, and the media channel's thread using BaseChannel as the
70// NetworkInterface.
71
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072class BaseChannel
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000073 : public rtc::MessageHandler, public sigslot::has_slots<>,
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +000074 public MediaChannel::NetworkInterface,
75 public ConnectionStatsGetter {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076 public:
torbjornga81a42f2015-09-23 02:16:58 -070077 BaseChannel(rtc::Thread* thread, MediaChannel* channel, BaseSession* session,
78 const std::string& content_name, bool rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079 virtual ~BaseChannel();
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +000080 bool Init();
wu@webrtc.org78187522013-10-07 23:32:02 +000081 // Deinit may be called multiple times and is simply ignored if it's alreay
82 // done.
83 void Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000085 rtc::Thread* worker_thread() const { return worker_thread_; }
torbjornga81a42f2015-09-23 02:16:58 -070086 BaseSession* session() const { return session_; }
87 const std::string& content_name() { return content_name_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088 TransportChannel* transport_channel() const {
89 return transport_channel_;
90 }
91 TransportChannel* rtcp_transport_channel() const {
92 return rtcp_transport_channel_;
93 }
94 bool enabled() const { return enabled_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095
96 // This function returns true if we are using SRTP.
97 bool secure() const { return srtp_filter_.IsActive(); }
98 // The following function returns true if we are using
99 // DTLS-based keying. If you turned off SRTP later, however
100 // you could have secure() == false and dtls_secure() == true.
101 bool secure_dtls() const { return dtls_keyed_; }
102 // This function returns true if we require secure channel for call setup.
103 bool secure_required() const { return secure_required_; }
104
105 bool writable() const { return writable_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700107 // Activate RTCP mux, regardless of the state so far. Once
108 // activated, it can not be deactivated, and if the remote
109 // description doesn't support RTCP mux, setting the remote
110 // description will fail.
111 void ActivateRtcpMux();
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000112 bool PushdownLocalDescription(const SessionDescription* local_desc,
113 ContentAction action,
114 std::string* error_desc);
115 bool PushdownRemoteDescription(const SessionDescription* remote_desc,
116 ContentAction action,
117 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118 // Channel control
119 bool SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000120 ContentAction action,
121 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122 bool SetRemoteContent(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
126 bool Enable(bool enable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127
128 // Multiplexing
129 bool AddRecvStream(const StreamParams& sp);
130 bool RemoveRecvStream(uint32 ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000131 bool AddSendStream(const StreamParams& sp);
132 bool RemoveSendStream(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133
134 // Monitoring
135 void StartConnectionMonitor(int cms);
136 void StopConnectionMonitor();
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000137 // For ConnectionStatsGetter, used by ConnectionMonitor
torbjornga81a42f2015-09-23 02:16:58 -0700138 virtual bool GetConnectionStats(ConnectionInfos* infos) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139
140 void set_srtp_signal_silent_time(uint32 silent_time) {
141 srtp_filter_.set_signal_silent_time(silent_time);
142 }
143
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000144 BundleFilter* bundle_filter() { return &bundle_filter_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000145
146 const std::vector<StreamParams>& local_streams() const {
147 return local_streams_;
148 }
149 const std::vector<StreamParams>& remote_streams() const {
150 return remote_streams_;
151 }
152
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000153 sigslot::signal2<BaseChannel*, bool> SignalDtlsSetupFailure;
154 void SignalDtlsSetupFailure_w(bool rtcp);
155 void SignalDtlsSetupFailure_s(bool rtcp);
156
buildbot@webrtc.org6bfd6192014-05-15 16:15:59 +0000157 // Used for latency measurements.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158 sigslot::signal1<BaseChannel*> SignalFirstPacketReceived;
159
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160 // Made public for easier testing.
torbjornga81a42f2015-09-23 02:16:58 -0700161 void SetReadyToSend(TransportChannel* channel, bool ready);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162
guoweis@webrtc.org4f852882015-03-12 20:09:44 +0000163 // Only public for unit tests. Otherwise, consider protected.
164 virtual int SetOption(SocketType type, rtc::Socket::Option o, int val);
165
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166 protected:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167 virtual MediaChannel* media_channel() const { return media_channel_; }
torbjornga81a42f2015-09-23 02:16:58 -0700168 // Sets the transport_channel_ and rtcp_transport_channel_. If
169 // |rtcp| is false, set rtcp_transport_channel_ is set to NULL. Get
170 // the transport channels from |session|.
171 // TODO(pthatcher): Pass in a Transport instead of a BaseSession.
172 bool SetTransportChannels(BaseSession* session, bool rtcp);
173 bool SetTransportChannels_w(BaseSession* session, bool rtcp);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000174 void set_transport_channel(TransportChannel* transport);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 void set_rtcp_transport_channel(TransportChannel* transport);
176 bool was_ever_writable() const { return was_ever_writable_; }
177 void set_local_content_direction(MediaContentDirection direction) {
178 local_content_direction_ = direction;
179 }
180 void set_remote_content_direction(MediaContentDirection direction) {
181 remote_content_direction_ = direction;
182 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700183 void set_secure_required(bool secure_required) {
184 secure_required_ = secure_required;
185 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186 bool IsReadyToReceive() const;
187 bool IsReadyToSend() const;
torbjornga81a42f2015-09-23 02:16:58 -0700188 rtc::Thread* signaling_thread() { return session_->signaling_thread(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189 SrtpFilter* srtp_filter() { return &srtp_filter_; }
torbjornga81a42f2015-09-23 02:16:58 -0700190 bool rtcp() const { return rtcp_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000192 void ConnectToTransportChannel(TransportChannel* tc);
193 void DisconnectFromTransportChannel(TransportChannel* tc);
194
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195 void FlushRtcpMessages();
196
197 // NetworkInterface implementation, called by MediaEngine
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000198 virtual bool SendPacket(rtc::Buffer* packet,
199 rtc::DiffServCodePoint dscp);
200 virtual bool SendRtcp(rtc::Buffer* packet,
201 rtc::DiffServCodePoint dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000202
203 // From TransportChannel
204 void OnWritableState(TransportChannel* channel);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000205 virtual void OnChannelRead(TransportChannel* channel,
206 const char* data,
207 size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000208 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000209 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210 void OnReadyToSend(TransportChannel* channel);
211
212 bool PacketIsRtcp(const TransportChannel* channel, const char* data,
213 size_t len);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000214 bool SendPacket(bool rtcp, rtc::Buffer* packet,
215 rtc::DiffServCodePoint dscp);
216 virtual bool WantsPacket(bool rtcp, rtc::Buffer* packet);
217 void HandlePacket(bool rtcp, rtc::Buffer* packet,
218 const rtc::PacketTime& packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000219
torbjornga81a42f2015-09-23 02:16:58 -0700220 // Apply the new local/remote session description.
221 void OnNewLocalDescription(BaseSession* session, ContentAction action);
222 void OnNewRemoteDescription(BaseSession* session, ContentAction action);
223
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000224 void EnableMedia_w();
225 void DisableMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000226 void ChannelWritable_w();
227 void ChannelNotWritable_w();
228 bool AddRecvStream_w(const StreamParams& sp);
229 bool RemoveRecvStream_w(uint32 ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000230 bool AddSendStream_w(const StreamParams& sp);
231 bool RemoveSendStream_w(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232 virtual bool ShouldSetupDtlsSrtp() const;
233 // Do the DTLS key expansion and impose it on the SRTP/SRTCP filters.
234 // |rtcp_channel| indicates whether to set up the RTP or RTCP filter.
235 bool SetupDtlsSrtp(bool rtcp_channel);
236 // Set the DTLS-SRTP cipher policy on this channel as appropriate.
237 bool SetDtlsSrtpCiphers(TransportChannel *tc, bool rtcp);
238
239 virtual void ChangeState() = 0;
240
241 // Gets the content info appropriate to the channel (audio or video).
242 virtual const ContentInfo* GetFirstContent(
243 const SessionDescription* sdesc) = 0;
244 bool UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000245 ContentAction action,
246 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000247 bool UpdateRemoteStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000248 ContentAction action,
249 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000250 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000251 ContentAction action,
252 std::string* error_desc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000253 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000254 ContentAction action,
255 std::string* error_desc) = 0;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700256 bool SetRtpTransportParameters_w(const MediaContentDescription* content,
257 ContentAction action,
258 ContentSource src,
259 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000260
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000261 // Helper method to get RTP Absoulute SendTime extension header id if
262 // present in remote supported extensions list.
263 void MaybeCacheRtpAbsSendTimeHeaderExtension(
264 const std::vector<RtpHeaderExtension>& extensions);
265
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000266 bool CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
267 bool* dtls,
268 std::string* error_desc);
269 bool SetSrtp_w(const std::vector<CryptoParams>& params,
270 ContentAction action,
271 ContentSource src,
272 std::string* error_desc);
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700273 void ActivateRtcpMux_w();
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000274 bool SetRtcpMux_w(bool enable,
275 ContentAction action,
276 ContentSource src,
277 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000278
279 // From MessageHandler
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000280 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281
282 // Handled in derived classes
283 // Get the SRTP ciphers to use for RTP media
284 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const = 0;
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000285 virtual void OnConnectionMonitorUpdate(ConnectionMonitor* monitor,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000286 const std::vector<ConnectionInfo>& infos) = 0;
287
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000288 // Helper function for invoking bool-returning methods on the worker thread.
289 template <class FunctorT>
290 bool InvokeOnWorker(const FunctorT& functor) {
291 return worker_thread_->Invoke<bool>(functor);
292 }
293
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000294 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000295 rtc::Thread* worker_thread_;
torbjornga81a42f2015-09-23 02:16:58 -0700296 BaseSession* session_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000297 MediaChannel* media_channel_;
298 std::vector<StreamParams> local_streams_;
299 std::vector<StreamParams> remote_streams_;
300
pthatcher@webrtc.org990a00c2015-03-13 18:20:33 +0000301 const std::string content_name_;
torbjornga81a42f2015-09-23 02:16:58 -0700302 bool rtcp_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303 TransportChannel* transport_channel_;
304 TransportChannel* rtcp_transport_channel_;
305 SrtpFilter srtp_filter_;
306 RtcpMuxFilter rtcp_mux_filter_;
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000307 BundleFilter bundle_filter_;
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000308 rtc::scoped_ptr<ConnectionMonitor> connection_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000309 bool enabled_;
310 bool writable_;
311 bool rtp_ready_to_send_;
312 bool rtcp_ready_to_send_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000313 bool was_ever_writable_;
314 MediaContentDirection local_content_direction_;
315 MediaContentDirection remote_content_direction_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000316 bool has_received_packet_;
317 bool dtls_keyed_;
318 bool secure_required_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000319 int rtp_abs_sendtime_extn_id_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000320};
321
322// VoiceChannel is a specialization that adds support for early media, DTMF,
323// and input/output level monitoring.
324class VoiceChannel : public BaseChannel {
325 public:
torbjornga81a42f2015-09-23 02:16:58 -0700326 VoiceChannel(rtc::Thread* thread, MediaEngineInterface* media_engine,
327 VoiceMediaChannel* channel, BaseSession* session,
328 const std::string& content_name, bool rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329 ~VoiceChannel();
330 bool Init();
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000331 bool SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer);
solenberg1dd98f32015-09-10 01:57:14 -0700332
333 // Configure sending media on the stream with SSRC |ssrc|
334 // If there is only one sending stream SSRC 0 can be used.
torbjornga81a42f2015-09-23 02:16:58 -0700335 bool SetAudioSend(uint32 ssrc, bool mute, const AudioOptions* options,
solenberg1dd98f32015-09-10 01:57:14 -0700336 AudioRenderer* renderer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000337
338 // downcasts a MediaChannel
339 virtual VoiceMediaChannel* media_channel() const {
340 return static_cast<VoiceMediaChannel*>(BaseChannel::media_channel());
341 }
342
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000343 void SetEarlyMedia(bool enable);
344 // This signal is emitted when we have gone a period of time without
345 // receiving early media. When received, a UI should start playing its
346 // own ringing sound
347 sigslot::signal1<VoiceChannel*> SignalEarlyMediaTimeout;
348
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000349 // TODO(ronghuawu): Replace PressDTMF with InsertDtmf.
350 bool PressDTMF(int digit, bool playout);
351 // Returns if the telephone-event has been negotiated.
352 bool CanInsertDtmf();
353 // Send and/or play a DTMF |event| according to the |flags|.
354 // The DTMF out-of-band signal will be used on sending.
355 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000356 // The valid value for the |event| are 0 which corresponding to DTMF
357 // event 0-9, *, #, A-D.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000358 bool InsertDtmf(uint32 ssrc, int event_code, int duration, int flags);
359 bool SetOutputScaling(uint32 ssrc, double left, double right);
360 // Get statistics about the current media session.
361 bool GetStats(VoiceMediaInfo* stats);
362
363 // Monitoring functions
364 sigslot::signal2<VoiceChannel*, const std::vector<ConnectionInfo>&>
365 SignalConnectionMonitor;
366
367 void StartMediaMonitor(int cms);
368 void StopMediaMonitor();
369 sigslot::signal2<VoiceChannel*, const VoiceMediaInfo&> SignalMediaMonitor;
370
371 void StartAudioMonitor(int cms);
372 void StopAudioMonitor();
373 bool IsAudioMonitorRunning() const;
374 sigslot::signal2<VoiceChannel*, const AudioInfo&> SignalAudioMonitor;
375
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000376 int GetInputLevel_w();
377 int GetOutputLevel_w();
378 void GetActiveStreams_w(AudioInfo::StreamList* actives);
379
380 // Signal errors from VoiceMediaChannel. Arguments are:
381 // ssrc(uint32), and error(VoiceMediaChannel::Error).
382 sigslot::signal3<VoiceChannel*, uint32, VoiceMediaChannel::Error>
383 SignalMediaError;
384
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000385 private:
386 // overrides from BaseChannel
387 virtual void OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000388 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000389 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000390 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000391 virtual void ChangeState();
392 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
393 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000394 ContentAction action,
395 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000396 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000397 ContentAction action,
398 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000399 void HandleEarlyMediaTimeout();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000400 bool InsertDtmf_w(uint32 ssrc, int event, int duration, int flags);
401 bool SetOutputScaling_w(uint32 ssrc, double left, double right);
402 bool GetStats_w(VoiceMediaInfo* stats);
403
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000404 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000405 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
406 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000407 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000408 virtual void OnMediaMonitorUpdate(
409 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info);
410 void OnAudioMonitorUpdate(AudioMonitor* monitor, const AudioInfo& info);
411 void OnVoiceChannelError(uint32 ssrc, VoiceMediaChannel::Error error);
412 void SendLastMediaError();
413 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000414
415 static const int kEarlyMediaTimeout = 1000;
Fredrik Solenberg0c022642015-08-05 12:25:22 +0200416 MediaEngineInterface* media_engine_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000417 bool received_media_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000418 rtc::scoped_ptr<VoiceMediaMonitor> media_monitor_;
419 rtc::scoped_ptr<AudioMonitor> audio_monitor_;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700420
421 // Last AudioSendParameters sent down to the media_channel() via
422 // SetSendParameters.
423 AudioSendParameters last_send_params_;
424 // Last AudioRecvParameters sent down to the media_channel() via
425 // SetRecvParameters.
426 AudioRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000427};
428
429// VideoChannel is a specialization for video.
430class VideoChannel : public BaseChannel {
431 public:
torbjornga81a42f2015-09-23 02:16:58 -0700432 VideoChannel(rtc::Thread* thread, VideoMediaChannel* channel,
433 BaseSession* session, const std::string& content_name,
Fredrik Solenberg0c022642015-08-05 12:25:22 +0200434 bool rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000435 ~VideoChannel();
436 bool Init();
437
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200438 // downcasts a MediaChannel
439 virtual VideoMediaChannel* media_channel() const {
440 return static_cast<VideoMediaChannel*>(BaseChannel::media_channel());
441 }
442
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000443 bool SetRenderer(uint32 ssrc, VideoRenderer* renderer);
444 bool ApplyViewRequest(const ViewRequest& request);
445
446 // TODO(pthatcher): Refactor to use a "capture id" instead of an
447 // ssrc here as the "key".
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000448 // Passes ownership of the capturer to the channel.
449 bool AddScreencast(uint32 ssrc, VideoCapturer* capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000450 bool SetCapturer(uint32 ssrc, VideoCapturer* capturer);
451 bool RemoveScreencast(uint32 ssrc);
452 // True if we've added a screencast. Doesn't matter if the capturer
453 // has been started or not.
454 bool IsScreencasting();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000455 int GetScreencastFps(uint32 ssrc);
456 int GetScreencastMaxPixels(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000457 // Get statistics about the current media session.
pbos@webrtc.org058b1f12015-03-04 08:54:32 +0000458 bool GetStats(VideoMediaInfo* stats);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000459
460 sigslot::signal2<VideoChannel*, const std::vector<ConnectionInfo>&>
461 SignalConnectionMonitor;
462
463 void StartMediaMonitor(int cms);
464 void StopMediaMonitor();
465 sigslot::signal2<VideoChannel*, const VideoMediaInfo&> SignalMediaMonitor;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000466 sigslot::signal2<uint32, rtc::WindowEvent> SignalScreencastWindowEvent;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000467
468 bool SendIntraFrame();
469 bool RequestIntraFrame();
470 sigslot::signal3<VideoChannel*, uint32, VideoMediaChannel::Error>
471 SignalMediaError;
472
solenberg1dd98f32015-09-10 01:57:14 -0700473 // Configure sending media on the stream with SSRC |ssrc|
474 // If there is only one sending stream SSRC 0 can be used.
475 bool SetVideoSend(uint32 ssrc, bool mute, const VideoOptions* options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000476
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477 private:
478 typedef std::map<uint32, VideoCapturer*> ScreencastMap;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000479 struct ScreencastDetailsData;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480
481 // overrides from BaseChannel
482 virtual void ChangeState();
483 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
484 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000485 ContentAction action,
486 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000487 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000488 ContentAction action,
489 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490 bool ApplyViewRequest_w(const ViewRequest& request);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000492 bool AddScreencast_w(uint32 ssrc, VideoCapturer* capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000493 bool RemoveScreencast_w(uint32 ssrc);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000494 void OnScreencastWindowEvent_s(uint32 ssrc, rtc::WindowEvent we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000495 bool IsScreencasting_w() const;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000496 void GetScreencastDetails_w(ScreencastDetailsData* d) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497 bool GetStats_w(VideoMediaInfo* stats);
498
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000499 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000500 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
501 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000502 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000503 virtual void OnMediaMonitorUpdate(
504 VideoMediaChannel* media_channel, const VideoMediaInfo& info);
505 virtual void OnScreencastWindowEvent(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000506 rtc::WindowEvent event);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000507 virtual void OnStateChange(VideoCapturer* capturer, CaptureState ev);
508 bool GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc);
509
510 void OnVideoChannelError(uint32 ssrc, VideoMediaChannel::Error error);
511 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000512
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000513 VideoRenderer* renderer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000514 ScreencastMap screencast_capturers_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000515 rtc::scoped_ptr<VideoMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000516
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000517 rtc::WindowEvent previous_we_;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700518
519 // Last VideoSendParameters sent down to the media_channel() via
520 // SetSendParameters.
521 VideoSendParameters last_send_params_;
522 // Last VideoRecvParameters sent down to the media_channel() via
523 // SetRecvParameters.
524 VideoRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000525};
526
527// DataChannel is a specialization for data.
528class DataChannel : public BaseChannel {
529 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000530 DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531 DataMediaChannel* media_channel,
torbjornga81a42f2015-09-23 02:16:58 -0700532 BaseSession* session,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000533 const std::string& content_name,
534 bool rtcp);
535 ~DataChannel();
536 bool Init();
537
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000538 virtual bool SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000539 const rtc::Buffer& payload,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000540 SendDataResult* result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000541
542 void StartMediaMonitor(int cms);
543 void StopMediaMonitor();
544
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000545 // Should be called on the signaling thread only.
546 bool ready_to_send_data() const {
547 return ready_to_send_data_;
548 }
549
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000550 sigslot::signal2<DataChannel*, const DataMediaInfo&> SignalMediaMonitor;
551 sigslot::signal2<DataChannel*, const std::vector<ConnectionInfo>&>
552 SignalConnectionMonitor;
553 sigslot::signal3<DataChannel*, uint32, DataMediaChannel::Error>
554 SignalMediaError;
555 sigslot::signal3<DataChannel*,
556 const ReceiveDataParams&,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000557 const rtc::Buffer&>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000558 SignalDataReceived;
559 // Signal for notifying when the channel becomes ready to send data.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000560 // That occurs when the channel is enabled, the transport is writable,
561 // both local and remote descriptions are set, and the channel is unblocked.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000562 sigslot::signal1<bool> SignalReadyToSendData;
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +0000563 // Signal for notifying that the remote side has closed the DataChannel.
564 sigslot::signal1<uint32> SignalStreamClosedRemotely;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000565
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000566 protected:
567 // downcasts a MediaChannel.
568 virtual DataMediaChannel* media_channel() const {
569 return static_cast<DataMediaChannel*>(BaseChannel::media_channel());
570 }
571
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000572 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000573 struct SendDataMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000574 SendDataMessageData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000575 const rtc::Buffer* payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000576 SendDataResult* result)
577 : params(params),
578 payload(payload),
579 result(result),
580 succeeded(false) {
581 }
582
583 const SendDataParams& params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000584 const rtc::Buffer* payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000585 SendDataResult* result;
586 bool succeeded;
587 };
588
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000589 struct DataReceivedMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000590 // We copy the data because the data will become invalid after we
591 // handle DataMediaChannel::SignalDataReceived but before we fire
592 // SignalDataReceived.
593 DataReceivedMessageData(
594 const ReceiveDataParams& params, const char* data, size_t len)
595 : params(params),
596 payload(data, len) {
597 }
598 const ReceiveDataParams params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000599 const rtc::Buffer payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000600 };
601
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000602 typedef rtc::TypedMessageData<bool> DataChannelReadyToSendMessageData;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000603
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000604 // overrides from BaseChannel
605 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
606 // If data_channel_type_ is DCT_NONE, set it. Otherwise, check that
607 // it's the same as what was set previously. Returns false if it's
608 // set to one type one type and changed to another type later.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000609 bool SetDataChannelType(DataChannelType new_data_channel_type,
610 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000611 // Same as SetDataChannelType, but extracts the type from the
612 // DataContentDescription.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000613 bool SetDataChannelTypeFromContent(const DataContentDescription* content,
614 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000615 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000616 ContentAction action,
617 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000618 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000619 ContentAction action,
620 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000621 virtual void ChangeState();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000622 virtual bool WantsPacket(bool rtcp, rtc::Buffer* packet);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000623
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000624 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000625 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
626 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000627 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000628 virtual void OnMediaMonitorUpdate(
629 DataMediaChannel* media_channel, const DataMediaInfo& info);
630 virtual bool ShouldSetupDtlsSrtp() const;
631 void OnDataReceived(
632 const ReceiveDataParams& params, const char* data, size_t len);
633 void OnDataChannelError(uint32 ssrc, DataMediaChannel::Error error);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000634 void OnDataChannelReadyToSend(bool writable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000635 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +0000636 void OnStreamClosedRemotely(uint32 sid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000638 rtc::scoped_ptr<DataMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639 // TODO(pthatcher): Make a separate SctpDataChannel and
640 // RtpDataChannel instead of using this.
641 DataChannelType data_channel_type_;
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000642 bool ready_to_send_data_;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700643
644 // Last DataSendParameters sent down to the media_channel() via
645 // SetSendParameters.
646 DataSendParameters last_send_params_;
647 // Last DataRecvParameters sent down to the media_channel() via
648 // SetRecvParameters.
649 DataRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000650};
651
652} // namespace cricket
653
654#endif // TALK_SESSION_MEDIA_CHANNEL_H_