blob: de66e9cd857d9d4a158460f114456f0a8b586e99 [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>
deadbeefcbecd352015-09-23 11:50:27 -070033#include <map>
34#include <set>
35#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037#include "talk/media/base/mediachannel.h"
38#include "talk/media/base/mediaengine.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039#include "talk/media/base/streamparams.h"
40#include "talk/media/base/videocapturer.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041#include "talk/session/media/audiomonitor.h"
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +000042#include "talk/session/media/bundlefilter.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043#include "talk/session/media/mediamonitor.h"
44#include "talk/session/media/mediasession.h"
45#include "talk/session/media/rtcpmuxfilter.h"
46#include "talk/session/media/srtpfilter.h"
Tommif888bb52015-12-12 01:37:01 +010047#include "webrtc/audio/audio_sink.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000048#include "webrtc/base/asyncudpsocket.h"
49#include "webrtc/base/criticalsection.h"
50#include "webrtc/base/network.h"
51#include "webrtc/base/sigslot.h"
52#include "webrtc/base/window.h"
Tommif888bb52015-12-12 01:37:01 +010053#include "webrtc/p2p/base/transportcontroller.h"
54#include "webrtc/p2p/client/socketmonitor.h"
55
56namespace webrtc {
57class AudioSinkInterface;
58} // namespace webrtc
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059
60namespace cricket {
61
62struct CryptoParams;
63class MediaContentDescription;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064
65enum SinkType {
66 SINK_PRE_CRYPTO, // Sink packets before encryption or after decryption.
67 SINK_POST_CRYPTO // Sink packets after encryption or before decryption.
68};
69
70// BaseChannel contains logic common to voice and video, including
solenberg1dd98f32015-09-10 01:57:14 -070071// enable, marshaling calls to a worker thread, and
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072// connection and media monitors.
wu@webrtc.org78187522013-10-07 23:32:02 +000073//
74// WARNING! SUBCLASSES MUST CALL Deinit() IN THEIR DESTRUCTORS!
75// This is required to avoid a data race between the destructor modifying the
76// vtable, and the media channel's thread using BaseChannel as the
77// NetworkInterface.
78
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079class BaseChannel
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000080 : public rtc::MessageHandler, public sigslot::has_slots<>,
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +000081 public MediaChannel::NetworkInterface,
82 public ConnectionStatsGetter {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083 public:
deadbeefcbecd352015-09-23 11:50:27 -070084 BaseChannel(rtc::Thread* thread,
85 MediaChannel* channel,
86 TransportController* transport_controller,
87 const std::string& content_name,
88 bool rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 virtual ~BaseChannel();
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +000090 bool Init();
wu@webrtc.org78187522013-10-07 23:32:02 +000091 // Deinit may be called multiple times and is simply ignored if it's alreay
92 // done.
93 void Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000095 rtc::Thread* worker_thread() const { return worker_thread_; }
deadbeefcbecd352015-09-23 11:50:27 -070096 const std::string& content_name() const { return content_name_; }
97 const std::string& transport_name() const { return transport_name_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098 TransportChannel* transport_channel() const {
99 return transport_channel_;
100 }
101 TransportChannel* rtcp_transport_channel() const {
102 return rtcp_transport_channel_;
103 }
104 bool enabled() const { return enabled_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105
106 // This function returns true if we are using SRTP.
107 bool secure() const { return srtp_filter_.IsActive(); }
108 // The following function returns true if we are using
109 // DTLS-based keying. If you turned off SRTP later, however
110 // you could have secure() == false and dtls_secure() == true.
111 bool secure_dtls() const { return dtls_keyed_; }
112 // This function returns true if we require secure channel for call setup.
113 bool secure_required() const { return secure_required_; }
114
115 bool writable() const { return writable_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700117 // Activate RTCP mux, regardless of the state so far. Once
118 // activated, it can not be deactivated, and if the remote
119 // description doesn't support RTCP mux, setting the remote
120 // description will fail.
121 void ActivateRtcpMux();
deadbeefcbecd352015-09-23 11:50:27 -0700122 bool SetTransport(const std::string& transport_name);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000123 bool PushdownLocalDescription(const SessionDescription* local_desc,
124 ContentAction action,
125 std::string* error_desc);
126 bool PushdownRemoteDescription(const SessionDescription* remote_desc,
127 ContentAction action,
128 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129 // Channel control
130 bool SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000131 ContentAction action,
132 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133 bool SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000134 ContentAction action,
135 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136
137 bool Enable(bool enable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138
139 // Multiplexing
140 bool AddRecvStream(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200141 bool RemoveRecvStream(uint32_t ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000142 bool AddSendStream(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200143 bool RemoveSendStream(uint32_t ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144
145 // Monitoring
146 void StartConnectionMonitor(int cms);
147 void StopConnectionMonitor();
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000148 // For ConnectionStatsGetter, used by ConnectionMonitor
deadbeefcbecd352015-09-23 11:50:27 -0700149 bool GetConnectionStats(ConnectionInfos* infos) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000151 BundleFilter* bundle_filter() { return &bundle_filter_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152
153 const std::vector<StreamParams>& local_streams() const {
154 return local_streams_;
155 }
156 const std::vector<StreamParams>& remote_streams() const {
157 return remote_streams_;
158 }
159
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000160 sigslot::signal2<BaseChannel*, bool> SignalDtlsSetupFailure;
161 void SignalDtlsSetupFailure_w(bool rtcp);
162 void SignalDtlsSetupFailure_s(bool rtcp);
163
buildbot@webrtc.org6bfd6192014-05-15 16:15:59 +0000164 // Used for latency measurements.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165 sigslot::signal1<BaseChannel*> SignalFirstPacketReceived;
166
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167 // Made public for easier testing.
deadbeefcbecd352015-09-23 11:50:27 -0700168 void SetReadyToSend(bool rtcp, bool ready);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169
guoweis@webrtc.org4f852882015-03-12 20:09:44 +0000170 // Only public for unit tests. Otherwise, consider protected.
rlesterec9d1872015-10-27 14:22:16 -0700171 int SetOption(SocketType type, rtc::Socket::Option o, int val)
172 override;
guoweis@webrtc.org4f852882015-03-12 20:09:44 +0000173
solenberg5b14b422015-10-01 04:10:31 -0700174 SrtpFilter* srtp_filter() { return &srtp_filter_; }
175
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176 protected:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177 virtual MediaChannel* media_channel() const { return media_channel_; }
deadbeefcbecd352015-09-23 11:50:27 -0700178 // Sets the |transport_channel_| (and |rtcp_transport_channel_|, if |rtcp_| is
179 // true). Gets the transport channels from |transport_controller_|.
180 bool SetTransport_w(const std::string& transport_name);
guoweis46383312015-12-17 16:45:59 -0800181
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000182 void set_transport_channel(TransportChannel* transport);
guoweis46383312015-12-17 16:45:59 -0800183 void set_rtcp_transport_channel(TransportChannel* transport,
184 bool update_writablity);
185
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186 bool was_ever_writable() const { return was_ever_writable_; }
187 void set_local_content_direction(MediaContentDirection direction) {
188 local_content_direction_ = direction;
189 }
190 void set_remote_content_direction(MediaContentDirection direction) {
191 remote_content_direction_ = direction;
192 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700193 void set_secure_required(bool secure_required) {
194 secure_required_ = secure_required;
195 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000196 bool IsReadyToReceive() const;
197 bool IsReadyToSend() const;
deadbeefcbecd352015-09-23 11:50:27 -0700198 rtc::Thread* signaling_thread() {
199 return transport_controller_->signaling_thread();
200 }
deadbeefcbecd352015-09-23 11:50:27 -0700201 bool rtcp_transport_enabled() const { return rtcp_transport_enabled_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000202
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000203 void ConnectToTransportChannel(TransportChannel* tc);
204 void DisconnectFromTransportChannel(TransportChannel* tc);
205
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206 void FlushRtcpMessages();
207
208 // NetworkInterface implementation, called by MediaEngine
rlesterec9d1872015-10-27 14:22:16 -0700209 bool SendPacket(rtc::Buffer* packet,
210 const rtc::PacketOptions& options) override;
211 bool SendRtcp(rtc::Buffer* packet, const rtc::PacketOptions& options)
212 override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000213
214 // From TransportChannel
215 void OnWritableState(TransportChannel* channel);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000216 virtual void OnChannelRead(TransportChannel* channel,
217 const char* data,
218 size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000219 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000220 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000221 void OnReadyToSend(TransportChannel* channel);
222
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800223 void OnDtlsState(TransportChannel* channel, DtlsTransportState state);
224
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000225 bool PacketIsRtcp(const TransportChannel* channel, const char* data,
226 size_t len);
stefanc1aeaf02015-10-15 07:26:07 -0700227 bool SendPacket(bool rtcp,
228 rtc::Buffer* packet,
229 const rtc::PacketOptions& options);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000230 virtual bool WantsPacket(bool rtcp, rtc::Buffer* packet);
231 void HandlePacket(bool rtcp, rtc::Buffer* packet,
232 const rtc::PacketTime& packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000233
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000234 void EnableMedia_w();
235 void DisableMedia_w();
deadbeefcbecd352015-09-23 11:50:27 -0700236 void UpdateWritableState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000237 void ChannelWritable_w();
238 void ChannelNotWritable_w();
239 bool AddRecvStream_w(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200240 bool RemoveRecvStream_w(uint32_t ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000241 bool AddSendStream_w(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200242 bool RemoveSendStream_w(uint32_t 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);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800247 void MaybeSetupDtlsSrtp_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000248 // Set the DTLS-SRTP cipher policy on this channel as appropriate.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800249 bool SetDtlsSrtpCryptoSuites(TransportChannel* tc, bool rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000250
251 virtual void ChangeState() = 0;
252
253 // Gets the content info appropriate to the channel (audio or video).
254 virtual const ContentInfo* GetFirstContent(
255 const SessionDescription* sdesc) = 0;
256 bool UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000257 ContentAction action,
258 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000259 bool UpdateRemoteStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000260 ContentAction action,
261 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000262 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000263 ContentAction action,
264 std::string* error_desc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000266 ContentAction action,
267 std::string* error_desc) = 0;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700268 bool SetRtpTransportParameters_w(const MediaContentDescription* content,
269 ContentAction action,
270 ContentSource src,
271 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000272
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000273 // Helper method to get RTP Absoulute SendTime extension header id if
274 // present in remote supported extensions list.
275 void MaybeCacheRtpAbsSendTimeHeaderExtension(
stefanc1aeaf02015-10-15 07:26:07 -0700276 const std::vector<RtpHeaderExtension>& extensions);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000277
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000278 bool CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
279 bool* dtls,
280 std::string* error_desc);
281 bool SetSrtp_w(const std::vector<CryptoParams>& params,
282 ContentAction action,
283 ContentSource src,
284 std::string* error_desc);
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700285 void ActivateRtcpMux_w();
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000286 bool SetRtcpMux_w(bool enable,
287 ContentAction action,
288 ContentSource src,
289 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000290
291 // From MessageHandler
rlesterec9d1872015-10-27 14:22:16 -0700292 void OnMessage(rtc::Message* pmsg) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000293
294 // Handled in derived classes
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800295 // Get the SRTP crypto suites to use for RTP media
296 virtual void GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const = 0;
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000297 virtual void OnConnectionMonitorUpdate(ConnectionMonitor* monitor,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000298 const std::vector<ConnectionInfo>& infos) = 0;
299
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000300 // Helper function for invoking bool-returning methods on the worker thread.
301 template <class FunctorT>
302 bool InvokeOnWorker(const FunctorT& functor) {
303 return worker_thread_->Invoke<bool>(functor);
304 }
305
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000306 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000307 rtc::Thread* worker_thread_;
deadbeefcbecd352015-09-23 11:50:27 -0700308 TransportController* transport_controller_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000309 MediaChannel* media_channel_;
310 std::vector<StreamParams> local_streams_;
311 std::vector<StreamParams> remote_streams_;
312
pthatcher@webrtc.org990a00c2015-03-13 18:20:33 +0000313 const std::string content_name_;
deadbeefcbecd352015-09-23 11:50:27 -0700314 std::string transport_name_;
315 bool rtcp_transport_enabled_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000316 TransportChannel* transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700317 std::vector<std::pair<rtc::Socket::Option, int> > socket_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000318 TransportChannel* rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700319 std::vector<std::pair<rtc::Socket::Option, int> > rtcp_socket_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000320 SrtpFilter srtp_filter_;
321 RtcpMuxFilter rtcp_mux_filter_;
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000322 BundleFilter bundle_filter_;
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000323 rtc::scoped_ptr<ConnectionMonitor> connection_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000324 bool enabled_;
325 bool writable_;
326 bool rtp_ready_to_send_;
327 bool rtcp_ready_to_send_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000328 bool was_ever_writable_;
329 MediaContentDirection local_content_direction_;
330 MediaContentDirection remote_content_direction_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000331 bool has_received_packet_;
332 bool dtls_keyed_;
333 bool secure_required_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000334 int rtp_abs_sendtime_extn_id_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000335};
336
337// VoiceChannel is a specialization that adds support for early media, DTMF,
338// and input/output level monitoring.
339class VoiceChannel : public BaseChannel {
340 public:
deadbeefcbecd352015-09-23 11:50:27 -0700341 VoiceChannel(rtc::Thread* thread,
342 MediaEngineInterface* media_engine,
343 VoiceMediaChannel* channel,
344 TransportController* transport_controller,
345 const std::string& content_name,
346 bool rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000347 ~VoiceChannel();
348 bool Init();
solenberg1dd98f32015-09-10 01:57:14 -0700349
350 // Configure sending media on the stream with SSRC |ssrc|
351 // If there is only one sending stream SSRC 0 can be used.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200352 bool SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -0700353 bool enable,
deadbeefcbecd352015-09-23 11:50:27 -0700354 const AudioOptions* options,
solenberg1dd98f32015-09-10 01:57:14 -0700355 AudioRenderer* renderer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000356
357 // downcasts a MediaChannel
358 virtual VoiceMediaChannel* media_channel() const {
359 return static_cast<VoiceMediaChannel*>(BaseChannel::media_channel());
360 }
361
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000362 void SetEarlyMedia(bool enable);
363 // This signal is emitted when we have gone a period of time without
364 // receiving early media. When received, a UI should start playing its
365 // own ringing sound
366 sigslot::signal1<VoiceChannel*> SignalEarlyMediaTimeout;
367
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000368 // Returns if the telephone-event has been negotiated.
369 bool CanInsertDtmf();
370 // Send and/or play a DTMF |event| according to the |flags|.
371 // The DTMF out-of-band signal will be used on sending.
372 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000373 // The valid value for the |event| are 0 which corresponding to DTMF
374 // event 0-9, *, #, A-D.
solenberg1d63dd02015-12-02 12:35:09 -0800375 bool InsertDtmf(uint32_t ssrc, int event_code, int duration);
solenberg4bac9c52015-10-09 02:32:53 -0700376 bool SetOutputVolume(uint32_t ssrc, double volume);
deadbeef2d110be2016-01-13 12:00:26 -0800377 void SetRawAudioSink(uint32_t ssrc,
378 rtc::scoped_ptr<webrtc::AudioSinkInterface> sink);
Tommif888bb52015-12-12 01:37:01 +0100379
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000380 // Get statistics about the current media session.
381 bool GetStats(VoiceMediaInfo* stats);
382
383 // Monitoring functions
384 sigslot::signal2<VoiceChannel*, const std::vector<ConnectionInfo>&>
385 SignalConnectionMonitor;
386
387 void StartMediaMonitor(int cms);
388 void StopMediaMonitor();
389 sigslot::signal2<VoiceChannel*, const VoiceMediaInfo&> SignalMediaMonitor;
390
391 void StartAudioMonitor(int cms);
392 void StopAudioMonitor();
393 bool IsAudioMonitorRunning() const;
394 sigslot::signal2<VoiceChannel*, const AudioInfo&> SignalAudioMonitor;
395
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000396 int GetInputLevel_w();
397 int GetOutputLevel_w();
398 void GetActiveStreams_w(AudioInfo::StreamList* actives);
399
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000400 private:
401 // overrides from BaseChannel
402 virtual void OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000403 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000404 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000405 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000406 virtual void ChangeState();
407 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
408 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000409 ContentAction action,
410 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000411 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000412 ContentAction action,
413 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000414 void HandleEarlyMediaTimeout();
solenberg1d63dd02015-12-02 12:35:09 -0800415 bool InsertDtmf_w(uint32_t ssrc, int event, int duration);
solenberg4bac9c52015-10-09 02:32:53 -0700416 bool SetOutputVolume_w(uint32_t ssrc, double volume);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000417 bool GetStats_w(VoiceMediaInfo* stats);
418
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000419 virtual void OnMessage(rtc::Message* pmsg);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800420 virtual void GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000421 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000422 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000423 virtual void OnMediaMonitorUpdate(
424 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info);
425 void OnAudioMonitorUpdate(AudioMonitor* monitor, const AudioInfo& info);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000426
427 static const int kEarlyMediaTimeout = 1000;
Fredrik Solenberg0c022642015-08-05 12:25:22 +0200428 MediaEngineInterface* media_engine_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000429 bool received_media_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000430 rtc::scoped_ptr<VoiceMediaMonitor> media_monitor_;
431 rtc::scoped_ptr<AudioMonitor> audio_monitor_;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700432
433 // Last AudioSendParameters sent down to the media_channel() via
434 // SetSendParameters.
435 AudioSendParameters last_send_params_;
436 // Last AudioRecvParameters sent down to the media_channel() via
437 // SetRecvParameters.
438 AudioRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000439};
440
441// VideoChannel is a specialization for video.
442class VideoChannel : public BaseChannel {
443 public:
deadbeefcbecd352015-09-23 11:50:27 -0700444 VideoChannel(rtc::Thread* thread,
445 VideoMediaChannel* channel,
446 TransportController* transport_controller,
447 const std::string& content_name,
Fredrik Solenberg0c022642015-08-05 12:25:22 +0200448 bool rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000449 ~VideoChannel();
450 bool Init();
451
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200452 // downcasts a MediaChannel
453 virtual VideoMediaChannel* media_channel() const {
454 return static_cast<VideoMediaChannel*>(BaseChannel::media_channel());
455 }
456
Peter Boström0c4e06b2015-10-07 12:23:21 +0200457 bool SetRenderer(uint32_t ssrc, VideoRenderer* renderer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000458
459 // TODO(pthatcher): Refactor to use a "capture id" instead of an
460 // ssrc here as the "key".
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000461 // Passes ownership of the capturer to the channel.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200462 bool AddScreencast(uint32_t ssrc, VideoCapturer* capturer);
463 bool SetCapturer(uint32_t ssrc, VideoCapturer* capturer);
464 bool RemoveScreencast(uint32_t ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000465 // True if we've added a screencast. Doesn't matter if the capturer
466 // has been started or not.
467 bool IsScreencasting();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000468 // Get statistics about the current media session.
pbos@webrtc.org058b1f12015-03-04 08:54:32 +0000469 bool GetStats(VideoMediaInfo* stats);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000470
471 sigslot::signal2<VideoChannel*, const std::vector<ConnectionInfo>&>
472 SignalConnectionMonitor;
473
474 void StartMediaMonitor(int cms);
475 void StopMediaMonitor();
476 sigslot::signal2<VideoChannel*, const VideoMediaInfo&> SignalMediaMonitor;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200477 sigslot::signal2<uint32_t, rtc::WindowEvent> SignalScreencastWindowEvent;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000478
Peter Boström0c4e06b2015-10-07 12:23:21 +0200479 bool SetVideoSend(uint32_t ssrc, bool enable, const VideoOptions* options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000481 private:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200482 typedef std::map<uint32_t, VideoCapturer*> ScreencastMap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483
484 // overrides from BaseChannel
485 virtual void ChangeState();
486 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
487 virtual bool SetLocalContent_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 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000491 ContentAction action,
492 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000493
Peter Boström0c4e06b2015-10-07 12:23:21 +0200494 bool AddScreencast_w(uint32_t ssrc, VideoCapturer* capturer);
495 bool RemoveScreencast_w(uint32_t ssrc);
496 void OnScreencastWindowEvent_s(uint32_t ssrc, rtc::WindowEvent we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497 bool IsScreencasting_w() const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000498 bool GetStats_w(VideoMediaInfo* stats);
499
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000500 virtual void OnMessage(rtc::Message* pmsg);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800501 virtual void GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000503 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504 virtual void OnMediaMonitorUpdate(
505 VideoMediaChannel* media_channel, const VideoMediaInfo& info);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200506 virtual void OnScreencastWindowEvent(uint32_t ssrc, rtc::WindowEvent event);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000507 virtual void OnStateChange(VideoCapturer* capturer, CaptureState ev);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200508 bool GetLocalSsrc(const VideoCapturer* capturer, uint32_t* ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000509
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000510 VideoRenderer* renderer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511 ScreencastMap screencast_capturers_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000512 rtc::scoped_ptr<VideoMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000513
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000514 rtc::WindowEvent previous_we_;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700515
516 // Last VideoSendParameters sent down to the media_channel() via
517 // SetSendParameters.
518 VideoSendParameters last_send_params_;
519 // Last VideoRecvParameters sent down to the media_channel() via
520 // SetRecvParameters.
521 VideoRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000522};
523
524// DataChannel is a specialization for data.
525class DataChannel : public BaseChannel {
526 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000527 DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528 DataMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -0700529 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000530 const std::string& content_name,
531 bool rtcp);
532 ~DataChannel();
533 bool Init();
534
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000535 virtual bool SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000536 const rtc::Buffer& payload,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000537 SendDataResult* result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000538
539 void StartMediaMonitor(int cms);
540 void StopMediaMonitor();
541
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000542 // Should be called on the signaling thread only.
543 bool ready_to_send_data() const {
544 return ready_to_send_data_;
545 }
546
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000547 sigslot::signal2<DataChannel*, const DataMediaInfo&> SignalMediaMonitor;
548 sigslot::signal2<DataChannel*, const std::vector<ConnectionInfo>&>
549 SignalConnectionMonitor;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200550 sigslot::signal3<DataChannel*, const ReceiveDataParams&, const rtc::Buffer&>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000551 SignalDataReceived;
552 // Signal for notifying when the channel becomes ready to send data.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000553 // That occurs when the channel is enabled, the transport is writable,
554 // both local and remote descriptions are set, and the channel is unblocked.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000555 sigslot::signal1<bool> SignalReadyToSendData;
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +0000556 // Signal for notifying that the remote side has closed the DataChannel.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200557 sigslot::signal1<uint32_t> SignalStreamClosedRemotely;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000558
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000559 protected:
560 // downcasts a MediaChannel.
561 virtual DataMediaChannel* media_channel() const {
562 return static_cast<DataMediaChannel*>(BaseChannel::media_channel());
563 }
564
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000565 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000566 struct SendDataMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000567 SendDataMessageData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000568 const rtc::Buffer* payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000569 SendDataResult* result)
570 : params(params),
571 payload(payload),
572 result(result),
573 succeeded(false) {
574 }
575
576 const SendDataParams& params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000577 const rtc::Buffer* payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000578 SendDataResult* result;
579 bool succeeded;
580 };
581
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000582 struct DataReceivedMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000583 // We copy the data because the data will become invalid after we
584 // handle DataMediaChannel::SignalDataReceived but before we fire
585 // SignalDataReceived.
586 DataReceivedMessageData(
587 const ReceiveDataParams& params, const char* data, size_t len)
588 : params(params),
589 payload(data, len) {
590 }
591 const ReceiveDataParams params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000592 const rtc::Buffer payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000593 };
594
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000595 typedef rtc::TypedMessageData<bool> DataChannelReadyToSendMessageData;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000596
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000597 // overrides from BaseChannel
598 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
599 // If data_channel_type_ is DCT_NONE, set it. Otherwise, check that
600 // it's the same as what was set previously. Returns false if it's
601 // set to one type one type and changed to another type later.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000602 bool SetDataChannelType(DataChannelType new_data_channel_type,
603 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000604 // Same as SetDataChannelType, but extracts the type from the
605 // DataContentDescription.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000606 bool SetDataChannelTypeFromContent(const DataContentDescription* content,
607 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000609 ContentAction action,
610 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000611 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000612 ContentAction action,
613 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000614 virtual void ChangeState();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000615 virtual bool WantsPacket(bool rtcp, rtc::Buffer* packet);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000616
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000617 virtual void OnMessage(rtc::Message* pmsg);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800618 virtual void GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000619 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000620 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000621 virtual void OnMediaMonitorUpdate(
622 DataMediaChannel* media_channel, const DataMediaInfo& info);
623 virtual bool ShouldSetupDtlsSrtp() const;
624 void OnDataReceived(
625 const ReceiveDataParams& params, const char* data, size_t len);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200626 void OnDataChannelError(uint32_t ssrc, DataMediaChannel::Error error);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000627 void OnDataChannelReadyToSend(bool writable);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200628 void OnStreamClosedRemotely(uint32_t sid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000630 rtc::scoped_ptr<DataMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000631 // TODO(pthatcher): Make a separate SctpDataChannel and
632 // RtpDataChannel instead of using this.
633 DataChannelType data_channel_type_;
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000634 bool ready_to_send_data_;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700635
636 // Last DataSendParameters sent down to the media_channel() via
637 // SetSendParameters.
638 DataSendParameters last_send_params_;
639 // Last DataRecvParameters sent down to the media_channel() via
640 // SetRecvParameters.
641 DataRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000642};
643
644} // namespace cricket
645
646#endif // TALK_SESSION_MEDIA_CHANNEL_H_