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 | |
Benjamin Wright | d81ac95 | 2018-08-29 17:02:10 -0700 | [diff] [blame] | 13 | #include <utility> |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 14 | #include <vector> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "api/mediastreaminterface.h" |
| 17 | #include "pc/localaudiosource.h" |
Steve Anton | 2d8609c | 2018-01-23 16:38:46 -0800 | [diff] [blame] | 18 | #include "pc/statscollector.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "rtc_base/checks.h" |
| 20 | #include "rtc_base/helpers.h" |
| 21 | #include "rtc_base/trace_event.h" |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 25 | namespace { |
| 26 | |
| 27 | // This function is only expected to be called on the signalling thread. |
| 28 | int GenerateUniqueId() { |
| 29 | static int g_unique_id = 0; |
| 30 | |
| 31 | return ++g_unique_id; |
| 32 | } |
| 33 | |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame^] | 34 | // Attaches the frame encryptor to the media channel through an invoke on a |
| 35 | // worker thread. This set must be done on the corresponding worker thread that |
| 36 | // the media channel was created on. |
| 37 | void AttachFrameEncryptorToMediaChannel( |
| 38 | rtc::Thread* worker_thread, |
| 39 | webrtc::FrameEncryptorInterface* frame_encryptor, |
| 40 | cricket::MediaChannel* media_channel) { |
| 41 | if (media_channel) { |
| 42 | return worker_thread->Invoke<void>(RTC_FROM_HERE, [&] { |
| 43 | media_channel->SetFrameEncryptor(frame_encryptor); |
| 44 | }); |
| 45 | } |
| 46 | } |
| 47 | |
Seth Hampson | 2d2c888 | 2018-05-16 16:02:32 -0700 | [diff] [blame] | 48 | // Returns an true if any RtpEncodingParameters member that isn't implemented |
| 49 | // contains a value. |
| 50 | bool UnimplementedRtpEncodingParameterHasValue( |
| 51 | const RtpEncodingParameters& encoding_params) { |
| 52 | if (encoding_params.codec_payload_type.has_value() || |
| 53 | encoding_params.fec.has_value() || encoding_params.rtx.has_value() || |
| 54 | encoding_params.dtx.has_value() || encoding_params.ptime.has_value() || |
Seth Hampson | 2d2c888 | 2018-05-16 16:02:32 -0700 | [diff] [blame] | 55 | !encoding_params.rid.empty() || |
| 56 | encoding_params.scale_resolution_down_by.has_value() || |
| 57 | encoding_params.scale_framerate_down_by.has_value() || |
| 58 | !encoding_params.dependency_rids.empty()) { |
| 59 | return true; |
| 60 | } |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | // Returns true if a "per-sender" encoding parameter contains a value that isn't |
| 65 | // its default. Currently max_bitrate_bps and bitrate_priority both are |
| 66 | // implemented "per-sender," meaning that these encoding parameters |
| 67 | // are used for the RtpSender as a whole, not for a specific encoding layer. |
| 68 | // This is done by setting these encoding parameters at index 0 of |
| 69 | // RtpParameters.encodings. This function can be used to check if these |
| 70 | // parameters are set at any index other than 0 of RtpParameters.encodings, |
| 71 | // because they are currently unimplemented to be used for a specific encoding |
| 72 | // layer. |
| 73 | bool PerSenderRtpEncodingParameterHasValue( |
| 74 | const RtpEncodingParameters& encoding_params) { |
Åsa Persson | 5565981 | 2018-06-18 17:51:32 +0200 | [diff] [blame] | 75 | if (encoding_params.bitrate_priority != kDefaultBitratePriority) { |
Seth Hampson | 2d2c888 | 2018-05-16 16:02:32 -0700 | [diff] [blame] | 76 | return true; |
| 77 | } |
| 78 | return false; |
| 79 | } |
| 80 | |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame^] | 81 | } // namespace |
| 82 | |
Seth Hampson | 2d2c888 | 2018-05-16 16:02:32 -0700 | [diff] [blame] | 83 | // Returns true if any RtpParameters member that isn't implemented contains a |
| 84 | // value. |
| 85 | bool UnimplementedRtpParameterHasValue(const RtpParameters& parameters) { |
Florent Castelli | 87b3c51 | 2018-07-18 16:00:28 +0200 | [diff] [blame] | 86 | if (!parameters.mid.empty()) { |
Seth Hampson | 2d2c888 | 2018-05-16 16:02:32 -0700 | [diff] [blame] | 87 | return true; |
| 88 | } |
| 89 | for (size_t i = 0; i < parameters.encodings.size(); ++i) { |
| 90 | if (UnimplementedRtpEncodingParameterHasValue(parameters.encodings[i])) { |
| 91 | return true; |
| 92 | } |
| 93 | // Encoding parameters that are per-sender should only contain value at |
| 94 | // index 0. |
| 95 | if (i != 0 && |
| 96 | PerSenderRtpEncodingParameterHasValue(parameters.encodings[i])) { |
| 97 | return true; |
| 98 | } |
| 99 | } |
| 100 | return false; |
| 101 | } |
| 102 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 103 | LocalAudioSinkAdapter::LocalAudioSinkAdapter() : sink_(nullptr) {} |
| 104 | |
| 105 | LocalAudioSinkAdapter::~LocalAudioSinkAdapter() { |
| 106 | rtc::CritScope lock(&lock_); |
| 107 | if (sink_) |
| 108 | sink_->OnClose(); |
| 109 | } |
| 110 | |
| 111 | void LocalAudioSinkAdapter::OnData(const void* audio_data, |
| 112 | int bits_per_sample, |
| 113 | int sample_rate, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 114 | size_t number_of_channels, |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 115 | size_t number_of_frames) { |
| 116 | rtc::CritScope lock(&lock_); |
| 117 | if (sink_) { |
| 118 | sink_->OnData(audio_data, bits_per_sample, sample_rate, number_of_channels, |
| 119 | number_of_frames); |
| 120 | } |
| 121 | } |
| 122 | |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 123 | void LocalAudioSinkAdapter::SetSink(cricket::AudioSource::Sink* sink) { |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 124 | rtc::CritScope lock(&lock_); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 125 | RTC_DCHECK(!sink || !sink_); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 126 | sink_ = sink; |
| 127 | } |
| 128 | |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 129 | AudioRtpSender::AudioRtpSender(rtc::Thread* worker_thread, |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 130 | const std::string& id, |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 131 | StatsCollector* stats) |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 132 | : worker_thread_(worker_thread), |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 133 | id_(id), |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 134 | stats_(stats), |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 135 | dtmf_sender_proxy_(DtmfSenderProxy::Create( |
| 136 | rtc::Thread::Current(), |
Steve Anton | b983bae | 2018-06-20 11:16:53 -0700 | [diff] [blame] | 137 | DtmfSender::Create(rtc::Thread::Current(), this))), |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 138 | sink_adapter_(new LocalAudioSinkAdapter()) { |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 139 | RTC_DCHECK(worker_thread); |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame^] | 140 | init_parameters_.encodings.emplace_back(); |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 141 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 142 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 143 | AudioRtpSender::~AudioRtpSender() { |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 144 | // For DtmfSender. |
| 145 | SignalDestroyed(); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 146 | Stop(); |
| 147 | } |
| 148 | |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 149 | bool AudioRtpSender::CanInsertDtmf() { |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 150 | if (!media_channel_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 151 | RTC_LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 152 | return false; |
| 153 | } |
| 154 | // Check that this RTP sender is active (description has been applied that |
| 155 | // matches an SSRC to its ID). |
| 156 | if (!ssrc_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 157 | RTC_LOG(LS_ERROR) << "CanInsertDtmf: Sender does not have SSRC."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 158 | return false; |
| 159 | } |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 160 | return worker_thread_->Invoke<bool>( |
| 161 | RTC_FROM_HERE, [&] { return media_channel_->CanInsertDtmf(); }); |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | bool AudioRtpSender::InsertDtmf(int code, int duration) { |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 165 | if (!media_channel_) { |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 166 | RTC_LOG(LS_ERROR) << "InsertDtmf: No audio channel exists."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 167 | return false; |
| 168 | } |
| 169 | if (!ssrc_) { |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 170 | RTC_LOG(LS_ERROR) << "InsertDtmf: Sender does not have SSRC."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 171 | return false; |
| 172 | } |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 173 | bool success = worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] { |
| 174 | return media_channel_->InsertDtmf(ssrc_, code, duration); |
| 175 | }); |
| 176 | if (!success) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 177 | RTC_LOG(LS_ERROR) << "Failed to insert DTMF to channel."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 178 | } |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 179 | return success; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | sigslot::signal0<>* AudioRtpSender::GetOnDestroyedSignal() { |
| 183 | return &SignalDestroyed; |
| 184 | } |
| 185 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 186 | void AudioRtpSender::OnChanged() { |
Peter Boström | dabc944 | 2016-04-11 11:45:14 +0200 | [diff] [blame] | 187 | TRACE_EVENT0("webrtc", "AudioRtpSender::OnChanged"); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 188 | RTC_DCHECK(!stopped_); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 189 | if (cached_track_enabled_ != track_->enabled()) { |
| 190 | cached_track_enabled_ = track_->enabled(); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 191 | if (can_send_track()) { |
| 192 | SetAudioSend(); |
| 193 | } |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
| 197 | bool AudioRtpSender::SetTrack(MediaStreamTrackInterface* track) { |
Peter Boström | dabc944 | 2016-04-11 11:45:14 +0200 | [diff] [blame] | 198 | TRACE_EVENT0("webrtc", "AudioRtpSender::SetTrack"); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 199 | if (stopped_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 200 | RTC_LOG(LS_ERROR) << "SetTrack can't be called on a stopped RtpSender."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 201 | return false; |
| 202 | } |
| 203 | if (track && track->kind() != MediaStreamTrackInterface::kAudioKind) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 204 | RTC_LOG(LS_ERROR) << "SetTrack called on audio RtpSender with " |
| 205 | << track->kind() << " track."; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 206 | return false; |
| 207 | } |
| 208 | AudioTrackInterface* audio_track = static_cast<AudioTrackInterface*>(track); |
| 209 | |
| 210 | // Detach from old track. |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 211 | if (track_) { |
| 212 | track_->RemoveSink(sink_adapter_.get()); |
| 213 | track_->UnregisterObserver(this); |
| 214 | } |
| 215 | |
| 216 | if (can_send_track() && stats_) { |
| 217 | stats_->RemoveLocalAudioTrack(track_.get(), ssrc_); |
| 218 | } |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 219 | |
| 220 | // Attach to new track. |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 221 | bool prev_can_send_track = can_send_track(); |
deadbeef | 5dd42fd | 2016-05-02 16:20:01 -0700 | [diff] [blame] | 222 | // Keep a reference to the old track to keep it alive until we call |
| 223 | // SetAudioSend. |
| 224 | rtc::scoped_refptr<AudioTrackInterface> old_track = track_; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 225 | track_ = audio_track; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 226 | if (track_) { |
| 227 | cached_track_enabled_ = track_->enabled(); |
| 228 | track_->RegisterObserver(this); |
| 229 | track_->AddSink(sink_adapter_.get()); |
| 230 | } |
| 231 | |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 232 | // Update audio channel. |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 233 | if (can_send_track()) { |
| 234 | SetAudioSend(); |
| 235 | if (stats_) { |
| 236 | stats_->AddLocalAudioTrack(track_.get(), ssrc_); |
| 237 | } |
| 238 | } else if (prev_can_send_track) { |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 239 | ClearAudioSend(); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 240 | } |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 241 | attachment_id_ = (track_ ? GenerateUniqueId() : 0); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 242 | return true; |
| 243 | } |
| 244 | |
Florent Castelli | cebf50f | 2018-05-03 15:31:53 +0200 | [diff] [blame] | 245 | RtpParameters AudioRtpSender::GetParameters() { |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame^] | 246 | if (stopped_) { |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 247 | return RtpParameters(); |
| 248 | } |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame^] | 249 | if (!media_channel_) { |
| 250 | RtpParameters result = init_parameters_; |
| 251 | last_transaction_id_ = rtc::CreateRandomUuid(); |
| 252 | result.transaction_id = last_transaction_id_.value(); |
| 253 | return result; |
| 254 | } |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 255 | return worker_thread_->Invoke<RtpParameters>(RTC_FROM_HERE, [&] { |
Florent Castelli | cebf50f | 2018-05-03 15:31:53 +0200 | [diff] [blame] | 256 | RtpParameters result = media_channel_->GetRtpSendParameters(ssrc_); |
| 257 | last_transaction_id_ = rtc::CreateRandomUuid(); |
| 258 | result.transaction_id = last_transaction_id_.value(); |
| 259 | return result; |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 260 | }); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Zach Stein | ba37b4b | 2018-01-23 15:02:36 -0800 | [diff] [blame] | 263 | RTCError AudioRtpSender::SetParameters(const RtpParameters& parameters) { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 264 | TRACE_EVENT0("webrtc", "AudioRtpSender::SetParameters"); |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame^] | 265 | if (stopped_) { |
Zach Stein | ba37b4b | 2018-01-23 15:02:36 -0800 | [diff] [blame] | 266 | return RTCError(RTCErrorType::INVALID_STATE); |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 267 | } |
Florent Castelli | cebf50f | 2018-05-03 15:31:53 +0200 | [diff] [blame] | 268 | if (!last_transaction_id_) { |
| 269 | LOG_AND_RETURN_ERROR( |
| 270 | RTCErrorType::INVALID_STATE, |
| 271 | "Failed to set parameters since getParameters() has never been called" |
| 272 | " on this sender"); |
| 273 | } |
| 274 | if (last_transaction_id_ != parameters.transaction_id) { |
| 275 | LOG_AND_RETURN_ERROR( |
| 276 | RTCErrorType::INVALID_MODIFICATION, |
| 277 | "Failed to set parameters since the transaction_id doesn't match" |
| 278 | " the last value returned from getParameters()"); |
| 279 | } |
| 280 | |
Seth Hampson | 2d2c888 | 2018-05-16 16:02:32 -0700 | [diff] [blame] | 281 | if (UnimplementedRtpParameterHasValue(parameters)) { |
| 282 | LOG_AND_RETURN_ERROR( |
| 283 | RTCErrorType::UNSUPPORTED_PARAMETER, |
| 284 | "Attempted to set an unimplemented parameter of RtpParameters."); |
| 285 | } |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame^] | 286 | if (!media_channel_) { |
| 287 | auto result = cricket::ValidateRtpParameters(init_parameters_, parameters); |
| 288 | if (result.ok()) { |
| 289 | init_parameters_ = parameters; |
| 290 | } |
| 291 | return result; |
| 292 | } |
Zach Stein | ba37b4b | 2018-01-23 15:02:36 -0800 | [diff] [blame] | 293 | return worker_thread_->Invoke<RTCError>(RTC_FROM_HERE, [&] { |
Florent Castelli | cebf50f | 2018-05-03 15:31:53 +0200 | [diff] [blame] | 294 | RTCError result = media_channel_->SetRtpSendParameters(ssrc_, parameters); |
| 295 | last_transaction_id_.reset(); |
| 296 | return result; |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 297 | }); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 298 | } |
| 299 | |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 300 | rtc::scoped_refptr<DtmfSenderInterface> AudioRtpSender::GetDtmfSender() const { |
| 301 | return dtmf_sender_proxy_; |
| 302 | } |
| 303 | |
Benjamin Wright | d81ac95 | 2018-08-29 17:02:10 -0700 | [diff] [blame] | 304 | void AudioRtpSender::SetFrameEncryptor( |
| 305 | rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor) { |
| 306 | frame_encryptor_ = std::move(frame_encryptor); |
Benjamin Wright | bfd412e | 2018-09-10 14:06:02 -0700 | [diff] [blame] | 307 | AttachFrameEncryptorToMediaChannel(worker_thread_, frame_encryptor_.get(), |
| 308 | media_channel_); |
Benjamin Wright | d81ac95 | 2018-08-29 17:02:10 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | rtc::scoped_refptr<FrameEncryptorInterface> AudioRtpSender::GetFrameEncryptor() |
| 312 | const { |
| 313 | return frame_encryptor_; |
| 314 | } |
| 315 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 316 | void AudioRtpSender::SetSsrc(uint32_t ssrc) { |
Peter Boström | dabc944 | 2016-04-11 11:45:14 +0200 | [diff] [blame] | 317 | TRACE_EVENT0("webrtc", "AudioRtpSender::SetSsrc"); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 318 | if (stopped_ || ssrc == ssrc_) { |
| 319 | return; |
| 320 | } |
| 321 | // If we are already sending with a particular SSRC, stop sending. |
| 322 | if (can_send_track()) { |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 323 | ClearAudioSend(); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 324 | if (stats_) { |
| 325 | stats_->RemoveLocalAudioTrack(track_.get(), ssrc_); |
| 326 | } |
| 327 | } |
| 328 | ssrc_ = ssrc; |
| 329 | if (can_send_track()) { |
| 330 | SetAudioSend(); |
| 331 | if (stats_) { |
| 332 | stats_->AddLocalAudioTrack(track_.get(), ssrc_); |
| 333 | } |
| 334 | } |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame^] | 335 | if (!init_parameters_.encodings.empty()) { |
| 336 | worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] { |
| 337 | RTC_DCHECK(media_channel_); |
| 338 | // Get the current parameters, which are constructed from the SDP. |
| 339 | // The number of layers in the SDP is currently authoritative to support |
| 340 | // SDP munging for Plan-B simulcast with "a=ssrc-group:SIM <ssrc-id>..." |
| 341 | // lines as described in RFC 5576. |
| 342 | // All fields should be default constructed and the SSRC field set, which |
| 343 | // we need to copy. |
| 344 | RtpParameters current_parameters = |
| 345 | media_channel_->GetRtpSendParameters(ssrc_); |
| 346 | for (size_t i = 0; i < init_parameters_.encodings.size(); ++i) { |
| 347 | init_parameters_.encodings[i].ssrc = |
| 348 | current_parameters.encodings[i].ssrc; |
| 349 | current_parameters.encodings[i] = init_parameters_.encodings[i]; |
| 350 | } |
| 351 | current_parameters.degradation_preference = |
| 352 | init_parameters_.degradation_preference; |
| 353 | media_channel_->SetRtpSendParameters(ssrc_, current_parameters); |
| 354 | init_parameters_.encodings.clear(); |
| 355 | }); |
| 356 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 357 | } |
| 358 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 359 | void AudioRtpSender::Stop() { |
Peter Boström | dabc944 | 2016-04-11 11:45:14 +0200 | [diff] [blame] | 360 | TRACE_EVENT0("webrtc", "AudioRtpSender::Stop"); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 361 | // TODO(deadbeef): Need to do more here to fully stop sending packets. |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 362 | if (stopped_) { |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 363 | return; |
| 364 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 365 | if (track_) { |
| 366 | track_->RemoveSink(sink_adapter_.get()); |
| 367 | track_->UnregisterObserver(this); |
| 368 | } |
| 369 | if (can_send_track()) { |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 370 | ClearAudioSend(); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 371 | if (stats_) { |
| 372 | stats_->RemoveLocalAudioTrack(track_.get(), ssrc_); |
| 373 | } |
| 374 | } |
Harald Alvestrand | 3d976f6 | 2018-03-19 19:05:06 +0100 | [diff] [blame] | 375 | media_channel_ = nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 376 | stopped_ = true; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Benjamin Wright | bfd412e | 2018-09-10 14:06:02 -0700 | [diff] [blame] | 379 | void AudioRtpSender::SetVoiceMediaChannel( |
| 380 | cricket::VoiceMediaChannel* voice_media_channel) { |
| 381 | media_channel_ = voice_media_channel; |
| 382 | AttachFrameEncryptorToMediaChannel(worker_thread_, frame_encryptor_.get(), |
| 383 | media_channel_); |
| 384 | } |
| 385 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 386 | void AudioRtpSender::SetAudioSend() { |
kwiberg | ee89e78 | 2017-08-09 17:22:01 -0700 | [diff] [blame] | 387 | RTC_DCHECK(!stopped_); |
| 388 | RTC_DCHECK(can_send_track()); |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 389 | if (!media_channel_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 390 | RTC_LOG(LS_ERROR) << "SetAudioSend: No audio channel exists."; |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 391 | return; |
| 392 | } |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 393 | cricket::AudioOptions options; |
agouaillard | b11fb25 | 2017-02-03 06:37:05 -0800 | [diff] [blame] | 394 | #if !defined(WEBRTC_CHROMIUM_BUILD) && !defined(WEBRTC_WEBKIT_BUILD) |
Tommi | 3c16978 | 2016-01-21 16:12:17 +0100 | [diff] [blame] | 395 | // TODO(tommi): Remove this hack when we move CreateAudioSource out of |
| 396 | // PeerConnection. This is a bit of a strange way to apply local audio |
| 397 | // options since it is also applied to all streams/channels, local or remote. |
tommi | 6eca7e3 | 2015-12-15 04:27:11 -0800 | [diff] [blame] | 398 | if (track_->enabled() && track_->GetSource() && |
| 399 | !track_->GetSource()->remote()) { |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 400 | // TODO(xians): Remove this static_cast since we should be able to connect |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 401 | // a remote audio track to a peer connection. |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 402 | options = static_cast<LocalAudioSource*>(track_->GetSource())->options(); |
| 403 | } |
Tommi | 3c16978 | 2016-01-21 16:12:17 +0100 | [diff] [blame] | 404 | #endif |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 405 | |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 406 | // |track_->enabled()| hops to the signaling thread, so call it before we hop |
| 407 | // to the worker thread or else it will deadlock. |
| 408 | bool track_enabled = track_->enabled(); |
| 409 | bool success = worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] { |
| 410 | return media_channel_->SetAudioSend(ssrc_, track_enabled, &options, |
| 411 | sink_adapter_.get()); |
| 412 | }); |
| 413 | if (!success) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 414 | RTC_LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc_; |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | |
| 418 | void AudioRtpSender::ClearAudioSend() { |
| 419 | RTC_DCHECK(ssrc_ != 0); |
| 420 | RTC_DCHECK(!stopped_); |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 421 | if (!media_channel_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 422 | RTC_LOG(LS_WARNING) << "ClearAudioSend: No audio channel exists."; |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 423 | return; |
| 424 | } |
| 425 | cricket::AudioOptions options; |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 426 | bool success = worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] { |
| 427 | return media_channel_->SetAudioSend(ssrc_, false, &options, nullptr); |
| 428 | }); |
| 429 | if (!success) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 430 | RTC_LOG(LS_WARNING) << "ClearAudioSend: ssrc is incorrect: " << ssrc_; |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 431 | } |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 434 | VideoRtpSender::VideoRtpSender(rtc::Thread* worker_thread, |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 435 | const std::string& id) |
| 436 | : worker_thread_(worker_thread), id_(id) { |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 437 | RTC_DCHECK(worker_thread); |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame^] | 438 | init_parameters_.encodings.emplace_back(); |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 439 | } |
| 440 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 441 | VideoRtpSender::~VideoRtpSender() { |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 442 | Stop(); |
| 443 | } |
| 444 | |
| 445 | void VideoRtpSender::OnChanged() { |
Peter Boström | dabc944 | 2016-04-11 11:45:14 +0200 | [diff] [blame] | 446 | TRACE_EVENT0("webrtc", "VideoRtpSender::OnChanged"); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 447 | RTC_DCHECK(!stopped_); |
Niels Möller | ff40b14 | 2018-04-09 08:49:14 +0200 | [diff] [blame] | 448 | if (cached_track_content_hint_ != track_->content_hint()) { |
pbos | 5214a0a | 2016-12-16 15:39:11 -0800 | [diff] [blame] | 449 | cached_track_content_hint_ = track_->content_hint(); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 450 | if (can_send_track()) { |
| 451 | SetVideoSend(); |
| 452 | } |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | |
| 456 | bool VideoRtpSender::SetTrack(MediaStreamTrackInterface* track) { |
Peter Boström | dabc944 | 2016-04-11 11:45:14 +0200 | [diff] [blame] | 457 | TRACE_EVENT0("webrtc", "VideoRtpSender::SetTrack"); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 458 | if (stopped_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 459 | RTC_LOG(LS_ERROR) << "SetTrack can't be called on a stopped RtpSender."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 460 | return false; |
| 461 | } |
| 462 | if (track && track->kind() != MediaStreamTrackInterface::kVideoKind) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 463 | RTC_LOG(LS_ERROR) << "SetTrack called on video RtpSender with " |
| 464 | << track->kind() << " track."; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 465 | return false; |
| 466 | } |
| 467 | VideoTrackInterface* video_track = static_cast<VideoTrackInterface*>(track); |
| 468 | |
| 469 | // Detach from old track. |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 470 | if (track_) { |
| 471 | track_->UnregisterObserver(this); |
| 472 | } |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 473 | |
| 474 | // Attach to new track. |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 475 | bool prev_can_send_track = can_send_track(); |
deadbeef | 5dd42fd | 2016-05-02 16:20:01 -0700 | [diff] [blame] | 476 | // 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] | 477 | // SetVideoSend. |
deadbeef | 5dd42fd | 2016-05-02 16:20:01 -0700 | [diff] [blame] | 478 | rtc::scoped_refptr<VideoTrackInterface> old_track = track_; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 479 | track_ = video_track; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 480 | if (track_) { |
pbos | 5214a0a | 2016-12-16 15:39:11 -0800 | [diff] [blame] | 481 | cached_track_content_hint_ = track_->content_hint(); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 482 | track_->RegisterObserver(this); |
| 483 | } |
| 484 | |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 485 | // Update video channel. |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 486 | if (can_send_track()) { |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 487 | SetVideoSend(); |
| 488 | } else if (prev_can_send_track) { |
deadbeef | 5a4a75a | 2016-06-02 16:23:38 -0700 | [diff] [blame] | 489 | ClearVideoSend(); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 490 | } |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 491 | attachment_id_ = (track_ ? GenerateUniqueId() : 0); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 492 | return true; |
| 493 | } |
| 494 | |
Florent Castelli | cebf50f | 2018-05-03 15:31:53 +0200 | [diff] [blame] | 495 | RtpParameters VideoRtpSender::GetParameters() { |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame^] | 496 | if (stopped_) { |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 497 | return RtpParameters(); |
| 498 | } |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame^] | 499 | if (!media_channel_) { |
| 500 | RtpParameters result = init_parameters_; |
| 501 | last_transaction_id_ = rtc::CreateRandomUuid(); |
| 502 | result.transaction_id = last_transaction_id_.value(); |
| 503 | return result; |
| 504 | } |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 505 | return worker_thread_->Invoke<RtpParameters>(RTC_FROM_HERE, [&] { |
Florent Castelli | cebf50f | 2018-05-03 15:31:53 +0200 | [diff] [blame] | 506 | RtpParameters result = media_channel_->GetRtpSendParameters(ssrc_); |
| 507 | last_transaction_id_ = rtc::CreateRandomUuid(); |
| 508 | result.transaction_id = last_transaction_id_.value(); |
| 509 | return result; |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 510 | }); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Zach Stein | ba37b4b | 2018-01-23 15:02:36 -0800 | [diff] [blame] | 513 | RTCError VideoRtpSender::SetParameters(const RtpParameters& parameters) { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 514 | TRACE_EVENT0("webrtc", "VideoRtpSender::SetParameters"); |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame^] | 515 | if (stopped_) { |
Zach Stein | ba37b4b | 2018-01-23 15:02:36 -0800 | [diff] [blame] | 516 | return RTCError(RTCErrorType::INVALID_STATE); |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 517 | } |
Florent Castelli | cebf50f | 2018-05-03 15:31:53 +0200 | [diff] [blame] | 518 | if (!last_transaction_id_) { |
| 519 | LOG_AND_RETURN_ERROR( |
| 520 | RTCErrorType::INVALID_STATE, |
| 521 | "Failed to set parameters since getParameters() has never been called" |
| 522 | " on this sender"); |
| 523 | } |
| 524 | if (last_transaction_id_ != parameters.transaction_id) { |
| 525 | LOG_AND_RETURN_ERROR( |
| 526 | RTCErrorType::INVALID_MODIFICATION, |
| 527 | "Failed to set parameters since the transaction_id doesn't match" |
| 528 | " the last value returned from getParameters()"); |
| 529 | } |
| 530 | |
Seth Hampson | 2d2c888 | 2018-05-16 16:02:32 -0700 | [diff] [blame] | 531 | if (UnimplementedRtpParameterHasValue(parameters)) { |
| 532 | LOG_AND_RETURN_ERROR( |
| 533 | RTCErrorType::UNSUPPORTED_PARAMETER, |
| 534 | "Attempted to set an unimplemented parameter of RtpParameters."); |
| 535 | } |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame^] | 536 | if (!media_channel_) { |
| 537 | auto result = cricket::ValidateRtpParameters(init_parameters_, parameters); |
| 538 | if (result.ok()) { |
| 539 | init_parameters_ = parameters; |
| 540 | } |
| 541 | return result; |
| 542 | } |
Zach Stein | ba37b4b | 2018-01-23 15:02:36 -0800 | [diff] [blame] | 543 | return worker_thread_->Invoke<RTCError>(RTC_FROM_HERE, [&] { |
Florent Castelli | cebf50f | 2018-05-03 15:31:53 +0200 | [diff] [blame] | 544 | RTCError result = media_channel_->SetRtpSendParameters(ssrc_, parameters); |
| 545 | last_transaction_id_.reset(); |
| 546 | return result; |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 547 | }); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 548 | } |
| 549 | |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 550 | rtc::scoped_refptr<DtmfSenderInterface> VideoRtpSender::GetDtmfSender() const { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 551 | RTC_LOG(LS_ERROR) << "Tried to get DTMF sender from video sender."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 552 | return nullptr; |
| 553 | } |
| 554 | |
Benjamin Wright | d81ac95 | 2018-08-29 17:02:10 -0700 | [diff] [blame] | 555 | void VideoRtpSender::SetFrameEncryptor( |
| 556 | rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor) { |
| 557 | frame_encryptor_ = std::move(frame_encryptor); |
Benjamin Wright | bfd412e | 2018-09-10 14:06:02 -0700 | [diff] [blame] | 558 | AttachFrameEncryptorToMediaChannel(worker_thread_, frame_encryptor_.get(), |
| 559 | media_channel_); |
Benjamin Wright | d81ac95 | 2018-08-29 17:02:10 -0700 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | rtc::scoped_refptr<FrameEncryptorInterface> VideoRtpSender::GetFrameEncryptor() |
| 563 | const { |
| 564 | return frame_encryptor_; |
| 565 | } |
| 566 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 567 | void VideoRtpSender::SetSsrc(uint32_t ssrc) { |
Peter Boström | dabc944 | 2016-04-11 11:45:14 +0200 | [diff] [blame] | 568 | TRACE_EVENT0("webrtc", "VideoRtpSender::SetSsrc"); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 569 | if (stopped_ || ssrc == ssrc_) { |
| 570 | return; |
| 571 | } |
| 572 | // If we are already sending with a particular SSRC, stop sending. |
| 573 | if (can_send_track()) { |
deadbeef | 5a4a75a | 2016-06-02 16:23:38 -0700 | [diff] [blame] | 574 | ClearVideoSend(); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 575 | } |
| 576 | ssrc_ = ssrc; |
| 577 | if (can_send_track()) { |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 578 | SetVideoSend(); |
| 579 | } |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame^] | 580 | if (!init_parameters_.encodings.empty()) { |
| 581 | worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] { |
| 582 | RTC_DCHECK(media_channel_); |
| 583 | // Get the current parameters, which are constructed from the SDP. |
| 584 | // The number of layers in the SDP is currently authoritative to support |
| 585 | // SDP munging for Plan-B simulcast with "a=ssrc-group:SIM <ssrc-id>..." |
| 586 | // lines as described in RFC 5576. |
| 587 | // All fields should be default constructed and the SSRC field set, which |
| 588 | // we need to copy. |
| 589 | RtpParameters current_parameters = |
| 590 | media_channel_->GetRtpSendParameters(ssrc_); |
| 591 | for (size_t i = 0; i < init_parameters_.encodings.size(); ++i) { |
| 592 | init_parameters_.encodings[i].ssrc = |
| 593 | current_parameters.encodings[i].ssrc; |
| 594 | current_parameters.encodings[i] = init_parameters_.encodings[i]; |
| 595 | } |
| 596 | current_parameters.degradation_preference = |
| 597 | init_parameters_.degradation_preference; |
| 598 | media_channel_->SetRtpSendParameters(ssrc_, current_parameters); |
| 599 | init_parameters_.encodings.clear(); |
| 600 | }); |
| 601 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 602 | } |
| 603 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 604 | void VideoRtpSender::Stop() { |
Peter Boström | dabc944 | 2016-04-11 11:45:14 +0200 | [diff] [blame] | 605 | TRACE_EVENT0("webrtc", "VideoRtpSender::Stop"); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 606 | // TODO(deadbeef): Need to do more here to fully stop sending packets. |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 607 | if (stopped_) { |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 608 | return; |
| 609 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 610 | if (track_) { |
| 611 | track_->UnregisterObserver(this); |
| 612 | } |
| 613 | if (can_send_track()) { |
deadbeef | 5a4a75a | 2016-06-02 16:23:38 -0700 | [diff] [blame] | 614 | ClearVideoSend(); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 615 | } |
Harald Alvestrand | 3d976f6 | 2018-03-19 19:05:06 +0100 | [diff] [blame] | 616 | media_channel_ = nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 617 | stopped_ = true; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 618 | } |
| 619 | |
Benjamin Wright | bfd412e | 2018-09-10 14:06:02 -0700 | [diff] [blame] | 620 | void VideoRtpSender::SetVideoMediaChannel( |
| 621 | cricket::VideoMediaChannel* video_media_channel) { |
| 622 | media_channel_ = video_media_channel; |
| 623 | AttachFrameEncryptorToMediaChannel(worker_thread_, frame_encryptor_.get(), |
| 624 | media_channel_); |
| 625 | } |
| 626 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 627 | void VideoRtpSender::SetVideoSend() { |
kwiberg | ee89e78 | 2017-08-09 17:22:01 -0700 | [diff] [blame] | 628 | RTC_DCHECK(!stopped_); |
| 629 | RTC_DCHECK(can_send_track()); |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 630 | if (!media_channel_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 631 | RTC_LOG(LS_ERROR) << "SetVideoSend: No video channel exists."; |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 632 | return; |
| 633 | } |
perkj | 0d3eef2 | 2016-03-09 02:39:17 +0100 | [diff] [blame] | 634 | cricket::VideoOptions options; |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 635 | VideoTrackSourceInterface* source = track_->GetSource(); |
perkj | 0d3eef2 | 2016-03-09 02:39:17 +0100 | [diff] [blame] | 636 | if (source) { |
Oskar Sundbom | 36f8f3e | 2017-11-16 10:54:27 +0100 | [diff] [blame] | 637 | options.is_screencast = source->is_screencast(); |
Per | c0d31e9 | 2016-03-31 17:23:39 +0200 | [diff] [blame] | 638 | options.video_noise_reduction = source->needs_denoising(); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 639 | } |
pbos | 5214a0a | 2016-12-16 15:39:11 -0800 | [diff] [blame] | 640 | switch (cached_track_content_hint_) { |
| 641 | case VideoTrackInterface::ContentHint::kNone: |
| 642 | break; |
| 643 | case VideoTrackInterface::ContentHint::kFluid: |
Oskar Sundbom | 36f8f3e | 2017-11-16 10:54:27 +0100 | [diff] [blame] | 644 | options.is_screencast = false; |
pbos | 5214a0a | 2016-12-16 15:39:11 -0800 | [diff] [blame] | 645 | break; |
| 646 | case VideoTrackInterface::ContentHint::kDetailed: |
Harald Alvestrand | c19ab07 | 2018-06-18 08:53:10 +0200 | [diff] [blame] | 647 | case VideoTrackInterface::ContentHint::kText: |
Oskar Sundbom | 36f8f3e | 2017-11-16 10:54:27 +0100 | [diff] [blame] | 648 | options.is_screencast = true; |
pbos | 5214a0a | 2016-12-16 15:39:11 -0800 | [diff] [blame] | 649 | break; |
| 650 | } |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 651 | bool success = worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 652 | return media_channel_->SetVideoSend(ssrc_, &options, track_); |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 653 | }); |
| 654 | RTC_DCHECK(success); |
deadbeef | 5a4a75a | 2016-06-02 16:23:38 -0700 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | void VideoRtpSender::ClearVideoSend() { |
| 658 | RTC_DCHECK(ssrc_ != 0); |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 659 | RTC_DCHECK(!stopped_); |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 660 | if (!media_channel_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 661 | RTC_LOG(LS_WARNING) << "SetVideoSend: No video channel exists."; |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 662 | return; |
| 663 | } |
| 664 | // Allow SetVideoSend to fail since |enable| is false and |source| is null. |
| 665 | // This the normal case when the underlying media channel has already been |
| 666 | // deleted. |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 667 | worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] { |
Niels Möller | ff40b14 | 2018-04-09 08:49:14 +0200 | [diff] [blame] | 668 | return media_channel_->SetVideoSend(ssrc_, nullptr, nullptr); |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 669 | }); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | } // namespace webrtc |