blob: 3a8550cfae3e424ade9f6b3449d957c8909d6c13 [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 +000064struct ViewRequest;
65
66enum SinkType {
67 SINK_PRE_CRYPTO, // Sink packets before encryption or after decryption.
68 SINK_POST_CRYPTO // Sink packets after encryption or before decryption.
69};
70
71// BaseChannel contains logic common to voice and video, including
solenberg1dd98f32015-09-10 01:57:14 -070072// enable, marshaling calls to a worker thread, and
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073// connection and media monitors.
wu@webrtc.org78187522013-10-07 23:32:02 +000074//
75// WARNING! SUBCLASSES MUST CALL Deinit() IN THEIR DESTRUCTORS!
76// This is required to avoid a data race between the destructor modifying the
77// vtable, and the media channel's thread using BaseChannel as the
78// NetworkInterface.
79
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080class BaseChannel
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000081 : public rtc::MessageHandler, public sigslot::has_slots<>,
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +000082 public MediaChannel::NetworkInterface,
83 public ConnectionStatsGetter {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084 public:
deadbeefcbecd352015-09-23 11:50:27 -070085 BaseChannel(rtc::Thread* thread,
86 MediaChannel* channel,
87 TransportController* transport_controller,
88 const std::string& content_name,
89 bool rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090 virtual ~BaseChannel();
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +000091 bool Init();
wu@webrtc.org78187522013-10-07 23:32:02 +000092 // Deinit may be called multiple times and is simply ignored if it's alreay
93 // done.
94 void Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000096 rtc::Thread* worker_thread() const { return worker_thread_; }
deadbeefcbecd352015-09-23 11:50:27 -070097 const std::string& content_name() const { return content_name_; }
98 const std::string& transport_name() const { return transport_name_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099 TransportChannel* transport_channel() const {
100 return transport_channel_;
101 }
102 TransportChannel* rtcp_transport_channel() const {
103 return rtcp_transport_channel_;
104 }
105 bool enabled() const { return enabled_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106
107 // This function returns true if we are using SRTP.
108 bool secure() const { return srtp_filter_.IsActive(); }
109 // The following function returns true if we are using
110 // DTLS-based keying. If you turned off SRTP later, however
111 // you could have secure() == false and dtls_secure() == true.
112 bool secure_dtls() const { return dtls_keyed_; }
113 // This function returns true if we require secure channel for call setup.
114 bool secure_required() const { return secure_required_; }
115
116 bool writable() const { return writable_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700118 // Activate RTCP mux, regardless of the state so far. Once
119 // activated, it can not be deactivated, and if the remote
120 // description doesn't support RTCP mux, setting the remote
121 // description will fail.
122 void ActivateRtcpMux();
deadbeefcbecd352015-09-23 11:50:27 -0700123 bool SetTransport(const std::string& transport_name);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000124 bool PushdownLocalDescription(const SessionDescription* local_desc,
125 ContentAction action,
126 std::string* error_desc);
127 bool PushdownRemoteDescription(const SessionDescription* remote_desc,
128 ContentAction action,
129 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130 // Channel control
131 bool SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000132 ContentAction action,
133 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134 bool SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000135 ContentAction action,
136 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137
138 bool Enable(bool enable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139
140 // Multiplexing
141 bool AddRecvStream(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200142 bool RemoveRecvStream(uint32_t ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000143 bool AddSendStream(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200144 bool RemoveSendStream(uint32_t ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000145
146 // Monitoring
147 void StartConnectionMonitor(int cms);
148 void StopConnectionMonitor();
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000149 // For ConnectionStatsGetter, used by ConnectionMonitor
deadbeefcbecd352015-09-23 11:50:27 -0700150 bool GetConnectionStats(ConnectionInfos* infos) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000152 BundleFilter* bundle_filter() { return &bundle_filter_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153
154 const std::vector<StreamParams>& local_streams() const {
155 return local_streams_;
156 }
157 const std::vector<StreamParams>& remote_streams() const {
158 return remote_streams_;
159 }
160
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000161 sigslot::signal2<BaseChannel*, bool> SignalDtlsSetupFailure;
162 void SignalDtlsSetupFailure_w(bool rtcp);
163 void SignalDtlsSetupFailure_s(bool rtcp);
164
buildbot@webrtc.org6bfd6192014-05-15 16:15:59 +0000165 // Used for latency measurements.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166 sigslot::signal1<BaseChannel*> SignalFirstPacketReceived;
167
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168 // Made public for easier testing.
deadbeefcbecd352015-09-23 11:50:27 -0700169 void SetReadyToSend(bool rtcp, bool ready);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000170
guoweis@webrtc.org4f852882015-03-12 20:09:44 +0000171 // Only public for unit tests. Otherwise, consider protected.
rlesterec9d1872015-10-27 14:22:16 -0700172 int SetOption(SocketType type, rtc::Socket::Option o, int val)
173 override;
guoweis@webrtc.org4f852882015-03-12 20:09:44 +0000174
solenberg5b14b422015-10-01 04:10:31 -0700175 SrtpFilter* srtp_filter() { return &srtp_filter_; }
176
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177 protected:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178 virtual MediaChannel* media_channel() const { return media_channel_; }
deadbeefcbecd352015-09-23 11:50:27 -0700179 // Sets the |transport_channel_| (and |rtcp_transport_channel_|, if |rtcp_| is
180 // true). Gets the transport channels from |transport_controller_|.
181 bool SetTransport_w(const std::string& transport_name);
guoweis46383312015-12-17 16:45:59 -0800182
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000183 void set_transport_channel(TransportChannel* transport);
guoweis46383312015-12-17 16:45:59 -0800184 void set_rtcp_transport_channel(TransportChannel* transport,
185 bool update_writablity);
186
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187 bool was_ever_writable() const { return was_ever_writable_; }
188 void set_local_content_direction(MediaContentDirection direction) {
189 local_content_direction_ = direction;
190 }
191 void set_remote_content_direction(MediaContentDirection direction) {
192 remote_content_direction_ = direction;
193 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700194 void set_secure_required(bool secure_required) {
195 secure_required_ = secure_required;
196 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197 bool IsReadyToReceive() const;
198 bool IsReadyToSend() const;
deadbeefcbecd352015-09-23 11:50:27 -0700199 rtc::Thread* signaling_thread() {
200 return transport_controller_->signaling_thread();
201 }
deadbeefcbecd352015-09-23 11:50:27 -0700202 bool rtcp_transport_enabled() const { return rtcp_transport_enabled_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000203
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000204 void ConnectToTransportChannel(TransportChannel* tc);
205 void DisconnectFromTransportChannel(TransportChannel* tc);
206
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000207 void FlushRtcpMessages();
208
209 // NetworkInterface implementation, called by MediaEngine
rlesterec9d1872015-10-27 14:22:16 -0700210 bool SendPacket(rtc::Buffer* packet,
211 const rtc::PacketOptions& options) override;
212 bool SendRtcp(rtc::Buffer* packet, const rtc::PacketOptions& options)
213 override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214
215 // From TransportChannel
216 void OnWritableState(TransportChannel* channel);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000217 virtual void OnChannelRead(TransportChannel* channel,
218 const char* data,
219 size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000220 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000221 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000222 void OnReadyToSend(TransportChannel* channel);
223
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800224 void OnDtlsState(TransportChannel* channel, DtlsTransportState state);
225
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000226 bool PacketIsRtcp(const TransportChannel* channel, const char* data,
227 size_t len);
stefanc1aeaf02015-10-15 07:26:07 -0700228 bool SendPacket(bool rtcp,
229 rtc::Buffer* packet,
230 const rtc::PacketOptions& options);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000231 virtual bool WantsPacket(bool rtcp, rtc::Buffer* packet);
232 void HandlePacket(bool rtcp, rtc::Buffer* packet,
233 const rtc::PacketTime& packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000234
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000235 void EnableMedia_w();
236 void DisableMedia_w();
deadbeefcbecd352015-09-23 11:50:27 -0700237 void UpdateWritableState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000238 void ChannelWritable_w();
239 void ChannelNotWritable_w();
240 bool AddRecvStream_w(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200241 bool RemoveRecvStream_w(uint32_t ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000242 bool AddSendStream_w(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200243 bool RemoveSendStream_w(uint32_t ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000244 virtual bool ShouldSetupDtlsSrtp() const;
245 // Do the DTLS key expansion and impose it on the SRTP/SRTCP filters.
246 // |rtcp_channel| indicates whether to set up the RTP or RTCP filter.
247 bool SetupDtlsSrtp(bool rtcp_channel);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800248 void MaybeSetupDtlsSrtp_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000249 // Set the DTLS-SRTP cipher policy on this channel as appropriate.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800250 bool SetDtlsSrtpCryptoSuites(TransportChannel* tc, bool rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251
252 virtual void ChangeState() = 0;
253
254 // Gets the content info appropriate to the channel (audio or video).
255 virtual const ContentInfo* GetFirstContent(
256 const SessionDescription* sdesc) = 0;
257 bool UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000258 ContentAction action,
259 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000260 bool UpdateRemoteStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000261 ContentAction action,
262 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000263 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000264 ContentAction action,
265 std::string* error_desc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000267 ContentAction action,
268 std::string* error_desc) = 0;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700269 bool SetRtpTransportParameters_w(const MediaContentDescription* content,
270 ContentAction action,
271 ContentSource src,
272 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000274 // Helper method to get RTP Absoulute SendTime extension header id if
275 // present in remote supported extensions list.
276 void MaybeCacheRtpAbsSendTimeHeaderExtension(
stefanc1aeaf02015-10-15 07:26:07 -0700277 const std::vector<RtpHeaderExtension>& extensions);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000278
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000279 bool CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
280 bool* dtls,
281 std::string* error_desc);
282 bool SetSrtp_w(const std::vector<CryptoParams>& params,
283 ContentAction action,
284 ContentSource src,
285 std::string* error_desc);
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700286 void ActivateRtcpMux_w();
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000287 bool SetRtcpMux_w(bool enable,
288 ContentAction action,
289 ContentSource src,
290 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000291
292 // From MessageHandler
rlesterec9d1872015-10-27 14:22:16 -0700293 void OnMessage(rtc::Message* pmsg) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000294
295 // Handled in derived classes
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800296 // Get the SRTP crypto suites to use for RTP media
297 virtual void GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const = 0;
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000298 virtual void OnConnectionMonitorUpdate(ConnectionMonitor* monitor,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299 const std::vector<ConnectionInfo>& infos) = 0;
300
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000301 // Helper function for invoking bool-returning methods on the worker thread.
302 template <class FunctorT>
303 bool InvokeOnWorker(const FunctorT& functor) {
304 return worker_thread_->Invoke<bool>(functor);
305 }
306
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000307 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000308 rtc::Thread* worker_thread_;
deadbeefcbecd352015-09-23 11:50:27 -0700309 TransportController* transport_controller_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000310 MediaChannel* media_channel_;
311 std::vector<StreamParams> local_streams_;
312 std::vector<StreamParams> remote_streams_;
313
pthatcher@webrtc.org990a00c2015-03-13 18:20:33 +0000314 const std::string content_name_;
deadbeefcbecd352015-09-23 11:50:27 -0700315 std::string transport_name_;
316 bool rtcp_transport_enabled_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000317 TransportChannel* transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700318 std::vector<std::pair<rtc::Socket::Option, int> > socket_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000319 TransportChannel* rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700320 std::vector<std::pair<rtc::Socket::Option, int> > rtcp_socket_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000321 SrtpFilter srtp_filter_;
322 RtcpMuxFilter rtcp_mux_filter_;
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000323 BundleFilter bundle_filter_;
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000324 rtc::scoped_ptr<ConnectionMonitor> connection_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000325 bool enabled_;
326 bool writable_;
327 bool rtp_ready_to_send_;
328 bool rtcp_ready_to_send_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329 bool was_ever_writable_;
330 MediaContentDirection local_content_direction_;
331 MediaContentDirection remote_content_direction_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000332 bool has_received_packet_;
333 bool dtls_keyed_;
334 bool secure_required_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000335 int rtp_abs_sendtime_extn_id_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336};
337
338// VoiceChannel is a specialization that adds support for early media, DTMF,
339// and input/output level monitoring.
340class VoiceChannel : public BaseChannel {
341 public:
deadbeefcbecd352015-09-23 11:50:27 -0700342 VoiceChannel(rtc::Thread* thread,
343 MediaEngineInterface* media_engine,
344 VoiceMediaChannel* channel,
345 TransportController* transport_controller,
346 const std::string& content_name,
347 bool rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000348 ~VoiceChannel();
349 bool Init();
solenberg1dd98f32015-09-10 01:57:14 -0700350
351 // Configure sending media on the stream with SSRC |ssrc|
352 // If there is only one sending stream SSRC 0 can be used.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200353 bool SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -0700354 bool enable,
deadbeefcbecd352015-09-23 11:50:27 -0700355 const AudioOptions* options,
solenberg1dd98f32015-09-10 01:57:14 -0700356 AudioRenderer* renderer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357
358 // downcasts a MediaChannel
359 virtual VoiceMediaChannel* media_channel() const {
360 return static_cast<VoiceMediaChannel*>(BaseChannel::media_channel());
361 }
362
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000363 void SetEarlyMedia(bool enable);
364 // This signal is emitted when we have gone a period of time without
365 // receiving early media. When received, a UI should start playing its
366 // own ringing sound
367 sigslot::signal1<VoiceChannel*> SignalEarlyMediaTimeout;
368
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000369 // Returns if the telephone-event has been negotiated.
370 bool CanInsertDtmf();
371 // Send and/or play a DTMF |event| according to the |flags|.
372 // The DTMF out-of-band signal will be used on sending.
373 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000374 // The valid value for the |event| are 0 which corresponding to DTMF
375 // event 0-9, *, #, A-D.
solenberg1d63dd02015-12-02 12:35:09 -0800376 bool InsertDtmf(uint32_t ssrc, int event_code, int duration);
solenberg4bac9c52015-10-09 02:32:53 -0700377 bool SetOutputVolume(uint32_t ssrc, double volume);
Tommif888bb52015-12-12 01:37:01 +0100378 void SetRawAudioSink(uint32_t ssrc,
379 rtc::scoped_ptr<webrtc::AudioSinkInterface> sink);
380
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000381 // Get statistics about the current media session.
382 bool GetStats(VoiceMediaInfo* stats);
383
384 // Monitoring functions
385 sigslot::signal2<VoiceChannel*, const std::vector<ConnectionInfo>&>
386 SignalConnectionMonitor;
387
388 void StartMediaMonitor(int cms);
389 void StopMediaMonitor();
390 sigslot::signal2<VoiceChannel*, const VoiceMediaInfo&> SignalMediaMonitor;
391
392 void StartAudioMonitor(int cms);
393 void StopAudioMonitor();
394 bool IsAudioMonitorRunning() const;
395 sigslot::signal2<VoiceChannel*, const AudioInfo&> SignalAudioMonitor;
396
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000397 int GetInputLevel_w();
398 int GetOutputLevel_w();
399 void GetActiveStreams_w(AudioInfo::StreamList* actives);
400
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000401 private:
402 // overrides from BaseChannel
403 virtual void OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000404 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000405 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000406 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000407 virtual void ChangeState();
408 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
409 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000410 ContentAction action,
411 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000412 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000413 ContentAction action,
414 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000415 void HandleEarlyMediaTimeout();
solenberg1d63dd02015-12-02 12:35:09 -0800416 bool InsertDtmf_w(uint32_t ssrc, int event, int duration);
solenberg4bac9c52015-10-09 02:32:53 -0700417 bool SetOutputVolume_w(uint32_t ssrc, double volume);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000418 bool GetStats_w(VoiceMediaInfo* stats);
419
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000420 virtual void OnMessage(rtc::Message* pmsg);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800421 virtual void GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000422 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000423 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000424 virtual void OnMediaMonitorUpdate(
425 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info);
426 void OnAudioMonitorUpdate(AudioMonitor* monitor, const AudioInfo& info);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000427
428 static const int kEarlyMediaTimeout = 1000;
Fredrik Solenberg0c022642015-08-05 12:25:22 +0200429 MediaEngineInterface* media_engine_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000430 bool received_media_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000431 rtc::scoped_ptr<VoiceMediaMonitor> media_monitor_;
432 rtc::scoped_ptr<AudioMonitor> audio_monitor_;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700433
434 // Last AudioSendParameters sent down to the media_channel() via
435 // SetSendParameters.
436 AudioSendParameters last_send_params_;
437 // Last AudioRecvParameters sent down to the media_channel() via
438 // SetRecvParameters.
439 AudioRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000440};
441
442// VideoChannel is a specialization for video.
443class VideoChannel : public BaseChannel {
444 public:
deadbeefcbecd352015-09-23 11:50:27 -0700445 VideoChannel(rtc::Thread* thread,
446 VideoMediaChannel* channel,
447 TransportController* transport_controller,
448 const std::string& content_name,
Fredrik Solenberg0c022642015-08-05 12:25:22 +0200449 bool rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000450 ~VideoChannel();
451 bool Init();
452
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200453 // downcasts a MediaChannel
454 virtual VideoMediaChannel* media_channel() const {
455 return static_cast<VideoMediaChannel*>(BaseChannel::media_channel());
456 }
457
Peter Boström0c4e06b2015-10-07 12:23:21 +0200458 bool SetRenderer(uint32_t ssrc, VideoRenderer* renderer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000459 bool ApplyViewRequest(const ViewRequest& request);
460
461 // TODO(pthatcher): Refactor to use a "capture id" instead of an
462 // ssrc here as the "key".
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000463 // Passes ownership of the capturer to the channel.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200464 bool AddScreencast(uint32_t ssrc, VideoCapturer* capturer);
465 bool SetCapturer(uint32_t ssrc, VideoCapturer* capturer);
466 bool RemoveScreencast(uint32_t ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000467 // True if we've added a screencast. Doesn't matter if the capturer
468 // has been started or not.
469 bool IsScreencasting();
Peter Boström0c4e06b2015-10-07 12:23:21 +0200470 int GetScreencastFps(uint32_t ssrc);
471 int GetScreencastMaxPixels(uint32_t ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000472 // Get statistics about the current media session.
pbos@webrtc.org058b1f12015-03-04 08:54:32 +0000473 bool GetStats(VideoMediaInfo* stats);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000474
475 sigslot::signal2<VideoChannel*, const std::vector<ConnectionInfo>&>
476 SignalConnectionMonitor;
477
478 void StartMediaMonitor(int cms);
479 void StopMediaMonitor();
480 sigslot::signal2<VideoChannel*, const VideoMediaInfo&> SignalMediaMonitor;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200481 sigslot::signal2<uint32_t, rtc::WindowEvent> SignalScreencastWindowEvent;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000482
483 bool SendIntraFrame();
484 bool RequestIntraFrame();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000485
Peter Boström0c4e06b2015-10-07 12:23:21 +0200486 bool SetVideoSend(uint32_t ssrc, bool enable, const VideoOptions* options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000487
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000488 private:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200489 typedef std::map<uint32_t, VideoCapturer*> ScreencastMap;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000490 struct ScreencastDetailsData;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491
492 // overrides from BaseChannel
493 virtual void ChangeState();
494 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
495 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000496 ContentAction action,
497 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000498 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000499 ContentAction action,
500 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000501 bool ApplyViewRequest_w(const ViewRequest& request);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502
Peter Boström0c4e06b2015-10-07 12:23:21 +0200503 bool AddScreencast_w(uint32_t ssrc, VideoCapturer* capturer);
504 bool RemoveScreencast_w(uint32_t ssrc);
505 void OnScreencastWindowEvent_s(uint32_t ssrc, rtc::WindowEvent we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000506 bool IsScreencasting_w() const;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000507 void GetScreencastDetails_w(ScreencastDetailsData* d) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000508 bool GetStats_w(VideoMediaInfo* stats);
509
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000510 virtual void OnMessage(rtc::Message* pmsg);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800511 virtual void GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000512 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000513 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000514 virtual void OnMediaMonitorUpdate(
515 VideoMediaChannel* media_channel, const VideoMediaInfo& info);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200516 virtual void OnScreencastWindowEvent(uint32_t ssrc, rtc::WindowEvent event);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000517 virtual void OnStateChange(VideoCapturer* capturer, CaptureState ev);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200518 bool GetLocalSsrc(const VideoCapturer* capturer, uint32_t* ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000519
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000520 VideoRenderer* renderer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000521 ScreencastMap screencast_capturers_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000522 rtc::scoped_ptr<VideoMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000523
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000524 rtc::WindowEvent previous_we_;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700525
526 // Last VideoSendParameters sent down to the media_channel() via
527 // SetSendParameters.
528 VideoSendParameters last_send_params_;
529 // Last VideoRecvParameters sent down to the media_channel() via
530 // SetRecvParameters.
531 VideoRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532};
533
534// DataChannel is a specialization for data.
535class DataChannel : public BaseChannel {
536 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000537 DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000538 DataMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -0700539 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000540 const std::string& content_name,
541 bool rtcp);
542 ~DataChannel();
543 bool Init();
544
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000545 virtual bool SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000546 const rtc::Buffer& payload,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000547 SendDataResult* result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000548
549 void StartMediaMonitor(int cms);
550 void StopMediaMonitor();
551
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000552 // Should be called on the signaling thread only.
553 bool ready_to_send_data() const {
554 return ready_to_send_data_;
555 }
556
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557 sigslot::signal2<DataChannel*, const DataMediaInfo&> SignalMediaMonitor;
558 sigslot::signal2<DataChannel*, const std::vector<ConnectionInfo>&>
559 SignalConnectionMonitor;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200560 sigslot::signal3<DataChannel*, const ReceiveDataParams&, const rtc::Buffer&>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000561 SignalDataReceived;
562 // Signal for notifying when the channel becomes ready to send data.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000563 // That occurs when the channel is enabled, the transport is writable,
564 // both local and remote descriptions are set, and the channel is unblocked.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000565 sigslot::signal1<bool> SignalReadyToSendData;
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +0000566 // Signal for notifying that the remote side has closed the DataChannel.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200567 sigslot::signal1<uint32_t> SignalStreamClosedRemotely;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000568
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000569 protected:
570 // downcasts a MediaChannel.
571 virtual DataMediaChannel* media_channel() const {
572 return static_cast<DataMediaChannel*>(BaseChannel::media_channel());
573 }
574
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000575 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000576 struct SendDataMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000577 SendDataMessageData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000578 const rtc::Buffer* payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000579 SendDataResult* result)
580 : params(params),
581 payload(payload),
582 result(result),
583 succeeded(false) {
584 }
585
586 const SendDataParams& params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000587 const rtc::Buffer* payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000588 SendDataResult* result;
589 bool succeeded;
590 };
591
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000592 struct DataReceivedMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000593 // We copy the data because the data will become invalid after we
594 // handle DataMediaChannel::SignalDataReceived but before we fire
595 // SignalDataReceived.
596 DataReceivedMessageData(
597 const ReceiveDataParams& params, const char* data, size_t len)
598 : params(params),
599 payload(data, len) {
600 }
601 const ReceiveDataParams params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000602 const rtc::Buffer payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000603 };
604
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000605 typedef rtc::TypedMessageData<bool> DataChannelReadyToSendMessageData;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000606
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000607 // overrides from BaseChannel
608 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
609 // If data_channel_type_ is DCT_NONE, set it. Otherwise, check that
610 // it's the same as what was set previously. Returns false if it's
611 // set to one type one type and changed to another type later.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000612 bool SetDataChannelType(DataChannelType new_data_channel_type,
613 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000614 // Same as SetDataChannelType, but extracts the type from the
615 // DataContentDescription.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000616 bool SetDataChannelTypeFromContent(const DataContentDescription* content,
617 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000618 virtual bool SetLocalContent_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 bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000622 ContentAction action,
623 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000624 virtual void ChangeState();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000625 virtual bool WantsPacket(bool rtcp, rtc::Buffer* packet);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000627 virtual void OnMessage(rtc::Message* pmsg);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800628 virtual void GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000630 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000631 virtual void OnMediaMonitorUpdate(
632 DataMediaChannel* media_channel, const DataMediaInfo& info);
633 virtual bool ShouldSetupDtlsSrtp() const;
634 void OnDataReceived(
635 const ReceiveDataParams& params, const char* data, size_t len);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200636 void OnDataChannelError(uint32_t ssrc, DataMediaChannel::Error error);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000637 void OnDataChannelReadyToSend(bool writable);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200638 void OnStreamClosedRemotely(uint32_t sid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000640 rtc::scoped_ptr<DataMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000641 // TODO(pthatcher): Make a separate SctpDataChannel and
642 // RtpDataChannel instead of using this.
643 DataChannelType data_channel_type_;
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000644 bool ready_to_send_data_;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700645
646 // Last DataSendParameters sent down to the media_channel() via
647 // SetSendParameters.
648 DataSendParameters last_send_params_;
649 // Last DataRecvParameters sent down to the media_channel() via
650 // SetRecvParameters.
651 DataRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000652};
653
654} // namespace cricket
655
656#endif // TALK_SESSION_MEDIA_CHANNEL_H_