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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "api/mediastreamtrackproxy.h" |
| 17 | #include "api/videosourceproxy.h" |
| 18 | #include "pc/audiotrack.h" |
| 19 | #include "pc/videotrack.h" |
| 20 | #include "rtc_base/trace_event.h" |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 24 | namespace { |
| 25 | |
| 26 | // This function is only expected to be called on the signalling thread. |
| 27 | int GenerateUniqueId() { |
| 28 | static int g_unique_id = 0; |
| 29 | |
| 30 | return ++g_unique_id; |
| 31 | } |
| 32 | |
| 33 | } // namespace |
| 34 | |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 35 | AudioRtpReceiver::AudioRtpReceiver( |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 36 | rtc::Thread* worker_thread, |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 37 | const std::string& receiver_id, |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 38 | const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 39 | : worker_thread_(worker_thread), |
| 40 | id_(receiver_id), |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 41 | source_(new rtc::RefCountedObject<RemoteAudioSource>(worker_thread)), |
| 42 | track_(AudioTrackProxy::Create(rtc::Thread::Current(), |
| 43 | AudioTrack::Create(receiver_id, source_))), |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 44 | cached_track_enabled_(track_->enabled()), |
| 45 | attachment_id_(GenerateUniqueId()) { |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 46 | RTC_DCHECK(worker_thread_); |
tommi | 6eca7e3 | 2015-12-15 04:27:11 -0800 | [diff] [blame] | 47 | RTC_DCHECK(track_->GetSource()->remote()); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 48 | track_->RegisterObserver(this); |
| 49 | track_->GetSource()->RegisterAudioObserver(this); |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 50 | SetStreams(streams); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | AudioRtpReceiver::~AudioRtpReceiver() { |
| 54 | track_->GetSource()->UnregisterAudioObserver(this); |
| 55 | track_->UnregisterObserver(this); |
| 56 | Stop(); |
| 57 | } |
| 58 | |
| 59 | void AudioRtpReceiver::OnChanged() { |
| 60 | if (cached_track_enabled_ != track_->enabled()) { |
| 61 | cached_track_enabled_ = track_->enabled(); |
| 62 | Reconfigure(); |
| 63 | } |
| 64 | } |
| 65 | |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 66 | bool AudioRtpReceiver::SetOutputVolume(double volume) { |
| 67 | RTC_DCHECK_GE(volume, 0.0); |
| 68 | RTC_DCHECK_LE(volume, 10.0); |
| 69 | RTC_DCHECK(media_channel_); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 70 | RTC_DCHECK(ssrc_); |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 71 | return worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 72 | return media_channel_->SetOutputVolume(*ssrc_, volume); |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 73 | }); |
| 74 | } |
| 75 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 76 | void AudioRtpReceiver::OnSetVolume(double volume) { |
kwiberg | ee89e78 | 2017-08-09 17:22:01 -0700 | [diff] [blame] | 77 | RTC_DCHECK_GE(volume, 0); |
| 78 | RTC_DCHECK_LE(volume, 10); |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 79 | cached_volume_ = volume; |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 80 | if (!media_channel_ || !ssrc_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 81 | RTC_LOG(LS_ERROR) |
| 82 | << "AudioRtpReceiver::OnSetVolume: No audio channel exists."; |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 83 | return; |
| 84 | } |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 85 | // When the track is disabled, the volume of the source, which is the |
| 86 | // corresponding WebRtc Voice Engine channel will be 0. So we do not allow |
| 87 | // setting the volume to the source when the track is disabled. |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 88 | if (!stopped_ && track_->enabled()) { |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 89 | if (!SetOutputVolume(cached_volume_)) { |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 90 | RTC_NOTREACHED(); |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 95 | RtpParameters AudioRtpReceiver::GetParameters() const { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 96 | if (!media_channel_ || !ssrc_ || stopped_) { |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 97 | return RtpParameters(); |
| 98 | } |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 99 | return worker_thread_->Invoke<RtpParameters>(RTC_FROM_HERE, [&] { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 100 | return media_channel_->GetRtpReceiveParameters(*ssrc_); |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 101 | }); |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | bool AudioRtpReceiver::SetParameters(const RtpParameters& parameters) { |
| 105 | TRACE_EVENT0("webrtc", "AudioRtpReceiver::SetParameters"); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 106 | if (!media_channel_ || !ssrc_ || stopped_) { |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 107 | return false; |
| 108 | } |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 109 | return worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 110 | return media_channel_->SetRtpReceiveParameters(*ssrc_, parameters); |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 111 | }); |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 112 | } |
| 113 | |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 114 | void AudioRtpReceiver::Stop() { |
| 115 | // TODO(deadbeef): Need to do more here to fully stop receiving packets. |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 116 | if (stopped_) { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 117 | return; |
| 118 | } |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 119 | if (media_channel_ && ssrc_) { |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 120 | // Allow that SetOutputVolume fail. This is the normal case when the |
| 121 | // underlying media channel has already been deleted. |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 122 | SetOutputVolume(0.0); |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 123 | } |
| 124 | stopped_ = true; |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 127 | void AudioRtpReceiver::SetupMediaChannel(uint32_t ssrc) { |
| 128 | if (!media_channel_) { |
| 129 | RTC_LOG(LS_ERROR) |
| 130 | << "AudioRtpReceiver::SetupMediaChannel: No audio channel exists."; |
| 131 | return; |
| 132 | } |
| 133 | if (ssrc_ == ssrc) { |
| 134 | return; |
| 135 | } |
| 136 | if (ssrc_) { |
| 137 | source_->Stop(media_channel_, *ssrc_); |
| 138 | } |
| 139 | ssrc_ = ssrc; |
| 140 | source_->Start(media_channel_, *ssrc_); |
| 141 | Reconfigure(); |
| 142 | } |
| 143 | |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 144 | void AudioRtpReceiver::SetStreams( |
| 145 | const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) { |
| 146 | // Remove remote track from any streams that are going away. |
| 147 | for (auto existing_stream : streams_) { |
| 148 | bool removed = true; |
| 149 | for (auto stream : streams) { |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 150 | if (existing_stream->id() == stream->id()) { |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 151 | RTC_DCHECK_EQ(existing_stream.get(), stream.get()); |
| 152 | removed = false; |
| 153 | break; |
| 154 | } |
| 155 | } |
| 156 | if (removed) { |
| 157 | existing_stream->RemoveTrack(track_); |
| 158 | } |
| 159 | } |
| 160 | // Add remote track to any streams that are new. |
| 161 | for (auto stream : streams) { |
| 162 | bool added = true; |
| 163 | for (auto existing_stream : streams_) { |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 164 | if (stream->id() == existing_stream->id()) { |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 165 | RTC_DCHECK_EQ(stream.get(), existing_stream.get()); |
| 166 | added = false; |
| 167 | break; |
| 168 | } |
| 169 | } |
| 170 | if (added) { |
| 171 | stream->AddTrack(track_); |
| 172 | } |
| 173 | } |
| 174 | streams_ = streams; |
| 175 | } |
| 176 | |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 177 | std::vector<RtpSource> AudioRtpReceiver::GetSources() const { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 178 | if (!media_channel_ || !ssrc_ || stopped_) { |
| 179 | return {}; |
| 180 | } |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 181 | return worker_thread_->Invoke<std::vector<RtpSource>>( |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 182 | RTC_FROM_HERE, [&] { return media_channel_->GetSources(*ssrc_); }); |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 183 | } |
| 184 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 185 | void AudioRtpReceiver::Reconfigure() { |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 186 | RTC_DCHECK(!stopped_); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 187 | if (!media_channel_ || !ssrc_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 188 | RTC_LOG(LS_ERROR) |
| 189 | << "AudioRtpReceiver::Reconfigure: No audio channel exists."; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 190 | return; |
| 191 | } |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 192 | if (!SetOutputVolume(track_->enabled() ? cached_volume_ : 0)) { |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 193 | RTC_NOTREACHED(); |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 194 | } |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 195 | } |
| 196 | |
zhihuang | 184a3fd | 2016-06-14 11:47:14 -0700 | [diff] [blame] | 197 | void AudioRtpReceiver::SetObserver(RtpReceiverObserverInterface* observer) { |
| 198 | observer_ = observer; |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 199 | // Deliver any notifications the observer may have missed by being set late. |
zhihuang | c4adabf | 2016-12-07 10:36:40 -0800 | [diff] [blame] | 200 | if (received_first_packet_ && observer_) { |
zhihuang | 184a3fd | 2016-06-14 11:47:14 -0700 | [diff] [blame] | 201 | observer_->OnFirstPacketReceived(media_type()); |
| 202 | } |
| 203 | } |
| 204 | |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 205 | void AudioRtpReceiver::NotifyFirstPacketReceived() { |
zhihuang | 184a3fd | 2016-06-14 11:47:14 -0700 | [diff] [blame] | 206 | if (observer_) { |
| 207 | observer_->OnFirstPacketReceived(media_type()); |
| 208 | } |
| 209 | received_first_packet_ = true; |
| 210 | } |
| 211 | |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 212 | VideoRtpReceiver::VideoRtpReceiver( |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 213 | rtc::Thread* worker_thread, |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 214 | const std::string& receiver_id, |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 215 | const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 216 | : worker_thread_(worker_thread), |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 217 | id_(receiver_id), |
perkj | f0dcfe2 | 2016-03-10 18:32:00 +0100 | [diff] [blame] | 218 | source_(new RefCountedObject<VideoTrackSource>(&broadcaster_, |
perkj | f0dcfe2 | 2016-03-10 18:32:00 +0100 | [diff] [blame] | 219 | true /* remote */)), |
| 220 | track_(VideoTrackProxy::Create( |
| 221 | rtc::Thread::Current(), |
nisse | 5b68ab5 | 2016-04-07 07:45:54 -0700 | [diff] [blame] | 222 | worker_thread, |
| 223 | VideoTrack::Create( |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 224 | receiver_id, |
nisse | 5b68ab5 | 2016-04-07 07:45:54 -0700 | [diff] [blame] | 225 | VideoTrackSourceProxy::Create(rtc::Thread::Current(), |
| 226 | worker_thread, |
perkj | 773be36 | 2017-07-31 23:22:01 -0700 | [diff] [blame] | 227 | source_), |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 228 | worker_thread))), |
| 229 | attachment_id_(GenerateUniqueId()) { |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 230 | RTC_DCHECK(worker_thread_); |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 231 | SetStreams(streams); |
perkj | f0dcfe2 | 2016-03-10 18:32:00 +0100 | [diff] [blame] | 232 | source_->SetState(MediaSourceInterface::kLive); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | VideoRtpReceiver::~VideoRtpReceiver() { |
| 236 | // Since cricket::VideoRenderer is not reference counted, |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 237 | // we need to remove it from the channel before we are deleted. |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 238 | Stop(); |
| 239 | } |
| 240 | |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 241 | bool VideoRtpReceiver::SetSink(rtc::VideoSinkInterface<VideoFrame>* sink) { |
| 242 | RTC_DCHECK(media_channel_); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 243 | RTC_DCHECK(ssrc_); |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 244 | return worker_thread_->Invoke<bool>( |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 245 | RTC_FROM_HERE, [&] { return media_channel_->SetSink(*ssrc_, sink); }); |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 246 | } |
| 247 | |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 248 | RtpParameters VideoRtpReceiver::GetParameters() const { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 249 | if (!media_channel_ || !ssrc_ || stopped_) { |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 250 | return RtpParameters(); |
| 251 | } |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 252 | return worker_thread_->Invoke<RtpParameters>(RTC_FROM_HERE, [&] { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 253 | return media_channel_->GetRtpReceiveParameters(*ssrc_); |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 254 | }); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | bool VideoRtpReceiver::SetParameters(const RtpParameters& parameters) { |
| 258 | TRACE_EVENT0("webrtc", "VideoRtpReceiver::SetParameters"); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 259 | if (!media_channel_ || !ssrc_ || stopped_) { |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 260 | return false; |
| 261 | } |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 262 | return worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 263 | return media_channel_->SetRtpReceiveParameters(*ssrc_, parameters); |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 264 | }); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 265 | } |
| 266 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 267 | void VideoRtpReceiver::Stop() { |
| 268 | // TODO(deadbeef): Need to do more here to fully stop receiving packets. |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 269 | if (stopped_) { |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 270 | return; |
| 271 | } |
perkj | f0dcfe2 | 2016-03-10 18:32:00 +0100 | [diff] [blame] | 272 | source_->SetState(MediaSourceInterface::kEnded); |
| 273 | source_->OnSourceDestroyed(); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 274 | if (!media_channel_ || !ssrc_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 275 | RTC_LOG(LS_WARNING) << "VideoRtpReceiver::Stop: No video channel exists."; |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 276 | } else { |
| 277 | // Allow that SetSink fail. This is the normal case when the underlying |
| 278 | // media channel has already been deleted. |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 279 | SetSink(nullptr); |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 280 | } |
| 281 | stopped_ = true; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 284 | void VideoRtpReceiver::SetupMediaChannel(uint32_t ssrc) { |
| 285 | if (!media_channel_) { |
| 286 | RTC_LOG(LS_ERROR) |
| 287 | << "VideoRtpReceiver::SetupMediaChannel: No video channel exists."; |
| 288 | } |
| 289 | if (ssrc_ == ssrc) { |
| 290 | return; |
| 291 | } |
| 292 | if (ssrc_) { |
| 293 | SetSink(nullptr); |
| 294 | } |
| 295 | ssrc_ = ssrc; |
| 296 | SetSink(&broadcaster_); |
| 297 | } |
| 298 | |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 299 | void VideoRtpReceiver::SetStreams( |
| 300 | const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) { |
| 301 | // Remove remote track from any streams that are going away. |
| 302 | for (auto existing_stream : streams_) { |
| 303 | bool removed = true; |
| 304 | for (auto stream : streams) { |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 305 | if (existing_stream->id() == stream->id()) { |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 306 | RTC_DCHECK_EQ(existing_stream.get(), stream.get()); |
| 307 | removed = false; |
| 308 | break; |
| 309 | } |
| 310 | } |
| 311 | if (removed) { |
| 312 | existing_stream->RemoveTrack(track_); |
| 313 | } |
| 314 | } |
| 315 | // Add remote track to any streams that are new. |
| 316 | for (auto stream : streams) { |
| 317 | bool added = true; |
| 318 | for (auto existing_stream : streams_) { |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 319 | if (stream->id() == existing_stream->id()) { |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 320 | RTC_DCHECK_EQ(stream.get(), existing_stream.get()); |
| 321 | added = false; |
| 322 | break; |
| 323 | } |
| 324 | } |
| 325 | if (added) { |
| 326 | stream->AddTrack(track_); |
| 327 | } |
| 328 | } |
| 329 | streams_ = streams; |
| 330 | } |
| 331 | |
zhihuang | 184a3fd | 2016-06-14 11:47:14 -0700 | [diff] [blame] | 332 | void VideoRtpReceiver::SetObserver(RtpReceiverObserverInterface* observer) { |
| 333 | observer_ = observer; |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 334 | // Deliver any notifications the observer may have missed by being set late. |
zhihuang | c4adabf | 2016-12-07 10:36:40 -0800 | [diff] [blame] | 335 | if (received_first_packet_ && observer_) { |
zhihuang | 184a3fd | 2016-06-14 11:47:14 -0700 | [diff] [blame] | 336 | observer_->OnFirstPacketReceived(media_type()); |
| 337 | } |
| 338 | } |
| 339 | |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 340 | void VideoRtpReceiver::NotifyFirstPacketReceived() { |
zhihuang | 184a3fd | 2016-06-14 11:47:14 -0700 | [diff] [blame] | 341 | if (observer_) { |
| 342 | observer_->OnFirstPacketReceived(media_type()); |
| 343 | } |
| 344 | received_first_packet_ = true; |
| 345 | } |
| 346 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 347 | } // namespace webrtc |