blob: 72a9114eb4df315dcdbd85e1fdc792ad3ddb704d [file] [log] [blame]
deadbeef6979b022015-09-24 16:47:53 -07001/*
2 * libjingle
3 * Copyright 2015 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
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
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/app/webrtc/rtpsender.h"
29
deadbeef70ab1a12015-09-28 16:53:55 -070030#include "talk/app/webrtc/localaudiosource.h"
31#include "talk/app/webrtc/videosourceinterface.h"
deadbeeffac06552015-11-25 11:26:01 -080032#include "webrtc/base/helpers.h"
deadbeef70ab1a12015-09-28 16:53:55 -070033
34namespace webrtc {
35
36LocalAudioSinkAdapter::LocalAudioSinkAdapter() : sink_(nullptr) {}
37
38LocalAudioSinkAdapter::~LocalAudioSinkAdapter() {
39 rtc::CritScope lock(&lock_);
40 if (sink_)
41 sink_->OnClose();
42}
43
44void LocalAudioSinkAdapter::OnData(const void* audio_data,
45 int bits_per_sample,
46 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -080047 size_t number_of_channels,
deadbeef70ab1a12015-09-28 16:53:55 -070048 size_t number_of_frames) {
49 rtc::CritScope lock(&lock_);
50 if (sink_) {
51 sink_->OnData(audio_data, bits_per_sample, sample_rate, number_of_channels,
52 number_of_frames);
53 }
54}
55
56void LocalAudioSinkAdapter::SetSink(cricket::AudioRenderer::Sink* sink) {
57 rtc::CritScope lock(&lock_);
58 ASSERT(!sink || !sink_);
59 sink_ = sink;
60}
61
62AudioRtpSender::AudioRtpSender(AudioTrackInterface* track,
deadbeeffac06552015-11-25 11:26:01 -080063 const std::string& stream_id,
64 AudioProviderInterface* provider,
65 StatsCollector* stats)
deadbeef70ab1a12015-09-28 16:53:55 -070066 : id_(track->id()),
deadbeeffac06552015-11-25 11:26:01 -080067 stream_id_(stream_id),
deadbeef5def7b92015-11-20 11:43:22 -080068 provider_(provider),
deadbeeffac06552015-11-25 11:26:01 -080069 stats_(stats),
70 track_(track),
deadbeef70ab1a12015-09-28 16:53:55 -070071 cached_track_enabled_(track->enabled()),
72 sink_adapter_(new LocalAudioSinkAdapter()) {
deadbeeffac06552015-11-25 11:26:01 -080073 RTC_DCHECK(provider != nullptr);
deadbeef70ab1a12015-09-28 16:53:55 -070074 track_->RegisterObserver(this);
75 track_->AddSink(sink_adapter_.get());
deadbeef70ab1a12015-09-28 16:53:55 -070076}
77
deadbeefe1f9d832016-01-14 15:35:42 -080078AudioRtpSender::AudioRtpSender(AudioTrackInterface* track,
79 AudioProviderInterface* provider,
80 StatsCollector* stats)
81 : id_(track->id()),
82 stream_id_(rtc::CreateRandomUuid()),
83 provider_(provider),
84 stats_(stats),
85 track_(track),
86 cached_track_enabled_(track->enabled()),
87 sink_adapter_(new LocalAudioSinkAdapter()) {
88 RTC_DCHECK(provider != nullptr);
89 track_->RegisterObserver(this);
90 track_->AddSink(sink_adapter_.get());
91}
92
deadbeeffac06552015-11-25 11:26:01 -080093AudioRtpSender::AudioRtpSender(AudioProviderInterface* provider,
94 StatsCollector* stats)
95 : id_(rtc::CreateRandomUuid()),
96 stream_id_(rtc::CreateRandomUuid()),
97 provider_(provider),
98 stats_(stats),
99 sink_adapter_(new LocalAudioSinkAdapter()) {}
100
deadbeef70ab1a12015-09-28 16:53:55 -0700101AudioRtpSender::~AudioRtpSender() {
deadbeef70ab1a12015-09-28 16:53:55 -0700102 Stop();
103}
104
105void AudioRtpSender::OnChanged() {
deadbeeffac06552015-11-25 11:26:01 -0800106 RTC_DCHECK(!stopped_);
deadbeef70ab1a12015-09-28 16:53:55 -0700107 if (cached_track_enabled_ != track_->enabled()) {
108 cached_track_enabled_ = track_->enabled();
deadbeeffac06552015-11-25 11:26:01 -0800109 if (can_send_track()) {
110 SetAudioSend();
111 }
deadbeef70ab1a12015-09-28 16:53:55 -0700112 }
113}
114
115bool AudioRtpSender::SetTrack(MediaStreamTrackInterface* track) {
deadbeeffac06552015-11-25 11:26:01 -0800116 if (stopped_) {
117 LOG(LS_ERROR) << "SetTrack can't be called on a stopped RtpSender.";
118 return false;
119 }
120 if (track && track->kind() != MediaStreamTrackInterface::kAudioKind) {
deadbeef70ab1a12015-09-28 16:53:55 -0700121 LOG(LS_ERROR) << "SetTrack called on audio RtpSender with " << track->kind()
122 << " track.";
123 return false;
124 }
125 AudioTrackInterface* audio_track = static_cast<AudioTrackInterface*>(track);
126
127 // Detach from old track.
deadbeeffac06552015-11-25 11:26:01 -0800128 if (track_) {
129 track_->RemoveSink(sink_adapter_.get());
130 track_->UnregisterObserver(this);
131 }
132
133 if (can_send_track() && stats_) {
134 stats_->RemoveLocalAudioTrack(track_.get(), ssrc_);
135 }
deadbeef70ab1a12015-09-28 16:53:55 -0700136
137 // Attach to new track.
deadbeeffac06552015-11-25 11:26:01 -0800138 bool prev_can_send_track = can_send_track();
deadbeef70ab1a12015-09-28 16:53:55 -0700139 track_ = audio_track;
deadbeeffac06552015-11-25 11:26:01 -0800140 if (track_) {
141 cached_track_enabled_ = track_->enabled();
142 track_->RegisterObserver(this);
143 track_->AddSink(sink_adapter_.get());
144 }
145
146 // Update audio provider.
147 if (can_send_track()) {
148 SetAudioSend();
149 if (stats_) {
150 stats_->AddLocalAudioTrack(track_.get(), ssrc_);
151 }
152 } else if (prev_can_send_track) {
153 cricket::AudioOptions options;
154 provider_->SetAudioSend(ssrc_, false, options, nullptr);
155 }
deadbeef70ab1a12015-09-28 16:53:55 -0700156 return true;
157}
158
deadbeeffac06552015-11-25 11:26:01 -0800159void AudioRtpSender::SetSsrc(uint32_t ssrc) {
160 if (stopped_ || ssrc == ssrc_) {
161 return;
162 }
163 // If we are already sending with a particular SSRC, stop sending.
164 if (can_send_track()) {
165 cricket::AudioOptions options;
166 provider_->SetAudioSend(ssrc_, false, options, nullptr);
167 if (stats_) {
168 stats_->RemoveLocalAudioTrack(track_.get(), ssrc_);
169 }
170 }
171 ssrc_ = ssrc;
172 if (can_send_track()) {
173 SetAudioSend();
174 if (stats_) {
175 stats_->AddLocalAudioTrack(track_.get(), ssrc_);
176 }
177 }
178}
179
deadbeef70ab1a12015-09-28 16:53:55 -0700180void AudioRtpSender::Stop() {
181 // TODO(deadbeef): Need to do more here to fully stop sending packets.
deadbeeffac06552015-11-25 11:26:01 -0800182 if (stopped_) {
deadbeef70ab1a12015-09-28 16:53:55 -0700183 return;
184 }
deadbeeffac06552015-11-25 11:26:01 -0800185 if (track_) {
186 track_->RemoveSink(sink_adapter_.get());
187 track_->UnregisterObserver(this);
188 }
189 if (can_send_track()) {
190 cricket::AudioOptions options;
191 provider_->SetAudioSend(ssrc_, false, options, nullptr);
192 if (stats_) {
193 stats_->RemoveLocalAudioTrack(track_.get(), ssrc_);
194 }
195 }
196 stopped_ = true;
deadbeef70ab1a12015-09-28 16:53:55 -0700197}
198
deadbeeffac06552015-11-25 11:26:01 -0800199void AudioRtpSender::SetAudioSend() {
200 RTC_DCHECK(!stopped_ && can_send_track());
deadbeef70ab1a12015-09-28 16:53:55 -0700201 cricket::AudioOptions options;
tommi6eca7e32015-12-15 04:27:11 -0800202 if (track_->enabled() && track_->GetSource() &&
203 !track_->GetSource()->remote()) {
deadbeef70ab1a12015-09-28 16:53:55 -0700204 // TODO(xians): Remove this static_cast since we should be able to connect
deadbeeffac06552015-11-25 11:26:01 -0800205 // a remote audio track to a peer connection.
deadbeef70ab1a12015-09-28 16:53:55 -0700206 options = static_cast<LocalAudioSource*>(track_->GetSource())->options();
207 }
208
209 // Use the renderer if the audio track has one, otherwise use the sink
210 // adapter owned by this class.
211 cricket::AudioRenderer* renderer =
212 track_->GetRenderer() ? track_->GetRenderer() : sink_adapter_.get();
213 ASSERT(renderer != nullptr);
214 provider_->SetAudioSend(ssrc_, track_->enabled(), options, renderer);
215}
216
217VideoRtpSender::VideoRtpSender(VideoTrackInterface* track,
deadbeeffac06552015-11-25 11:26:01 -0800218 const std::string& stream_id,
deadbeef70ab1a12015-09-28 16:53:55 -0700219 VideoProviderInterface* provider)
220 : id_(track->id()),
deadbeeffac06552015-11-25 11:26:01 -0800221 stream_id_(stream_id),
deadbeef5def7b92015-11-20 11:43:22 -0800222 provider_(provider),
deadbeeffac06552015-11-25 11:26:01 -0800223 track_(track),
deadbeef70ab1a12015-09-28 16:53:55 -0700224 cached_track_enabled_(track->enabled()) {
deadbeeffac06552015-11-25 11:26:01 -0800225 RTC_DCHECK(provider != nullptr);
deadbeef70ab1a12015-09-28 16:53:55 -0700226 track_->RegisterObserver(this);
deadbeef70ab1a12015-09-28 16:53:55 -0700227}
228
deadbeefe1f9d832016-01-14 15:35:42 -0800229VideoRtpSender::VideoRtpSender(VideoTrackInterface* track,
230 VideoProviderInterface* provider)
231 : id_(track->id()),
232 stream_id_(rtc::CreateRandomUuid()),
233 provider_(provider),
234 track_(track),
235 cached_track_enabled_(track->enabled()) {
236 RTC_DCHECK(provider != nullptr);
237 track_->RegisterObserver(this);
238}
239
deadbeeffac06552015-11-25 11:26:01 -0800240VideoRtpSender::VideoRtpSender(VideoProviderInterface* provider)
241 : id_(rtc::CreateRandomUuid()),
242 stream_id_(rtc::CreateRandomUuid()),
243 provider_(provider) {}
244
deadbeef70ab1a12015-09-28 16:53:55 -0700245VideoRtpSender::~VideoRtpSender() {
deadbeef70ab1a12015-09-28 16:53:55 -0700246 Stop();
247}
248
249void VideoRtpSender::OnChanged() {
deadbeeffac06552015-11-25 11:26:01 -0800250 RTC_DCHECK(!stopped_);
deadbeef70ab1a12015-09-28 16:53:55 -0700251 if (cached_track_enabled_ != track_->enabled()) {
252 cached_track_enabled_ = track_->enabled();
deadbeeffac06552015-11-25 11:26:01 -0800253 if (can_send_track()) {
254 SetVideoSend();
255 }
deadbeef70ab1a12015-09-28 16:53:55 -0700256 }
257}
258
259bool VideoRtpSender::SetTrack(MediaStreamTrackInterface* track) {
deadbeeffac06552015-11-25 11:26:01 -0800260 if (stopped_) {
261 LOG(LS_ERROR) << "SetTrack can't be called on a stopped RtpSender.";
262 return false;
263 }
264 if (track && track->kind() != MediaStreamTrackInterface::kVideoKind) {
deadbeef70ab1a12015-09-28 16:53:55 -0700265 LOG(LS_ERROR) << "SetTrack called on video RtpSender with " << track->kind()
266 << " track.";
267 return false;
268 }
269 VideoTrackInterface* video_track = static_cast<VideoTrackInterface*>(track);
270
271 // Detach from old track.
deadbeeffac06552015-11-25 11:26:01 -0800272 if (track_) {
273 track_->UnregisterObserver(this);
274 }
deadbeef70ab1a12015-09-28 16:53:55 -0700275
276 // Attach to new track.
deadbeeffac06552015-11-25 11:26:01 -0800277 bool prev_can_send_track = can_send_track();
deadbeef70ab1a12015-09-28 16:53:55 -0700278 track_ = video_track;
deadbeeffac06552015-11-25 11:26:01 -0800279 if (track_) {
280 cached_track_enabled_ = track_->enabled();
281 track_->RegisterObserver(this);
282 }
283
284 // Update video provider.
285 if (can_send_track()) {
286 VideoSourceInterface* source = track_->GetSource();
287 // TODO(deadbeef): If SetTrack is called with a disabled track, and the
288 // previous track was enabled, this could cause a frame from the new track
289 // to slip out. Really, what we need is for SetCaptureDevice and
290 // SetVideoSend
291 // to be combined into one atomic operation, all the way down to
292 // WebRtcVideoSendStream.
293 provider_->SetCaptureDevice(ssrc_,
294 source ? source->GetVideoCapturer() : nullptr);
295 SetVideoSend();
296 } else if (prev_can_send_track) {
297 provider_->SetCaptureDevice(ssrc_, nullptr);
298 provider_->SetVideoSend(ssrc_, false, nullptr);
299 }
deadbeef70ab1a12015-09-28 16:53:55 -0700300 return true;
301}
302
deadbeeffac06552015-11-25 11:26:01 -0800303void VideoRtpSender::SetSsrc(uint32_t ssrc) {
304 if (stopped_ || ssrc == ssrc_) {
305 return;
306 }
307 // If we are already sending with a particular SSRC, stop sending.
308 if (can_send_track()) {
309 provider_->SetCaptureDevice(ssrc_, nullptr);
310 provider_->SetVideoSend(ssrc_, false, nullptr);
311 }
312 ssrc_ = ssrc;
313 if (can_send_track()) {
314 VideoSourceInterface* source = track_->GetSource();
315 provider_->SetCaptureDevice(ssrc_,
316 source ? source->GetVideoCapturer() : nullptr);
317 SetVideoSend();
318 }
319}
320
deadbeef70ab1a12015-09-28 16:53:55 -0700321void VideoRtpSender::Stop() {
322 // TODO(deadbeef): Need to do more here to fully stop sending packets.
deadbeeffac06552015-11-25 11:26:01 -0800323 if (stopped_) {
deadbeef70ab1a12015-09-28 16:53:55 -0700324 return;
325 }
deadbeeffac06552015-11-25 11:26:01 -0800326 if (track_) {
327 track_->UnregisterObserver(this);
328 }
329 if (can_send_track()) {
330 provider_->SetCaptureDevice(ssrc_, nullptr);
331 provider_->SetVideoSend(ssrc_, false, nullptr);
332 }
333 stopped_ = true;
deadbeef70ab1a12015-09-28 16:53:55 -0700334}
335
deadbeeffac06552015-11-25 11:26:01 -0800336void VideoRtpSender::SetVideoSend() {
337 RTC_DCHECK(!stopped_ && can_send_track());
deadbeef70ab1a12015-09-28 16:53:55 -0700338 const cricket::VideoOptions* options = nullptr;
339 VideoSourceInterface* source = track_->GetSource();
340 if (track_->enabled() && source) {
341 options = source->options();
342 }
343 provider_->SetVideoSend(ssrc_, track_->enabled(), options);
344}
345
346} // namespace webrtc