wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2014 The WebRTC project authors. All Rights Reserved. |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [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. |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #ifndef PC_REMOTE_AUDIO_SOURCE_H_ |
| 12 | #define PC_REMOTE_AUDIO_SOURCE_H_ |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 13 | |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 +0000 | [diff] [blame] | 14 | #include <stdint.h> |
| 15 | |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 16 | #include <list> |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 17 | #include <string> |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 18 | |
Saurav Das | 749f660 | 2019-12-04 09:31:36 -0800 | [diff] [blame] | 19 | #include "absl/types/optional.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "api/call/audio_sink.h" |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 +0000 | [diff] [blame] | 21 | #include "api/media_stream_interface.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "api/notifier.h" |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 +0000 | [diff] [blame] | 23 | #include "media/base/media_channel.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 24 | #include "rtc_base/message_handler.h" |
Markus Handell | 6fcd0f8 | 2020-07-07 19:08:53 +0200 | [diff] [blame] | 25 | #include "rtc_base/synchronization/mutex.h" |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 +0000 | [diff] [blame] | 26 | #include "rtc_base/thread.h" |
| 27 | #include "rtc_base/thread_message.h" |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 28 | |
| 29 | namespace rtc { |
| 30 | struct Message; |
| 31 | class Thread; |
| 32 | } // namespace rtc |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 33 | |
| 34 | namespace webrtc { |
| 35 | |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 36 | // This class implements the audio source used by the remote audio track. |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 37 | // This class works by configuring itself as a sink with the underlying media |
| 38 | // engine, then when receiving data will fan out to all added sinks. |
Steve Anton | 3b80aac | 2017-10-19 10:17:12 -0700 | [diff] [blame] | 39 | class RemoteAudioSource : public Notifier<AudioSourceInterface>, |
| 40 | rtc::MessageHandler { |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 41 | public: |
Henrik Boström | c335b0e | 2021-04-08 07:25:38 +0200 | [diff] [blame] | 42 | // In Unified Plan, receivers map to m= sections and their tracks and sources |
| 43 | // survive SSRCs being reconfigured. The life cycle of the remote audio source |
| 44 | // is associated with the life cycle of the m= section, and thus even if an |
| 45 | // audio channel is destroyed the RemoteAudioSource should kSurvive. |
| 46 | // |
| 47 | // In Plan B however, remote audio sources map 1:1 with an SSRCs and if an |
| 48 | // audio channel is destroyed, the RemoteAudioSource should kEnd. |
| 49 | enum class OnAudioChannelGoneAction { |
| 50 | kSurvive, |
| 51 | kEnd, |
| 52 | }; |
| 53 | |
| 54 | explicit RemoteAudioSource( |
| 55 | rtc::Thread* worker_thread, |
| 56 | OnAudioChannelGoneAction on_audio_channel_gone_action); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 57 | |
| 58 | // Register and unregister remote audio source with the underlying media |
| 59 | // engine. |
Saurav Das | 749f660 | 2019-12-04 09:31:36 -0800 | [diff] [blame] | 60 | void Start(cricket::VoiceMediaChannel* media_channel, |
| 61 | absl::optional<uint32_t> ssrc); |
| 62 | void Stop(cricket::VoiceMediaChannel* media_channel, |
| 63 | absl::optional<uint32_t> ssrc); |
Henrik Boström | c335b0e | 2021-04-08 07:25:38 +0200 | [diff] [blame] | 64 | void SetState(SourceState new_state); |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 65 | |
| 66 | // MediaSourceInterface implementation. |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 67 | MediaSourceInterface::SourceState state() const override; |
tommi | 6eca7e3 | 2015-12-15 04:27:11 -0800 | [diff] [blame] | 68 | bool remote() const override; |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 69 | |
| 70 | // AudioSourceInterface implementation. |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 71 | void SetVolume(double volume) override; |
| 72 | void RegisterAudioObserver(AudioObserver* observer) override; |
| 73 | void UnregisterAudioObserver(AudioObserver* observer) override; |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 74 | |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 75 | void AddSink(AudioTrackSinkInterface* sink) override; |
| 76 | void RemoveSink(AudioTrackSinkInterface* sink) override; |
| 77 | |
| 78 | protected: |
| 79 | ~RemoteAudioSource() override; |
| 80 | |
| 81 | private: |
| 82 | // These are callbacks from the media engine. |
| 83 | class AudioDataProxy; |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 +0000 | [diff] [blame] | 84 | |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 85 | void OnData(const AudioSinkInterface::Data& audio); |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 86 | void OnAudioChannelGone(); |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 87 | |
Steve Anton | 3b80aac | 2017-10-19 10:17:12 -0700 | [diff] [blame] | 88 | void OnMessage(rtc::Message* msg) override; |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 89 | |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 90 | rtc::Thread* const main_thread_; |
| 91 | rtc::Thread* const worker_thread_; |
Henrik Boström | c335b0e | 2021-04-08 07:25:38 +0200 | [diff] [blame] | 92 | const OnAudioChannelGoneAction on_audio_channel_gone_action_; |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 93 | std::list<AudioObserver*> audio_observers_; |
Markus Handell | 6fcd0f8 | 2020-07-07 19:08:53 +0200 | [diff] [blame] | 94 | Mutex sink_lock_; |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 95 | std::list<AudioTrackSinkInterface*> sinks_; |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 96 | SourceState state_; |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | } // namespace webrtc |
| 100 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 101 | #endif // PC_REMOTE_AUDIO_SOURCE_H_ |