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