blob: c012442d1376332ee693b614c45ccae3289500fb [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:361/*
kjellanderb24317b2016-02-10 15:54:432 * Copyright 2011 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:363 *
kjellanderb24317b2016-02-10 15:54:434 * 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:369 */
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:1310
Steve Anton10542f22019-01-11 17:11:0011#include "pc/audio_track.h"
henrike@webrtc.org28e20752013-07-10 00:45:3612
Mirko Bonadei92ea95e2017-09-15 04:47:3113#include "rtc_base/checks.h"
tommi6eca7e32015-12-15 12:27:1114
henrike@webrtc.org28e20752013-07-10 00:45:3615namespace webrtc {
16
tommi6eca7e32015-12-15 12:27:1117// static
Tommi36207d62017-11-28 19:13:0318rtc::scoped_refptr<AudioTrack> AudioTrack::Create(
Niels Möllerc397fc62022-05-30 09:26:4019 absl::string_view id,
Tommi36207d62017-11-28 19:13:0320 const rtc::scoped_refptr<AudioSourceInterface>& source) {
Tommi87f70902021-04-27 12:43:0821 return rtc::make_ref_counted<AudioTrack>(id, source);
tommi6eca7e32015-12-15 12:27:1122}
23
Niels Möllerc397fc62022-05-30 09:26:4024AudioTrack::AudioTrack(absl::string_view label,
Tommi36207d62017-11-28 19:13:0325 const rtc::scoped_refptr<AudioSourceInterface>& source)
tommi6eca7e32015-12-15 12:27:1126 : MediaStreamTrack<AudioTrackInterface>(label), audio_source_(source) {
27 if (audio_source_) {
28 audio_source_->RegisterObserver(this);
29 OnChanged();
30 }
31}
32
33AudioTrack::~AudioTrack() {
Tomas Gunnarssonfe328ca2022-02-16 19:02:1234 RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
tommi6eca7e32015-12-15 12:27:1135 set_state(MediaStreamTrackInterface::kEnded);
36 if (audio_source_)
37 audio_source_->UnregisterObserver(this);
henrike@webrtc.org28e20752013-07-10 00:45:3638}
39
40std::string AudioTrack::kind() const {
deadbeeffac06552015-11-25 19:26:0141 return kAudioKind;
henrike@webrtc.org28e20752013-07-10 00:45:3642}
43
tommi6eca7e32015-12-15 12:27:1144AudioSourceInterface* AudioTrack::GetSource() const {
Tommi816134a2021-05-24 14:54:4145 // Callable from any thread.
tommi6eca7e32015-12-15 12:27:1146 return audio_source_.get();
47}
48
49void AudioTrack::AddSink(AudioTrackSinkInterface* sink) {
Tomas Gunnarssonfe328ca2022-02-16 19:02:1250 RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
tommi6eca7e32015-12-15 12:27:1151 if (audio_source_)
52 audio_source_->AddSink(sink);
53}
54
55void AudioTrack::RemoveSink(AudioTrackSinkInterface* sink) {
Tomas Gunnarssonfe328ca2022-02-16 19:02:1256 RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
tommi6eca7e32015-12-15 12:27:1157 if (audio_source_)
58 audio_source_->RemoveSink(sink);
59}
60
61void AudioTrack::OnChanged() {
Tomas Gunnarssonfe328ca2022-02-16 19:02:1262 RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
perkjc8f952d2016-03-23 07:33:5663 if (audio_source_->state() == MediaSourceInterface::kEnded) {
64 set_state(kEnded);
65 } else {
66 set_state(kLive);
tommi6eca7e32015-12-15 12:27:1167 }
henrike@webrtc.org28e20752013-07-10 00:45:3668}
69
70} // namespace webrtc