blob: 0a7ce9d1a9e8355bc83ac4433a751ef22deb0354 [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"
deadbeefcbecd352015-09-23 11:50:27 -070041#include "webrtc/p2p/base/transportcontroller.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000042#include "webrtc/p2p/client/socketmonitor.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043#include "talk/session/media/audiomonitor.h"
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +000044#include "talk/session/media/bundlefilter.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045#include "talk/session/media/mediamonitor.h"
46#include "talk/session/media/mediasession.h"
47#include "talk/session/media/rtcpmuxfilter.h"
48#include "talk/session/media/srtpfilter.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000049#include "webrtc/base/asyncudpsocket.h"
50#include "webrtc/base/criticalsection.h"
51#include "webrtc/base/network.h"
52#include "webrtc/base/sigslot.h"
53#include "webrtc/base/window.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054
55namespace cricket {
56
57struct CryptoParams;
58class MediaContentDescription;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059struct ViewRequest;
60
61enum SinkType {
62 SINK_PRE_CRYPTO, // Sink packets before encryption or after decryption.
63 SINK_POST_CRYPTO // Sink packets after encryption or before decryption.
64};
65
66// BaseChannel contains logic common to voice and video, including
solenberg1dd98f32015-09-10 01:57:14 -070067// enable, marshaling calls to a worker thread, and
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068// connection and media monitors.
wu@webrtc.org78187522013-10-07 23:32:02 +000069//
70// WARNING! SUBCLASSES MUST CALL Deinit() IN THEIR DESTRUCTORS!
71// This is required to avoid a data race between the destructor modifying the
72// vtable, and the media channel's thread using BaseChannel as the
73// NetworkInterface.
74
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075class BaseChannel
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000076 : public rtc::MessageHandler, public sigslot::has_slots<>,
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +000077 public MediaChannel::NetworkInterface,
78 public ConnectionStatsGetter {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079 public:
deadbeefcbecd352015-09-23 11:50:27 -070080 BaseChannel(rtc::Thread* thread,
81 MediaChannel* channel,
82 TransportController* transport_controller,
83 const std::string& content_name,
84 bool rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085 virtual ~BaseChannel();
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +000086 bool Init();
wu@webrtc.org78187522013-10-07 23:32:02 +000087 // Deinit may be called multiple times and is simply ignored if it's alreay
88 // done.
89 void Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000091 rtc::Thread* worker_thread() const { return worker_thread_; }
deadbeefcbecd352015-09-23 11:50:27 -070092 const std::string& content_name() const { return content_name_; }
93 const std::string& transport_name() const { return transport_name_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 TransportChannel* transport_channel() const {
95 return transport_channel_;
96 }
97 TransportChannel* rtcp_transport_channel() const {
98 return rtcp_transport_channel_;
99 }
100 bool enabled() const { return enabled_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101
102 // This function returns true if we are using SRTP.
103 bool secure() const { return srtp_filter_.IsActive(); }
104 // The following function returns true if we are using
105 // DTLS-based keying. If you turned off SRTP later, however
106 // you could have secure() == false and dtls_secure() == true.
107 bool secure_dtls() const { return dtls_keyed_; }
108 // This function returns true if we require secure channel for call setup.
109 bool secure_required() const { return secure_required_; }
110
111 bool writable() const { return writable_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700113 // Activate RTCP mux, regardless of the state so far. Once
114 // activated, it can not be deactivated, and if the remote
115 // description doesn't support RTCP mux, setting the remote
116 // description will fail.
117 void ActivateRtcpMux();
deadbeefcbecd352015-09-23 11:50:27 -0700118 bool SetTransport(const std::string& transport_name);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000119 bool PushdownLocalDescription(const SessionDescription* local_desc,
120 ContentAction action,
121 std::string* error_desc);
122 bool PushdownRemoteDescription(const SessionDescription* remote_desc,
123 ContentAction action,
124 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125 // Channel control
126 bool SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000127 ContentAction action,
128 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129 bool SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000130 ContentAction action,
131 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132
133 bool Enable(bool enable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134
135 // Multiplexing
136 bool AddRecvStream(const StreamParams& sp);
137 bool RemoveRecvStream(uint32 ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000138 bool AddSendStream(const StreamParams& sp);
139 bool RemoveSendStream(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140
141 // Monitoring
142 void StartConnectionMonitor(int cms);
143 void StopConnectionMonitor();
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000144 // For ConnectionStatsGetter, used by ConnectionMonitor
deadbeefcbecd352015-09-23 11:50:27 -0700145 bool GetConnectionStats(ConnectionInfos* infos) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146
147 void set_srtp_signal_silent_time(uint32 silent_time) {
148 srtp_filter_.set_signal_silent_time(silent_time);
149 }
150
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.
171 virtual int SetOption(SocketType type, rtc::Socket::Option o, int val);
172
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000173 protected:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000174 virtual MediaChannel* media_channel() const { return media_channel_; }
deadbeefcbecd352015-09-23 11:50:27 -0700175 // Sets the |transport_channel_| (and |rtcp_transport_channel_|, if |rtcp_| is
176 // true). Gets the transport channels from |transport_controller_|.
177 bool SetTransport_w(const std::string& transport_name);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000178 void set_transport_channel(TransportChannel* transport);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000179 void set_rtcp_transport_channel(TransportChannel* transport);
180 bool was_ever_writable() const { return was_ever_writable_; }
181 void set_local_content_direction(MediaContentDirection direction) {
182 local_content_direction_ = direction;
183 }
184 void set_remote_content_direction(MediaContentDirection direction) {
185 remote_content_direction_ = direction;
186 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700187 void set_secure_required(bool secure_required) {
188 secure_required_ = secure_required;
189 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190 bool IsReadyToReceive() const;
191 bool IsReadyToSend() const;
deadbeefcbecd352015-09-23 11:50:27 -0700192 rtc::Thread* signaling_thread() {
193 return transport_controller_->signaling_thread();
194 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195 SrtpFilter* srtp_filter() { return &srtp_filter_; }
deadbeefcbecd352015-09-23 11:50:27 -0700196 bool rtcp_transport_enabled() const { return rtcp_transport_enabled_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000198 void ConnectToTransportChannel(TransportChannel* tc);
199 void DisconnectFromTransportChannel(TransportChannel* tc);
200
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201 void FlushRtcpMessages();
202
203 // NetworkInterface implementation, called by MediaEngine
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000204 virtual bool SendPacket(rtc::Buffer* packet,
205 rtc::DiffServCodePoint dscp);
206 virtual bool SendRtcp(rtc::Buffer* packet,
207 rtc::DiffServCodePoint dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208
209 // From TransportChannel
210 void OnWritableState(TransportChannel* channel);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000211 virtual void OnChannelRead(TransportChannel* channel,
212 const char* data,
213 size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000214 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000215 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000216 void OnReadyToSend(TransportChannel* channel);
217
218 bool PacketIsRtcp(const TransportChannel* channel, const char* data,
219 size_t len);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000220 bool SendPacket(bool rtcp, rtc::Buffer* packet,
221 rtc::DiffServCodePoint dscp);
222 virtual bool WantsPacket(bool rtcp, rtc::Buffer* packet);
223 void HandlePacket(bool rtcp, rtc::Buffer* packet,
224 const rtc::PacketTime& packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000225
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000226 void EnableMedia_w();
227 void DisableMedia_w();
deadbeefcbecd352015-09-23 11:50:27 -0700228 void UpdateWritableState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000229 void ChannelWritable_w();
230 void ChannelNotWritable_w();
231 bool AddRecvStream_w(const StreamParams& sp);
232 bool RemoveRecvStream_w(uint32 ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000233 bool AddSendStream_w(const StreamParams& sp);
234 bool RemoveSendStream_w(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000235 virtual bool ShouldSetupDtlsSrtp() const;
236 // Do the DTLS key expansion and impose it on the SRTP/SRTCP filters.
237 // |rtcp_channel| indicates whether to set up the RTP or RTCP filter.
238 bool SetupDtlsSrtp(bool rtcp_channel);
239 // Set the DTLS-SRTP cipher policy on this channel as appropriate.
240 bool SetDtlsSrtpCiphers(TransportChannel *tc, bool rtcp);
241
242 virtual void ChangeState() = 0;
243
244 // Gets the content info appropriate to the channel (audio or video).
245 virtual const ContentInfo* GetFirstContent(
246 const SessionDescription* sdesc) = 0;
247 bool UpdateLocalStreams_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 bool UpdateRemoteStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000251 ContentAction action,
252 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000253 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000254 ContentAction action,
255 std::string* error_desc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000257 ContentAction action,
258 std::string* error_desc) = 0;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700259 bool SetRtpTransportParameters_w(const MediaContentDescription* content,
260 ContentAction action,
261 ContentSource src,
262 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000263
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000264 // Helper method to get RTP Absoulute SendTime extension header id if
265 // present in remote supported extensions list.
266 void MaybeCacheRtpAbsSendTimeHeaderExtension(
267 const std::vector<RtpHeaderExtension>& extensions);
268
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000269 bool CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
270 bool* dtls,
271 std::string* error_desc);
272 bool SetSrtp_w(const std::vector<CryptoParams>& params,
273 ContentAction action,
274 ContentSource src,
275 std::string* error_desc);
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700276 void ActivateRtcpMux_w();
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000277 bool SetRtcpMux_w(bool enable,
278 ContentAction action,
279 ContentSource src,
280 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281
282 // From MessageHandler
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000283 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284
285 // Handled in derived classes
286 // Get the SRTP ciphers to use for RTP media
287 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const = 0;
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000288 virtual void OnConnectionMonitorUpdate(ConnectionMonitor* monitor,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000289 const std::vector<ConnectionInfo>& infos) = 0;
290
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000291 // Helper function for invoking bool-returning methods on the worker thread.
292 template <class FunctorT>
293 bool InvokeOnWorker(const FunctorT& functor) {
294 return worker_thread_->Invoke<bool>(functor);
295 }
296
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000297 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000298 rtc::Thread* worker_thread_;
deadbeefcbecd352015-09-23 11:50:27 -0700299 TransportController* transport_controller_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000300 MediaChannel* media_channel_;
301 std::vector<StreamParams> local_streams_;
302 std::vector<StreamParams> remote_streams_;
303
pthatcher@webrtc.org990a00c2015-03-13 18:20:33 +0000304 const std::string content_name_;
deadbeefcbecd352015-09-23 11:50:27 -0700305 std::string transport_name_;
306 bool rtcp_transport_enabled_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000307 TransportChannel* transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700308 std::vector<std::pair<rtc::Socket::Option, int> > socket_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000309 TransportChannel* rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700310 std::vector<std::pair<rtc::Socket::Option, int> > rtcp_socket_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000311 SrtpFilter srtp_filter_;
312 RtcpMuxFilter rtcp_mux_filter_;
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000313 BundleFilter bundle_filter_;
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000314 rtc::scoped_ptr<ConnectionMonitor> connection_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000315 bool enabled_;
316 bool writable_;
317 bool rtp_ready_to_send_;
318 bool rtcp_ready_to_send_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000319 bool was_ever_writable_;
320 MediaContentDirection local_content_direction_;
321 MediaContentDirection remote_content_direction_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000322 bool has_received_packet_;
323 bool dtls_keyed_;
324 bool secure_required_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000325 int rtp_abs_sendtime_extn_id_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000326};
327
328// VoiceChannel is a specialization that adds support for early media, DTMF,
329// and input/output level monitoring.
330class VoiceChannel : public BaseChannel {
331 public:
deadbeefcbecd352015-09-23 11:50:27 -0700332 VoiceChannel(rtc::Thread* thread,
333 MediaEngineInterface* media_engine,
334 VoiceMediaChannel* channel,
335 TransportController* transport_controller,
336 const std::string& content_name,
337 bool rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338 ~VoiceChannel();
339 bool Init();
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000340 bool SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer);
solenberg1dd98f32015-09-10 01:57:14 -0700341
342 // Configure sending media on the stream with SSRC |ssrc|
343 // If there is only one sending stream SSRC 0 can be used.
deadbeefcbecd352015-09-23 11:50:27 -0700344 bool SetAudioSend(uint32 ssrc,
345 bool mute,
346 const AudioOptions* options,
solenberg1dd98f32015-09-10 01:57:14 -0700347 AudioRenderer* renderer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000348
349 // downcasts a MediaChannel
350 virtual VoiceMediaChannel* media_channel() const {
351 return static_cast<VoiceMediaChannel*>(BaseChannel::media_channel());
352 }
353
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000354 void SetEarlyMedia(bool enable);
355 // This signal is emitted when we have gone a period of time without
356 // receiving early media. When received, a UI should start playing its
357 // own ringing sound
358 sigslot::signal1<VoiceChannel*> SignalEarlyMediaTimeout;
359
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000360 // TODO(ronghuawu): Replace PressDTMF with InsertDtmf.
361 bool PressDTMF(int digit, bool playout);
362 // Returns if the telephone-event has been negotiated.
363 bool CanInsertDtmf();
364 // Send and/or play a DTMF |event| according to the |flags|.
365 // The DTMF out-of-band signal will be used on sending.
366 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000367 // The valid value for the |event| are 0 which corresponding to DTMF
368 // event 0-9, *, #, A-D.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000369 bool InsertDtmf(uint32 ssrc, int event_code, int duration, int flags);
370 bool SetOutputScaling(uint32 ssrc, double left, double right);
371 // Get statistics about the current media session.
372 bool GetStats(VoiceMediaInfo* stats);
373
374 // Monitoring functions
375 sigslot::signal2<VoiceChannel*, const std::vector<ConnectionInfo>&>
376 SignalConnectionMonitor;
377
378 void StartMediaMonitor(int cms);
379 void StopMediaMonitor();
380 sigslot::signal2<VoiceChannel*, const VoiceMediaInfo&> SignalMediaMonitor;
381
382 void StartAudioMonitor(int cms);
383 void StopAudioMonitor();
384 bool IsAudioMonitorRunning() const;
385 sigslot::signal2<VoiceChannel*, const AudioInfo&> SignalAudioMonitor;
386
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000387 int GetInputLevel_w();
388 int GetOutputLevel_w();
389 void GetActiveStreams_w(AudioInfo::StreamList* actives);
390
391 // Signal errors from VoiceMediaChannel. Arguments are:
392 // ssrc(uint32), and error(VoiceMediaChannel::Error).
393 sigslot::signal3<VoiceChannel*, uint32, VoiceMediaChannel::Error>
394 SignalMediaError;
395
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000396 private:
397 // overrides from BaseChannel
398 virtual void OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000399 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000400 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000401 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000402 virtual void ChangeState();
403 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
404 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000405 ContentAction action,
406 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000407 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000408 ContentAction action,
409 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000410 void HandleEarlyMediaTimeout();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000411 bool InsertDtmf_w(uint32 ssrc, int event, int duration, int flags);
412 bool SetOutputScaling_w(uint32 ssrc, double left, double right);
413 bool GetStats_w(VoiceMediaInfo* stats);
414
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000415 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000416 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
417 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000418 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000419 virtual void OnMediaMonitorUpdate(
420 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info);
421 void OnAudioMonitorUpdate(AudioMonitor* monitor, const AudioInfo& info);
422 void OnVoiceChannelError(uint32 ssrc, VoiceMediaChannel::Error error);
423 void SendLastMediaError();
424 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000425
426 static const int kEarlyMediaTimeout = 1000;
Fredrik Solenberg0c022642015-08-05 12:25:22 +0200427 MediaEngineInterface* media_engine_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000428 bool received_media_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000429 rtc::scoped_ptr<VoiceMediaMonitor> media_monitor_;
430 rtc::scoped_ptr<AudioMonitor> audio_monitor_;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700431
432 // Last AudioSendParameters sent down to the media_channel() via
433 // SetSendParameters.
434 AudioSendParameters last_send_params_;
435 // Last AudioRecvParameters sent down to the media_channel() via
436 // SetRecvParameters.
437 AudioRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000438};
439
440// VideoChannel is a specialization for video.
441class VideoChannel : public BaseChannel {
442 public:
deadbeefcbecd352015-09-23 11:50:27 -0700443 VideoChannel(rtc::Thread* thread,
444 VideoMediaChannel* channel,
445 TransportController* transport_controller,
446 const std::string& content_name,
Fredrik Solenberg0c022642015-08-05 12:25:22 +0200447 bool rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000448 ~VideoChannel();
449 bool Init();
450
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200451 // downcasts a MediaChannel
452 virtual VideoMediaChannel* media_channel() const {
453 return static_cast<VideoMediaChannel*>(BaseChannel::media_channel());
454 }
455
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000456 bool SetRenderer(uint32 ssrc, VideoRenderer* renderer);
457 bool ApplyViewRequest(const ViewRequest& request);
458
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.
462 bool AddScreencast(uint32 ssrc, VideoCapturer* capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000463 bool SetCapturer(uint32 ssrc, VideoCapturer* capturer);
464 bool RemoveScreencast(uint32 ssrc);
465 // True if we've added a screencast. Doesn't matter if the capturer
466 // has been started or not.
467 bool IsScreencasting();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000468 int GetScreencastFps(uint32 ssrc);
469 int GetScreencastMaxPixels(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000470 // Get statistics about the current media session.
pbos@webrtc.org058b1f12015-03-04 08:54:32 +0000471 bool GetStats(VideoMediaInfo* stats);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000472
473 sigslot::signal2<VideoChannel*, const std::vector<ConnectionInfo>&>
474 SignalConnectionMonitor;
475
476 void StartMediaMonitor(int cms);
477 void StopMediaMonitor();
478 sigslot::signal2<VideoChannel*, const VideoMediaInfo&> SignalMediaMonitor;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000479 sigslot::signal2<uint32, rtc::WindowEvent> SignalScreencastWindowEvent;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480
481 bool SendIntraFrame();
482 bool RequestIntraFrame();
483 sigslot::signal3<VideoChannel*, uint32, VideoMediaChannel::Error>
484 SignalMediaError;
485
solenberg1dd98f32015-09-10 01:57:14 -0700486 // Configure sending media on the stream with SSRC |ssrc|
487 // If there is only one sending stream SSRC 0 can be used.
488 bool SetVideoSend(uint32 ssrc, bool mute, const VideoOptions* options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490 private:
491 typedef std::map<uint32, VideoCapturer*> ScreencastMap;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000492 struct ScreencastDetailsData;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000493
494 // overrides from BaseChannel
495 virtual void ChangeState();
496 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
497 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000498 ContentAction action,
499 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000500 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000501 ContentAction action,
502 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000503 bool ApplyViewRequest_w(const ViewRequest& request);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000505 bool AddScreencast_w(uint32 ssrc, VideoCapturer* capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000506 bool RemoveScreencast_w(uint32 ssrc);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000507 void OnScreencastWindowEvent_s(uint32 ssrc, rtc::WindowEvent we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000508 bool IsScreencasting_w() const;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000509 void GetScreencastDetails_w(ScreencastDetailsData* d) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000510 bool GetStats_w(VideoMediaInfo* stats);
511
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000512 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000513 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
514 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000515 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000516 virtual void OnMediaMonitorUpdate(
517 VideoMediaChannel* media_channel, const VideoMediaInfo& info);
518 virtual void OnScreencastWindowEvent(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000519 rtc::WindowEvent event);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000520 virtual void OnStateChange(VideoCapturer* capturer, CaptureState ev);
521 bool GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc);
522
523 void OnVideoChannelError(uint32 ssrc, VideoMediaChannel::Error error);
524 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000525
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526 VideoRenderer* renderer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000527 ScreencastMap screencast_capturers_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000528 rtc::scoped_ptr<VideoMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000530 rtc::WindowEvent previous_we_;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700531
532 // Last VideoSendParameters sent down to the media_channel() via
533 // SetSendParameters.
534 VideoSendParameters last_send_params_;
535 // Last VideoRecvParameters sent down to the media_channel() via
536 // SetRecvParameters.
537 VideoRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000538};
539
540// DataChannel is a specialization for data.
541class DataChannel : public BaseChannel {
542 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000543 DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000544 DataMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -0700545 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546 const std::string& content_name,
547 bool rtcp);
548 ~DataChannel();
549 bool Init();
550
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000551 virtual bool SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000552 const rtc::Buffer& payload,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000553 SendDataResult* result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000554
555 void StartMediaMonitor(int cms);
556 void StopMediaMonitor();
557
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000558 // Should be called on the signaling thread only.
559 bool ready_to_send_data() const {
560 return ready_to_send_data_;
561 }
562
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000563 sigslot::signal2<DataChannel*, const DataMediaInfo&> SignalMediaMonitor;
564 sigslot::signal2<DataChannel*, const std::vector<ConnectionInfo>&>
565 SignalConnectionMonitor;
566 sigslot::signal3<DataChannel*, uint32, DataMediaChannel::Error>
567 SignalMediaError;
568 sigslot::signal3<DataChannel*,
569 const ReceiveDataParams&,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000570 const rtc::Buffer&>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000571 SignalDataReceived;
572 // Signal for notifying when the channel becomes ready to send data.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000573 // That occurs when the channel is enabled, the transport is writable,
574 // both local and remote descriptions are set, and the channel is unblocked.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000575 sigslot::signal1<bool> SignalReadyToSendData;
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +0000576 // Signal for notifying that the remote side has closed the DataChannel.
577 sigslot::signal1<uint32> SignalStreamClosedRemotely;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000578
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000579 protected:
580 // downcasts a MediaChannel.
581 virtual DataMediaChannel* media_channel() const {
582 return static_cast<DataMediaChannel*>(BaseChannel::media_channel());
583 }
584
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000585 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000586 struct SendDataMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000587 SendDataMessageData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000588 const rtc::Buffer* payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589 SendDataResult* result)
590 : params(params),
591 payload(payload),
592 result(result),
593 succeeded(false) {
594 }
595
596 const SendDataParams& params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000597 const rtc::Buffer* payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000598 SendDataResult* result;
599 bool succeeded;
600 };
601
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000602 struct DataReceivedMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000603 // We copy the data because the data will become invalid after we
604 // handle DataMediaChannel::SignalDataReceived but before we fire
605 // SignalDataReceived.
606 DataReceivedMessageData(
607 const ReceiveDataParams& params, const char* data, size_t len)
608 : params(params),
609 payload(data, len) {
610 }
611 const ReceiveDataParams params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000612 const rtc::Buffer payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000613 };
614
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000615 typedef rtc::TypedMessageData<bool> DataChannelReadyToSendMessageData;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000616
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000617 // overrides from BaseChannel
618 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
619 // If data_channel_type_ is DCT_NONE, set it. Otherwise, check that
620 // it's the same as what was set previously. Returns false if it's
621 // set to one type one type and changed to another type later.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000622 bool SetDataChannelType(DataChannelType new_data_channel_type,
623 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000624 // Same as SetDataChannelType, but extracts the type from the
625 // DataContentDescription.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000626 bool SetDataChannelTypeFromContent(const DataContentDescription* content,
627 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000628 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000629 ContentAction action,
630 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000631 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000632 ContentAction action,
633 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000634 virtual void ChangeState();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000635 virtual bool WantsPacket(bool rtcp, rtc::Buffer* packet);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000636
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000637 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000638 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
639 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000640 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000641 virtual void OnMediaMonitorUpdate(
642 DataMediaChannel* media_channel, const DataMediaInfo& info);
643 virtual bool ShouldSetupDtlsSrtp() const;
644 void OnDataReceived(
645 const ReceiveDataParams& params, const char* data, size_t len);
646 void OnDataChannelError(uint32 ssrc, DataMediaChannel::Error error);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000647 void OnDataChannelReadyToSend(bool writable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000648 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +0000649 void OnStreamClosedRemotely(uint32 sid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000650
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000651 rtc::scoped_ptr<DataMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000652 // TODO(pthatcher): Make a separate SctpDataChannel and
653 // RtpDataChannel instead of using this.
654 DataChannelType data_channel_type_;
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000655 bool ready_to_send_data_;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700656
657 // Last DataSendParameters sent down to the media_channel() via
658 // SetSendParameters.
659 DataSendParameters last_send_params_;
660 // Last DataRecvParameters sent down to the media_channel() via
661 // SetRecvParameters.
662 DataRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000663};
664
665} // namespace cricket
666
667#endif // TALK_SESSION_MEDIA_CHANNEL_H_