blob: ae8914d6344cddab4a578f0cee9fa67920278dd9 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2011 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.org28e20752013-07-10 00:45:36 +00009 */
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +000010
Steve Anton10542f22019-01-11 09:11:00 -080011#include "pc/audio_track.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080014#include "rtc_base/ref_counted_object.h"
tommi6eca7e32015-12-15 04:27:11 -080015
henrike@webrtc.org28e20752013-07-10 00:45:36 +000016namespace webrtc {
17
tommi6eca7e32015-12-15 04:27:11 -080018// static
Tommi36207d62017-11-28 20:13:03 +010019rtc::scoped_refptr<AudioTrack> AudioTrack::Create(
tommi6eca7e32015-12-15 04:27:11 -080020 const std::string& id,
Tommi36207d62017-11-28 20:13:03 +010021 const rtc::scoped_refptr<AudioSourceInterface>& source) {
Tommi87f70902021-04-27 14:43:08 +020022 return rtc::make_ref_counted<AudioTrack>(id, source);
tommi6eca7e32015-12-15 04:27:11 -080023}
24
henrike@webrtc.org28e20752013-07-10 00:45:36 +000025AudioTrack::AudioTrack(const std::string& label,
Tommi36207d62017-11-28 20:13:03 +010026 const rtc::scoped_refptr<AudioSourceInterface>& source)
tommi6eca7e32015-12-15 04:27:11 -080027 : MediaStreamTrack<AudioTrackInterface>(label), audio_source_(source) {
28 if (audio_source_) {
29 audio_source_->RegisterObserver(this);
30 OnChanged();
31 }
32}
33
34AudioTrack::~AudioTrack() {
Tomas Gunnarssonfe328ca2022-02-16 20:02:12 +010035 RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
tommi6eca7e32015-12-15 04:27:11 -080036 set_state(MediaStreamTrackInterface::kEnded);
37 if (audio_source_)
38 audio_source_->UnregisterObserver(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039}
40
41std::string AudioTrack::kind() const {
deadbeeffac06552015-11-25 11:26:01 -080042 return kAudioKind;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043}
44
tommi6eca7e32015-12-15 04:27:11 -080045AudioSourceInterface* AudioTrack::GetSource() const {
Tommi816134a2021-05-24 16:54:41 +020046 // Callable from any thread.
tommi6eca7e32015-12-15 04:27:11 -080047 return audio_source_.get();
48}
49
50void AudioTrack::AddSink(AudioTrackSinkInterface* sink) {
Tomas Gunnarssonfe328ca2022-02-16 20:02:12 +010051 RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
tommi6eca7e32015-12-15 04:27:11 -080052 if (audio_source_)
53 audio_source_->AddSink(sink);
54}
55
56void AudioTrack::RemoveSink(AudioTrackSinkInterface* sink) {
Tomas Gunnarssonfe328ca2022-02-16 20:02:12 +010057 RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
tommi6eca7e32015-12-15 04:27:11 -080058 if (audio_source_)
59 audio_source_->RemoveSink(sink);
60}
61
62void AudioTrack::OnChanged() {
Tomas Gunnarssonfe328ca2022-02-16 20:02:12 +010063 RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
perkjc8f952d2016-03-23 00:33:56 -070064 if (audio_source_->state() == MediaSourceInterface::kEnded) {
65 set_state(kEnded);
66 } else {
67 set_state(kLive);
tommi6eca7e32015-12-15 04:27:11 -080068 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069}
70
71} // namespace webrtc