OLD | NEW |
1 /* | 1 /* |
2 * libjingle | 2 * libjingle |
3 * Copyright 2004--2011 Google Inc. | 3 * Copyright 2004--2011 Google Inc. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are met: | 6 * modification, are permitted provided that the following conditions are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright notice, | 8 * 1. Redistributions of source code must retain the above copyright notice, |
9 * this list of conditions and the following disclaimer. | 9 * this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright notice, | 10 * 2. Redistributions in binary form must reproduce the above copyright notice, |
11 * this list of conditions and the following disclaimer in the documentation | 11 * this list of conditions and the following disclaimer in the documentation |
12 * and/or other materials provided with the distribution. | 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 | 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. | 14 * derived from this software without specific prior written permission. |
15 * | 15 * |
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED | 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 | 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | 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, | 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | 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 | 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 */ | 26 */ |
27 | 27 |
28 #include "talk/app/webrtc/audiotrack.h" | 28 #include "talk/app/webrtc/audiotrack.h" |
29 | 29 |
30 #include <string> | 30 #include "webrtc/base/checks.h" |
| 31 |
| 32 using rtc::scoped_refptr; |
31 | 33 |
32 namespace webrtc { | 34 namespace webrtc { |
33 | 35 |
34 const char MediaStreamTrackInterface::kAudioKind[] = "audio"; | 36 const char MediaStreamTrackInterface::kAudioKind[] = "audio"; |
35 | 37 |
| 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 |
36 AudioTrack::AudioTrack(const std::string& label, | 45 AudioTrack::AudioTrack(const std::string& label, |
37 AudioSourceInterface* audio_source) | 46 const scoped_refptr<AudioSourceInterface>& source) |
38 : MediaStreamTrack<AudioTrackInterface>(label), | 47 : MediaStreamTrack<AudioTrackInterface>(label), audio_source_(source) { |
39 audio_source_(audio_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); |
40 } | 59 } |
41 | 60 |
42 std::string AudioTrack::kind() const { | 61 std::string AudioTrack::kind() const { |
| 62 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
43 return kAudioKind; | 63 return kAudioKind; |
44 } | 64 } |
45 | 65 |
46 rtc::scoped_refptr<AudioTrack> AudioTrack::Create( | 66 AudioSourceInterface* AudioTrack::GetSource() const { |
47 const std::string& id, AudioSourceInterface* source) { | 67 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
48 rtc::RefCountedObject<AudioTrack>* track = | 68 return audio_source_.get(); |
49 new rtc::RefCountedObject<AudioTrack>(id, source); | 69 } |
50 return track; | 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); |
51 } | 106 } |
52 | 107 |
53 } // namespace webrtc | 108 } // namespace webrtc |
OLD | NEW |