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