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/rtpreceiver.h" |
deadbeef | 6979b02 | 2015-09-24 16:47:53 -0700 | [diff] [blame] | 12 | |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 13 | #include <utility> |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 14 | #include <vector> |
| 15 | |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 16 | #include "api/mediastreamproxy.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "api/mediastreamtrackproxy.h" |
| 18 | #include "api/videosourceproxy.h" |
| 19 | #include "pc/audiotrack.h" |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 20 | #include "pc/mediastream.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "pc/videotrack.h" |
| 22 | #include "rtc_base/trace_event.h" |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
| 25 | |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 26 | namespace { |
| 27 | |
| 28 | // This function is only expected to be called on the signalling thread. |
| 29 | int GenerateUniqueId() { |
| 30 | static int g_unique_id = 0; |
| 31 | |
| 32 | return ++g_unique_id; |
| 33 | } |
| 34 | |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 35 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> CreateStreamsFromIds( |
| 36 | std::vector<std::string> stream_ids) { |
| 37 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams( |
| 38 | stream_ids.size()); |
| 39 | for (size_t i = 0; i < stream_ids.size(); ++i) { |
| 40 | streams[i] = MediaStreamProxy::Create( |
| 41 | rtc::Thread::Current(), MediaStream::Create(std::move(stream_ids[i]))); |
| 42 | } |
| 43 | return streams; |
| 44 | } |
| 45 | |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 46 | // Attempt to attach the frame decryptor to the current media channel on the |
| 47 | // correct worker thread only if both the media channel exists and a ssrc has |
| 48 | // been allocated to the stream. |
| 49 | void MaybeAttachFrameDecryptorToMediaChannel( |
| 50 | const absl::optional<uint32_t>& ssrc, |
Benjamin Wright | bfd412e | 2018-09-10 14:06:02 -0700 | [diff] [blame] | 51 | rtc::Thread* worker_thread, |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 52 | rtc::scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor, |
Benjamin Wright | c462a6e | 2018-10-26 13:16:16 -0700 | [diff] [blame^] | 53 | cricket::MediaChannel* media_channel, |
| 54 | bool stopped) { |
| 55 | if (media_channel && frame_decryptor && ssrc.has_value() && !stopped) { |
Benjamin Wright | 6cc9cca | 2018-10-09 17:29:54 -0700 | [diff] [blame] | 56 | worker_thread->Invoke<void>(RTC_FROM_HERE, [&] { |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 57 | media_channel->SetFrameDecryptor(*ssrc, frame_decryptor); |
Benjamin Wright | bfd412e | 2018-09-10 14:06:02 -0700 | [diff] [blame] | 58 | }); |
| 59 | } |
| 60 | } |
| 61 | |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 62 | } // namespace |
| 63 | |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 64 | AudioRtpReceiver::AudioRtpReceiver(rtc::Thread* worker_thread, |
| 65 | std::string receiver_id, |
| 66 | std::vector<std::string> stream_ids) |
| 67 | : AudioRtpReceiver(worker_thread, |
| 68 | receiver_id, |
| 69 | CreateStreamsFromIds(std::move(stream_ids))) {} |
| 70 | |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 71 | AudioRtpReceiver::AudioRtpReceiver( |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 72 | rtc::Thread* worker_thread, |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 73 | const std::string& receiver_id, |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 74 | const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 75 | : worker_thread_(worker_thread), |
| 76 | id_(receiver_id), |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 77 | source_(new rtc::RefCountedObject<RemoteAudioSource>(worker_thread)), |
| 78 | track_(AudioTrackProxy::Create(rtc::Thread::Current(), |
| 79 | AudioTrack::Create(receiver_id, source_))), |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 80 | cached_track_enabled_(track_->enabled()), |
| 81 | attachment_id_(GenerateUniqueId()) { |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 82 | RTC_DCHECK(worker_thread_); |
tommi | 6eca7e3 | 2015-12-15 04:27:11 -0800 | [diff] [blame] | 83 | RTC_DCHECK(track_->GetSource()->remote()); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 84 | track_->RegisterObserver(this); |
| 85 | track_->GetSource()->RegisterAudioObserver(this); |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 86 | SetStreams(streams); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | AudioRtpReceiver::~AudioRtpReceiver() { |
| 90 | track_->GetSource()->UnregisterAudioObserver(this); |
| 91 | track_->UnregisterObserver(this); |
| 92 | Stop(); |
| 93 | } |
| 94 | |
| 95 | void AudioRtpReceiver::OnChanged() { |
| 96 | if (cached_track_enabled_ != track_->enabled()) { |
| 97 | cached_track_enabled_ = track_->enabled(); |
| 98 | Reconfigure(); |
| 99 | } |
| 100 | } |
| 101 | |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 102 | bool AudioRtpReceiver::SetOutputVolume(double volume) { |
| 103 | RTC_DCHECK_GE(volume, 0.0); |
| 104 | RTC_DCHECK_LE(volume, 10.0); |
| 105 | RTC_DCHECK(media_channel_); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 106 | RTC_DCHECK(ssrc_); |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 107 | return worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 108 | return media_channel_->SetOutputVolume(*ssrc_, volume); |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 109 | }); |
| 110 | } |
| 111 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 112 | void AudioRtpReceiver::OnSetVolume(double volume) { |
kwiberg | ee89e78 | 2017-08-09 17:22:01 -0700 | [diff] [blame] | 113 | RTC_DCHECK_GE(volume, 0); |
| 114 | RTC_DCHECK_LE(volume, 10); |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 115 | cached_volume_ = volume; |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 116 | if (!media_channel_ || !ssrc_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 117 | RTC_LOG(LS_ERROR) |
| 118 | << "AudioRtpReceiver::OnSetVolume: No audio channel exists."; |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 119 | return; |
| 120 | } |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 121 | // When the track is disabled, the volume of the source, which is the |
| 122 | // corresponding WebRtc Voice Engine channel will be 0. So we do not allow |
| 123 | // setting the volume to the source when the track is disabled. |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 124 | if (!stopped_ && track_->enabled()) { |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 125 | if (!SetOutputVolume(cached_volume_)) { |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 126 | RTC_NOTREACHED(); |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 131 | std::vector<std::string> AudioRtpReceiver::stream_ids() const { |
| 132 | std::vector<std::string> stream_ids(streams_.size()); |
| 133 | for (size_t i = 0; i < streams_.size(); ++i) |
| 134 | stream_ids[i] = streams_[i]->id(); |
| 135 | return stream_ids; |
| 136 | } |
| 137 | |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 138 | RtpParameters AudioRtpReceiver::GetParameters() const { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 139 | if (!media_channel_ || !ssrc_ || stopped_) { |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 140 | return RtpParameters(); |
| 141 | } |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 142 | return worker_thread_->Invoke<RtpParameters>(RTC_FROM_HERE, [&] { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 143 | return media_channel_->GetRtpReceiveParameters(*ssrc_); |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 144 | }); |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | bool AudioRtpReceiver::SetParameters(const RtpParameters& parameters) { |
| 148 | TRACE_EVENT0("webrtc", "AudioRtpReceiver::SetParameters"); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 149 | if (!media_channel_ || !ssrc_ || stopped_) { |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 150 | return false; |
| 151 | } |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 152 | return worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 153 | return media_channel_->SetRtpReceiveParameters(*ssrc_, parameters); |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 154 | }); |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Benjamin Wright | d81ac95 | 2018-08-29 17:02:10 -0700 | [diff] [blame] | 157 | void AudioRtpReceiver::SetFrameDecryptor( |
| 158 | rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) { |
| 159 | frame_decryptor_ = std::move(frame_decryptor); |
Benjamin Wright | 6cc9cca | 2018-10-09 17:29:54 -0700 | [diff] [blame] | 160 | // Special Case: Set the frame decryptor to any value on any existing channel. |
Benjamin Wright | c462a6e | 2018-10-26 13:16:16 -0700 | [diff] [blame^] | 161 | if (media_channel_ && ssrc_.has_value() && !stopped_) { |
Benjamin Wright | 6cc9cca | 2018-10-09 17:29:54 -0700 | [diff] [blame] | 162 | worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] { |
| 163 | media_channel_->SetFrameDecryptor(*ssrc_, frame_decryptor_); |
| 164 | }); |
| 165 | } |
Benjamin Wright | d81ac95 | 2018-08-29 17:02:10 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | rtc::scoped_refptr<FrameDecryptorInterface> |
| 169 | AudioRtpReceiver::GetFrameDecryptor() const { |
| 170 | return frame_decryptor_; |
| 171 | } |
| 172 | |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 173 | void AudioRtpReceiver::Stop() { |
| 174 | // TODO(deadbeef): Need to do more here to fully stop receiving packets. |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 175 | if (stopped_) { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 176 | return; |
| 177 | } |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 178 | if (media_channel_ && ssrc_) { |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 179 | // Allow that SetOutputVolume fail. This is the normal case when the |
| 180 | // underlying media channel has already been deleted. |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 181 | SetOutputVolume(0.0); |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 182 | } |
| 183 | stopped_ = true; |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 186 | void AudioRtpReceiver::SetupMediaChannel(uint32_t ssrc) { |
| 187 | if (!media_channel_) { |
| 188 | RTC_LOG(LS_ERROR) |
| 189 | << "AudioRtpReceiver::SetupMediaChannel: No audio channel exists."; |
| 190 | return; |
| 191 | } |
| 192 | if (ssrc_ == ssrc) { |
| 193 | return; |
| 194 | } |
| 195 | if (ssrc_) { |
| 196 | source_->Stop(media_channel_, *ssrc_); |
| 197 | } |
| 198 | ssrc_ = ssrc; |
| 199 | source_->Start(media_channel_, *ssrc_); |
| 200 | Reconfigure(); |
| 201 | } |
| 202 | |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 203 | void AudioRtpReceiver::set_stream_ids(std::vector<std::string> stream_ids) { |
| 204 | SetStreams(CreateStreamsFromIds(std::move(stream_ids))); |
| 205 | } |
| 206 | |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 207 | void AudioRtpReceiver::SetStreams( |
| 208 | const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) { |
| 209 | // Remove remote track from any streams that are going away. |
| 210 | for (auto existing_stream : streams_) { |
| 211 | bool removed = true; |
| 212 | for (auto stream : streams) { |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 213 | if (existing_stream->id() == stream->id()) { |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 214 | RTC_DCHECK_EQ(existing_stream.get(), stream.get()); |
| 215 | removed = false; |
| 216 | break; |
| 217 | } |
| 218 | } |
| 219 | if (removed) { |
| 220 | existing_stream->RemoveTrack(track_); |
| 221 | } |
| 222 | } |
| 223 | // Add remote track to any streams that are new. |
| 224 | for (auto stream : streams) { |
| 225 | bool added = true; |
| 226 | for (auto existing_stream : streams_) { |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 227 | if (stream->id() == existing_stream->id()) { |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 228 | RTC_DCHECK_EQ(stream.get(), existing_stream.get()); |
| 229 | added = false; |
| 230 | break; |
| 231 | } |
| 232 | } |
| 233 | if (added) { |
| 234 | stream->AddTrack(track_); |
| 235 | } |
| 236 | } |
| 237 | streams_ = streams; |
| 238 | } |
| 239 | |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 240 | std::vector<RtpSource> AudioRtpReceiver::GetSources() const { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 241 | if (!media_channel_ || !ssrc_ || stopped_) { |
| 242 | return {}; |
| 243 | } |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 244 | return worker_thread_->Invoke<std::vector<RtpSource>>( |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 245 | RTC_FROM_HERE, [&] { return media_channel_->GetSources(*ssrc_); }); |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 246 | } |
| 247 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 248 | void AudioRtpReceiver::Reconfigure() { |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 249 | RTC_DCHECK(!stopped_); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 250 | if (!media_channel_ || !ssrc_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 251 | RTC_LOG(LS_ERROR) |
| 252 | << "AudioRtpReceiver::Reconfigure: No audio channel exists."; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 253 | return; |
| 254 | } |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 255 | if (!SetOutputVolume(track_->enabled() ? cached_volume_ : 0)) { |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 256 | RTC_NOTREACHED(); |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 257 | } |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 258 | // Reattach the frame decryptor if we were reconfigured. |
Benjamin Wright | c462a6e | 2018-10-26 13:16:16 -0700 | [diff] [blame^] | 259 | MaybeAttachFrameDecryptorToMediaChannel( |
| 260 | ssrc_, worker_thread_, frame_decryptor_, media_channel_, stopped_); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 261 | } |
| 262 | |
zhihuang | 184a3fd | 2016-06-14 11:47:14 -0700 | [diff] [blame] | 263 | void AudioRtpReceiver::SetObserver(RtpReceiverObserverInterface* observer) { |
| 264 | observer_ = observer; |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 265 | // Deliver any notifications the observer may have missed by being set late. |
zhihuang | c4adabf | 2016-12-07 10:36:40 -0800 | [diff] [blame] | 266 | if (received_first_packet_ && observer_) { |
zhihuang | 184a3fd | 2016-06-14 11:47:14 -0700 | [diff] [blame] | 267 | observer_->OnFirstPacketReceived(media_type()); |
| 268 | } |
| 269 | } |
| 270 | |
Benjamin Wright | bfd412e | 2018-09-10 14:06:02 -0700 | [diff] [blame] | 271 | void AudioRtpReceiver::SetVoiceMediaChannel( |
| 272 | cricket::VoiceMediaChannel* voice_media_channel) { |
| 273 | media_channel_ = voice_media_channel; |
Benjamin Wright | bfd412e | 2018-09-10 14:06:02 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 276 | void AudioRtpReceiver::NotifyFirstPacketReceived() { |
zhihuang | 184a3fd | 2016-06-14 11:47:14 -0700 | [diff] [blame] | 277 | if (observer_) { |
| 278 | observer_->OnFirstPacketReceived(media_type()); |
| 279 | } |
| 280 | received_first_packet_ = true; |
| 281 | } |
| 282 | |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 283 | VideoRtpReceiver::VideoRtpReceiver(rtc::Thread* worker_thread, |
| 284 | std::string receiver_id, |
| 285 | std::vector<std::string> stream_ids) |
| 286 | : VideoRtpReceiver(worker_thread, |
| 287 | receiver_id, |
| 288 | CreateStreamsFromIds(std::move(stream_ids))) {} |
| 289 | |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 290 | VideoRtpReceiver::VideoRtpReceiver( |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 291 | rtc::Thread* worker_thread, |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 292 | const std::string& receiver_id, |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 293 | const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 294 | : worker_thread_(worker_thread), |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 295 | id_(receiver_id), |
Niels Möller | 5d67f82 | 2018-05-23 16:28:17 +0200 | [diff] [blame] | 296 | source_(new RefCountedObject<VideoRtpTrackSource>()), |
perkj | f0dcfe2 | 2016-03-10 18:32:00 +0100 | [diff] [blame] | 297 | track_(VideoTrackProxy::Create( |
| 298 | rtc::Thread::Current(), |
nisse | 5b68ab5 | 2016-04-07 07:45:54 -0700 | [diff] [blame] | 299 | worker_thread, |
| 300 | VideoTrack::Create( |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 301 | receiver_id, |
nisse | 5b68ab5 | 2016-04-07 07:45:54 -0700 | [diff] [blame] | 302 | VideoTrackSourceProxy::Create(rtc::Thread::Current(), |
| 303 | worker_thread, |
perkj | 773be36 | 2017-07-31 23:22:01 -0700 | [diff] [blame] | 304 | source_), |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 305 | worker_thread))), |
| 306 | attachment_id_(GenerateUniqueId()) { |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 307 | RTC_DCHECK(worker_thread_); |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 308 | SetStreams(streams); |
perkj | f0dcfe2 | 2016-03-10 18:32:00 +0100 | [diff] [blame] | 309 | source_->SetState(MediaSourceInterface::kLive); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | VideoRtpReceiver::~VideoRtpReceiver() { |
| 313 | // Since cricket::VideoRenderer is not reference counted, |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 314 | // we need to remove it from the channel before we are deleted. |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 315 | Stop(); |
| 316 | } |
| 317 | |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 318 | std::vector<std::string> VideoRtpReceiver::stream_ids() const { |
| 319 | std::vector<std::string> stream_ids(streams_.size()); |
| 320 | for (size_t i = 0; i < streams_.size(); ++i) |
| 321 | stream_ids[i] = streams_[i]->id(); |
| 322 | return stream_ids; |
| 323 | } |
| 324 | |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 325 | bool VideoRtpReceiver::SetSink(rtc::VideoSinkInterface<VideoFrame>* sink) { |
| 326 | RTC_DCHECK(media_channel_); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 327 | RTC_DCHECK(ssrc_); |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 328 | return worker_thread_->Invoke<bool>( |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 329 | RTC_FROM_HERE, [&] { return media_channel_->SetSink(*ssrc_, sink); }); |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 330 | } |
| 331 | |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 332 | RtpParameters VideoRtpReceiver::GetParameters() const { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 333 | if (!media_channel_ || !ssrc_ || stopped_) { |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 334 | return RtpParameters(); |
| 335 | } |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 336 | return worker_thread_->Invoke<RtpParameters>(RTC_FROM_HERE, [&] { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 337 | return media_channel_->GetRtpReceiveParameters(*ssrc_); |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 338 | }); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | bool VideoRtpReceiver::SetParameters(const RtpParameters& parameters) { |
| 342 | TRACE_EVENT0("webrtc", "VideoRtpReceiver::SetParameters"); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 343 | if (!media_channel_ || !ssrc_ || stopped_) { |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 344 | return false; |
| 345 | } |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 346 | return worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 347 | return media_channel_->SetRtpReceiveParameters(*ssrc_, parameters); |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 348 | }); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Benjamin Wright | d81ac95 | 2018-08-29 17:02:10 -0700 | [diff] [blame] | 351 | void VideoRtpReceiver::SetFrameDecryptor( |
| 352 | rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) { |
| 353 | frame_decryptor_ = std::move(frame_decryptor); |
Benjamin Wright | 6cc9cca | 2018-10-09 17:29:54 -0700 | [diff] [blame] | 354 | // Special Case: Set the frame decryptor to any value on any existing channel. |
Benjamin Wright | c462a6e | 2018-10-26 13:16:16 -0700 | [diff] [blame^] | 355 | if (media_channel_ && ssrc_.has_value() && !stopped_) { |
Benjamin Wright | 6cc9cca | 2018-10-09 17:29:54 -0700 | [diff] [blame] | 356 | worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] { |
| 357 | media_channel_->SetFrameDecryptor(*ssrc_, frame_decryptor_); |
| 358 | }); |
| 359 | } |
Benjamin Wright | d81ac95 | 2018-08-29 17:02:10 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | rtc::scoped_refptr<FrameDecryptorInterface> |
| 363 | VideoRtpReceiver::GetFrameDecryptor() const { |
| 364 | return frame_decryptor_; |
| 365 | } |
| 366 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 367 | void VideoRtpReceiver::Stop() { |
| 368 | // TODO(deadbeef): Need to do more here to fully stop receiving packets. |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 369 | if (stopped_) { |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 370 | return; |
| 371 | } |
perkj | f0dcfe2 | 2016-03-10 18:32:00 +0100 | [diff] [blame] | 372 | source_->SetState(MediaSourceInterface::kEnded); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 373 | if (!media_channel_ || !ssrc_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 374 | RTC_LOG(LS_WARNING) << "VideoRtpReceiver::Stop: No video channel exists."; |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 375 | } else { |
| 376 | // Allow that SetSink fail. This is the normal case when the underlying |
| 377 | // media channel has already been deleted. |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 378 | SetSink(nullptr); |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 379 | } |
| 380 | stopped_ = true; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 383 | void VideoRtpReceiver::SetupMediaChannel(uint32_t ssrc) { |
| 384 | if (!media_channel_) { |
| 385 | RTC_LOG(LS_ERROR) |
| 386 | << "VideoRtpReceiver::SetupMediaChannel: No video channel exists."; |
| 387 | } |
| 388 | if (ssrc_ == ssrc) { |
| 389 | return; |
| 390 | } |
| 391 | if (ssrc_) { |
| 392 | SetSink(nullptr); |
| 393 | } |
| 394 | ssrc_ = ssrc; |
Niels Möller | 5d67f82 | 2018-05-23 16:28:17 +0200 | [diff] [blame] | 395 | SetSink(source_->sink()); |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 396 | // Attach any existing frame decryptor to the media channel. |
Benjamin Wright | c462a6e | 2018-10-26 13:16:16 -0700 | [diff] [blame^] | 397 | MaybeAttachFrameDecryptorToMediaChannel( |
| 398 | ssrc_, worker_thread_, frame_decryptor_, media_channel_, stopped_); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 399 | } |
| 400 | |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 401 | void VideoRtpReceiver::set_stream_ids(std::vector<std::string> stream_ids) { |
| 402 | SetStreams(CreateStreamsFromIds(std::move(stream_ids))); |
| 403 | } |
| 404 | |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 405 | void VideoRtpReceiver::SetStreams( |
| 406 | const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) { |
| 407 | // Remove remote track from any streams that are going away. |
| 408 | for (auto existing_stream : streams_) { |
| 409 | bool removed = true; |
| 410 | for (auto stream : streams) { |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 411 | if (existing_stream->id() == stream->id()) { |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 412 | RTC_DCHECK_EQ(existing_stream.get(), stream.get()); |
| 413 | removed = false; |
| 414 | break; |
| 415 | } |
| 416 | } |
| 417 | if (removed) { |
| 418 | existing_stream->RemoveTrack(track_); |
| 419 | } |
| 420 | } |
| 421 | // Add remote track to any streams that are new. |
| 422 | for (auto stream : streams) { |
| 423 | bool added = true; |
| 424 | for (auto existing_stream : streams_) { |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 425 | if (stream->id() == existing_stream->id()) { |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 426 | RTC_DCHECK_EQ(stream.get(), existing_stream.get()); |
| 427 | added = false; |
| 428 | break; |
| 429 | } |
| 430 | } |
| 431 | if (added) { |
| 432 | stream->AddTrack(track_); |
| 433 | } |
| 434 | } |
| 435 | streams_ = streams; |
| 436 | } |
| 437 | |
zhihuang | 184a3fd | 2016-06-14 11:47:14 -0700 | [diff] [blame] | 438 | void VideoRtpReceiver::SetObserver(RtpReceiverObserverInterface* observer) { |
| 439 | observer_ = observer; |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 440 | // Deliver any notifications the observer may have missed by being set late. |
zhihuang | c4adabf | 2016-12-07 10:36:40 -0800 | [diff] [blame] | 441 | if (received_first_packet_ && observer_) { |
zhihuang | 184a3fd | 2016-06-14 11:47:14 -0700 | [diff] [blame] | 442 | observer_->OnFirstPacketReceived(media_type()); |
| 443 | } |
| 444 | } |
| 445 | |
Benjamin Wright | bfd412e | 2018-09-10 14:06:02 -0700 | [diff] [blame] | 446 | void VideoRtpReceiver::SetVideoMediaChannel( |
| 447 | cricket::VideoMediaChannel* video_media_channel) { |
| 448 | media_channel_ = video_media_channel; |
Benjamin Wright | bfd412e | 2018-09-10 14:06:02 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 451 | void VideoRtpReceiver::NotifyFirstPacketReceived() { |
zhihuang | 184a3fd | 2016-06-14 11:47:14 -0700 | [diff] [blame] | 452 | if (observer_) { |
| 453 | observer_->OnFirstPacketReceived(media_type()); |
| 454 | } |
| 455 | received_first_packet_ = true; |
| 456 | } |
| 457 | |
Jonas Oreland | 49ac595 | 2018-09-26 16:04:32 +0200 | [diff] [blame] | 458 | std::vector<RtpSource> VideoRtpReceiver::GetSources() const { |
| 459 | if (!media_channel_ || !ssrc_ || stopped_) { |
| 460 | return {}; |
| 461 | } |
| 462 | return worker_thread_->Invoke<std::vector<RtpSource>>( |
| 463 | RTC_FROM_HERE, [&] { return media_channel_->GetSources(*ssrc_); }); |
| 464 | } |
| 465 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 466 | } // namespace webrtc |