1 /*
2 * libjingle
3 * Copyright 2004--2011 Google Inc.
4 *
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 */
27
28 #include "talk/app/webrtc/audiotrack.h"
29
30 #include "webrtc/base/checks.h"
31
32 using rtc::scoped_refptr;
33
34 namespace webrtc {
35
36 const char MediaStreamTrackInterface::kAudioKind[] = "audio";
37
38 // static
Create(const std::string & id,const scoped_refptr<AudioSourceInterface> & source)39 scoped_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
AudioTrack(const std::string & label,const scoped_refptr<AudioSourceInterface> & source)45 AudioTrack::AudioTrack(const std::string& label,
46 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
~AudioTrack()54 AudioTrack::~AudioTrack() {
55 RTC_DCHECK(thread_checker_.CalledOnValidThread());
56 set_state(MediaStreamTrackInterface::kEnded);
57 if (audio_source_)
58 audio_source_->UnregisterObserver(this);
59 }
60
kind() const61 std::string AudioTrack::kind() const {
62 RTC_DCHECK(thread_checker_.CalledOnValidThread());
63 return kAudioKind;
64 }
65
GetSource() const66 AudioSourceInterface* AudioTrack::GetSource() const {
67 RTC_DCHECK(thread_checker_.CalledOnValidThread());
68 return audio_source_.get();
69 }
70
AddSink(AudioTrackSinkInterface * sink)71 void AudioTrack::AddSink(AudioTrackSinkInterface* sink) {
72 RTC_DCHECK(thread_checker_.CalledOnValidThread());
73 if (audio_source_)
74 audio_source_->AddSink(sink);
75 }
76
RemoveSink(AudioTrackSinkInterface * sink)77 void AudioTrack::RemoveSink(AudioTrackSinkInterface* sink) {
78 RTC_DCHECK(thread_checker_.CalledOnValidThread());
79 if (audio_source_)
80 audio_source_->RemoveSink(sink);
81 }
82
OnChanged()83 void 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);
106 }
107
108 } // namespace webrtc
109