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