xref: /aosp_15_r20/external/webrtc/pc/remote_audio_source.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 2014 The WebRTC project authors. All Rights Reserved.
3  *
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.
9  */
10 
11 #ifndef PC_REMOTE_AUDIO_SOURCE_H_
12 #define PC_REMOTE_AUDIO_SOURCE_H_
13 
14 #include <stdint.h>
15 
16 #include <list>
17 #include <string>
18 
19 #include "absl/types/optional.h"
20 #include "api/call/audio_sink.h"
21 #include "api/media_stream_interface.h"
22 #include "api/notifier.h"
23 #include "api/task_queue/task_queue_base.h"
24 #include "media/base/media_channel.h"
25 #include "rtc_base/synchronization/mutex.h"
26 
27 namespace webrtc {
28 
29 // This class implements the audio source used by the remote audio track.
30 // This class works by configuring itself as a sink with the underlying media
31 // engine, then when receiving data will fan out to all added sinks.
32 class RemoteAudioSource : public Notifier<AudioSourceInterface> {
33  public:
34   // In Unified Plan, receivers map to m= sections and their tracks and sources
35   // survive SSRCs being reconfigured. The life cycle of the remote audio source
36   // is associated with the life cycle of the m= section, and thus even if an
37   // audio channel is destroyed the RemoteAudioSource should kSurvive.
38   //
39   // In Plan B however, remote audio sources map 1:1 with an SSRCs and if an
40   // audio channel is destroyed, the RemoteAudioSource should kEnd.
41   enum class OnAudioChannelGoneAction {
42     kSurvive,
43     kEnd,
44   };
45 
46   explicit RemoteAudioSource(
47       TaskQueueBase* worker_thread,
48       OnAudioChannelGoneAction on_audio_channel_gone_action);
49 
50   // Register and unregister remote audio source with the underlying media
51   // engine.
52   void Start(cricket::VoiceMediaChannel* media_channel,
53              absl::optional<uint32_t> ssrc);
54   void Stop(cricket::VoiceMediaChannel* media_channel,
55             absl::optional<uint32_t> ssrc);
56   void SetState(SourceState new_state);
57 
58   // MediaSourceInterface implementation.
59   MediaSourceInterface::SourceState state() const override;
60   bool remote() const override;
61 
62   // AudioSourceInterface implementation.
63   void SetVolume(double volume) override;
64   void RegisterAudioObserver(AudioObserver* observer) override;
65   void UnregisterAudioObserver(AudioObserver* observer) override;
66 
67   void AddSink(AudioTrackSinkInterface* sink) override;
68   void RemoveSink(AudioTrackSinkInterface* sink) override;
69 
70  protected:
71   ~RemoteAudioSource() override;
72 
73  private:
74   // These are callbacks from the media engine.
75   class AudioDataProxy;
76 
77   void OnData(const AudioSinkInterface::Data& audio);
78   void OnAudioChannelGone();
79 
80   TaskQueueBase* const main_thread_;
81   TaskQueueBase* const worker_thread_;
82   const OnAudioChannelGoneAction on_audio_channel_gone_action_;
83   std::list<AudioObserver*> audio_observers_;
84   Mutex sink_lock_;
85   std::list<AudioTrackSinkInterface*> sinks_;
86   SourceState state_;
87 };
88 
89 }  // namespace webrtc
90 
91 #endif  // PC_REMOTE_AUDIO_SOURCE_H_
92