blob: 408399122a699cf9e043be71f6d1cf6c9d163328 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "pc/audiotrack.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "rtc_base/checks.h"
Niels Möller84255bb2017-10-06 13:43:23 +020014#include "rtc_base/refcountedobject.h"
tommi6eca7e32015-12-15 04:27:11 -080015
16using rtc::scoped_refptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017
18namespace webrtc {
19
tommi6eca7e32015-12-15 04:27:11 -080020// static
21scoped_refptr<AudioTrack> AudioTrack::Create(
22 const std::string& id,
23 const scoped_refptr<AudioSourceInterface>& source) {
24 return new rtc::RefCountedObject<AudioTrack>(id, source);
25}
26
henrike@webrtc.org28e20752013-07-10 00:45:36 +000027AudioTrack::AudioTrack(const std::string& label,
tommi6eca7e32015-12-15 04:27:11 -080028 const scoped_refptr<AudioSourceInterface>& source)
29 : MediaStreamTrack<AudioTrackInterface>(label), audio_source_(source) {
30 if (audio_source_) {
31 audio_source_->RegisterObserver(this);
32 OnChanged();
33 }
34}
35
36AudioTrack::~AudioTrack() {
37 RTC_DCHECK(thread_checker_.CalledOnValidThread());
38 set_state(MediaStreamTrackInterface::kEnded);
39 if (audio_source_)
40 audio_source_->UnregisterObserver(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041}
42
43std::string AudioTrack::kind() const {
tommi6eca7e32015-12-15 04:27:11 -080044 RTC_DCHECK(thread_checker_.CalledOnValidThread());
deadbeeffac06552015-11-25 11:26:01 -080045 return kAudioKind;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046}
47
tommi6eca7e32015-12-15 04:27:11 -080048AudioSourceInterface* AudioTrack::GetSource() const {
49 RTC_DCHECK(thread_checker_.CalledOnValidThread());
50 return audio_source_.get();
51}
52
53void AudioTrack::AddSink(AudioTrackSinkInterface* sink) {
54 RTC_DCHECK(thread_checker_.CalledOnValidThread());
55 if (audio_source_)
56 audio_source_->AddSink(sink);
57}
58
59void AudioTrack::RemoveSink(AudioTrackSinkInterface* sink) {
60 RTC_DCHECK(thread_checker_.CalledOnValidThread());
61 if (audio_source_)
62 audio_source_->RemoveSink(sink);
63}
64
65void AudioTrack::OnChanged() {
66 RTC_DCHECK(thread_checker_.CalledOnValidThread());
perkjc8f952d2016-03-23 00:33:56 -070067 if (audio_source_->state() == MediaSourceInterface::kEnded) {
68 set_state(kEnded);
69 } else {
70 set_state(kLive);
tommi6eca7e32015-12-15 04:27:11 -080071 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072}
73
74} // namespace webrtc