blob: 02607da6762eeb02293e7b27b69b8098c51528e6 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2011 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
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.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +000010
ossu7bb87ee2017-01-23 04:56:25 -080011#include "webrtc/pc/audiotrack.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
Edward Lemurc20978e2017-07-06 19:44:34 +020013#include "webrtc/rtc_base/checks.h"
tommi6eca7e32015-12-15 04:27:11 -080014
15using rtc::scoped_refptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000016
17namespace webrtc {
18
tommi6eca7e32015-12-15 04:27:11 -080019// static
20scoped_refptr<AudioTrack> AudioTrack::Create(
21 const std::string& id,
22 const scoped_refptr<AudioSourceInterface>& source) {
23 return new rtc::RefCountedObject<AudioTrack>(id, source);
24}
25
henrike@webrtc.org28e20752013-07-10 00:45:36 +000026AudioTrack::AudioTrack(const std::string& label,
tommi6eca7e32015-12-15 04:27:11 -080027 const scoped_refptr<AudioSourceInterface>& source)
28 : MediaStreamTrack<AudioTrackInterface>(label), audio_source_(source) {
29 if (audio_source_) {
30 audio_source_->RegisterObserver(this);
31 OnChanged();
32 }
33}
34
35AudioTrack::~AudioTrack() {
36 RTC_DCHECK(thread_checker_.CalledOnValidThread());
37 set_state(MediaStreamTrackInterface::kEnded);
38 if (audio_source_)
39 audio_source_->UnregisterObserver(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040}
41
42std::string AudioTrack::kind() const {
tommi6eca7e32015-12-15 04:27:11 -080043 RTC_DCHECK(thread_checker_.CalledOnValidThread());
deadbeeffac06552015-11-25 11:26:01 -080044 return kAudioKind;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045}
46
tommi6eca7e32015-12-15 04:27:11 -080047AudioSourceInterface* AudioTrack::GetSource() const {
48 RTC_DCHECK(thread_checker_.CalledOnValidThread());
49 return audio_source_.get();
50}
51
52void AudioTrack::AddSink(AudioTrackSinkInterface* sink) {
53 RTC_DCHECK(thread_checker_.CalledOnValidThread());
54 if (audio_source_)
55 audio_source_->AddSink(sink);
56}
57
58void AudioTrack::RemoveSink(AudioTrackSinkInterface* sink) {
59 RTC_DCHECK(thread_checker_.CalledOnValidThread());
60 if (audio_source_)
61 audio_source_->RemoveSink(sink);
62}
63
64void AudioTrack::OnChanged() {
65 RTC_DCHECK(thread_checker_.CalledOnValidThread());
perkjc8f952d2016-03-23 00:33:56 -070066 if (audio_source_->state() == MediaSourceInterface::kEnded) {
67 set_state(kEnded);
68 } else {
69 set_state(kLive);
tommi6eca7e32015-12-15 04:27:11 -080070 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071}
72
73} // namespace webrtc