| OLD | NEW |
| 1 /* | 1 /* |
| 2 * libjingle | 2 * libjingle |
| 3 * Copyright 2015 Google Inc. | 3 * Copyright 2015 Google Inc. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions are met: | 6 * modification, are permitted provided that the following conditions are met: |
| 7 * | 7 * |
| 8 * 1. Redistributions of source code must retain the above copyright notice, | 8 * 1. Redistributions of source code must retain the above copyright notice, |
| 9 * this list of conditions and the following disclaimer. | 9 * this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright notice, | 10 * 2. Redistributions in binary form must reproduce the above copyright notice, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 */ | 26 */ |
| 27 | 27 |
| 28 #include "talk/app/webrtc/rtpsender.h" | 28 #include "talk/app/webrtc/rtpsender.h" |
| 29 | 29 |
| 30 #include "talk/app/webrtc/localaudiosource.h" | 30 #include "talk/app/webrtc/localaudiosource.h" |
| 31 #include "talk/app/webrtc/videosourceinterface.h" | 31 #include "talk/app/webrtc/videosourceinterface.h" |
| 32 #include "webrtc/base/helpers.h" | |
| 33 | 32 |
| 34 namespace webrtc { | 33 namespace webrtc { |
| 35 | 34 |
| 36 LocalAudioSinkAdapter::LocalAudioSinkAdapter() : sink_(nullptr) {} | 35 LocalAudioSinkAdapter::LocalAudioSinkAdapter() : sink_(nullptr) {} |
| 37 | 36 |
| 38 LocalAudioSinkAdapter::~LocalAudioSinkAdapter() { | 37 LocalAudioSinkAdapter::~LocalAudioSinkAdapter() { |
| 39 rtc::CritScope lock(&lock_); | 38 rtc::CritScope lock(&lock_); |
| 40 if (sink_) | 39 if (sink_) |
| 41 sink_->OnClose(); | 40 sink_->OnClose(); |
| 42 } | 41 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 53 } | 52 } |
| 54 } | 53 } |
| 55 | 54 |
| 56 void LocalAudioSinkAdapter::SetSink(cricket::AudioRenderer::Sink* sink) { | 55 void LocalAudioSinkAdapter::SetSink(cricket::AudioRenderer::Sink* sink) { |
| 57 rtc::CritScope lock(&lock_); | 56 rtc::CritScope lock(&lock_); |
| 58 ASSERT(!sink || !sink_); | 57 ASSERT(!sink || !sink_); |
| 59 sink_ = sink; | 58 sink_ = sink; |
| 60 } | 59 } |
| 61 | 60 |
| 62 AudioRtpSender::AudioRtpSender(AudioTrackInterface* track, | 61 AudioRtpSender::AudioRtpSender(AudioTrackInterface* track, |
| 63 const std::string& stream_id, | 62 uint32_t ssrc, |
| 64 AudioProviderInterface* provider, | 63 AudioProviderInterface* provider) |
| 65 StatsCollector* stats) | |
| 66 : id_(track->id()), | 64 : id_(track->id()), |
| 67 stream_id_(stream_id), | 65 track_(track), |
| 66 ssrc_(ssrc), |
| 68 provider_(provider), | 67 provider_(provider), |
| 69 stats_(stats), | |
| 70 track_(track), | |
| 71 cached_track_enabled_(track->enabled()), | 68 cached_track_enabled_(track->enabled()), |
| 72 sink_adapter_(new LocalAudioSinkAdapter()) { | 69 sink_adapter_(new LocalAudioSinkAdapter()) { |
| 73 RTC_DCHECK(provider != nullptr); | |
| 74 track_->RegisterObserver(this); | 70 track_->RegisterObserver(this); |
| 75 track_->AddSink(sink_adapter_.get()); | 71 track_->AddSink(sink_adapter_.get()); |
| 72 Reconfigure(); |
| 76 } | 73 } |
| 77 | 74 |
| 78 AudioRtpSender::AudioRtpSender(AudioProviderInterface* provider, | |
| 79 StatsCollector* stats) | |
| 80 : id_(rtc::CreateRandomUuid()), | |
| 81 stream_id_(rtc::CreateRandomUuid()), | |
| 82 provider_(provider), | |
| 83 stats_(stats), | |
| 84 sink_adapter_(new LocalAudioSinkAdapter()) {} | |
| 85 | |
| 86 AudioRtpSender::~AudioRtpSender() { | 75 AudioRtpSender::~AudioRtpSender() { |
| 76 track_->RemoveSink(sink_adapter_.get()); |
| 77 track_->UnregisterObserver(this); |
| 87 Stop(); | 78 Stop(); |
| 88 } | 79 } |
| 89 | 80 |
| 90 void AudioRtpSender::OnChanged() { | 81 void AudioRtpSender::OnChanged() { |
| 91 RTC_DCHECK(!stopped_); | |
| 92 if (cached_track_enabled_ != track_->enabled()) { | 82 if (cached_track_enabled_ != track_->enabled()) { |
| 93 cached_track_enabled_ = track_->enabled(); | 83 cached_track_enabled_ = track_->enabled(); |
| 94 if (can_send_track()) { | 84 Reconfigure(); |
| 95 SetAudioSend(); | |
| 96 } | |
| 97 } | 85 } |
| 98 } | 86 } |
| 99 | 87 |
| 100 bool AudioRtpSender::SetTrack(MediaStreamTrackInterface* track) { | 88 bool AudioRtpSender::SetTrack(MediaStreamTrackInterface* track) { |
| 101 if (stopped_) { | 89 if (track->kind() != "audio") { |
| 102 LOG(LS_ERROR) << "SetTrack can't be called on a stopped RtpSender."; | |
| 103 return false; | |
| 104 } | |
| 105 if (track && track->kind() != MediaStreamTrackInterface::kAudioTrackKind) { | |
| 106 LOG(LS_ERROR) << "SetTrack called on audio RtpSender with " << track->kind() | 90 LOG(LS_ERROR) << "SetTrack called on audio RtpSender with " << track->kind() |
| 107 << " track."; | 91 << " track."; |
| 108 return false; | 92 return false; |
| 109 } | 93 } |
| 110 AudioTrackInterface* audio_track = static_cast<AudioTrackInterface*>(track); | 94 AudioTrackInterface* audio_track = static_cast<AudioTrackInterface*>(track); |
| 111 | 95 |
| 112 // Detach from old track. | 96 // Detach from old track. |
| 113 if (track_) { | 97 track_->RemoveSink(sink_adapter_.get()); |
| 114 track_->RemoveSink(sink_adapter_.get()); | 98 track_->UnregisterObserver(this); |
| 115 track_->UnregisterObserver(this); | |
| 116 } | |
| 117 | |
| 118 if (can_send_track() && stats_) { | |
| 119 stats_->RemoveLocalAudioTrack(track_.get(), ssrc_); | |
| 120 } | |
| 121 | 99 |
| 122 // Attach to new track. | 100 // Attach to new track. |
| 123 bool prev_can_send_track = can_send_track(); | |
| 124 track_ = audio_track; | 101 track_ = audio_track; |
| 125 if (track_) { | 102 cached_track_enabled_ = track_->enabled(); |
| 126 cached_track_enabled_ = track_->enabled(); | 103 track_->RegisterObserver(this); |
| 127 track_->RegisterObserver(this); | 104 track_->AddSink(sink_adapter_.get()); |
| 128 track_->AddSink(sink_adapter_.get()); | 105 Reconfigure(); |
| 129 } | |
| 130 | |
| 131 // Update audio provider. | |
| 132 if (can_send_track()) { | |
| 133 SetAudioSend(); | |
| 134 if (stats_) { | |
| 135 stats_->AddLocalAudioTrack(track_.get(), ssrc_); | |
| 136 } | |
| 137 } else if (prev_can_send_track) { | |
| 138 cricket::AudioOptions options; | |
| 139 provider_->SetAudioSend(ssrc_, false, options, nullptr); | |
| 140 } | |
| 141 return true; | 106 return true; |
| 142 } | 107 } |
| 143 | 108 |
| 144 void AudioRtpSender::SetSsrc(uint32_t ssrc) { | |
| 145 if (stopped_ || ssrc == ssrc_) { | |
| 146 return; | |
| 147 } | |
| 148 // If we are already sending with a particular SSRC, stop sending. | |
| 149 if (can_send_track()) { | |
| 150 cricket::AudioOptions options; | |
| 151 provider_->SetAudioSend(ssrc_, false, options, nullptr); | |
| 152 if (stats_) { | |
| 153 stats_->RemoveLocalAudioTrack(track_.get(), ssrc_); | |
| 154 } | |
| 155 } | |
| 156 ssrc_ = ssrc; | |
| 157 if (can_send_track()) { | |
| 158 SetAudioSend(); | |
| 159 if (stats_) { | |
| 160 stats_->AddLocalAudioTrack(track_.get(), ssrc_); | |
| 161 } | |
| 162 } | |
| 163 } | |
| 164 | |
| 165 void AudioRtpSender::Stop() { | 109 void AudioRtpSender::Stop() { |
| 166 // TODO(deadbeef): Need to do more here to fully stop sending packets. | 110 // TODO(deadbeef): Need to do more here to fully stop sending packets. |
| 167 if (stopped_) { | 111 if (!provider_) { |
| 168 return; | 112 return; |
| 169 } | 113 } |
| 170 if (track_) { | 114 cricket::AudioOptions options; |
| 171 track_->RemoveSink(sink_adapter_.get()); | 115 provider_->SetAudioSend(ssrc_, false, options, nullptr); |
| 172 track_->UnregisterObserver(this); | 116 provider_ = nullptr; |
| 173 } | |
| 174 if (can_send_track()) { | |
| 175 cricket::AudioOptions options; | |
| 176 provider_->SetAudioSend(ssrc_, false, options, nullptr); | |
| 177 if (stats_) { | |
| 178 stats_->RemoveLocalAudioTrack(track_.get(), ssrc_); | |
| 179 } | |
| 180 } | |
| 181 stopped_ = true; | |
| 182 } | 117 } |
| 183 | 118 |
| 184 void AudioRtpSender::SetAudioSend() { | 119 void AudioRtpSender::Reconfigure() { |
| 185 RTC_DCHECK(!stopped_ && can_send_track()); | 120 if (!provider_) { |
| 121 return; |
| 122 } |
| 186 cricket::AudioOptions options; | 123 cricket::AudioOptions options; |
| 187 if (track_->enabled() && track_->GetSource()) { | 124 if (track_->enabled() && track_->GetSource()) { |
| 188 // TODO(xians): Remove this static_cast since we should be able to connect | 125 // TODO(xians): Remove this static_cast since we should be able to connect |
| 189 // a remote audio track to a peer connection. | 126 // a remote audio track to peer connection. |
| 190 options = static_cast<LocalAudioSource*>(track_->GetSource())->options(); | 127 options = static_cast<LocalAudioSource*>(track_->GetSource())->options(); |
| 191 } | 128 } |
| 192 | 129 |
| 193 // Use the renderer if the audio track has one, otherwise use the sink | 130 // Use the renderer if the audio track has one, otherwise use the sink |
| 194 // adapter owned by this class. | 131 // adapter owned by this class. |
| 195 cricket::AudioRenderer* renderer = | 132 cricket::AudioRenderer* renderer = |
| 196 track_->GetRenderer() ? track_->GetRenderer() : sink_adapter_.get(); | 133 track_->GetRenderer() ? track_->GetRenderer() : sink_adapter_.get(); |
| 197 ASSERT(renderer != nullptr); | 134 ASSERT(renderer != nullptr); |
| 198 provider_->SetAudioSend(ssrc_, track_->enabled(), options, renderer); | 135 provider_->SetAudioSend(ssrc_, track_->enabled(), options, renderer); |
| 199 } | 136 } |
| 200 | 137 |
| 201 VideoRtpSender::VideoRtpSender(VideoTrackInterface* track, | 138 VideoRtpSender::VideoRtpSender(VideoTrackInterface* track, |
| 202 const std::string& stream_id, | 139 uint32_t ssrc, |
| 203 VideoProviderInterface* provider) | 140 VideoProviderInterface* provider) |
| 204 : id_(track->id()), | 141 : id_(track->id()), |
| 205 stream_id_(stream_id), | 142 track_(track), |
| 143 ssrc_(ssrc), |
| 206 provider_(provider), | 144 provider_(provider), |
| 207 track_(track), | |
| 208 cached_track_enabled_(track->enabled()) { | 145 cached_track_enabled_(track->enabled()) { |
| 209 RTC_DCHECK(provider != nullptr); | |
| 210 track_->RegisterObserver(this); | 146 track_->RegisterObserver(this); |
| 147 VideoSourceInterface* source = track_->GetSource(); |
| 148 if (source) { |
| 149 provider_->SetCaptureDevice(ssrc_, source->GetVideoCapturer()); |
| 150 } |
| 151 Reconfigure(); |
| 211 } | 152 } |
| 212 | 153 |
| 213 VideoRtpSender::VideoRtpSender(VideoProviderInterface* provider) | |
| 214 : id_(rtc::CreateRandomUuid()), | |
| 215 stream_id_(rtc::CreateRandomUuid()), | |
| 216 provider_(provider) {} | |
| 217 | |
| 218 VideoRtpSender::~VideoRtpSender() { | 154 VideoRtpSender::~VideoRtpSender() { |
| 155 track_->UnregisterObserver(this); |
| 219 Stop(); | 156 Stop(); |
| 220 } | 157 } |
| 221 | 158 |
| 222 void VideoRtpSender::OnChanged() { | 159 void VideoRtpSender::OnChanged() { |
| 223 RTC_DCHECK(!stopped_); | |
| 224 if (cached_track_enabled_ != track_->enabled()) { | 160 if (cached_track_enabled_ != track_->enabled()) { |
| 225 cached_track_enabled_ = track_->enabled(); | 161 cached_track_enabled_ = track_->enabled(); |
| 226 if (can_send_track()) { | 162 Reconfigure(); |
| 227 SetVideoSend(); | |
| 228 } | |
| 229 } | 163 } |
| 230 } | 164 } |
| 231 | 165 |
| 232 bool VideoRtpSender::SetTrack(MediaStreamTrackInterface* track) { | 166 bool VideoRtpSender::SetTrack(MediaStreamTrackInterface* track) { |
| 233 if (stopped_) { | 167 if (track->kind() != "video") { |
| 234 LOG(LS_ERROR) << "SetTrack can't be called on a stopped RtpSender."; | |
| 235 return false; | |
| 236 } | |
| 237 if (track && track->kind() != MediaStreamTrackInterface::kVideoTrackKind) { | |
| 238 LOG(LS_ERROR) << "SetTrack called on video RtpSender with " << track->kind() | 168 LOG(LS_ERROR) << "SetTrack called on video RtpSender with " << track->kind() |
| 239 << " track."; | 169 << " track."; |
| 240 return false; | 170 return false; |
| 241 } | 171 } |
| 242 VideoTrackInterface* video_track = static_cast<VideoTrackInterface*>(track); | 172 VideoTrackInterface* video_track = static_cast<VideoTrackInterface*>(track); |
| 243 | 173 |
| 244 // Detach from old track. | 174 // Detach from old track. |
| 245 if (track_) { | 175 track_->UnregisterObserver(this); |
| 246 track_->UnregisterObserver(this); | |
| 247 } | |
| 248 | 176 |
| 249 // Attach to new track. | 177 // Attach to new track. |
| 250 bool prev_can_send_track = can_send_track(); | |
| 251 track_ = video_track; | 178 track_ = video_track; |
| 252 if (track_) { | 179 cached_track_enabled_ = track_->enabled(); |
| 253 cached_track_enabled_ = track_->enabled(); | 180 track_->RegisterObserver(this); |
| 254 track_->RegisterObserver(this); | 181 Reconfigure(); |
| 255 } | |
| 256 | |
| 257 // Update video provider. | |
| 258 if (can_send_track()) { | |
| 259 VideoSourceInterface* source = track_->GetSource(); | |
| 260 // TODO(deadbeef): If SetTrack is called with a disabled track, and the | |
| 261 // previous track was enabled, this could cause a frame from the new track | |
| 262 // to slip out. Really, what we need is for SetCaptureDevice and | |
| 263 // SetVideoSend | |
| 264 // to be combined into one atomic operation, all the way down to | |
| 265 // WebRtcVideoSendStream. | |
| 266 provider_->SetCaptureDevice(ssrc_, | |
| 267 source ? source->GetVideoCapturer() : nullptr); | |
| 268 SetVideoSend(); | |
| 269 } else if (prev_can_send_track) { | |
| 270 provider_->SetCaptureDevice(ssrc_, nullptr); | |
| 271 provider_->SetVideoSend(ssrc_, false, nullptr); | |
| 272 } | |
| 273 return true; | 182 return true; |
| 274 } | 183 } |
| 275 | 184 |
| 276 void VideoRtpSender::SetSsrc(uint32_t ssrc) { | |
| 277 if (stopped_ || ssrc == ssrc_) { | |
| 278 return; | |
| 279 } | |
| 280 // If we are already sending with a particular SSRC, stop sending. | |
| 281 if (can_send_track()) { | |
| 282 provider_->SetCaptureDevice(ssrc_, nullptr); | |
| 283 provider_->SetVideoSend(ssrc_, false, nullptr); | |
| 284 } | |
| 285 ssrc_ = ssrc; | |
| 286 if (can_send_track()) { | |
| 287 VideoSourceInterface* source = track_->GetSource(); | |
| 288 provider_->SetCaptureDevice(ssrc_, | |
| 289 source ? source->GetVideoCapturer() : nullptr); | |
| 290 SetVideoSend(); | |
| 291 } | |
| 292 } | |
| 293 | |
| 294 void VideoRtpSender::Stop() { | 185 void VideoRtpSender::Stop() { |
| 295 // TODO(deadbeef): Need to do more here to fully stop sending packets. | 186 // TODO(deadbeef): Need to do more here to fully stop sending packets. |
| 296 if (stopped_) { | 187 if (!provider_) { |
| 297 return; | 188 return; |
| 298 } | 189 } |
| 299 if (track_) { | 190 provider_->SetCaptureDevice(ssrc_, nullptr); |
| 300 track_->UnregisterObserver(this); | 191 provider_->SetVideoSend(ssrc_, false, nullptr); |
| 301 } | 192 provider_ = nullptr; |
| 302 if (can_send_track()) { | |
| 303 provider_->SetCaptureDevice(ssrc_, nullptr); | |
| 304 provider_->SetVideoSend(ssrc_, false, nullptr); | |
| 305 } | |
| 306 stopped_ = true; | |
| 307 } | 193 } |
| 308 | 194 |
| 309 void VideoRtpSender::SetVideoSend() { | 195 void VideoRtpSender::Reconfigure() { |
| 310 RTC_DCHECK(!stopped_ && can_send_track()); | 196 if (!provider_) { |
| 197 return; |
| 198 } |
| 311 const cricket::VideoOptions* options = nullptr; | 199 const cricket::VideoOptions* options = nullptr; |
| 312 VideoSourceInterface* source = track_->GetSource(); | 200 VideoSourceInterface* source = track_->GetSource(); |
| 313 if (track_->enabled() && source) { | 201 if (track_->enabled() && source) { |
| 314 options = source->options(); | 202 options = source->options(); |
| 315 } | 203 } |
| 316 provider_->SetVideoSend(ssrc_, track_->enabled(), options); | 204 provider_->SetVideoSend(ssrc_, track_->enabled(), options); |
| 317 } | 205 } |
| 318 | 206 |
| 319 } // namespace webrtc | 207 } // namespace webrtc |
| OLD | NEW |