blob: 80a0256a02b8c6c802ec5bd73da0ce98a80f0879 [file] [log] [blame]
deadbeef6979b022015-09-24 23:47:531/*
kjellanderb24317b2016-02-10 15:54:432 * Copyright 2015 The WebRTC project authors. All Rights Reserved.
deadbeef6979b022015-09-24 23:47:533 *
kjellanderb24317b2016-02-10 15:54:434 * 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.
deadbeef6979b022015-09-24 23:47:539 */
10
Henrik Kjellander15583c12016-02-10 09:53:1211#include "webrtc/api/rtpreceiver.h"
deadbeef6979b022015-09-24 23:47:5312
perkjf0dcfe22016-03-10 17:32:0013#include "webrtc/api/mediastreamtrackproxy.h"
perkjd61bf802016-03-24 10:16:1914#include "webrtc/api/audiotrack.h"
nisse5b68ab52016-04-07 14:45:5415#include "webrtc/api/videosourceproxy.h"
perkjf0dcfe22016-03-10 17:32:0016#include "webrtc/api/videotrack.h"
Taylor Brandstetterdb0cd9e2016-05-16 18:40:3017#include "webrtc/base/trace_event.h"
deadbeef70ab1a12015-09-28 23:53:5518
19namespace webrtc {
20
perkjd61bf802016-03-24 10:16:1921AudioRtpReceiver::AudioRtpReceiver(MediaStreamInterface* stream,
22 const std::string& track_id,
Peter Boström0c4e06b2015-10-07 10:23:2123 uint32_t ssrc,
Taylor Brandstetterba29c6a2016-06-27 23:30:3524 cricket::VoiceChannel* channel)
perkjd61bf802016-03-24 10:16:1925 : id_(track_id),
deadbeef70ab1a12015-09-28 23:53:5526 ssrc_(ssrc),
Taylor Brandstetterba29c6a2016-06-27 23:30:3527 channel_(channel),
perkjd61bf802016-03-24 10:16:1928 track_(AudioTrackProxy::Create(
29 rtc::Thread::Current(),
30 AudioTrack::Create(track_id,
Taylor Brandstetterba29c6a2016-06-27 23:30:3531 RemoteAudioSource::Create(ssrc, channel)))),
perkjd61bf802016-03-24 10:16:1932 cached_track_enabled_(track_->enabled()) {
tommi6eca7e32015-12-15 12:27:1133 RTC_DCHECK(track_->GetSource()->remote());
deadbeef70ab1a12015-09-28 23:53:5534 track_->RegisterObserver(this);
35 track_->GetSource()->RegisterAudioObserver(this);
36 Reconfigure();
perkjd61bf802016-03-24 10:16:1937 stream->AddTrack(track_);
Taylor Brandstetterba29c6a2016-06-27 23:30:3538 if (channel_) {
39 channel_->SignalFirstPacketReceived.connect(
40 this, &AudioRtpReceiver::OnFirstPacketReceived);
41 }
deadbeef70ab1a12015-09-28 23:53:5542}
43
44AudioRtpReceiver::~AudioRtpReceiver() {
45 track_->GetSource()->UnregisterAudioObserver(this);
46 track_->UnregisterObserver(this);
47 Stop();
48}
49
50void AudioRtpReceiver::OnChanged() {
51 if (cached_track_enabled_ != track_->enabled()) {
52 cached_track_enabled_ = track_->enabled();
53 Reconfigure();
54 }
55}
56
57void AudioRtpReceiver::OnSetVolume(double volume) {
Taylor Brandstetterba29c6a2016-06-27 23:30:3558 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 }
deadbeef70ab1a12015-09-28 23:53:5564 // 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.
Taylor Brandstetterba29c6a2016-06-27 23:30:3567 if (!stopped_ && track_->enabled()) {
68 if (!channel_->SetOutputVolume(ssrc_, cached_volume_)) {
69 RTC_DCHECK(false);
70 }
71 }
deadbeef70ab1a12015-09-28 23:53:5572}
73
Taylor Brandstetterdb0cd9e2016-05-16 18:40:3074RtpParameters AudioRtpReceiver::GetParameters() const {
Taylor Brandstetterba29c6a2016-06-27 23:30:3575 if (!channel_ || stopped_) {
76 return RtpParameters();
77 }
78 return channel_->GetRtpReceiveParameters(ssrc_);
Taylor Brandstetterdb0cd9e2016-05-16 18:40:3079}
80
81bool AudioRtpReceiver::SetParameters(const RtpParameters& parameters) {
82 TRACE_EVENT0("webrtc", "AudioRtpReceiver::SetParameters");
Taylor Brandstetterba29c6a2016-06-27 23:30:3583 if (!channel_ || stopped_) {
84 return false;
85 }
86 return channel_->SetRtpReceiveParameters(ssrc_, parameters);
Taylor Brandstetterdb0cd9e2016-05-16 18:40:3087}
88
deadbeefa601f5c2016-06-06 21:27:3989void AudioRtpReceiver::Stop() {
90 // TODO(deadbeef): Need to do more here to fully stop receiving packets.
Taylor Brandstetterba29c6a2016-06-27 23:30:3591 if (stopped_) {
deadbeefa601f5c2016-06-06 21:27:3992 return;
93 }
Taylor Brandstetterba29c6a2016-06-27 23:30:3594 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;
deadbeefa601f5c2016-06-06 21:27:39100}
101
deadbeef70ab1a12015-09-28 23:53:55102void AudioRtpReceiver::Reconfigure() {
Taylor Brandstetterba29c6a2016-06-27 23:30:35103 RTC_DCHECK(!stopped_);
104 if (!channel_) {
105 LOG(LS_ERROR) << "AudioRtpReceiver::Reconfigure: No audio channel exists.";
deadbeef70ab1a12015-09-28 23:53:55106 return;
107 }
Taylor Brandstetterba29c6a2016-06-27 23:30:35108 if (!channel_->SetOutputVolume(ssrc_,
109 track_->enabled() ? cached_volume_ : 0)) {
110 RTC_DCHECK(false);
111 }
deadbeef70ab1a12015-09-28 23:53:55112}
113
zhihuang184a3fd2016-06-14 18:47:14114void AudioRtpReceiver::SetObserver(RtpReceiverObserverInterface* observer) {
115 observer_ = observer;
Taylor Brandstetterba29c6a2016-06-27 23:30:35116 // Deliver any notifications the observer may have missed by being set late.
zhihuangc4adabf2016-12-07 18:36:40117 if (received_first_packet_ && observer_) {
zhihuang184a3fd2016-06-14 18:47:14118 observer_->OnFirstPacketReceived(media_type());
119 }
120}
121
Taylor Brandstetterba29c6a2016-06-27 23:30:35122void 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
133void AudioRtpReceiver::OnFirstPacketReceived(cricket::BaseChannel* channel) {
zhihuang184a3fd2016-06-14 18:47:14134 if (observer_) {
135 observer_->OnFirstPacketReceived(media_type());
136 }
137 received_first_packet_ = true;
138}
139
perkjf0dcfe22016-03-10 17:32:00140VideoRtpReceiver::VideoRtpReceiver(MediaStreamInterface* stream,
141 const std::string& track_id,
142 rtc::Thread* worker_thread,
Peter Boström0c4e06b2015-10-07 10:23:21143 uint32_t ssrc,
Taylor Brandstetterba29c6a2016-06-27 23:30:35144 cricket::VideoChannel* channel)
perkjf0dcfe22016-03-10 17:32:00145 : id_(track_id),
146 ssrc_(ssrc),
Taylor Brandstetterba29c6a2016-06-27 23:30:35147 channel_(channel),
perkjf0dcfe22016-03-10 17:32:00148 source_(new RefCountedObject<VideoTrackSource>(&broadcaster_,
perkjf0dcfe22016-03-10 17:32:00149 true /* remote */)),
150 track_(VideoTrackProxy::Create(
151 rtc::Thread::Current(),
nisse5b68ab52016-04-07 14:45:54152 worker_thread,
153 VideoTrack::Create(
154 track_id,
155 VideoTrackSourceProxy::Create(rtc::Thread::Current(),
156 worker_thread,
157 source_)))) {
perkjf0dcfe22016-03-10 17:32:00158 source_->SetState(MediaSourceInterface::kLive);
Taylor Brandstetterba29c6a2016-06-27 23:30:35159 if (!channel_) {
160 LOG(LS_ERROR)
161 << "VideoRtpReceiver::VideoRtpReceiver: No video channel exists.";
162 } else {
163 if (!channel_->SetSink(ssrc_, &broadcaster_)) {
164 RTC_DCHECK(false);
165 }
166 }
perkjf0dcfe22016-03-10 17:32:00167 stream->AddTrack(track_);
Taylor Brandstetterba29c6a2016-06-27 23:30:35168 if (channel_) {
169 channel_->SignalFirstPacketReceived.connect(
170 this, &VideoRtpReceiver::OnFirstPacketReceived);
171 }
deadbeef70ab1a12015-09-28 23:53:55172}
173
174VideoRtpReceiver::~VideoRtpReceiver() {
175 // Since cricket::VideoRenderer is not reference counted,
Taylor Brandstetterba29c6a2016-06-27 23:30:35176 // we need to remove it from the channel before we are deleted.
deadbeef70ab1a12015-09-28 23:53:55177 Stop();
178}
179
deadbeefa601f5c2016-06-06 21:27:39180RtpParameters VideoRtpReceiver::GetParameters() const {
Taylor Brandstetterba29c6a2016-06-27 23:30:35181 if (!channel_ || stopped_) {
182 return RtpParameters();
183 }
184 return channel_->GetRtpReceiveParameters(ssrc_);
deadbeefa601f5c2016-06-06 21:27:39185}
186
187bool VideoRtpReceiver::SetParameters(const RtpParameters& parameters) {
188 TRACE_EVENT0("webrtc", "VideoRtpReceiver::SetParameters");
Taylor Brandstetterba29c6a2016-06-27 23:30:35189 if (!channel_ || stopped_) {
190 return false;
191 }
192 return channel_->SetRtpReceiveParameters(ssrc_, parameters);
deadbeefa601f5c2016-06-06 21:27:39193}
194
deadbeef70ab1a12015-09-28 23:53:55195void VideoRtpReceiver::Stop() {
196 // TODO(deadbeef): Need to do more here to fully stop receiving packets.
Taylor Brandstetterba29c6a2016-06-27 23:30:35197 if (stopped_) {
deadbeef70ab1a12015-09-28 23:53:55198 return;
199 }
perkjf0dcfe22016-03-10 17:32:00200 source_->SetState(MediaSourceInterface::kEnded);
201 source_->OnSourceDestroyed();
Taylor Brandstetterba29c6a2016-06-27 23:30:35202 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;
deadbeef70ab1a12015-09-28 23:53:55210}
211
zhihuang184a3fd2016-06-14 18:47:14212void VideoRtpReceiver::SetObserver(RtpReceiverObserverInterface* observer) {
213 observer_ = observer;
Taylor Brandstetterba29c6a2016-06-27 23:30:35214 // Deliver any notifications the observer may have missed by being set late.
zhihuangc4adabf2016-12-07 18:36:40215 if (received_first_packet_ && observer_) {
zhihuang184a3fd2016-06-14 18:47:14216 observer_->OnFirstPacketReceived(media_type());
217 }
218}
219
Taylor Brandstetterba29c6a2016-06-27 23:30:35220void 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_DCHECK(false);
229 }
230 channel_->SignalFirstPacketReceived.connect(
231 this, &VideoRtpReceiver::OnFirstPacketReceived);
232 }
233}
234
235void VideoRtpReceiver::OnFirstPacketReceived(cricket::BaseChannel* channel) {
zhihuang184a3fd2016-06-14 18:47:14236 if (observer_) {
237 observer_->OnFirstPacketReceived(media_type());
238 }
239 received_first_packet_ = true;
240}
241
deadbeef70ab1a12015-09-28 23:53:55242} // namespace webrtc