henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 15:54:43 | [diff] [blame] | 2 | * Copyright 2011 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 15:54:43 | [diff] [blame] | 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. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 9 | */ |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 | [diff] [blame] | 10 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #include "pc/audio_track.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 13 | #include "rtc_base/checks.h" |
tommi | 6eca7e3 | 2015-12-15 12:27:11 | [diff] [blame] | 14 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 15 | namespace webrtc { |
| 16 | |
tommi | 6eca7e3 | 2015-12-15 12:27:11 | [diff] [blame] | 17 | // static |
Tommi | 36207d6 | 2017-11-28 19:13:03 | [diff] [blame] | 18 | rtc::scoped_refptr<AudioTrack> AudioTrack::Create( |
Niels Möller | c397fc6 | 2022-05-30 09:26:40 | [diff] [blame] | 19 | absl::string_view id, |
Tommi | 36207d6 | 2017-11-28 19:13:03 | [diff] [blame] | 20 | const rtc::scoped_refptr<AudioSourceInterface>& source) { |
Tommi | 87f7090 | 2021-04-27 12:43:08 | [diff] [blame] | 21 | return rtc::make_ref_counted<AudioTrack>(id, source); |
tommi | 6eca7e3 | 2015-12-15 12:27:11 | [diff] [blame] | 22 | } |
| 23 | |
Niels Möller | c397fc6 | 2022-05-30 09:26:40 | [diff] [blame] | 24 | AudioTrack::AudioTrack(absl::string_view label, |
Tommi | 36207d6 | 2017-11-28 19:13:03 | [diff] [blame] | 25 | const rtc::scoped_refptr<AudioSourceInterface>& source) |
tommi | 6eca7e3 | 2015-12-15 12:27:11 | [diff] [blame] | 26 | : MediaStreamTrack<AudioTrackInterface>(label), audio_source_(source) { |
| 27 | if (audio_source_) { |
| 28 | audio_source_->RegisterObserver(this); |
| 29 | OnChanged(); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | AudioTrack::~AudioTrack() { |
Tomas Gunnarsson | fe328ca | 2022-02-16 19:02:12 | [diff] [blame] | 34 | RTC_DCHECK_RUN_ON(&signaling_thread_checker_); |
tommi | 6eca7e3 | 2015-12-15 12:27:11 | [diff] [blame] | 35 | set_state(MediaStreamTrackInterface::kEnded); |
| 36 | if (audio_source_) |
| 37 | audio_source_->UnregisterObserver(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | std::string AudioTrack::kind() const { |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 41 | return kAudioKind; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 42 | } |
| 43 | |
tommi | 6eca7e3 | 2015-12-15 12:27:11 | [diff] [blame] | 44 | AudioSourceInterface* AudioTrack::GetSource() const { |
Tommi | 816134a | 2021-05-24 14:54:41 | [diff] [blame] | 45 | // Callable from any thread. |
tommi | 6eca7e3 | 2015-12-15 12:27:11 | [diff] [blame] | 46 | return audio_source_.get(); |
| 47 | } |
| 48 | |
| 49 | void AudioTrack::AddSink(AudioTrackSinkInterface* sink) { |
Tomas Gunnarsson | fe328ca | 2022-02-16 19:02:12 | [diff] [blame] | 50 | RTC_DCHECK_RUN_ON(&signaling_thread_checker_); |
tommi | 6eca7e3 | 2015-12-15 12:27:11 | [diff] [blame] | 51 | if (audio_source_) |
| 52 | audio_source_->AddSink(sink); |
| 53 | } |
| 54 | |
| 55 | void AudioTrack::RemoveSink(AudioTrackSinkInterface* sink) { |
Tomas Gunnarsson | fe328ca | 2022-02-16 19:02:12 | [diff] [blame] | 56 | RTC_DCHECK_RUN_ON(&signaling_thread_checker_); |
tommi | 6eca7e3 | 2015-12-15 12:27:11 | [diff] [blame] | 57 | if (audio_source_) |
| 58 | audio_source_->RemoveSink(sink); |
| 59 | } |
| 60 | |
| 61 | void AudioTrack::OnChanged() { |
Tomas Gunnarsson | fe328ca | 2022-02-16 19:02:12 | [diff] [blame] | 62 | RTC_DCHECK_RUN_ON(&signaling_thread_checker_); |
perkj | c8f952d | 2016-03-23 07:33:56 | [diff] [blame] | 63 | if (audio_source_->state() == MediaSourceInterface::kEnded) { |
| 64 | set_state(kEnded); |
| 65 | } else { |
| 66 | set_state(kLive); |
tommi | 6eca7e3 | 2015-12-15 12:27:11 | [diff] [blame] | 67 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | } // namespace webrtc |