henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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 | */ |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 27 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 28 | #include "webrtc/api/audiotrack.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 29 | |
tommi | 6eca7e3 | 2015-12-15 04:27:11 -0800 | [diff] [blame] | 30 | #include "webrtc/base/checks.h" |
| 31 | |
| 32 | using rtc::scoped_refptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 33 | |
| 34 | namespace webrtc { |
| 35 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 36 | const char MediaStreamTrackInterface::kAudioKind[] = "audio"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 37 | |
tommi | 6eca7e3 | 2015-12-15 04:27:11 -0800 | [diff] [blame] | 38 | // static |
| 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 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 45 | AudioTrack::AudioTrack(const std::string& label, |
tommi | 6eca7e3 | 2015-12-15 04:27:11 -0800 | [diff] [blame] | 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 | |
| 54 | AudioTrack::~AudioTrack() { |
| 55 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 56 | set_state(MediaStreamTrackInterface::kEnded); |
| 57 | if (audio_source_) |
| 58 | audio_source_->UnregisterObserver(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | std::string AudioTrack::kind() const { |
tommi | 6eca7e3 | 2015-12-15 04:27:11 -0800 | [diff] [blame] | 62 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 63 | return kAudioKind; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 64 | } |
| 65 | |
tommi | 6eca7e3 | 2015-12-15 04:27:11 -0800 | [diff] [blame] | 66 | AudioSourceInterface* AudioTrack::GetSource() const { |
| 67 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 68 | return audio_source_.get(); |
| 69 | } |
| 70 | |
| 71 | void AudioTrack::AddSink(AudioTrackSinkInterface* sink) { |
| 72 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 73 | if (audio_source_) |
| 74 | audio_source_->AddSink(sink); |
| 75 | } |
| 76 | |
| 77 | void AudioTrack::RemoveSink(AudioTrackSinkInterface* sink) { |
| 78 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 79 | if (audio_source_) |
| 80 | audio_source_->RemoveSink(sink); |
| 81 | } |
| 82 | |
| 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); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | } // namespace webrtc |