blob: 927379d588822bbff358043a5a142ab3fb9bce11 [file] [log] [blame]
deadbeef6979b022015-09-24 16:47:53 -07001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2015 The WebRTC project authors. All Rights Reserved.
deadbeef6979b022015-09-24 16:47:53 -07003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
deadbeef6979b022015-09-24 16:47:53 -07009 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "pc/rtpsender.h"
deadbeef6979b022015-09-24 16:47:53 -070012
Steve Anton36b29d12017-10-30 09:57:42 -070013#include <vector>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "api/mediastreaminterface.h"
16#include "pc/localaudiosource.h"
Steve Anton2d8609c2018-01-23 16:38:46 -080017#include "pc/statscollector.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/checks.h"
19#include "rtc_base/helpers.h"
20#include "rtc_base/trace_event.h"
deadbeef70ab1a12015-09-28 16:53:55 -070021
22namespace webrtc {
23
Harald Alvestrandc72af932018-01-11 17:18:19 +010024namespace {
25
26// This function is only expected to be called on the signalling thread.
27int GenerateUniqueId() {
28 static int g_unique_id = 0;
29
30 return ++g_unique_id;
31}
32
Seth Hampson2d2c8882018-05-16 16:02:32 -070033// Returns an true if any RtpEncodingParameters member that isn't implemented
34// contains a value.
35bool UnimplementedRtpEncodingParameterHasValue(
36 const RtpEncodingParameters& encoding_params) {
37 if (encoding_params.codec_payload_type.has_value() ||
38 encoding_params.fec.has_value() || encoding_params.rtx.has_value() ||
39 encoding_params.dtx.has_value() || encoding_params.ptime.has_value() ||
40 encoding_params.max_framerate.has_value() ||
41 !encoding_params.rid.empty() ||
42 encoding_params.scale_resolution_down_by.has_value() ||
43 encoding_params.scale_framerate_down_by.has_value() ||
44 !encoding_params.dependency_rids.empty()) {
45 return true;
46 }
47 return false;
48}
49
50// Returns true if a "per-sender" encoding parameter contains a value that isn't
51// its default. Currently max_bitrate_bps and bitrate_priority both are
52// implemented "per-sender," meaning that these encoding parameters
53// are used for the RtpSender as a whole, not for a specific encoding layer.
54// This is done by setting these encoding parameters at index 0 of
55// RtpParameters.encodings. This function can be used to check if these
56// parameters are set at any index other than 0 of RtpParameters.encodings,
57// because they are currently unimplemented to be used for a specific encoding
58// layer.
59bool PerSenderRtpEncodingParameterHasValue(
60 const RtpEncodingParameters& encoding_params) {
Åsa Persson55659812018-06-18 17:51:32 +020061 if (encoding_params.bitrate_priority != kDefaultBitratePriority) {
Seth Hampson2d2c8882018-05-16 16:02:32 -070062 return true;
63 }
64 return false;
65}
66
67// Returns true if any RtpParameters member that isn't implemented contains a
68// value.
69bool UnimplementedRtpParameterHasValue(const RtpParameters& parameters) {
Florent Castelliabe301f2018-06-12 18:33:49 +020070 if (!parameters.mid.empty() ||
Seth Hampson2d2c8882018-05-16 16:02:32 -070071 parameters.degradation_preference != DegradationPreference::BALANCED) {
72 return true;
73 }
74 for (size_t i = 0; i < parameters.encodings.size(); ++i) {
75 if (UnimplementedRtpEncodingParameterHasValue(parameters.encodings[i])) {
76 return true;
77 }
78 // Encoding parameters that are per-sender should only contain value at
79 // index 0.
80 if (i != 0 &&
81 PerSenderRtpEncodingParameterHasValue(parameters.encodings[i])) {
82 return true;
83 }
84 }
85 return false;
86}
87
Harald Alvestrandc72af932018-01-11 17:18:19 +010088} // namespace
89
deadbeef70ab1a12015-09-28 16:53:55 -070090LocalAudioSinkAdapter::LocalAudioSinkAdapter() : sink_(nullptr) {}
91
92LocalAudioSinkAdapter::~LocalAudioSinkAdapter() {
93 rtc::CritScope lock(&lock_);
94 if (sink_)
95 sink_->OnClose();
96}
97
98void LocalAudioSinkAdapter::OnData(const void* audio_data,
99 int bits_per_sample,
100 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800101 size_t number_of_channels,
deadbeef70ab1a12015-09-28 16:53:55 -0700102 size_t number_of_frames) {
103 rtc::CritScope lock(&lock_);
104 if (sink_) {
105 sink_->OnData(audio_data, bits_per_sample, sample_rate, number_of_channels,
106 number_of_frames);
107 }
108}
109
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800110void LocalAudioSinkAdapter::SetSink(cricket::AudioSource::Sink* sink) {
deadbeef70ab1a12015-09-28 16:53:55 -0700111 rtc::CritScope lock(&lock_);
nisseede5da42017-01-12 05:15:36 -0800112 RTC_DCHECK(!sink || !sink_);
deadbeef70ab1a12015-09-28 16:53:55 -0700113 sink_ = sink;
114}
115
Steve Anton47136dd2018-01-12 10:49:35 -0800116AudioRtpSender::AudioRtpSender(rtc::Thread* worker_thread,
Steve Anton111fdfd2018-06-25 13:03:36 -0700117 const std::string& id,
deadbeefe1f9d832016-01-14 15:35:42 -0800118 StatsCollector* stats)
Steve Anton47136dd2018-01-12 10:49:35 -0800119 : worker_thread_(worker_thread),
Steve Anton111fdfd2018-06-25 13:03:36 -0700120 id_(id),
deadbeefe1f9d832016-01-14 15:35:42 -0800121 stats_(stats),
Steve Anton02ee47c2018-01-10 16:26:06 -0800122 dtmf_sender_proxy_(DtmfSenderProxy::Create(
123 rtc::Thread::Current(),
Steve Antonb983bae2018-06-20 11:16:53 -0700124 DtmfSender::Create(rtc::Thread::Current(), this))),
Steve Anton111fdfd2018-06-25 13:03:36 -0700125 sink_adapter_(new LocalAudioSinkAdapter()) {
Steve Anton47136dd2018-01-12 10:49:35 -0800126 RTC_DCHECK(worker_thread);
deadbeef20cb0c12017-02-01 20:27:00 -0800127}
deadbeeffac06552015-11-25 11:26:01 -0800128
deadbeef70ab1a12015-09-28 16:53:55 -0700129AudioRtpSender::~AudioRtpSender() {
deadbeef20cb0c12017-02-01 20:27:00 -0800130 // For DtmfSender.
131 SignalDestroyed();
deadbeef70ab1a12015-09-28 16:53:55 -0700132 Stop();
133}
134
deadbeef20cb0c12017-02-01 20:27:00 -0800135bool AudioRtpSender::CanInsertDtmf() {
Steve Anton47136dd2018-01-12 10:49:35 -0800136 if (!media_channel_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100137 RTC_LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
deadbeef20cb0c12017-02-01 20:27:00 -0800138 return false;
139 }
140 // Check that this RTP sender is active (description has been applied that
141 // matches an SSRC to its ID).
142 if (!ssrc_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100143 RTC_LOG(LS_ERROR) << "CanInsertDtmf: Sender does not have SSRC.";
deadbeef20cb0c12017-02-01 20:27:00 -0800144 return false;
145 }
Steve Anton47136dd2018-01-12 10:49:35 -0800146 return worker_thread_->Invoke<bool>(
147 RTC_FROM_HERE, [&] { return media_channel_->CanInsertDtmf(); });
deadbeef20cb0c12017-02-01 20:27:00 -0800148}
149
150bool AudioRtpSender::InsertDtmf(int code, int duration) {
Steve Anton47136dd2018-01-12 10:49:35 -0800151 if (!media_channel_) {
Jonas Olsson45cc8902018-02-13 10:37:07 +0100152 RTC_LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
deadbeef20cb0c12017-02-01 20:27:00 -0800153 return false;
154 }
155 if (!ssrc_) {
Jonas Olsson45cc8902018-02-13 10:37:07 +0100156 RTC_LOG(LS_ERROR) << "InsertDtmf: Sender does not have SSRC.";
deadbeef20cb0c12017-02-01 20:27:00 -0800157 return false;
158 }
Steve Anton47136dd2018-01-12 10:49:35 -0800159 bool success = worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] {
160 return media_channel_->InsertDtmf(ssrc_, code, duration);
161 });
162 if (!success) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100163 RTC_LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
deadbeef20cb0c12017-02-01 20:27:00 -0800164 }
Steve Anton47136dd2018-01-12 10:49:35 -0800165 return success;
deadbeef20cb0c12017-02-01 20:27:00 -0800166}
167
168sigslot::signal0<>* AudioRtpSender::GetOnDestroyedSignal() {
169 return &SignalDestroyed;
170}
171
deadbeef70ab1a12015-09-28 16:53:55 -0700172void AudioRtpSender::OnChanged() {
Peter Boströmdabc9442016-04-11 11:45:14 +0200173 TRACE_EVENT0("webrtc", "AudioRtpSender::OnChanged");
deadbeeffac06552015-11-25 11:26:01 -0800174 RTC_DCHECK(!stopped_);
deadbeef70ab1a12015-09-28 16:53:55 -0700175 if (cached_track_enabled_ != track_->enabled()) {
176 cached_track_enabled_ = track_->enabled();
deadbeeffac06552015-11-25 11:26:01 -0800177 if (can_send_track()) {
178 SetAudioSend();
179 }
deadbeef70ab1a12015-09-28 16:53:55 -0700180 }
181}
182
183bool AudioRtpSender::SetTrack(MediaStreamTrackInterface* track) {
Peter Boströmdabc9442016-04-11 11:45:14 +0200184 TRACE_EVENT0("webrtc", "AudioRtpSender::SetTrack");
deadbeeffac06552015-11-25 11:26:01 -0800185 if (stopped_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100186 RTC_LOG(LS_ERROR) << "SetTrack can't be called on a stopped RtpSender.";
deadbeeffac06552015-11-25 11:26:01 -0800187 return false;
188 }
189 if (track && track->kind() != MediaStreamTrackInterface::kAudioKind) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100190 RTC_LOG(LS_ERROR) << "SetTrack called on audio RtpSender with "
191 << track->kind() << " track.";
deadbeef70ab1a12015-09-28 16:53:55 -0700192 return false;
193 }
194 AudioTrackInterface* audio_track = static_cast<AudioTrackInterface*>(track);
195
196 // Detach from old track.
deadbeeffac06552015-11-25 11:26:01 -0800197 if (track_) {
198 track_->RemoveSink(sink_adapter_.get());
199 track_->UnregisterObserver(this);
200 }
201
202 if (can_send_track() && stats_) {
203 stats_->RemoveLocalAudioTrack(track_.get(), ssrc_);
204 }
deadbeef70ab1a12015-09-28 16:53:55 -0700205
206 // Attach to new track.
deadbeeffac06552015-11-25 11:26:01 -0800207 bool prev_can_send_track = can_send_track();
deadbeef5dd42fd2016-05-02 16:20:01 -0700208 // Keep a reference to the old track to keep it alive until we call
209 // SetAudioSend.
210 rtc::scoped_refptr<AudioTrackInterface> old_track = track_;
deadbeef70ab1a12015-09-28 16:53:55 -0700211 track_ = audio_track;
deadbeeffac06552015-11-25 11:26:01 -0800212 if (track_) {
213 cached_track_enabled_ = track_->enabled();
214 track_->RegisterObserver(this);
215 track_->AddSink(sink_adapter_.get());
216 }
217
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700218 // Update audio channel.
deadbeeffac06552015-11-25 11:26:01 -0800219 if (can_send_track()) {
220 SetAudioSend();
221 if (stats_) {
222 stats_->AddLocalAudioTrack(track_.get(), ssrc_);
223 }
224 } else if (prev_can_send_track) {
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700225 ClearAudioSend();
deadbeeffac06552015-11-25 11:26:01 -0800226 }
Steve Anton111fdfd2018-06-25 13:03:36 -0700227 attachment_id_ = (track_ ? GenerateUniqueId() : 0);
deadbeef70ab1a12015-09-28 16:53:55 -0700228 return true;
229}
230
Florent Castellicebf50f2018-05-03 15:31:53 +0200231RtpParameters AudioRtpSender::GetParameters() {
Steve Anton47136dd2018-01-12 10:49:35 -0800232 if (!media_channel_ || stopped_) {
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700233 return RtpParameters();
234 }
Steve Anton47136dd2018-01-12 10:49:35 -0800235 return worker_thread_->Invoke<RtpParameters>(RTC_FROM_HERE, [&] {
Florent Castellicebf50f2018-05-03 15:31:53 +0200236 RtpParameters result = media_channel_->GetRtpSendParameters(ssrc_);
237 last_transaction_id_ = rtc::CreateRandomUuid();
238 result.transaction_id = last_transaction_id_.value();
239 return result;
Steve Anton47136dd2018-01-12 10:49:35 -0800240 });
deadbeefa601f5c2016-06-06 14:27:39 -0700241}
242
Zach Steinba37b4b2018-01-23 15:02:36 -0800243RTCError AudioRtpSender::SetParameters(const RtpParameters& parameters) {
deadbeefa601f5c2016-06-06 14:27:39 -0700244 TRACE_EVENT0("webrtc", "AudioRtpSender::SetParameters");
Steve Anton47136dd2018-01-12 10:49:35 -0800245 if (!media_channel_ || stopped_) {
Zach Steinba37b4b2018-01-23 15:02:36 -0800246 return RTCError(RTCErrorType::INVALID_STATE);
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700247 }
Florent Castellicebf50f2018-05-03 15:31:53 +0200248 if (!last_transaction_id_) {
249 LOG_AND_RETURN_ERROR(
250 RTCErrorType::INVALID_STATE,
251 "Failed to set parameters since getParameters() has never been called"
252 " on this sender");
253 }
254 if (last_transaction_id_ != parameters.transaction_id) {
255 LOG_AND_RETURN_ERROR(
256 RTCErrorType::INVALID_MODIFICATION,
257 "Failed to set parameters since the transaction_id doesn't match"
258 " the last value returned from getParameters()");
259 }
260
Seth Hampson2d2c8882018-05-16 16:02:32 -0700261 if (UnimplementedRtpParameterHasValue(parameters)) {
262 LOG_AND_RETURN_ERROR(
263 RTCErrorType::UNSUPPORTED_PARAMETER,
264 "Attempted to set an unimplemented parameter of RtpParameters.");
265 }
Zach Steinba37b4b2018-01-23 15:02:36 -0800266 return worker_thread_->Invoke<RTCError>(RTC_FROM_HERE, [&] {
Florent Castellicebf50f2018-05-03 15:31:53 +0200267 RTCError result = media_channel_->SetRtpSendParameters(ssrc_, parameters);
268 last_transaction_id_.reset();
269 return result;
Steve Anton47136dd2018-01-12 10:49:35 -0800270 });
deadbeefa601f5c2016-06-06 14:27:39 -0700271}
272
deadbeef20cb0c12017-02-01 20:27:00 -0800273rtc::scoped_refptr<DtmfSenderInterface> AudioRtpSender::GetDtmfSender() const {
274 return dtmf_sender_proxy_;
275}
276
deadbeeffac06552015-11-25 11:26:01 -0800277void AudioRtpSender::SetSsrc(uint32_t ssrc) {
Peter Boströmdabc9442016-04-11 11:45:14 +0200278 TRACE_EVENT0("webrtc", "AudioRtpSender::SetSsrc");
deadbeeffac06552015-11-25 11:26:01 -0800279 if (stopped_ || ssrc == ssrc_) {
280 return;
281 }
282 // If we are already sending with a particular SSRC, stop sending.
283 if (can_send_track()) {
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700284 ClearAudioSend();
deadbeeffac06552015-11-25 11:26:01 -0800285 if (stats_) {
286 stats_->RemoveLocalAudioTrack(track_.get(), ssrc_);
287 }
288 }
289 ssrc_ = ssrc;
290 if (can_send_track()) {
291 SetAudioSend();
292 if (stats_) {
293 stats_->AddLocalAudioTrack(track_.get(), ssrc_);
294 }
295 }
296}
297
deadbeef70ab1a12015-09-28 16:53:55 -0700298void AudioRtpSender::Stop() {
Peter Boströmdabc9442016-04-11 11:45:14 +0200299 TRACE_EVENT0("webrtc", "AudioRtpSender::Stop");
deadbeef70ab1a12015-09-28 16:53:55 -0700300 // TODO(deadbeef): Need to do more here to fully stop sending packets.
deadbeeffac06552015-11-25 11:26:01 -0800301 if (stopped_) {
deadbeef70ab1a12015-09-28 16:53:55 -0700302 return;
303 }
deadbeeffac06552015-11-25 11:26:01 -0800304 if (track_) {
305 track_->RemoveSink(sink_adapter_.get());
306 track_->UnregisterObserver(this);
307 }
308 if (can_send_track()) {
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700309 ClearAudioSend();
deadbeeffac06552015-11-25 11:26:01 -0800310 if (stats_) {
311 stats_->RemoveLocalAudioTrack(track_.get(), ssrc_);
312 }
313 }
Harald Alvestrand3d976f62018-03-19 19:05:06 +0100314 media_channel_ = nullptr;
deadbeeffac06552015-11-25 11:26:01 -0800315 stopped_ = true;
deadbeef70ab1a12015-09-28 16:53:55 -0700316}
317
deadbeeffac06552015-11-25 11:26:01 -0800318void AudioRtpSender::SetAudioSend() {
kwibergee89e782017-08-09 17:22:01 -0700319 RTC_DCHECK(!stopped_);
320 RTC_DCHECK(can_send_track());
Steve Anton47136dd2018-01-12 10:49:35 -0800321 if (!media_channel_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100322 RTC_LOG(LS_ERROR) << "SetAudioSend: No audio channel exists.";
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700323 return;
324 }
deadbeef70ab1a12015-09-28 16:53:55 -0700325 cricket::AudioOptions options;
agouaillardb11fb252017-02-03 06:37:05 -0800326#if !defined(WEBRTC_CHROMIUM_BUILD) && !defined(WEBRTC_WEBKIT_BUILD)
Tommi3c169782016-01-21 16:12:17 +0100327 // TODO(tommi): Remove this hack when we move CreateAudioSource out of
328 // PeerConnection. This is a bit of a strange way to apply local audio
329 // options since it is also applied to all streams/channels, local or remote.
tommi6eca7e32015-12-15 04:27:11 -0800330 if (track_->enabled() && track_->GetSource() &&
331 !track_->GetSource()->remote()) {
deadbeef70ab1a12015-09-28 16:53:55 -0700332 // TODO(xians): Remove this static_cast since we should be able to connect
deadbeeffac06552015-11-25 11:26:01 -0800333 // a remote audio track to a peer connection.
deadbeef70ab1a12015-09-28 16:53:55 -0700334 options = static_cast<LocalAudioSource*>(track_->GetSource())->options();
335 }
Tommi3c169782016-01-21 16:12:17 +0100336#endif
deadbeef70ab1a12015-09-28 16:53:55 -0700337
Steve Anton47136dd2018-01-12 10:49:35 -0800338 // |track_->enabled()| hops to the signaling thread, so call it before we hop
339 // to the worker thread or else it will deadlock.
340 bool track_enabled = track_->enabled();
341 bool success = worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] {
342 return media_channel_->SetAudioSend(ssrc_, track_enabled, &options,
343 sink_adapter_.get());
344 });
345 if (!success) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100346 RTC_LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc_;
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700347 }
348}
349
350void AudioRtpSender::ClearAudioSend() {
351 RTC_DCHECK(ssrc_ != 0);
352 RTC_DCHECK(!stopped_);
Steve Anton47136dd2018-01-12 10:49:35 -0800353 if (!media_channel_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100354 RTC_LOG(LS_WARNING) << "ClearAudioSend: No audio channel exists.";
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700355 return;
356 }
357 cricket::AudioOptions options;
Steve Anton47136dd2018-01-12 10:49:35 -0800358 bool success = worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] {
359 return media_channel_->SetAudioSend(ssrc_, false, &options, nullptr);
360 });
361 if (!success) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100362 RTC_LOG(LS_WARNING) << "ClearAudioSend: ssrc is incorrect: " << ssrc_;
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700363 }
deadbeef70ab1a12015-09-28 16:53:55 -0700364}
365
Steve Anton47136dd2018-01-12 10:49:35 -0800366VideoRtpSender::VideoRtpSender(rtc::Thread* worker_thread,
Steve Anton111fdfd2018-06-25 13:03:36 -0700367 const std::string& id)
368 : worker_thread_(worker_thread), id_(id) {
Steve Anton47136dd2018-01-12 10:49:35 -0800369 RTC_DCHECK(worker_thread);
deadbeef20cb0c12017-02-01 20:27:00 -0800370}
371
deadbeef70ab1a12015-09-28 16:53:55 -0700372VideoRtpSender::~VideoRtpSender() {
deadbeef70ab1a12015-09-28 16:53:55 -0700373 Stop();
374}
375
376void VideoRtpSender::OnChanged() {
Peter Boströmdabc9442016-04-11 11:45:14 +0200377 TRACE_EVENT0("webrtc", "VideoRtpSender::OnChanged");
deadbeeffac06552015-11-25 11:26:01 -0800378 RTC_DCHECK(!stopped_);
Niels Möllerff40b142018-04-09 08:49:14 +0200379 if (cached_track_content_hint_ != track_->content_hint()) {
pbos5214a0a2016-12-16 15:39:11 -0800380 cached_track_content_hint_ = track_->content_hint();
deadbeeffac06552015-11-25 11:26:01 -0800381 if (can_send_track()) {
382 SetVideoSend();
383 }
deadbeef70ab1a12015-09-28 16:53:55 -0700384 }
385}
386
387bool VideoRtpSender::SetTrack(MediaStreamTrackInterface* track) {
Peter Boströmdabc9442016-04-11 11:45:14 +0200388 TRACE_EVENT0("webrtc", "VideoRtpSender::SetTrack");
deadbeeffac06552015-11-25 11:26:01 -0800389 if (stopped_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100390 RTC_LOG(LS_ERROR) << "SetTrack can't be called on a stopped RtpSender.";
deadbeeffac06552015-11-25 11:26:01 -0800391 return false;
392 }
393 if (track && track->kind() != MediaStreamTrackInterface::kVideoKind) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100394 RTC_LOG(LS_ERROR) << "SetTrack called on video RtpSender with "
395 << track->kind() << " track.";
deadbeef70ab1a12015-09-28 16:53:55 -0700396 return false;
397 }
398 VideoTrackInterface* video_track = static_cast<VideoTrackInterface*>(track);
399
400 // Detach from old track.
deadbeeffac06552015-11-25 11:26:01 -0800401 if (track_) {
402 track_->UnregisterObserver(this);
403 }
deadbeef70ab1a12015-09-28 16:53:55 -0700404
405 // Attach to new track.
deadbeeffac06552015-11-25 11:26:01 -0800406 bool prev_can_send_track = can_send_track();
deadbeef5dd42fd2016-05-02 16:20:01 -0700407 // Keep a reference to the old track to keep it alive until we call
deadbeef5a4a75a2016-06-02 16:23:38 -0700408 // SetVideoSend.
deadbeef5dd42fd2016-05-02 16:20:01 -0700409 rtc::scoped_refptr<VideoTrackInterface> old_track = track_;
deadbeef70ab1a12015-09-28 16:53:55 -0700410 track_ = video_track;
deadbeeffac06552015-11-25 11:26:01 -0800411 if (track_) {
pbos5214a0a2016-12-16 15:39:11 -0800412 cached_track_content_hint_ = track_->content_hint();
deadbeeffac06552015-11-25 11:26:01 -0800413 track_->RegisterObserver(this);
414 }
415
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700416 // Update video channel.
deadbeeffac06552015-11-25 11:26:01 -0800417 if (can_send_track()) {
deadbeeffac06552015-11-25 11:26:01 -0800418 SetVideoSend();
419 } else if (prev_can_send_track) {
deadbeef5a4a75a2016-06-02 16:23:38 -0700420 ClearVideoSend();
deadbeeffac06552015-11-25 11:26:01 -0800421 }
Steve Anton111fdfd2018-06-25 13:03:36 -0700422 attachment_id_ = (track_ ? GenerateUniqueId() : 0);
deadbeef70ab1a12015-09-28 16:53:55 -0700423 return true;
424}
425
Florent Castellicebf50f2018-05-03 15:31:53 +0200426RtpParameters VideoRtpSender::GetParameters() {
Steve Anton47136dd2018-01-12 10:49:35 -0800427 if (!media_channel_ || stopped_) {
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700428 return RtpParameters();
429 }
Steve Anton47136dd2018-01-12 10:49:35 -0800430 return worker_thread_->Invoke<RtpParameters>(RTC_FROM_HERE, [&] {
Florent Castellicebf50f2018-05-03 15:31:53 +0200431 RtpParameters result = media_channel_->GetRtpSendParameters(ssrc_);
432 last_transaction_id_ = rtc::CreateRandomUuid();
433 result.transaction_id = last_transaction_id_.value();
434 return result;
Steve Anton47136dd2018-01-12 10:49:35 -0800435 });
deadbeefa601f5c2016-06-06 14:27:39 -0700436}
437
Zach Steinba37b4b2018-01-23 15:02:36 -0800438RTCError VideoRtpSender::SetParameters(const RtpParameters& parameters) {
deadbeefa601f5c2016-06-06 14:27:39 -0700439 TRACE_EVENT0("webrtc", "VideoRtpSender::SetParameters");
Steve Anton47136dd2018-01-12 10:49:35 -0800440 if (!media_channel_ || stopped_) {
Zach Steinba37b4b2018-01-23 15:02:36 -0800441 return RTCError(RTCErrorType::INVALID_STATE);
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700442 }
Florent Castellicebf50f2018-05-03 15:31:53 +0200443 if (!last_transaction_id_) {
444 LOG_AND_RETURN_ERROR(
445 RTCErrorType::INVALID_STATE,
446 "Failed to set parameters since getParameters() has never been called"
447 " on this sender");
448 }
449 if (last_transaction_id_ != parameters.transaction_id) {
450 LOG_AND_RETURN_ERROR(
451 RTCErrorType::INVALID_MODIFICATION,
452 "Failed to set parameters since the transaction_id doesn't match"
453 " the last value returned from getParameters()");
454 }
455
Seth Hampson2d2c8882018-05-16 16:02:32 -0700456 if (UnimplementedRtpParameterHasValue(parameters)) {
457 LOG_AND_RETURN_ERROR(
458 RTCErrorType::UNSUPPORTED_PARAMETER,
459 "Attempted to set an unimplemented parameter of RtpParameters.");
460 }
Zach Steinba37b4b2018-01-23 15:02:36 -0800461 return worker_thread_->Invoke<RTCError>(RTC_FROM_HERE, [&] {
Florent Castellicebf50f2018-05-03 15:31:53 +0200462 RTCError result = media_channel_->SetRtpSendParameters(ssrc_, parameters);
463 last_transaction_id_.reset();
464 return result;
Steve Anton47136dd2018-01-12 10:49:35 -0800465 });
deadbeefa601f5c2016-06-06 14:27:39 -0700466}
467
deadbeef20cb0c12017-02-01 20:27:00 -0800468rtc::scoped_refptr<DtmfSenderInterface> VideoRtpSender::GetDtmfSender() const {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100469 RTC_LOG(LS_ERROR) << "Tried to get DTMF sender from video sender.";
deadbeef20cb0c12017-02-01 20:27:00 -0800470 return nullptr;
471}
472
deadbeeffac06552015-11-25 11:26:01 -0800473void VideoRtpSender::SetSsrc(uint32_t ssrc) {
Peter Boströmdabc9442016-04-11 11:45:14 +0200474 TRACE_EVENT0("webrtc", "VideoRtpSender::SetSsrc");
deadbeeffac06552015-11-25 11:26:01 -0800475 if (stopped_ || ssrc == ssrc_) {
476 return;
477 }
478 // If we are already sending with a particular SSRC, stop sending.
479 if (can_send_track()) {
deadbeef5a4a75a2016-06-02 16:23:38 -0700480 ClearVideoSend();
deadbeeffac06552015-11-25 11:26:01 -0800481 }
482 ssrc_ = ssrc;
483 if (can_send_track()) {
deadbeeffac06552015-11-25 11:26:01 -0800484 SetVideoSend();
485 }
486}
487
deadbeef70ab1a12015-09-28 16:53:55 -0700488void VideoRtpSender::Stop() {
Peter Boströmdabc9442016-04-11 11:45:14 +0200489 TRACE_EVENT0("webrtc", "VideoRtpSender::Stop");
deadbeef70ab1a12015-09-28 16:53:55 -0700490 // TODO(deadbeef): Need to do more here to fully stop sending packets.
deadbeeffac06552015-11-25 11:26:01 -0800491 if (stopped_) {
deadbeef70ab1a12015-09-28 16:53:55 -0700492 return;
493 }
deadbeeffac06552015-11-25 11:26:01 -0800494 if (track_) {
495 track_->UnregisterObserver(this);
496 }
497 if (can_send_track()) {
deadbeef5a4a75a2016-06-02 16:23:38 -0700498 ClearVideoSend();
deadbeeffac06552015-11-25 11:26:01 -0800499 }
Harald Alvestrand3d976f62018-03-19 19:05:06 +0100500 media_channel_ = nullptr;
deadbeeffac06552015-11-25 11:26:01 -0800501 stopped_ = true;
deadbeef70ab1a12015-09-28 16:53:55 -0700502}
503
deadbeeffac06552015-11-25 11:26:01 -0800504void VideoRtpSender::SetVideoSend() {
kwibergee89e782017-08-09 17:22:01 -0700505 RTC_DCHECK(!stopped_);
506 RTC_DCHECK(can_send_track());
Steve Anton47136dd2018-01-12 10:49:35 -0800507 if (!media_channel_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100508 RTC_LOG(LS_ERROR) << "SetVideoSend: No video channel exists.";
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700509 return;
510 }
perkj0d3eef22016-03-09 02:39:17 +0100511 cricket::VideoOptions options;
perkja3ede6c2016-03-08 01:27:48 +0100512 VideoTrackSourceInterface* source = track_->GetSource();
perkj0d3eef22016-03-09 02:39:17 +0100513 if (source) {
Oskar Sundbom36f8f3e2017-11-16 10:54:27 +0100514 options.is_screencast = source->is_screencast();
Perc0d31e92016-03-31 17:23:39 +0200515 options.video_noise_reduction = source->needs_denoising();
deadbeef70ab1a12015-09-28 16:53:55 -0700516 }
pbos5214a0a2016-12-16 15:39:11 -0800517 switch (cached_track_content_hint_) {
518 case VideoTrackInterface::ContentHint::kNone:
519 break;
520 case VideoTrackInterface::ContentHint::kFluid:
Oskar Sundbom36f8f3e2017-11-16 10:54:27 +0100521 options.is_screencast = false;
pbos5214a0a2016-12-16 15:39:11 -0800522 break;
523 case VideoTrackInterface::ContentHint::kDetailed:
Harald Alvestrandc19ab072018-06-18 08:53:10 +0200524 case VideoTrackInterface::ContentHint::kText:
Oskar Sundbom36f8f3e2017-11-16 10:54:27 +0100525 options.is_screencast = true;
pbos5214a0a2016-12-16 15:39:11 -0800526 break;
527 }
Steve Anton47136dd2018-01-12 10:49:35 -0800528 bool success = worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] {
Yves Gerey665174f2018-06-19 15:03:05 +0200529 return media_channel_->SetVideoSend(ssrc_, &options, track_);
Steve Anton47136dd2018-01-12 10:49:35 -0800530 });
531 RTC_DCHECK(success);
deadbeef5a4a75a2016-06-02 16:23:38 -0700532}
533
534void VideoRtpSender::ClearVideoSend() {
535 RTC_DCHECK(ssrc_ != 0);
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700536 RTC_DCHECK(!stopped_);
Steve Anton47136dd2018-01-12 10:49:35 -0800537 if (!media_channel_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100538 RTC_LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700539 return;
540 }
541 // Allow SetVideoSend to fail since |enable| is false and |source| is null.
542 // This the normal case when the underlying media channel has already been
543 // deleted.
Steve Anton47136dd2018-01-12 10:49:35 -0800544 worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] {
Niels Möllerff40b142018-04-09 08:49:14 +0200545 return media_channel_->SetVideoSend(ssrc_, nullptr, nullptr);
Steve Anton47136dd2018-01-12 10:49:35 -0800546 });
deadbeef70ab1a12015-09-28 16:53:55 -0700547}
548
549} // namespace webrtc