blob: 2a3e5e3878366500085dff0e4727ca0c4936a6d4 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:361/*
kjellanderb24317b2016-02-10 15:54:432 * Copyright 2011 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:363 *
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.
henrike@webrtc.org28e20752013-07-10 00:45:369 */
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:1310
Mirko Bonadei92ea95e2017-09-15 04:47:3111#include "pc/audiotrack.h"
henrike@webrtc.org28e20752013-07-10 00:45:3612
Mirko Bonadei92ea95e2017-09-15 04:47:3113#include "rtc_base/checks.h"
Niels Möller84255bb2017-10-06 11:43:2314#include "rtc_base/refcountedobject.h"
tommi6eca7e32015-12-15 12:27:1115
henrike@webrtc.org28e20752013-07-10 00:45:3616namespace webrtc {
17
tommi6eca7e32015-12-15 12:27:1118// static
Tommi36207d62017-11-28 19:13:0319rtc::scoped_refptr<AudioTrack> AudioTrack::Create(
tommi6eca7e32015-12-15 12:27:1120 const std::string& id,
Tommi36207d62017-11-28 19:13:0321 const rtc::scoped_refptr<AudioSourceInterface>& source) {
tommi6eca7e32015-12-15 12:27:1122 return new rtc::RefCountedObject<AudioTrack>(id, source);
23}
24
henrike@webrtc.org28e20752013-07-10 00:45:3625AudioTrack::AudioTrack(const std::string& label,
Tommi36207d62017-11-28 19:13:0326 const rtc::scoped_refptr<AudioSourceInterface>& source)
tommi6eca7e32015-12-15 12:27:1127 : MediaStreamTrack<AudioTrackInterface>(label), audio_source_(source) {
28 if (audio_source_) {
29 audio_source_->RegisterObserver(this);
30 OnChanged();
31 }
32}
33
34AudioTrack::~AudioTrack() {
35 RTC_DCHECK(thread_checker_.CalledOnValidThread());
36 set_state(MediaStreamTrackInterface::kEnded);
37 if (audio_source_)
38 audio_source_->UnregisterObserver(this);
henrike@webrtc.org28e20752013-07-10 00:45:3639}
40
41std::string AudioTrack::kind() const {
tommi6eca7e32015-12-15 12:27:1142 RTC_DCHECK(thread_checker_.CalledOnValidThread());
deadbeeffac06552015-11-25 19:26:0143 return kAudioKind;
henrike@webrtc.org28e20752013-07-10 00:45:3644}
45
tommi6eca7e32015-12-15 12:27:1146AudioSourceInterface* AudioTrack::GetSource() const {
47 RTC_DCHECK(thread_checker_.CalledOnValidThread());
48 return audio_source_.get();
49}
50
51void AudioTrack::AddSink(AudioTrackSinkInterface* sink) {
52 RTC_DCHECK(thread_checker_.CalledOnValidThread());
53 if (audio_source_)
54 audio_source_->AddSink(sink);
55}
56
57void AudioTrack::RemoveSink(AudioTrackSinkInterface* sink) {
58 RTC_DCHECK(thread_checker_.CalledOnValidThread());
59 if (audio_source_)
60 audio_source_->RemoveSink(sink);
61}
62
63void AudioTrack::OnChanged() {
64 RTC_DCHECK(thread_checker_.CalledOnValidThread());
perkjc8f952d2016-03-23 07:33:5665 if (audio_source_->state() == MediaSourceInterface::kEnded) {
66 set_state(kEnded);
67 } else {
68 set_state(kLive);
tommi6eca7e32015-12-15 12:27:1169 }
henrike@webrtc.org28e20752013-07-10 00:45:3670}
71
72} // namespace webrtc