| 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. |
| 9 | */ |
| 10 |
| 11 | #include "pc/audio_track.h" |
| 12 |
| 13 | #include "rtc_base/checks.h" |
| 14 |
| 15 | namespace webrtc { |
| 16 |
| 17 | // static |
| 18 | rtc::scoped_refptr<AudioTrack> AudioTrack::Create( |
| 19 | absl::string_view id, |
| 20 | const rtc::scoped_refptr<AudioSourceInterface>& source) { |
| 21 | return rtc::make_ref_counted<AudioTrack>(id, source); |
| 22 | } |
| 23 |
| 24 | AudioTrack::AudioTrack(absl::string_view label, |
| 25 | const rtc::scoped_refptr<AudioSourceInterface>& source) |
| 26 | : MediaStreamTrack<AudioTrackInterface>(label), audio_source_(source) { |
| 27 | if (audio_source_) { |
| 28 | audio_source_->RegisterObserver(this); |
| 29 | OnChanged(); |
| 30 | } |
| 31 | } |
| 32 |
| 33 | AudioTrack::~AudioTrack() { |
| 34 | RTC_DCHECK_RUN_ON(&signaling_thread_checker_); |
| 35 | set_state(MediaStreamTrackInterface::kEnded); |
| 36 | if (audio_source_) |
| 37 | audio_source_->UnregisterObserver(this); |
| 38 | } |
| 39 |
| 40 | std::string AudioTrack::kind() const { |
| 41 | return kAudioKind; |
| 42 | } |
| 43 |
| 44 | AudioSourceInterface* AudioTrack::GetSource() const { |
| 45 | // Callable from any thread. |
| 46 | return audio_source_.get(); |
| 47 | } |
| 48 |
| 49 | void AudioTrack::AddSink(AudioTrackSinkInterface* sink) { |
| 50 | RTC_DCHECK_RUN_ON(&signaling_thread_checker_); |
| 51 | if (audio_source_) |
| 52 | audio_source_->AddSink(sink); |
| 53 | } |
| 54 |
| 55 | void AudioTrack::RemoveSink(AudioTrackSinkInterface* sink) { |
| 56 | RTC_DCHECK_RUN_ON(&signaling_thread_checker_); |
| 57 | if (audio_source_) |
| 58 | audio_source_->RemoveSink(sink); |
| 59 | } |
| 60 |
| 61 | void AudioTrack::OnChanged() { |
| 62 | RTC_DCHECK_RUN_ON(&signaling_thread_checker_); |
| 63 | if (audio_source_->state() == MediaSourceInterface::kEnded) { |
| 64 | set_state(kEnded); |
| 65 | } else { |
| 66 | set_state(kLive); |
| 67 | } |
| 68 | } |
| 69 |
| 70 | } // namespace webrtc |
| 71 |