1 /*
2  *  Copyright 2011 The WebRTC project authors. All Rights Reserved.
3  *
4  *  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.
9  */
10 
11 #include "pc/audio_track.h"
12 
13 #include "rtc_base/checks.h"
14 #include "rtc_base/ref_counted_object.h"
15 
16 namespace webrtc {
17 
18 // static
Create(const std::string & id,const rtc::scoped_refptr<AudioSourceInterface> & source)19 rtc::scoped_refptr<AudioTrack> AudioTrack::Create(
20     const std::string& id,
21     const rtc::scoped_refptr<AudioSourceInterface>& source) {
22   return new rtc::RefCountedObject<AudioTrack>(id, source);
23 }
24 
AudioTrack(const std::string & label,const rtc::scoped_refptr<AudioSourceInterface> & source)25 AudioTrack::AudioTrack(const std::string& label,
26                        const rtc::scoped_refptr<AudioSourceInterface>& source)
27     : MediaStreamTrack<AudioTrackInterface>(label), audio_source_(source) {
28   if (audio_source_) {
29     audio_source_->RegisterObserver(this);
30     OnChanged();
31   }
32 }
33 
~AudioTrack()34 AudioTrack::~AudioTrack() {
35   RTC_DCHECK(thread_checker_.IsCurrent());
36   set_state(MediaStreamTrackInterface::kEnded);
37   if (audio_source_)
38     audio_source_->UnregisterObserver(this);
39 }
40 
kind() const41 std::string AudioTrack::kind() const {
42   return kAudioKind;
43 }
44 
GetSource() const45 AudioSourceInterface* AudioTrack::GetSource() const {
46   RTC_DCHECK(thread_checker_.IsCurrent());
47   return audio_source_.get();
48 }
49 
AddSink(AudioTrackSinkInterface * sink)50 void AudioTrack::AddSink(AudioTrackSinkInterface* sink) {
51   RTC_DCHECK(thread_checker_.IsCurrent());
52   if (audio_source_)
53     audio_source_->AddSink(sink);
54 }
55 
RemoveSink(AudioTrackSinkInterface * sink)56 void AudioTrack::RemoveSink(AudioTrackSinkInterface* sink) {
57   RTC_DCHECK(thread_checker_.IsCurrent());
58   if (audio_source_)
59     audio_source_->RemoveSink(sink);
60 }
61 
OnChanged()62 void AudioTrack::OnChanged() {
63   RTC_DCHECK(thread_checker_.IsCurrent());
64   if (audio_source_->state() == MediaSourceInterface::kEnded) {
65     set_state(kEnded);
66   } else {
67     set_state(kLive);
68   }
69 }
70 
71 }  // namespace webrtc
72