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