| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/api/rtpreceiver.h" | 11 #include "webrtc/api/rtpreceiver.h" |
| 12 | 12 |
| 13 #include "webrtc/api/mediastreamtrackproxy.h" | 13 #include "webrtc/api/mediastreamtrackproxy.h" |
| 14 #include "webrtc/api/audiotrack.h" | 14 #include "webrtc/api/audiotrack.h" |
| 15 #include "webrtc/api/videosourceproxy.h" | 15 #include "webrtc/api/videosourceproxy.h" |
| 16 #include "webrtc/api/videotrack.h" | 16 #include "webrtc/api/videotrack.h" |
| 17 #include "webrtc/base/trace_event.h" | 17 #include "webrtc/base/trace_event.h" |
| 18 | 18 |
| 19 namespace webrtc { | 19 namespace webrtc { |
| 20 | 20 |
| 21 AudioRtpReceiver::AudioRtpReceiver(MediaStreamInterface* stream, | 21 AudioRtpReceiver::AudioRtpReceiver(MediaStreamInterface* stream, |
| 22 const std::string& track_id, | 22 const std::string& track_id, |
| 23 uint32_t ssrc, | 23 uint32_t ssrc, |
| 24 cricket::VoiceChannel* channel) | 24 AudioProviderInterface* provider) |
| 25 : id_(track_id), | 25 : id_(track_id), |
| 26 ssrc_(ssrc), | 26 ssrc_(ssrc), |
| 27 channel_(channel), | 27 provider_(provider), |
| 28 track_(AudioTrackProxy::Create( | 28 track_(AudioTrackProxy::Create( |
| 29 rtc::Thread::Current(), | 29 rtc::Thread::Current(), |
| 30 AudioTrack::Create(track_id, | 30 AudioTrack::Create(track_id, |
| 31 RemoteAudioSource::Create(ssrc, channel)))), | 31 RemoteAudioSource::Create(ssrc, provider)))), |
| 32 cached_track_enabled_(track_->enabled()) { | 32 cached_track_enabled_(track_->enabled()) { |
| 33 RTC_DCHECK(track_->GetSource()->remote()); | 33 RTC_DCHECK(track_->GetSource()->remote()); |
| 34 track_->RegisterObserver(this); | 34 track_->RegisterObserver(this); |
| 35 track_->GetSource()->RegisterAudioObserver(this); | 35 track_->GetSource()->RegisterAudioObserver(this); |
| 36 Reconfigure(); | 36 Reconfigure(); |
| 37 stream->AddTrack(track_); | 37 stream->AddTrack(track_); |
| 38 if (channel_) { | 38 provider_->SignalFirstAudioPacketReceived.connect( |
| 39 channel_->SignalFirstPacketReceived.connect( | 39 this, &AudioRtpReceiver::OnFirstAudioPacketReceived); |
| 40 this, &AudioRtpReceiver::OnFirstPacketReceived); | |
| 41 } | |
| 42 } | 40 } |
| 43 | 41 |
| 44 AudioRtpReceiver::~AudioRtpReceiver() { | 42 AudioRtpReceiver::~AudioRtpReceiver() { |
| 45 track_->GetSource()->UnregisterAudioObserver(this); | 43 track_->GetSource()->UnregisterAudioObserver(this); |
| 46 track_->UnregisterObserver(this); | 44 track_->UnregisterObserver(this); |
| 47 Stop(); | 45 Stop(); |
| 48 } | 46 } |
| 49 | 47 |
| 50 void AudioRtpReceiver::OnChanged() { | 48 void AudioRtpReceiver::OnChanged() { |
| 51 if (cached_track_enabled_ != track_->enabled()) { | 49 if (cached_track_enabled_ != track_->enabled()) { |
| 52 cached_track_enabled_ = track_->enabled(); | 50 cached_track_enabled_ = track_->enabled(); |
| 53 Reconfigure(); | 51 Reconfigure(); |
| 54 } | 52 } |
| 55 } | 53 } |
| 56 | 54 |
| 57 void AudioRtpReceiver::OnSetVolume(double volume) { | 55 void AudioRtpReceiver::OnSetVolume(double volume) { |
| 58 RTC_DCHECK(volume >= 0 && volume <= 10); | |
| 59 cached_volume_ = volume; | |
| 60 if (!channel_) { | |
| 61 LOG(LS_ERROR) << "AudioRtpReceiver::OnSetVolume: No audio channel exists."; | |
| 62 return; | |
| 63 } | |
| 64 // When the track is disabled, the volume of the source, which is the | 56 // When the track is disabled, the volume of the source, which is the |
| 65 // corresponding WebRtc Voice Engine channel will be 0. So we do not allow | 57 // corresponding WebRtc Voice Engine channel will be 0. So we do not allow |
| 66 // setting the volume to the source when the track is disabled. | 58 // setting the volume to the source when the track is disabled. |
| 67 if (!stopped_ && track_->enabled()) { | 59 if (provider_ && track_->enabled()) |
| 68 RTC_DCHECK(channel_->SetOutputVolume(ssrc_, cached_volume_)); | 60 provider_->SetAudioPlayoutVolume(ssrc_, volume); |
| 69 } | |
| 70 } | 61 } |
| 71 | 62 |
| 72 RtpParameters AudioRtpReceiver::GetParameters() const { | 63 RtpParameters AudioRtpReceiver::GetParameters() const { |
| 73 if (!channel_ || stopped_) { | 64 return provider_->GetAudioRtpReceiveParameters(ssrc_); |
| 74 return RtpParameters(); | |
| 75 } | |
| 76 return channel_->GetRtpReceiveParameters(ssrc_); | |
| 77 } | 65 } |
| 78 | 66 |
| 79 bool AudioRtpReceiver::SetParameters(const RtpParameters& parameters) { | 67 bool AudioRtpReceiver::SetParameters(const RtpParameters& parameters) { |
| 80 TRACE_EVENT0("webrtc", "AudioRtpReceiver::SetParameters"); | 68 TRACE_EVENT0("webrtc", "AudioRtpReceiver::SetParameters"); |
| 81 if (!channel_ || stopped_) { | 69 return provider_->SetAudioRtpReceiveParameters(ssrc_, parameters); |
| 82 return false; | |
| 83 } | |
| 84 return channel_->SetRtpReceiveParameters(ssrc_, parameters); | |
| 85 } | 70 } |
| 86 | 71 |
| 87 void AudioRtpReceiver::Stop() { | 72 void AudioRtpReceiver::Stop() { |
| 88 // TODO(deadbeef): Need to do more here to fully stop receiving packets. | 73 // TODO(deadbeef): Need to do more here to fully stop receiving packets. |
| 89 if (stopped_) { | 74 if (!provider_) { |
| 90 return; | 75 return; |
| 91 } | 76 } |
| 92 if (channel_) { | 77 provider_->SetAudioPlayout(ssrc_, false); |
| 93 // Allow that SetOutputVolume fail. This is the normal case when the | 78 provider_ = nullptr; |
| 94 // underlying media channel has already been deleted. | |
| 95 channel_->SetOutputVolume(ssrc_, 0); | |
| 96 } | |
| 97 stopped_ = true; | |
| 98 } | 79 } |
| 99 | 80 |
| 100 void AudioRtpReceiver::Reconfigure() { | 81 void AudioRtpReceiver::Reconfigure() { |
| 101 RTC_DCHECK(!stopped_); | 82 if (!provider_) { |
| 102 if (!channel_) { | |
| 103 LOG(LS_ERROR) << "AudioRtpReceiver::Reconfigure: No audio channel exists."; | |
| 104 return; | 83 return; |
| 105 } | 84 } |
| 106 RTC_DCHECK( | 85 provider_->SetAudioPlayout(ssrc_, track_->enabled()); |
| 107 channel_->SetOutputVolume(ssrc_, track_->enabled() ? cached_volume_ : 0)); | |
| 108 } | 86 } |
| 109 | 87 |
| 110 void AudioRtpReceiver::SetObserver(RtpReceiverObserverInterface* observer) { | 88 void AudioRtpReceiver::SetObserver(RtpReceiverObserverInterface* observer) { |
| 111 observer_ = observer; | 89 observer_ = observer; |
| 112 // Deliver any notifications the observer may have missed by being set late. | 90 // If received the first packet before setting the observer, call the |
| 91 // observer. |
| 113 if (received_first_packet_) { | 92 if (received_first_packet_) { |
| 114 observer_->OnFirstPacketReceived(media_type()); | 93 observer_->OnFirstPacketReceived(media_type()); |
| 115 } | 94 } |
| 116 } | 95 } |
| 117 | 96 |
| 118 void AudioRtpReceiver::SetChannel(cricket::VoiceChannel* channel) { | 97 void AudioRtpReceiver::OnFirstAudioPacketReceived() { |
| 119 if (channel_) { | |
| 120 channel_->SignalFirstPacketReceived.disconnect(this); | |
| 121 } | |
| 122 channel_ = channel; | |
| 123 if (channel_) { | |
| 124 channel_->SignalFirstPacketReceived.connect( | |
| 125 this, &AudioRtpReceiver::OnFirstPacketReceived); | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 void AudioRtpReceiver::OnFirstPacketReceived(cricket::BaseChannel* channel) { | |
| 130 if (observer_) { | 98 if (observer_) { |
| 131 observer_->OnFirstPacketReceived(media_type()); | 99 observer_->OnFirstPacketReceived(media_type()); |
| 132 } | 100 } |
| 133 received_first_packet_ = true; | 101 received_first_packet_ = true; |
| 134 } | 102 } |
| 135 | 103 |
| 136 VideoRtpReceiver::VideoRtpReceiver(MediaStreamInterface* stream, | 104 VideoRtpReceiver::VideoRtpReceiver(MediaStreamInterface* stream, |
| 137 const std::string& track_id, | 105 const std::string& track_id, |
| 138 rtc::Thread* worker_thread, | 106 rtc::Thread* worker_thread, |
| 139 uint32_t ssrc, | 107 uint32_t ssrc, |
| 140 cricket::VideoChannel* channel) | 108 VideoProviderInterface* provider) |
| 141 : id_(track_id), | 109 : id_(track_id), |
| 142 ssrc_(ssrc), | 110 ssrc_(ssrc), |
| 143 channel_(channel), | 111 provider_(provider), |
| 144 source_(new RefCountedObject<VideoTrackSource>(&broadcaster_, | 112 source_(new RefCountedObject<VideoTrackSource>(&broadcaster_, |
| 145 true /* remote */)), | 113 true /* remote */)), |
| 146 track_(VideoTrackProxy::Create( | 114 track_(VideoTrackProxy::Create( |
| 147 rtc::Thread::Current(), | 115 rtc::Thread::Current(), |
| 148 worker_thread, | 116 worker_thread, |
| 149 VideoTrack::Create( | 117 VideoTrack::Create( |
| 150 track_id, | 118 track_id, |
| 151 VideoTrackSourceProxy::Create(rtc::Thread::Current(), | 119 VideoTrackSourceProxy::Create(rtc::Thread::Current(), |
| 152 worker_thread, | 120 worker_thread, |
| 153 source_)))) { | 121 source_)))) { |
| 154 source_->SetState(MediaSourceInterface::kLive); | 122 source_->SetState(MediaSourceInterface::kLive); |
| 155 if (!channel_) { | 123 provider_->SetVideoPlayout(ssrc_, true, &broadcaster_); |
| 156 LOG(LS_ERROR) | |
| 157 << "VideoRtpReceiver::VideoRtpReceiver: No video channel exists."; | |
| 158 } else { | |
| 159 RTC_DCHECK(channel_->SetSink(ssrc_, &broadcaster_)); | |
| 160 } | |
| 161 stream->AddTrack(track_); | 124 stream->AddTrack(track_); |
| 162 if (channel_) { | 125 provider_->SignalFirstVideoPacketReceived.connect( |
| 163 channel_->SignalFirstPacketReceived.connect( | 126 this, &VideoRtpReceiver::OnFirstVideoPacketReceived); |
| 164 this, &VideoRtpReceiver::OnFirstPacketReceived); | |
| 165 } | |
| 166 } | 127 } |
| 167 | 128 |
| 168 VideoRtpReceiver::~VideoRtpReceiver() { | 129 VideoRtpReceiver::~VideoRtpReceiver() { |
| 169 // Since cricket::VideoRenderer is not reference counted, | 130 // Since cricket::VideoRenderer is not reference counted, |
| 170 // we need to remove it from the channel before we are deleted. | 131 // we need to remove it from the provider before we are deleted. |
| 171 Stop(); | 132 Stop(); |
| 172 } | 133 } |
| 173 | 134 |
| 174 RtpParameters VideoRtpReceiver::GetParameters() const { | 135 RtpParameters VideoRtpReceiver::GetParameters() const { |
| 175 if (!channel_ || stopped_) { | 136 return provider_->GetVideoRtpReceiveParameters(ssrc_); |
| 176 return RtpParameters(); | |
| 177 } | |
| 178 return channel_->GetRtpReceiveParameters(ssrc_); | |
| 179 } | 137 } |
| 180 | 138 |
| 181 bool VideoRtpReceiver::SetParameters(const RtpParameters& parameters) { | 139 bool VideoRtpReceiver::SetParameters(const RtpParameters& parameters) { |
| 182 TRACE_EVENT0("webrtc", "VideoRtpReceiver::SetParameters"); | 140 TRACE_EVENT0("webrtc", "VideoRtpReceiver::SetParameters"); |
| 183 if (!channel_ || stopped_) { | 141 return provider_->SetVideoRtpReceiveParameters(ssrc_, parameters); |
| 184 return false; | |
| 185 } | |
| 186 return channel_->SetRtpReceiveParameters(ssrc_, parameters); | |
| 187 } | 142 } |
| 188 | 143 |
| 189 void VideoRtpReceiver::Stop() { | 144 void VideoRtpReceiver::Stop() { |
| 190 // TODO(deadbeef): Need to do more here to fully stop receiving packets. | 145 // TODO(deadbeef): Need to do more here to fully stop receiving packets. |
| 191 if (stopped_) { | 146 if (!provider_) { |
| 192 return; | 147 return; |
| 193 } | 148 } |
| 194 source_->SetState(MediaSourceInterface::kEnded); | 149 source_->SetState(MediaSourceInterface::kEnded); |
| 195 source_->OnSourceDestroyed(); | 150 source_->OnSourceDestroyed(); |
| 196 if (!channel_) { | 151 provider_->SetVideoPlayout(ssrc_, false, nullptr); |
| 197 LOG(LS_WARNING) << "VideoRtpReceiver::Stop: No video channel exists."; | 152 provider_ = nullptr; |
| 198 } else { | |
| 199 // Allow that SetSink fail. This is the normal case when the underlying | |
| 200 // media channel has already been deleted. | |
| 201 channel_->SetSink(ssrc_, nullptr); | |
| 202 } | |
| 203 stopped_ = true; | |
| 204 } | 153 } |
| 205 | 154 |
| 206 void VideoRtpReceiver::SetObserver(RtpReceiverObserverInterface* observer) { | 155 void VideoRtpReceiver::SetObserver(RtpReceiverObserverInterface* observer) { |
| 207 observer_ = observer; | 156 observer_ = observer; |
| 208 // Deliver any notifications the observer may have missed by being set late. | 157 // If received the first packet before setting the observer, call the |
| 158 // observer. |
| 209 if (received_first_packet_) { | 159 if (received_first_packet_) { |
| 210 observer_->OnFirstPacketReceived(media_type()); | 160 observer_->OnFirstPacketReceived(media_type()); |
| 211 } | 161 } |
| 212 } | 162 } |
| 213 | 163 |
| 214 void VideoRtpReceiver::SetChannel(cricket::VideoChannel* channel) { | 164 void VideoRtpReceiver::OnFirstVideoPacketReceived() { |
| 215 if (channel_) { | |
| 216 channel_->SignalFirstPacketReceived.disconnect(this); | |
| 217 } | |
| 218 channel_ = channel; | |
| 219 if (channel_) { | |
| 220 channel_->SignalFirstPacketReceived.connect( | |
| 221 this, &VideoRtpReceiver::OnFirstPacketReceived); | |
| 222 } | |
| 223 } | |
| 224 | |
| 225 void VideoRtpReceiver::OnFirstPacketReceived(cricket::BaseChannel* channel) { | |
| 226 if (observer_) { | 165 if (observer_) { |
| 227 observer_->OnFirstPacketReceived(media_type()); | 166 observer_->OnFirstPacketReceived(media_type()); |
| 228 } | 167 } |
| 229 received_first_packet_ = true; | 168 received_first_packet_ = true; |
| 230 } | 169 } |
| 231 | 170 |
| 232 } // namespace webrtc | 171 } // namespace webrtc |
| OLD | NEW |