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()) { |
Piotr (Peter) Slatala | 95ca6e1 | 2018-11-13 07:57:07 -0800 | [diff] [blame] | 408 | options = track_->GetSource()->options(); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 409 | } |
Tommi | 3c16978 | 2016-01-21 16:12:17 +0100 | [diff] [blame] | 410 | #endif |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 411 | |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 412 | // |track_->enabled()| hops to the signaling thread, so call it before we hop |
| 413 | // to the worker thread or else it will deadlock. |
| 414 | bool track_enabled = track_->enabled(); |
| 415 | bool success = worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] { |
| 416 | return media_channel_->SetAudioSend(ssrc_, track_enabled, &options, |
| 417 | sink_adapter_.get()); |
| 418 | }); |
| 419 | if (!success) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 420 | RTC_LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc_; |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
| 424 | void AudioRtpSender::ClearAudioSend() { |
| 425 | RTC_DCHECK(ssrc_ != 0); |
| 426 | RTC_DCHECK(!stopped_); |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 427 | if (!media_channel_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 428 | RTC_LOG(LS_WARNING) << "ClearAudioSend: No audio channel exists."; |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 429 | return; |
| 430 | } |
| 431 | cricket::AudioOptions options; |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 432 | bool success = worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] { |
| 433 | return media_channel_->SetAudioSend(ssrc_, false, &options, nullptr); |
| 434 | }); |
| 435 | if (!success) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 436 | RTC_LOG(LS_WARNING) << "ClearAudioSend: ssrc is incorrect: " << ssrc_; |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 437 | } |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 440 | VideoRtpSender::VideoRtpSender(rtc::Thread* worker_thread, |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 441 | const std::string& id) |
| 442 | : worker_thread_(worker_thread), id_(id) { |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 443 | RTC_DCHECK(worker_thread); |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 444 | init_parameters_.encodings.emplace_back(); |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 445 | } |
| 446 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 447 | VideoRtpSender::~VideoRtpSender() { |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 448 | Stop(); |
| 449 | } |
| 450 | |
| 451 | void VideoRtpSender::OnChanged() { |
Peter Boström | dabc944 | 2016-04-11 11:45:14 +0200 | [diff] [blame] | 452 | TRACE_EVENT0("webrtc", "VideoRtpSender::OnChanged"); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 453 | RTC_DCHECK(!stopped_); |
Niels Möller | ff40b14 | 2018-04-09 08:49:14 +0200 | [diff] [blame] | 454 | if (cached_track_content_hint_ != track_->content_hint()) { |
pbos | 5214a0a | 2016-12-16 15:39:11 -0800 | [diff] [blame] | 455 | cached_track_content_hint_ = track_->content_hint(); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 456 | if (can_send_track()) { |
| 457 | SetVideoSend(); |
| 458 | } |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | |
| 462 | bool VideoRtpSender::SetTrack(MediaStreamTrackInterface* track) { |
Peter Boström | dabc944 | 2016-04-11 11:45:14 +0200 | [diff] [blame] | 463 | TRACE_EVENT0("webrtc", "VideoRtpSender::SetTrack"); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 464 | if (stopped_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 465 | RTC_LOG(LS_ERROR) << "SetTrack can't be called on a stopped RtpSender."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 466 | return false; |
| 467 | } |
| 468 | if (track && track->kind() != MediaStreamTrackInterface::kVideoKind) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 469 | RTC_LOG(LS_ERROR) << "SetTrack called on video RtpSender with " |
| 470 | << track->kind() << " track."; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 471 | return false; |
| 472 | } |
| 473 | VideoTrackInterface* video_track = static_cast<VideoTrackInterface*>(track); |
| 474 | |
| 475 | // Detach from old track. |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 476 | if (track_) { |
| 477 | track_->UnregisterObserver(this); |
| 478 | } |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 479 | |
| 480 | // Attach to new track. |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 481 | bool prev_can_send_track = can_send_track(); |
deadbeef | 5dd42fd | 2016-05-02 16:20:01 -0700 | [diff] [blame] | 482 | // 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] | 483 | // SetVideoSend. |
deadbeef | 5dd42fd | 2016-05-02 16:20:01 -0700 | [diff] [blame] | 484 | rtc::scoped_refptr<VideoTrackInterface> old_track = track_; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 485 | track_ = video_track; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 486 | if (track_) { |
pbos | 5214a0a | 2016-12-16 15:39:11 -0800 | [diff] [blame] | 487 | cached_track_content_hint_ = track_->content_hint(); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 488 | track_->RegisterObserver(this); |
| 489 | } |
| 490 | |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 491 | // Update video channel. |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 492 | if (can_send_track()) { |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 493 | SetVideoSend(); |
| 494 | } else if (prev_can_send_track) { |
deadbeef | 5a4a75a | 2016-06-02 16:23:38 -0700 | [diff] [blame] | 495 | ClearVideoSend(); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 496 | } |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 497 | attachment_id_ = (track_ ? GenerateUniqueId() : 0); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 498 | return true; |
| 499 | } |
| 500 | |
Florent Castelli | cebf50f | 2018-05-03 15:31:53 +0200 | [diff] [blame] | 501 | RtpParameters VideoRtpSender::GetParameters() { |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 502 | if (stopped_) { |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 503 | return RtpParameters(); |
| 504 | } |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 505 | if (!media_channel_) { |
| 506 | RtpParameters result = init_parameters_; |
| 507 | last_transaction_id_ = rtc::CreateRandomUuid(); |
| 508 | result.transaction_id = last_transaction_id_.value(); |
| 509 | return result; |
| 510 | } |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 511 | return worker_thread_->Invoke<RtpParameters>(RTC_FROM_HERE, [&] { |
Florent Castelli | cebf50f | 2018-05-03 15:31:53 +0200 | [diff] [blame] | 512 | RtpParameters result = media_channel_->GetRtpSendParameters(ssrc_); |
| 513 | last_transaction_id_ = rtc::CreateRandomUuid(); |
| 514 | result.transaction_id = last_transaction_id_.value(); |
| 515 | return result; |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 516 | }); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 517 | } |
| 518 | |
Zach Stein | ba37b4b | 2018-01-23 15:02:36 -0800 | [diff] [blame] | 519 | RTCError VideoRtpSender::SetParameters(const RtpParameters& parameters) { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 520 | TRACE_EVENT0("webrtc", "VideoRtpSender::SetParameters"); |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 521 | if (stopped_) { |
Zach Stein | ba37b4b | 2018-01-23 15:02:36 -0800 | [diff] [blame] | 522 | return RTCError(RTCErrorType::INVALID_STATE); |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 523 | } |
Florent Castelli | cebf50f | 2018-05-03 15:31:53 +0200 | [diff] [blame] | 524 | if (!last_transaction_id_) { |
| 525 | LOG_AND_RETURN_ERROR( |
| 526 | RTCErrorType::INVALID_STATE, |
| 527 | "Failed to set parameters since getParameters() has never been called" |
| 528 | " on this sender"); |
| 529 | } |
| 530 | if (last_transaction_id_ != parameters.transaction_id) { |
| 531 | LOG_AND_RETURN_ERROR( |
| 532 | RTCErrorType::INVALID_MODIFICATION, |
| 533 | "Failed to set parameters since the transaction_id doesn't match" |
| 534 | " the last value returned from getParameters()"); |
| 535 | } |
| 536 | |
Seth Hampson | 2d2c888 | 2018-05-16 16:02:32 -0700 | [diff] [blame] | 537 | if (UnimplementedRtpParameterHasValue(parameters)) { |
| 538 | LOG_AND_RETURN_ERROR( |
| 539 | RTCErrorType::UNSUPPORTED_PARAMETER, |
| 540 | "Attempted to set an unimplemented parameter of RtpParameters."); |
| 541 | } |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 542 | if (!media_channel_) { |
| 543 | auto result = cricket::ValidateRtpParameters(init_parameters_, parameters); |
| 544 | if (result.ok()) { |
| 545 | init_parameters_ = parameters; |
| 546 | } |
| 547 | return result; |
| 548 | } |
Zach Stein | ba37b4b | 2018-01-23 15:02:36 -0800 | [diff] [blame] | 549 | return worker_thread_->Invoke<RTCError>(RTC_FROM_HERE, [&] { |
Florent Castelli | cebf50f | 2018-05-03 15:31:53 +0200 | [diff] [blame] | 550 | RTCError result = media_channel_->SetRtpSendParameters(ssrc_, parameters); |
| 551 | last_transaction_id_.reset(); |
| 552 | return result; |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 553 | }); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 554 | } |
| 555 | |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 556 | rtc::scoped_refptr<DtmfSenderInterface> VideoRtpSender::GetDtmfSender() const { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 557 | RTC_LOG(LS_ERROR) << "Tried to get DTMF sender from video sender."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 558 | return nullptr; |
| 559 | } |
| 560 | |
Benjamin Wright | d81ac95 | 2018-08-29 17:02:10 -0700 | [diff] [blame] | 561 | void VideoRtpSender::SetFrameEncryptor( |
| 562 | rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor) { |
| 563 | frame_encryptor_ = std::move(frame_encryptor); |
Benjamin Wright | 6cc9cca | 2018-10-09 17:29:54 -0700 | [diff] [blame] | 564 | // 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] | 565 | if (media_channel_ && ssrc_ && !stopped_) { |
Benjamin Wright | 6cc9cca | 2018-10-09 17:29:54 -0700 | [diff] [blame] | 566 | worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] { |
| 567 | media_channel_->SetFrameEncryptor(ssrc_, frame_encryptor_); |
| 568 | }); |
| 569 | } |
Benjamin Wright | d81ac95 | 2018-08-29 17:02:10 -0700 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | rtc::scoped_refptr<FrameEncryptorInterface> VideoRtpSender::GetFrameEncryptor() |
| 573 | const { |
| 574 | return frame_encryptor_; |
| 575 | } |
| 576 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 577 | void VideoRtpSender::SetSsrc(uint32_t ssrc) { |
Peter Boström | dabc944 | 2016-04-11 11:45:14 +0200 | [diff] [blame] | 578 | TRACE_EVENT0("webrtc", "VideoRtpSender::SetSsrc"); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 579 | if (stopped_ || ssrc == ssrc_) { |
| 580 | return; |
| 581 | } |
| 582 | // If we are already sending with a particular SSRC, stop sending. |
| 583 | if (can_send_track()) { |
deadbeef | 5a4a75a | 2016-06-02 16:23:38 -0700 | [diff] [blame] | 584 | ClearVideoSend(); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 585 | } |
| 586 | ssrc_ = ssrc; |
| 587 | if (can_send_track()) { |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 588 | SetVideoSend(); |
| 589 | } |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 590 | if (!init_parameters_.encodings.empty()) { |
| 591 | worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] { |
| 592 | RTC_DCHECK(media_channel_); |
| 593 | // Get the current parameters, which are constructed from the SDP. |
| 594 | // The number of layers in the SDP is currently authoritative to support |
| 595 | // SDP munging for Plan-B simulcast with "a=ssrc-group:SIM <ssrc-id>..." |
| 596 | // lines as described in RFC 5576. |
| 597 | // All fields should be default constructed and the SSRC field set, which |
| 598 | // we need to copy. |
| 599 | RtpParameters current_parameters = |
| 600 | media_channel_->GetRtpSendParameters(ssrc_); |
| 601 | for (size_t i = 0; i < init_parameters_.encodings.size(); ++i) { |
| 602 | init_parameters_.encodings[i].ssrc = |
| 603 | current_parameters.encodings[i].ssrc; |
| 604 | current_parameters.encodings[i] = init_parameters_.encodings[i]; |
| 605 | } |
| 606 | current_parameters.degradation_preference = |
| 607 | init_parameters_.degradation_preference; |
| 608 | media_channel_->SetRtpSendParameters(ssrc_, current_parameters); |
| 609 | init_parameters_.encodings.clear(); |
| 610 | }); |
| 611 | } |
Benjamin Wright | c462a6e | 2018-10-26 13:16:16 -0700 | [diff] [blame] | 612 | MaybeAttachFrameEncryptorToMediaChannel( |
| 613 | ssrc_, worker_thread_, frame_encryptor_, media_channel_, stopped_); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 614 | } |
| 615 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 616 | void VideoRtpSender::Stop() { |
Peter Boström | dabc944 | 2016-04-11 11:45:14 +0200 | [diff] [blame] | 617 | TRACE_EVENT0("webrtc", "VideoRtpSender::Stop"); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 618 | // TODO(deadbeef): Need to do more here to fully stop sending packets. |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 619 | if (stopped_) { |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 620 | return; |
| 621 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 622 | if (track_) { |
| 623 | track_->UnregisterObserver(this); |
| 624 | } |
| 625 | if (can_send_track()) { |
deadbeef | 5a4a75a | 2016-06-02 16:23:38 -0700 | [diff] [blame] | 626 | ClearVideoSend(); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 627 | } |
Harald Alvestrand | 3d976f6 | 2018-03-19 19:05:06 +0100 | [diff] [blame] | 628 | media_channel_ = nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 629 | stopped_ = true; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Benjamin Wright | bfd412e | 2018-09-10 14:06:02 -0700 | [diff] [blame] | 632 | void VideoRtpSender::SetVideoMediaChannel( |
| 633 | cricket::VideoMediaChannel* video_media_channel) { |
| 634 | media_channel_ = video_media_channel; |
Benjamin Wright | bfd412e | 2018-09-10 14:06:02 -0700 | [diff] [blame] | 635 | } |
| 636 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 637 | void VideoRtpSender::SetVideoSend() { |
kwiberg | ee89e78 | 2017-08-09 17:22:01 -0700 | [diff] [blame] | 638 | RTC_DCHECK(!stopped_); |
| 639 | RTC_DCHECK(can_send_track()); |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 640 | if (!media_channel_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 641 | RTC_LOG(LS_ERROR) << "SetVideoSend: No video channel exists."; |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 642 | return; |
| 643 | } |
perkj | 0d3eef2 | 2016-03-09 02:39:17 +0100 | [diff] [blame] | 644 | cricket::VideoOptions options; |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 645 | VideoTrackSourceInterface* source = track_->GetSource(); |
perkj | 0d3eef2 | 2016-03-09 02:39:17 +0100 | [diff] [blame] | 646 | if (source) { |
Oskar Sundbom | 36f8f3e | 2017-11-16 10:54:27 +0100 | [diff] [blame] | 647 | options.is_screencast = source->is_screencast(); |
Per | c0d31e9 | 2016-03-31 17:23:39 +0200 | [diff] [blame] | 648 | options.video_noise_reduction = source->needs_denoising(); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 649 | } |
pbos | 5214a0a | 2016-12-16 15:39:11 -0800 | [diff] [blame] | 650 | switch (cached_track_content_hint_) { |
| 651 | case VideoTrackInterface::ContentHint::kNone: |
| 652 | break; |
| 653 | case VideoTrackInterface::ContentHint::kFluid: |
Oskar Sundbom | 36f8f3e | 2017-11-16 10:54:27 +0100 | [diff] [blame] | 654 | options.is_screencast = false; |
pbos | 5214a0a | 2016-12-16 15:39:11 -0800 | [diff] [blame] | 655 | break; |
| 656 | case VideoTrackInterface::ContentHint::kDetailed: |
Harald Alvestrand | c19ab07 | 2018-06-18 08:53:10 +0200 | [diff] [blame] | 657 | case VideoTrackInterface::ContentHint::kText: |
Oskar Sundbom | 36f8f3e | 2017-11-16 10:54:27 +0100 | [diff] [blame] | 658 | options.is_screencast = true; |
pbos | 5214a0a | 2016-12-16 15:39:11 -0800 | [diff] [blame] | 659 | break; |
| 660 | } |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 661 | bool success = worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 662 | return media_channel_->SetVideoSend(ssrc_, &options, track_); |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 663 | }); |
| 664 | RTC_DCHECK(success); |
deadbeef | 5a4a75a | 2016-06-02 16:23:38 -0700 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | void VideoRtpSender::ClearVideoSend() { |
| 668 | RTC_DCHECK(ssrc_ != 0); |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 669 | RTC_DCHECK(!stopped_); |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 670 | if (!media_channel_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 671 | RTC_LOG(LS_WARNING) << "SetVideoSend: No video channel exists."; |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 672 | return; |
| 673 | } |
| 674 | // Allow SetVideoSend to fail since |enable| is false and |source| is null. |
| 675 | // This the normal case when the underlying media channel has already been |
| 676 | // deleted. |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 677 | worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] { |
Niels Möller | ff40b14 | 2018-04-09 08:49:14 +0200 | [diff] [blame] | 678 | return media_channel_->SetVideoSend(ssrc_, nullptr, nullptr); |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 679 | }); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | } // namespace webrtc |