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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "pc/rtp_sender.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" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 17 | #include "api/media_stream_interface.h" |
| 18 | #include "media/base/media_engine.h" |
| 19 | #include "pc/stats_collector.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() || |
Seth Hampson | 2d2c888 | 2018-05-16 16:02:32 -0700 | [diff] [blame] | 45 | encoding_params.scale_framerate_down_by.has_value() || |
| 46 | !encoding_params.dependency_rids.empty()) { |
| 47 | return true; |
| 48 | } |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | // Returns true if a "per-sender" encoding parameter contains a value that isn't |
| 53 | // its default. Currently max_bitrate_bps and bitrate_priority both are |
| 54 | // implemented "per-sender," meaning that these encoding parameters |
| 55 | // are used for the RtpSender as a whole, not for a specific encoding layer. |
| 56 | // This is done by setting these encoding parameters at index 0 of |
| 57 | // RtpParameters.encodings. This function can be used to check if these |
| 58 | // parameters are set at any index other than 0 of RtpParameters.encodings, |
| 59 | // because they are currently unimplemented to be used for a specific encoding |
| 60 | // layer. |
| 61 | bool PerSenderRtpEncodingParameterHasValue( |
| 62 | const RtpEncodingParameters& encoding_params) { |
Tim Haloun | 648d28a | 2018-10-18 16:52:22 -0700 | [diff] [blame] | 63 | if (encoding_params.bitrate_priority != kDefaultBitratePriority || |
| 64 | encoding_params.network_priority != kDefaultBitratePriority) { |
Seth Hampson | 2d2c888 | 2018-05-16 16:02:32 -0700 | [diff] [blame] | 65 | return true; |
| 66 | } |
| 67 | return false; |
| 68 | } |
| 69 | |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 70 | // Attempt to attach the frame decryptor to the current media channel on the |
| 71 | // correct worker thread only if both the media channel exists and a ssrc has |
| 72 | // been allocated to the stream. |
| 73 | void MaybeAttachFrameEncryptorToMediaChannel( |
Benjamin Wright | 6cc9cca | 2018-10-09 17:29:54 -0700 | [diff] [blame] | 74 | const uint32_t ssrc, |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 75 | rtc::Thread* worker_thread, |
| 76 | rtc::scoped_refptr<webrtc::FrameEncryptorInterface> frame_encryptor, |
Benjamin Wright | c462a6e | 2018-10-26 13:16:16 -0700 | [diff] [blame] | 77 | cricket::MediaChannel* media_channel, |
| 78 | bool stopped) { |
| 79 | if (media_channel && frame_encryptor && ssrc && !stopped) { |
Benjamin Wright | 6cc9cca | 2018-10-09 17:29:54 -0700 | [diff] [blame] | 80 | worker_thread->Invoke<void>(RTC_FROM_HERE, [&] { |
| 81 | media_channel->SetFrameEncryptor(ssrc, frame_encryptor); |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 82 | }); |
| 83 | } |
| 84 | } |
| 85 | |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 86 | } // namespace |
| 87 | |
Seth Hampson | 2d2c888 | 2018-05-16 16:02:32 -0700 | [diff] [blame] | 88 | // Returns true if any RtpParameters member that isn't implemented contains a |
| 89 | // value. |
| 90 | bool UnimplementedRtpParameterHasValue(const RtpParameters& parameters) { |
Florent Castelli | 87b3c51 | 2018-07-18 16:00:28 +0200 | [diff] [blame] | 91 | if (!parameters.mid.empty()) { |
Seth Hampson | 2d2c888 | 2018-05-16 16:02:32 -0700 | [diff] [blame] | 92 | return true; |
| 93 | } |
| 94 | for (size_t i = 0; i < parameters.encodings.size(); ++i) { |
| 95 | if (UnimplementedRtpEncodingParameterHasValue(parameters.encodings[i])) { |
| 96 | return true; |
| 97 | } |
| 98 | // Encoding parameters that are per-sender should only contain value at |
| 99 | // index 0. |
| 100 | if (i != 0 && |
| 101 | PerSenderRtpEncodingParameterHasValue(parameters.encodings[i])) { |
| 102 | return true; |
| 103 | } |
| 104 | } |
| 105 | return false; |
| 106 | } |
| 107 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 108 | LocalAudioSinkAdapter::LocalAudioSinkAdapter() : sink_(nullptr) {} |
| 109 | |
| 110 | LocalAudioSinkAdapter::~LocalAudioSinkAdapter() { |
| 111 | rtc::CritScope lock(&lock_); |
| 112 | if (sink_) |
| 113 | sink_->OnClose(); |
| 114 | } |
| 115 | |
| 116 | void LocalAudioSinkAdapter::OnData(const void* audio_data, |
| 117 | int bits_per_sample, |
| 118 | int sample_rate, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 119 | size_t number_of_channels, |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 120 | size_t number_of_frames) { |
| 121 | rtc::CritScope lock(&lock_); |
| 122 | if (sink_) { |
| 123 | sink_->OnData(audio_data, bits_per_sample, sample_rate, number_of_channels, |
| 124 | number_of_frames); |
| 125 | } |
| 126 | } |
| 127 | |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 128 | void LocalAudioSinkAdapter::SetSink(cricket::AudioSource::Sink* sink) { |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 129 | rtc::CritScope lock(&lock_); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 130 | RTC_DCHECK(!sink || !sink_); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 131 | sink_ = sink; |
| 132 | } |
| 133 | |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 134 | AudioRtpSender::AudioRtpSender(rtc::Thread* worker_thread, |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 135 | const std::string& id, |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 136 | StatsCollector* stats) |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 137 | : worker_thread_(worker_thread), |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 138 | id_(id), |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 139 | stats_(stats), |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 140 | dtmf_sender_proxy_(DtmfSenderProxy::Create( |
| 141 | rtc::Thread::Current(), |
Steve Anton | b983bae | 2018-06-20 11:16:53 -0700 | [diff] [blame] | 142 | DtmfSender::Create(rtc::Thread::Current(), this))), |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 143 | sink_adapter_(new LocalAudioSinkAdapter()) { |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 144 | RTC_DCHECK(worker_thread); |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 145 | init_parameters_.encodings.emplace_back(); |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 146 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 147 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 148 | AudioRtpSender::~AudioRtpSender() { |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 149 | // For DtmfSender. |
| 150 | SignalDestroyed(); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 151 | Stop(); |
| 152 | } |
| 153 | |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 154 | bool AudioRtpSender::CanInsertDtmf() { |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 155 | if (!media_channel_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 156 | RTC_LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 157 | return false; |
| 158 | } |
| 159 | // Check that this RTP sender is active (description has been applied that |
| 160 | // matches an SSRC to its ID). |
| 161 | if (!ssrc_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 162 | RTC_LOG(LS_ERROR) << "CanInsertDtmf: Sender does not have SSRC."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 163 | return false; |
| 164 | } |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 165 | return worker_thread_->Invoke<bool>( |
| 166 | RTC_FROM_HERE, [&] { return media_channel_->CanInsertDtmf(); }); |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | bool AudioRtpSender::InsertDtmf(int code, int duration) { |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 170 | if (!media_channel_) { |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 171 | RTC_LOG(LS_ERROR) << "InsertDtmf: No audio channel exists."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 172 | return false; |
| 173 | } |
| 174 | if (!ssrc_) { |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 175 | RTC_LOG(LS_ERROR) << "InsertDtmf: Sender does not have SSRC."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 176 | return false; |
| 177 | } |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 178 | bool success = worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] { |
| 179 | return media_channel_->InsertDtmf(ssrc_, code, duration); |
| 180 | }); |
| 181 | if (!success) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 182 | RTC_LOG(LS_ERROR) << "Failed to insert DTMF to channel."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 183 | } |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 184 | return success; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | sigslot::signal0<>* AudioRtpSender::GetOnDestroyedSignal() { |
| 188 | return &SignalDestroyed; |
| 189 | } |
| 190 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 191 | void AudioRtpSender::OnChanged() { |
Peter Boström | dabc944 | 2016-04-11 11:45:14 +0200 | [diff] [blame] | 192 | TRACE_EVENT0("webrtc", "AudioRtpSender::OnChanged"); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 193 | RTC_DCHECK(!stopped_); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 194 | if (cached_track_enabled_ != track_->enabled()) { |
| 195 | cached_track_enabled_ = track_->enabled(); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 196 | if (can_send_track()) { |
| 197 | SetAudioSend(); |
| 198 | } |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
| 202 | bool AudioRtpSender::SetTrack(MediaStreamTrackInterface* track) { |
Peter Boström | dabc944 | 2016-04-11 11:45:14 +0200 | [diff] [blame] | 203 | TRACE_EVENT0("webrtc", "AudioRtpSender::SetTrack"); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 204 | if (stopped_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 205 | RTC_LOG(LS_ERROR) << "SetTrack can't be called on a stopped RtpSender."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 206 | return false; |
| 207 | } |
| 208 | if (track && track->kind() != MediaStreamTrackInterface::kAudioKind) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 209 | RTC_LOG(LS_ERROR) << "SetTrack called on audio RtpSender with " |
| 210 | << track->kind() << " track."; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 211 | return false; |
| 212 | } |
| 213 | AudioTrackInterface* audio_track = static_cast<AudioTrackInterface*>(track); |
| 214 | |
| 215 | // Detach from old track. |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 216 | if (track_) { |
| 217 | track_->RemoveSink(sink_adapter_.get()); |
| 218 | track_->UnregisterObserver(this); |
| 219 | } |
| 220 | |
| 221 | if (can_send_track() && stats_) { |
| 222 | stats_->RemoveLocalAudioTrack(track_.get(), ssrc_); |
| 223 | } |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 224 | |
| 225 | // Attach to new track. |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 226 | bool prev_can_send_track = can_send_track(); |
deadbeef | 5dd42fd | 2016-05-02 16:20:01 -0700 | [diff] [blame] | 227 | // Keep a reference to the old track to keep it alive until we call |
| 228 | // SetAudioSend. |
| 229 | rtc::scoped_refptr<AudioTrackInterface> old_track = track_; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 230 | track_ = audio_track; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 231 | if (track_) { |
| 232 | cached_track_enabled_ = track_->enabled(); |
| 233 | track_->RegisterObserver(this); |
| 234 | track_->AddSink(sink_adapter_.get()); |
| 235 | } |
| 236 | |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 237 | // Update audio channel. |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 238 | if (can_send_track()) { |
| 239 | SetAudioSend(); |
| 240 | if (stats_) { |
| 241 | stats_->AddLocalAudioTrack(track_.get(), ssrc_); |
| 242 | } |
| 243 | } else if (prev_can_send_track) { |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 244 | ClearAudioSend(); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 245 | } |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 246 | attachment_id_ = (track_ ? GenerateUniqueId() : 0); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 247 | return true; |
| 248 | } |
| 249 | |
Florent Castelli | cebf50f | 2018-05-03 15:31:53 +0200 | [diff] [blame] | 250 | RtpParameters AudioRtpSender::GetParameters() { |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 251 | if (stopped_) { |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 252 | return RtpParameters(); |
| 253 | } |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 254 | if (!media_channel_) { |
| 255 | RtpParameters result = init_parameters_; |
| 256 | last_transaction_id_ = rtc::CreateRandomUuid(); |
| 257 | result.transaction_id = last_transaction_id_.value(); |
| 258 | return result; |
| 259 | } |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 260 | return worker_thread_->Invoke<RtpParameters>(RTC_FROM_HERE, [&] { |
Florent Castelli | cebf50f | 2018-05-03 15:31:53 +0200 | [diff] [blame] | 261 | RtpParameters result = media_channel_->GetRtpSendParameters(ssrc_); |
| 262 | last_transaction_id_ = rtc::CreateRandomUuid(); |
| 263 | result.transaction_id = last_transaction_id_.value(); |
| 264 | return result; |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 265 | }); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Zach Stein | ba37b4b | 2018-01-23 15:02:36 -0800 | [diff] [blame] | 268 | RTCError AudioRtpSender::SetParameters(const RtpParameters& parameters) { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 269 | TRACE_EVENT0("webrtc", "AudioRtpSender::SetParameters"); |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 270 | if (stopped_) { |
Zach Stein | ba37b4b | 2018-01-23 15:02:36 -0800 | [diff] [blame] | 271 | return RTCError(RTCErrorType::INVALID_STATE); |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 272 | } |
Florent Castelli | cebf50f | 2018-05-03 15:31:53 +0200 | [diff] [blame] | 273 | if (!last_transaction_id_) { |
| 274 | LOG_AND_RETURN_ERROR( |
| 275 | RTCErrorType::INVALID_STATE, |
| 276 | "Failed to set parameters since getParameters() has never been called" |
| 277 | " on this sender"); |
| 278 | } |
| 279 | if (last_transaction_id_ != parameters.transaction_id) { |
| 280 | LOG_AND_RETURN_ERROR( |
| 281 | RTCErrorType::INVALID_MODIFICATION, |
| 282 | "Failed to set parameters since the transaction_id doesn't match" |
| 283 | " the last value returned from getParameters()"); |
| 284 | } |
| 285 | |
Seth Hampson | 2d2c888 | 2018-05-16 16:02:32 -0700 | [diff] [blame] | 286 | if (UnimplementedRtpParameterHasValue(parameters)) { |
| 287 | LOG_AND_RETURN_ERROR( |
| 288 | RTCErrorType::UNSUPPORTED_PARAMETER, |
| 289 | "Attempted to set an unimplemented parameter of RtpParameters."); |
| 290 | } |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 291 | if (!media_channel_) { |
Florent Castelli | c1a0bcb | 2019-01-29 14:26:48 +0100 | [diff] [blame^] | 292 | auto result = cricket::CheckRtpParametersInvalidModificationAndValues( |
| 293 | init_parameters_, parameters); |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 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_) { |
Florent Castelli | c1a0bcb | 2019-01-29 14:26:48 +0100 | [diff] [blame^] | 547 | auto result = cricket::CheckRtpParametersInvalidModificationAndValues( |
| 548 | init_parameters_, parameters); |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 549 | if (result.ok()) { |
| 550 | init_parameters_ = parameters; |
| 551 | } |
| 552 | return result; |
| 553 | } |
Zach Stein | ba37b4b | 2018-01-23 15:02:36 -0800 | [diff] [blame] | 554 | return worker_thread_->Invoke<RTCError>(RTC_FROM_HERE, [&] { |
Florent Castelli | cebf50f | 2018-05-03 15:31:53 +0200 | [diff] [blame] | 555 | RTCError result = media_channel_->SetRtpSendParameters(ssrc_, parameters); |
| 556 | last_transaction_id_.reset(); |
| 557 | return result; |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 558 | }); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 559 | } |
| 560 | |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 561 | rtc::scoped_refptr<DtmfSenderInterface> VideoRtpSender::GetDtmfSender() const { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 562 | RTC_LOG(LS_ERROR) << "Tried to get DTMF sender from video sender."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 563 | return nullptr; |
| 564 | } |
| 565 | |
Benjamin Wright | d81ac95 | 2018-08-29 17:02:10 -0700 | [diff] [blame] | 566 | void VideoRtpSender::SetFrameEncryptor( |
| 567 | rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor) { |
| 568 | frame_encryptor_ = std::move(frame_encryptor); |
Benjamin Wright | 6cc9cca | 2018-10-09 17:29:54 -0700 | [diff] [blame] | 569 | // 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] | 570 | if (media_channel_ && ssrc_ && !stopped_) { |
Benjamin Wright | 6cc9cca | 2018-10-09 17:29:54 -0700 | [diff] [blame] | 571 | worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] { |
| 572 | media_channel_->SetFrameEncryptor(ssrc_, frame_encryptor_); |
| 573 | }); |
| 574 | } |
Benjamin Wright | d81ac95 | 2018-08-29 17:02:10 -0700 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | rtc::scoped_refptr<FrameEncryptorInterface> VideoRtpSender::GetFrameEncryptor() |
| 578 | const { |
| 579 | return frame_encryptor_; |
| 580 | } |
| 581 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 582 | void VideoRtpSender::SetSsrc(uint32_t ssrc) { |
Peter Boström | dabc944 | 2016-04-11 11:45:14 +0200 | [diff] [blame] | 583 | TRACE_EVENT0("webrtc", "VideoRtpSender::SetSsrc"); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 584 | if (stopped_ || ssrc == ssrc_) { |
| 585 | return; |
| 586 | } |
| 587 | // If we are already sending with a particular SSRC, stop sending. |
| 588 | if (can_send_track()) { |
deadbeef | 5a4a75a | 2016-06-02 16:23:38 -0700 | [diff] [blame] | 589 | ClearVideoSend(); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 590 | } |
| 591 | ssrc_ = ssrc; |
| 592 | if (can_send_track()) { |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 593 | SetVideoSend(); |
| 594 | } |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 595 | if (!init_parameters_.encodings.empty()) { |
| 596 | worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] { |
| 597 | RTC_DCHECK(media_channel_); |
| 598 | // Get the current parameters, which are constructed from the SDP. |
| 599 | // The number of layers in the SDP is currently authoritative to support |
| 600 | // SDP munging for Plan-B simulcast with "a=ssrc-group:SIM <ssrc-id>..." |
| 601 | // lines as described in RFC 5576. |
| 602 | // All fields should be default constructed and the SSRC field set, which |
| 603 | // we need to copy. |
| 604 | RtpParameters current_parameters = |
| 605 | media_channel_->GetRtpSendParameters(ssrc_); |
| 606 | for (size_t i = 0; i < init_parameters_.encodings.size(); ++i) { |
| 607 | init_parameters_.encodings[i].ssrc = |
| 608 | current_parameters.encodings[i].ssrc; |
| 609 | current_parameters.encodings[i] = init_parameters_.encodings[i]; |
| 610 | } |
| 611 | current_parameters.degradation_preference = |
| 612 | init_parameters_.degradation_preference; |
| 613 | media_channel_->SetRtpSendParameters(ssrc_, current_parameters); |
| 614 | init_parameters_.encodings.clear(); |
| 615 | }); |
| 616 | } |
Benjamin Wright | c462a6e | 2018-10-26 13:16:16 -0700 | [diff] [blame] | 617 | MaybeAttachFrameEncryptorToMediaChannel( |
| 618 | ssrc_, worker_thread_, frame_encryptor_, media_channel_, stopped_); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 619 | } |
| 620 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 621 | void VideoRtpSender::Stop() { |
Peter Boström | dabc944 | 2016-04-11 11:45:14 +0200 | [diff] [blame] | 622 | TRACE_EVENT0("webrtc", "VideoRtpSender::Stop"); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 623 | // TODO(deadbeef): Need to do more here to fully stop sending packets. |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 624 | if (stopped_) { |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 625 | return; |
| 626 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 627 | if (track_) { |
| 628 | track_->UnregisterObserver(this); |
| 629 | } |
| 630 | if (can_send_track()) { |
deadbeef | 5a4a75a | 2016-06-02 16:23:38 -0700 | [diff] [blame] | 631 | ClearVideoSend(); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 632 | } |
Harald Alvestrand | 3d976f6 | 2018-03-19 19:05:06 +0100 | [diff] [blame] | 633 | media_channel_ = nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 634 | stopped_ = true; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 635 | } |
| 636 | |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 637 | void VideoRtpSender::SetMediaChannel(cricket::MediaChannel* media_channel) { |
| 638 | RTC_DCHECK(media_channel == nullptr || |
| 639 | media_channel->media_type() == media_type()); |
| 640 | media_channel_ = static_cast<cricket::VideoMediaChannel*>(media_channel); |
Benjamin Wright | bfd412e | 2018-09-10 14:06:02 -0700 | [diff] [blame] | 641 | } |
| 642 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 643 | void VideoRtpSender::SetVideoSend() { |
kwiberg | ee89e78 | 2017-08-09 17:22:01 -0700 | [diff] [blame] | 644 | RTC_DCHECK(!stopped_); |
| 645 | RTC_DCHECK(can_send_track()); |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 646 | if (!media_channel_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 647 | RTC_LOG(LS_ERROR) << "SetVideoSend: No video channel exists."; |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 648 | return; |
| 649 | } |
perkj | 0d3eef2 | 2016-03-09 02:39:17 +0100 | [diff] [blame] | 650 | cricket::VideoOptions options; |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 651 | VideoTrackSourceInterface* source = track_->GetSource(); |
perkj | 0d3eef2 | 2016-03-09 02:39:17 +0100 | [diff] [blame] | 652 | if (source) { |
Oskar Sundbom | 36f8f3e | 2017-11-16 10:54:27 +0100 | [diff] [blame] | 653 | options.is_screencast = source->is_screencast(); |
Per | c0d31e9 | 2016-03-31 17:23:39 +0200 | [diff] [blame] | 654 | options.video_noise_reduction = source->needs_denoising(); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 655 | } |
pbos | 5214a0a | 2016-12-16 15:39:11 -0800 | [diff] [blame] | 656 | switch (cached_track_content_hint_) { |
| 657 | case VideoTrackInterface::ContentHint::kNone: |
| 658 | break; |
| 659 | case VideoTrackInterface::ContentHint::kFluid: |
Oskar Sundbom | 36f8f3e | 2017-11-16 10:54:27 +0100 | [diff] [blame] | 660 | options.is_screencast = false; |
pbos | 5214a0a | 2016-12-16 15:39:11 -0800 | [diff] [blame] | 661 | break; |
| 662 | case VideoTrackInterface::ContentHint::kDetailed: |
Harald Alvestrand | c19ab07 | 2018-06-18 08:53:10 +0200 | [diff] [blame] | 663 | case VideoTrackInterface::ContentHint::kText: |
Oskar Sundbom | 36f8f3e | 2017-11-16 10:54:27 +0100 | [diff] [blame] | 664 | options.is_screencast = true; |
pbos | 5214a0a | 2016-12-16 15:39:11 -0800 | [diff] [blame] | 665 | break; |
| 666 | } |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 667 | bool success = worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 668 | return media_channel_->SetVideoSend(ssrc_, &options, track_); |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 669 | }); |
| 670 | RTC_DCHECK(success); |
deadbeef | 5a4a75a | 2016-06-02 16:23:38 -0700 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | void VideoRtpSender::ClearVideoSend() { |
| 674 | RTC_DCHECK(ssrc_ != 0); |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 675 | RTC_DCHECK(!stopped_); |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 676 | if (!media_channel_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 677 | RTC_LOG(LS_WARNING) << "SetVideoSend: No video channel exists."; |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 678 | return; |
| 679 | } |
| 680 | // Allow SetVideoSend to fail since |enable| is false and |source| is null. |
| 681 | // This the normal case when the underlying media channel has already been |
| 682 | // deleted. |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 683 | worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] { |
Niels Möller | ff40b14 | 2018-04-09 08:49:14 +0200 | [diff] [blame] | 684 | return media_channel_->SetVideoSend(ssrc_, nullptr, nullptr); |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 685 | }); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | } // namespace webrtc |