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