blob: 1ae05359364d1fcdda52c60ed9403c3798adb8cc [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
Henrik Kjellander15583c12016-02-10 10:53:12 +01003 * Copyright 2011 Google Inc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +000027
Henrik Kjellander15583c12016-02-10 10:53:12 +010028#include "webrtc/api/audiotrack.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000029
tommi6eca7e32015-12-15 04:27:11 -080030#include "webrtc/base/checks.h"
31
32using rtc::scoped_refptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033
34namespace webrtc {
35
deadbeeffac06552015-11-25 11:26:01 -080036const char MediaStreamTrackInterface::kAudioKind[] = "audio";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037
tommi6eca7e32015-12-15 04:27:11 -080038// static
39scoped_refptr<AudioTrack> AudioTrack::Create(
40 const std::string& id,
41 const scoped_refptr<AudioSourceInterface>& source) {
42 return new rtc::RefCountedObject<AudioTrack>(id, source);
43}
44
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045AudioTrack::AudioTrack(const std::string& label,
tommi6eca7e32015-12-15 04:27:11 -080046 const scoped_refptr<AudioSourceInterface>& source)
47 : MediaStreamTrack<AudioTrackInterface>(label), audio_source_(source) {
48 if (audio_source_) {
49 audio_source_->RegisterObserver(this);
50 OnChanged();
51 }
52}
53
54AudioTrack::~AudioTrack() {
55 RTC_DCHECK(thread_checker_.CalledOnValidThread());
56 set_state(MediaStreamTrackInterface::kEnded);
57 if (audio_source_)
58 audio_source_->UnregisterObserver(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059}
60
61std::string AudioTrack::kind() const {
tommi6eca7e32015-12-15 04:27:11 -080062 RTC_DCHECK(thread_checker_.CalledOnValidThread());
deadbeeffac06552015-11-25 11:26:01 -080063 return kAudioKind;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064}
65
tommi6eca7e32015-12-15 04:27:11 -080066AudioSourceInterface* AudioTrack::GetSource() const {
67 RTC_DCHECK(thread_checker_.CalledOnValidThread());
68 return audio_source_.get();
69}
70
71void AudioTrack::AddSink(AudioTrackSinkInterface* sink) {
72 RTC_DCHECK(thread_checker_.CalledOnValidThread());
73 if (audio_source_)
74 audio_source_->AddSink(sink);
75}
76
77void AudioTrack::RemoveSink(AudioTrackSinkInterface* sink) {
78 RTC_DCHECK(thread_checker_.CalledOnValidThread());
79 if (audio_source_)
80 audio_source_->RemoveSink(sink);
81}
82
83void AudioTrack::OnChanged() {
84 RTC_DCHECK(thread_checker_.CalledOnValidThread());
85 if (state() == kFailed)
86 return; // We can't recover from this state (do we ever set it?).
87
88 TrackState new_state = kInitializing;
89
90 // |audio_source_| must be non-null if we ever get here.
91 switch (audio_source_->state()) {
92 case MediaSourceInterface::kLive:
93 case MediaSourceInterface::kMuted:
94 new_state = kLive;
95 break;
96 case MediaSourceInterface::kEnded:
97 new_state = kEnded;
98 break;
99 case MediaSourceInterface::kInitializing:
100 default:
101 // use kInitializing.
102 break;
103 }
104
105 set_state(new_state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106}
107
108} // namespace webrtc