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