blob: 15906120f6d431fbb3e0bec5406062163630c18e [file] [log] [blame]
deadbeef6979b022015-09-24 16:47:53 -07001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2015 The WebRTC project authors. All Rights Reserved.
deadbeef6979b022015-09-24 16:47:53 -07003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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 16:47:53 -07009 */
10
Henrik Kjellander15583c12016-02-10 10:53:12 +010011#include "webrtc/api/rtpreceiver.h"
deadbeef6979b022015-09-24 16:47:53 -070012
perkjf0dcfe22016-03-10 18:32:00 +010013#include "webrtc/api/mediastreamtrackproxy.h"
14#include "webrtc/api/videotrack.h"
deadbeef70ab1a12015-09-28 16:53:55 -070015
16namespace webrtc {
17
18AudioRtpReceiver::AudioRtpReceiver(AudioTrackInterface* track,
Peter Boström0c4e06b2015-10-07 12:23:21 +020019 uint32_t ssrc,
deadbeef70ab1a12015-09-28 16:53:55 -070020 AudioProviderInterface* provider)
21 : id_(track->id()),
22 track_(track),
23 ssrc_(ssrc),
24 provider_(provider),
25 cached_track_enabled_(track->enabled()) {
tommi6eca7e32015-12-15 04:27:11 -080026 RTC_DCHECK(track_->GetSource()->remote());
deadbeef70ab1a12015-09-28 16:53:55 -070027 track_->RegisterObserver(this);
28 track_->GetSource()->RegisterAudioObserver(this);
29 Reconfigure();
30}
31
32AudioRtpReceiver::~AudioRtpReceiver() {
33 track_->GetSource()->UnregisterAudioObserver(this);
34 track_->UnregisterObserver(this);
35 Stop();
36}
37
38void AudioRtpReceiver::OnChanged() {
39 if (cached_track_enabled_ != track_->enabled()) {
40 cached_track_enabled_ = track_->enabled();
41 Reconfigure();
42 }
43}
44
45void AudioRtpReceiver::OnSetVolume(double volume) {
46 // When the track is disabled, the volume of the source, which is the
47 // corresponding WebRtc Voice Engine channel will be 0. So we do not allow
48 // setting the volume to the source when the track is disabled.
49 if (provider_ && track_->enabled())
50 provider_->SetAudioPlayoutVolume(ssrc_, volume);
51}
52
53void AudioRtpReceiver::Stop() {
54 // TODO(deadbeef): Need to do more here to fully stop receiving packets.
55 if (!provider_) {
56 return;
57 }
solenbergd4cec0d2015-10-09 08:55:48 -070058 provider_->SetAudioPlayout(ssrc_, false);
deadbeef70ab1a12015-09-28 16:53:55 -070059 provider_ = nullptr;
60}
61
62void AudioRtpReceiver::Reconfigure() {
63 if (!provider_) {
64 return;
65 }
solenbergd4cec0d2015-10-09 08:55:48 -070066 provider_->SetAudioPlayout(ssrc_, track_->enabled());
deadbeef70ab1a12015-09-28 16:53:55 -070067}
68
perkjf0dcfe22016-03-10 18:32:00 +010069VideoRtpReceiver::VideoRtpReceiver(MediaStreamInterface* stream,
70 const std::string& track_id,
71 rtc::Thread* worker_thread,
Peter Boström0c4e06b2015-10-07 12:23:21 +020072 uint32_t ssrc,
deadbeef70ab1a12015-09-28 16:53:55 -070073 VideoProviderInterface* provider)
perkjf0dcfe22016-03-10 18:32:00 +010074 : id_(track_id),
75 ssrc_(ssrc),
76 provider_(provider),
77 source_(new RefCountedObject<VideoTrackSource>(&broadcaster_,
78 worker_thread,
79 true /* remote */)),
80 track_(VideoTrackProxy::Create(
81 rtc::Thread::Current(),
82 VideoTrack::Create(track_id, source_.get()))) {
83 source_->SetState(MediaSourceInterface::kLive);
84 // TODO(perkj): It should be enough to set the source state. All tracks
85 // belonging to the same source should get its state from the source.
86 // I.e. if a track has been cloned from a remote source.
87 track_->set_state(webrtc::MediaStreamTrackInterface::kLive);
88 provider_->SetVideoPlayout(ssrc_, true, &broadcaster_);
89 stream->AddTrack(track_);
deadbeef70ab1a12015-09-28 16:53:55 -070090}
91
92VideoRtpReceiver::~VideoRtpReceiver() {
93 // Since cricket::VideoRenderer is not reference counted,
94 // we need to remove it from the provider before we are deleted.
95 Stop();
96}
97
98void VideoRtpReceiver::Stop() {
99 // TODO(deadbeef): Need to do more here to fully stop receiving packets.
100 if (!provider_) {
101 return;
102 }
perkjf0dcfe22016-03-10 18:32:00 +0100103 source_->SetState(MediaSourceInterface::kEnded);
104 source_->OnSourceDestroyed();
105 // TODO(perkj): It should be enough to set the source state. All tracks
106 // belonging to the same source should get its state from the source.
107 track_->set_state(MediaStreamTrackInterface::kEnded);
deadbeef70ab1a12015-09-28 16:53:55 -0700108 provider_->SetVideoPlayout(ssrc_, false, nullptr);
109 provider_ = nullptr;
110}
111
112} // namespace webrtc