deadbeef | 6979b02 | 2015-09-24 16:47:53 -0700 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
deadbeef | 6979b02 | 2015-09-24 16:47:53 -0700 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 4 | * 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. |
deadbeef | 6979b02 | 2015-09-24 16:47:53 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "pc/rtpsender.h" |
deadbeef | 6979b02 | 2015-09-24 16:47:53 -0700 | [diff] [blame] | 12 | |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 13 | #include <vector> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "api/mediastreaminterface.h" |
| 16 | #include "pc/localaudiosource.h" |
Steve Anton | 2d8609c | 2018-01-23 16:38:46 -0800 | [diff] [blame] | 17 | #include "pc/statscollector.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/checks.h" |
| 19 | #include "rtc_base/helpers.h" |
| 20 | #include "rtc_base/trace_event.h" |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 24 | namespace { |
| 25 | |
| 26 | // This function is only expected to be called on the signalling thread. |
| 27 | int GenerateUniqueId() { |
| 28 | static int g_unique_id = 0; |
| 29 | |
| 30 | return ++g_unique_id; |
| 31 | } |
| 32 | |
| 33 | } // namespace |
| 34 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 35 | LocalAudioSinkAdapter::LocalAudioSinkAdapter() : sink_(nullptr) {} |
| 36 | |
| 37 | LocalAudioSinkAdapter::~LocalAudioSinkAdapter() { |
| 38 | rtc::CritScope lock(&lock_); |
| 39 | if (sink_) |
| 40 | sink_->OnClose(); |
| 41 | } |
| 42 | |
| 43 | void LocalAudioSinkAdapter::OnData(const void* audio_data, |
| 44 | int bits_per_sample, |
| 45 | int sample_rate, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 46 | size_t number_of_channels, |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 47 | size_t number_of_frames) { |
| 48 | rtc::CritScope lock(&lock_); |
| 49 | if (sink_) { |
| 50 | sink_->OnData(audio_data, bits_per_sample, sample_rate, number_of_channels, |
| 51 | number_of_frames); |
| 52 | } |
| 53 | } |
| 54 | |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 55 | void LocalAudioSinkAdapter::SetSink(cricket::AudioSource::Sink* sink) { |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 56 | rtc::CritScope lock(&lock_); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 57 | RTC_DCHECK(!sink || !sink_); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 58 | sink_ = sink; |
| 59 | } |
| 60 | |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 61 | AudioRtpSender::AudioRtpSender(rtc::Thread* worker_thread, |
| 62 | StatsCollector* stats) |
| 63 | : AudioRtpSender(worker_thread, nullptr, {rtc::CreateRandomUuid()}, stats) { |
| 64 | } |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 65 | |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 66 | AudioRtpSender::AudioRtpSender(rtc::Thread* worker_thread, |
| 67 | rtc::scoped_refptr<AudioTrackInterface> track, |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 68 | const std::vector<std::string>& stream_labels, |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 69 | StatsCollector* stats) |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 70 | : worker_thread_(worker_thread), |
| 71 | id_(track ? track->id() : rtc::CreateRandomUuid()), |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 72 | stream_ids_(stream_labels), |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 73 | stats_(stats), |
| 74 | track_(track), |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 75 | dtmf_sender_proxy_(DtmfSenderProxy::Create( |
| 76 | rtc::Thread::Current(), |
| 77 | DtmfSender::Create(track_, rtc::Thread::Current(), this))), |
| 78 | cached_track_enabled_(track ? track->enabled() : false), |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 79 | sink_adapter_(new LocalAudioSinkAdapter()), |
| 80 | attachment_id_(track ? GenerateUniqueId() : 0) { |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 81 | RTC_DCHECK(worker_thread); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 82 | // TODO(bugs.webrtc.org/7932): Remove once zero or multiple streams are |
| 83 | // supported. |
| 84 | RTC_DCHECK_EQ(stream_labels.size(), 1u); |
| 85 | if (track_) { |
| 86 | track_->RegisterObserver(this); |
| 87 | track_->AddSink(sink_adapter_.get()); |
| 88 | } |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 89 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 90 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 91 | AudioRtpSender::~AudioRtpSender() { |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 92 | // For DtmfSender. |
| 93 | SignalDestroyed(); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 94 | Stop(); |
| 95 | } |
| 96 | |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 97 | bool AudioRtpSender::CanInsertDtmf() { |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 98 | if (!media_channel_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 99 | RTC_LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 100 | return false; |
| 101 | } |
| 102 | // Check that this RTP sender is active (description has been applied that |
| 103 | // matches an SSRC to its ID). |
| 104 | if (!ssrc_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 105 | RTC_LOG(LS_ERROR) << "CanInsertDtmf: Sender does not have SSRC."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 106 | return false; |
| 107 | } |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 108 | return worker_thread_->Invoke<bool>( |
| 109 | RTC_FROM_HERE, [&] { return media_channel_->CanInsertDtmf(); }); |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | bool AudioRtpSender::InsertDtmf(int code, int duration) { |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 113 | if (!media_channel_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 114 | RTC_LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 115 | return false; |
| 116 | } |
| 117 | if (!ssrc_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 118 | RTC_LOG(LS_ERROR) << "CanInsertDtmf: Sender does not have SSRC."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 119 | return false; |
| 120 | } |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 121 | bool success = worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] { |
| 122 | return media_channel_->InsertDtmf(ssrc_, code, duration); |
| 123 | }); |
| 124 | if (!success) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 125 | RTC_LOG(LS_ERROR) << "Failed to insert DTMF to channel."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 126 | } |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 127 | return success; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | sigslot::signal0<>* AudioRtpSender::GetOnDestroyedSignal() { |
| 131 | return &SignalDestroyed; |
| 132 | } |
| 133 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 134 | void AudioRtpSender::OnChanged() { |
Peter Boström | dabc944 | 2016-04-11 11:45:14 +0200 | [diff] [blame] | 135 | TRACE_EVENT0("webrtc", "AudioRtpSender::OnChanged"); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 136 | RTC_DCHECK(!stopped_); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 137 | if (cached_track_enabled_ != track_->enabled()) { |
| 138 | cached_track_enabled_ = track_->enabled(); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 139 | if (can_send_track()) { |
| 140 | SetAudioSend(); |
| 141 | } |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | bool AudioRtpSender::SetTrack(MediaStreamTrackInterface* track) { |
Peter Boström | dabc944 | 2016-04-11 11:45:14 +0200 | [diff] [blame] | 146 | TRACE_EVENT0("webrtc", "AudioRtpSender::SetTrack"); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 147 | if (stopped_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 148 | RTC_LOG(LS_ERROR) << "SetTrack can't be called on a stopped RtpSender."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 149 | return false; |
| 150 | } |
| 151 | if (track && track->kind() != MediaStreamTrackInterface::kAudioKind) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 152 | RTC_LOG(LS_ERROR) << "SetTrack called on audio RtpSender with " |
| 153 | << track->kind() << " track."; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 154 | return false; |
| 155 | } |
| 156 | AudioTrackInterface* audio_track = static_cast<AudioTrackInterface*>(track); |
| 157 | |
| 158 | // Detach from old track. |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 159 | if (track_) { |
| 160 | track_->RemoveSink(sink_adapter_.get()); |
| 161 | track_->UnregisterObserver(this); |
| 162 | } |
| 163 | |
| 164 | if (can_send_track() && stats_) { |
| 165 | stats_->RemoveLocalAudioTrack(track_.get(), ssrc_); |
| 166 | } |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 167 | |
| 168 | // Attach to new track. |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 169 | bool prev_can_send_track = can_send_track(); |
deadbeef | 5dd42fd | 2016-05-02 16:20:01 -0700 | [diff] [blame] | 170 | // Keep a reference to the old track to keep it alive until we call |
| 171 | // SetAudioSend. |
| 172 | rtc::scoped_refptr<AudioTrackInterface> old_track = track_; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 173 | track_ = audio_track; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 174 | if (track_) { |
| 175 | cached_track_enabled_ = track_->enabled(); |
| 176 | track_->RegisterObserver(this); |
| 177 | track_->AddSink(sink_adapter_.get()); |
| 178 | } |
| 179 | |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 180 | // Update audio channel. |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 181 | if (can_send_track()) { |
| 182 | SetAudioSend(); |
| 183 | if (stats_) { |
| 184 | stats_->AddLocalAudioTrack(track_.get(), ssrc_); |
| 185 | } |
| 186 | } else if (prev_can_send_track) { |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 187 | ClearAudioSend(); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 188 | } |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 189 | attachment_id_ = GenerateUniqueId(); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 190 | return true; |
| 191 | } |
| 192 | |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 193 | RtpParameters AudioRtpSender::GetParameters() const { |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 194 | if (!media_channel_ || stopped_) { |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 195 | return RtpParameters(); |
| 196 | } |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 197 | return worker_thread_->Invoke<RtpParameters>(RTC_FROM_HERE, [&] { |
| 198 | return media_channel_->GetRtpSendParameters(ssrc_); |
| 199 | }); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Zach Stein | ba37b4b | 2018-01-23 15:02:36 -0800 | [diff] [blame] | 202 | RTCError AudioRtpSender::SetParameters(const RtpParameters& parameters) { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 203 | TRACE_EVENT0("webrtc", "AudioRtpSender::SetParameters"); |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 204 | if (!media_channel_ || stopped_) { |
Zach Stein | ba37b4b | 2018-01-23 15:02:36 -0800 | [diff] [blame] | 205 | return RTCError(RTCErrorType::INVALID_STATE); |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 206 | } |
Zach Stein | ba37b4b | 2018-01-23 15:02:36 -0800 | [diff] [blame] | 207 | return worker_thread_->Invoke<RTCError>(RTC_FROM_HERE, [&] { |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 208 | return media_channel_->SetRtpSendParameters(ssrc_, parameters); |
| 209 | }); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 210 | } |
| 211 | |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 212 | rtc::scoped_refptr<DtmfSenderInterface> AudioRtpSender::GetDtmfSender() const { |
| 213 | return dtmf_sender_proxy_; |
| 214 | } |
| 215 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 216 | void AudioRtpSender::SetSsrc(uint32_t ssrc) { |
Peter Boström | dabc944 | 2016-04-11 11:45:14 +0200 | [diff] [blame] | 217 | TRACE_EVENT0("webrtc", "AudioRtpSender::SetSsrc"); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 218 | if (stopped_ || ssrc == ssrc_) { |
| 219 | return; |
| 220 | } |
| 221 | // If we are already sending with a particular SSRC, stop sending. |
| 222 | if (can_send_track()) { |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 223 | ClearAudioSend(); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 224 | if (stats_) { |
| 225 | stats_->RemoveLocalAudioTrack(track_.get(), ssrc_); |
| 226 | } |
| 227 | } |
| 228 | ssrc_ = ssrc; |
| 229 | if (can_send_track()) { |
| 230 | SetAudioSend(); |
| 231 | if (stats_) { |
| 232 | stats_->AddLocalAudioTrack(track_.get(), ssrc_); |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 237 | void AudioRtpSender::Stop() { |
Peter Boström | dabc944 | 2016-04-11 11:45:14 +0200 | [diff] [blame] | 238 | TRACE_EVENT0("webrtc", "AudioRtpSender::Stop"); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 239 | // TODO(deadbeef): Need to do more here to fully stop sending packets. |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 240 | if (stopped_) { |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 241 | return; |
| 242 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 243 | if (track_) { |
| 244 | track_->RemoveSink(sink_adapter_.get()); |
| 245 | track_->UnregisterObserver(this); |
| 246 | } |
| 247 | if (can_send_track()) { |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 248 | ClearAudioSend(); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 249 | if (stats_) { |
| 250 | stats_->RemoveLocalAudioTrack(track_.get(), ssrc_); |
| 251 | } |
| 252 | } |
| 253 | stopped_ = true; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 254 | } |
| 255 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 256 | void AudioRtpSender::SetAudioSend() { |
kwiberg | ee89e78 | 2017-08-09 17:22:01 -0700 | [diff] [blame] | 257 | RTC_DCHECK(!stopped_); |
| 258 | RTC_DCHECK(can_send_track()); |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 259 | if (!media_channel_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 260 | RTC_LOG(LS_ERROR) << "SetAudioSend: No audio channel exists."; |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 261 | return; |
| 262 | } |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 263 | cricket::AudioOptions options; |
agouaillard | b11fb25 | 2017-02-03 06:37:05 -0800 | [diff] [blame] | 264 | #if !defined(WEBRTC_CHROMIUM_BUILD) && !defined(WEBRTC_WEBKIT_BUILD) |
Tommi | 3c16978 | 2016-01-21 16:12:17 +0100 | [diff] [blame] | 265 | // TODO(tommi): Remove this hack when we move CreateAudioSource out of |
| 266 | // PeerConnection. This is a bit of a strange way to apply local audio |
| 267 | // options since it is also applied to all streams/channels, local or remote. |
tommi | 6eca7e3 | 2015-12-15 04:27:11 -0800 | [diff] [blame] | 268 | if (track_->enabled() && track_->GetSource() && |
| 269 | !track_->GetSource()->remote()) { |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 270 | // TODO(xians): Remove this static_cast since we should be able to connect |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 271 | // a remote audio track to a peer connection. |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 272 | options = static_cast<LocalAudioSource*>(track_->GetSource())->options(); |
| 273 | } |
Tommi | 3c16978 | 2016-01-21 16:12:17 +0100 | [diff] [blame] | 274 | #endif |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 275 | |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 276 | // |track_->enabled()| hops to the signaling thread, so call it before we hop |
| 277 | // to the worker thread or else it will deadlock. |
| 278 | bool track_enabled = track_->enabled(); |
| 279 | bool success = worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] { |
| 280 | return media_channel_->SetAudioSend(ssrc_, track_enabled, &options, |
| 281 | sink_adapter_.get()); |
| 282 | }); |
| 283 | if (!success) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 284 | RTC_LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc_; |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
| 288 | void AudioRtpSender::ClearAudioSend() { |
| 289 | RTC_DCHECK(ssrc_ != 0); |
| 290 | RTC_DCHECK(!stopped_); |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 291 | if (!media_channel_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 292 | RTC_LOG(LS_WARNING) << "ClearAudioSend: No audio channel exists."; |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 293 | return; |
| 294 | } |
| 295 | cricket::AudioOptions options; |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 296 | bool success = worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] { |
| 297 | return media_channel_->SetAudioSend(ssrc_, false, &options, nullptr); |
| 298 | }); |
| 299 | if (!success) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 300 | RTC_LOG(LS_WARNING) << "ClearAudioSend: ssrc is incorrect: " << ssrc_; |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 301 | } |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 304 | VideoRtpSender::VideoRtpSender(rtc::Thread* worker_thread) |
| 305 | : VideoRtpSender(worker_thread, nullptr, {rtc::CreateRandomUuid()}) {} |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 306 | |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 307 | VideoRtpSender::VideoRtpSender(rtc::Thread* worker_thread, |
| 308 | rtc::scoped_refptr<VideoTrackInterface> track, |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 309 | const std::vector<std::string>& stream_labels) |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 310 | : worker_thread_(worker_thread), |
| 311 | id_(track ? track->id() : rtc::CreateRandomUuid()), |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 312 | stream_ids_(stream_labels), |
| 313 | track_(track), |
| 314 | cached_track_enabled_(track ? track->enabled() : false), |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 315 | cached_track_content_hint_( |
| 316 | track ? track->content_hint() |
| 317 | : VideoTrackInterface::ContentHint::kNone), |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 318 | attachment_id_(track ? GenerateUniqueId() : 0) { |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 319 | RTC_DCHECK(worker_thread); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 320 | // TODO(bugs.webrtc.org/7932): Remove once zero or multiple streams are |
| 321 | // supported. |
| 322 | RTC_DCHECK_EQ(stream_labels.size(), 1u); |
| 323 | if (track_) { |
| 324 | track_->RegisterObserver(this); |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 325 | } |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 326 | } |
| 327 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 328 | VideoRtpSender::~VideoRtpSender() { |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 329 | Stop(); |
| 330 | } |
| 331 | |
| 332 | void VideoRtpSender::OnChanged() { |
Peter Boström | dabc944 | 2016-04-11 11:45:14 +0200 | [diff] [blame] | 333 | TRACE_EVENT0("webrtc", "VideoRtpSender::OnChanged"); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 334 | RTC_DCHECK(!stopped_); |
pbos | 5214a0a | 2016-12-16 15:39:11 -0800 | [diff] [blame] | 335 | if (cached_track_enabled_ != track_->enabled() || |
| 336 | cached_track_content_hint_ != track_->content_hint()) { |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 337 | cached_track_enabled_ = track_->enabled(); |
pbos | 5214a0a | 2016-12-16 15:39:11 -0800 | [diff] [blame] | 338 | cached_track_content_hint_ = track_->content_hint(); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 339 | if (can_send_track()) { |
| 340 | SetVideoSend(); |
| 341 | } |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | |
| 345 | bool VideoRtpSender::SetTrack(MediaStreamTrackInterface* track) { |
Peter Boström | dabc944 | 2016-04-11 11:45:14 +0200 | [diff] [blame] | 346 | TRACE_EVENT0("webrtc", "VideoRtpSender::SetTrack"); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 347 | if (stopped_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 348 | RTC_LOG(LS_ERROR) << "SetTrack can't be called on a stopped RtpSender."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 349 | return false; |
| 350 | } |
| 351 | if (track && track->kind() != MediaStreamTrackInterface::kVideoKind) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 352 | RTC_LOG(LS_ERROR) << "SetTrack called on video RtpSender with " |
| 353 | << track->kind() << " track."; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 354 | return false; |
| 355 | } |
| 356 | VideoTrackInterface* video_track = static_cast<VideoTrackInterface*>(track); |
| 357 | |
| 358 | // Detach from old track. |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 359 | if (track_) { |
| 360 | track_->UnregisterObserver(this); |
| 361 | } |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 362 | |
| 363 | // Attach to new track. |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 364 | bool prev_can_send_track = can_send_track(); |
deadbeef | 5dd42fd | 2016-05-02 16:20:01 -0700 | [diff] [blame] | 365 | // Keep a reference to the old track to keep it alive until we call |
deadbeef | 5a4a75a | 2016-06-02 16:23:38 -0700 | [diff] [blame] | 366 | // SetVideoSend. |
deadbeef | 5dd42fd | 2016-05-02 16:20:01 -0700 | [diff] [blame] | 367 | rtc::scoped_refptr<VideoTrackInterface> old_track = track_; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 368 | track_ = video_track; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 369 | if (track_) { |
| 370 | cached_track_enabled_ = track_->enabled(); |
pbos | 5214a0a | 2016-12-16 15:39:11 -0800 | [diff] [blame] | 371 | cached_track_content_hint_ = track_->content_hint(); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 372 | track_->RegisterObserver(this); |
| 373 | } |
| 374 | |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 375 | // Update video channel. |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 376 | if (can_send_track()) { |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 377 | SetVideoSend(); |
| 378 | } else if (prev_can_send_track) { |
deadbeef | 5a4a75a | 2016-06-02 16:23:38 -0700 | [diff] [blame] | 379 | ClearVideoSend(); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 380 | } |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 381 | attachment_id_ = GenerateUniqueId(); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 382 | return true; |
| 383 | } |
| 384 | |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 385 | RtpParameters VideoRtpSender::GetParameters() const { |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 386 | if (!media_channel_ || stopped_) { |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 387 | return RtpParameters(); |
| 388 | } |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 389 | return worker_thread_->Invoke<RtpParameters>(RTC_FROM_HERE, [&] { |
| 390 | return media_channel_->GetRtpSendParameters(ssrc_); |
| 391 | }); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Zach Stein | ba37b4b | 2018-01-23 15:02:36 -0800 | [diff] [blame] | 394 | RTCError VideoRtpSender::SetParameters(const RtpParameters& parameters) { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 395 | TRACE_EVENT0("webrtc", "VideoRtpSender::SetParameters"); |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 396 | if (!media_channel_ || stopped_) { |
Zach Stein | ba37b4b | 2018-01-23 15:02:36 -0800 | [diff] [blame] | 397 | return RTCError(RTCErrorType::INVALID_STATE); |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 398 | } |
Zach Stein | ba37b4b | 2018-01-23 15:02:36 -0800 | [diff] [blame] | 399 | return worker_thread_->Invoke<RTCError>(RTC_FROM_HERE, [&] { |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 400 | return media_channel_->SetRtpSendParameters(ssrc_, parameters); |
| 401 | }); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 402 | } |
| 403 | |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 404 | rtc::scoped_refptr<DtmfSenderInterface> VideoRtpSender::GetDtmfSender() const { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 405 | RTC_LOG(LS_ERROR) << "Tried to get DTMF sender from video sender."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 406 | return nullptr; |
| 407 | } |
| 408 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 409 | void VideoRtpSender::SetSsrc(uint32_t ssrc) { |
Peter Boström | dabc944 | 2016-04-11 11:45:14 +0200 | [diff] [blame] | 410 | TRACE_EVENT0("webrtc", "VideoRtpSender::SetSsrc"); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 411 | if (stopped_ || ssrc == ssrc_) { |
| 412 | return; |
| 413 | } |
| 414 | // If we are already sending with a particular SSRC, stop sending. |
| 415 | if (can_send_track()) { |
deadbeef | 5a4a75a | 2016-06-02 16:23:38 -0700 | [diff] [blame] | 416 | ClearVideoSend(); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 417 | } |
| 418 | ssrc_ = ssrc; |
| 419 | if (can_send_track()) { |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 420 | SetVideoSend(); |
| 421 | } |
| 422 | } |
| 423 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 424 | void VideoRtpSender::Stop() { |
Peter Boström | dabc944 | 2016-04-11 11:45:14 +0200 | [diff] [blame] | 425 | TRACE_EVENT0("webrtc", "VideoRtpSender::Stop"); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 426 | // TODO(deadbeef): Need to do more here to fully stop sending packets. |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 427 | if (stopped_) { |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 428 | return; |
| 429 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 430 | if (track_) { |
| 431 | track_->UnregisterObserver(this); |
| 432 | } |
| 433 | if (can_send_track()) { |
deadbeef | 5a4a75a | 2016-06-02 16:23:38 -0700 | [diff] [blame] | 434 | ClearVideoSend(); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 435 | } |
| 436 | stopped_ = true; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 437 | } |
| 438 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 439 | void VideoRtpSender::SetVideoSend() { |
kwiberg | ee89e78 | 2017-08-09 17:22:01 -0700 | [diff] [blame] | 440 | RTC_DCHECK(!stopped_); |
| 441 | RTC_DCHECK(can_send_track()); |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 442 | if (!media_channel_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 443 | RTC_LOG(LS_ERROR) << "SetVideoSend: No video channel exists."; |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 444 | return; |
| 445 | } |
perkj | 0d3eef2 | 2016-03-09 02:39:17 +0100 | [diff] [blame] | 446 | cricket::VideoOptions options; |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 447 | VideoTrackSourceInterface* source = track_->GetSource(); |
perkj | 0d3eef2 | 2016-03-09 02:39:17 +0100 | [diff] [blame] | 448 | if (source) { |
Oskar Sundbom | 36f8f3e | 2017-11-16 10:54:27 +0100 | [diff] [blame] | 449 | options.is_screencast = source->is_screencast(); |
Per | c0d31e9 | 2016-03-31 17:23:39 +0200 | [diff] [blame] | 450 | options.video_noise_reduction = source->needs_denoising(); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 451 | } |
pbos | 5214a0a | 2016-12-16 15:39:11 -0800 | [diff] [blame] | 452 | switch (cached_track_content_hint_) { |
| 453 | case VideoTrackInterface::ContentHint::kNone: |
| 454 | break; |
| 455 | case VideoTrackInterface::ContentHint::kFluid: |
Oskar Sundbom | 36f8f3e | 2017-11-16 10:54:27 +0100 | [diff] [blame] | 456 | options.is_screencast = false; |
pbos | 5214a0a | 2016-12-16 15:39:11 -0800 | [diff] [blame] | 457 | break; |
| 458 | case VideoTrackInterface::ContentHint::kDetailed: |
Oskar Sundbom | 36f8f3e | 2017-11-16 10:54:27 +0100 | [diff] [blame] | 459 | options.is_screencast = true; |
pbos | 5214a0a | 2016-12-16 15:39:11 -0800 | [diff] [blame] | 460 | break; |
| 461 | } |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 462 | // |track_->enabled()| hops to the signaling thread, so call it before we hop |
| 463 | // to the worker thread or else it will deadlock. |
| 464 | bool track_enabled = track_->enabled(); |
| 465 | bool success = worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] { |
| 466 | return media_channel_->SetVideoSend(ssrc_, track_enabled, &options, track_); |
| 467 | }); |
| 468 | RTC_DCHECK(success); |
deadbeef | 5a4a75a | 2016-06-02 16:23:38 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | void VideoRtpSender::ClearVideoSend() { |
| 472 | RTC_DCHECK(ssrc_ != 0); |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 473 | RTC_DCHECK(!stopped_); |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 474 | if (!media_channel_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 475 | RTC_LOG(LS_WARNING) << "SetVideoSend: No video channel exists."; |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 476 | return; |
| 477 | } |
| 478 | // Allow SetVideoSend to fail since |enable| is false and |source| is null. |
| 479 | // This the normal case when the underlying media channel has already been |
| 480 | // deleted. |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 481 | worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] { |
| 482 | return media_channel_->SetVideoSend(ssrc_, false, nullptr, nullptr); |
| 483 | }); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | } // namespace webrtc |