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 "media/base/media_channel.h"
24 #include "pc/channel.h"
25 #include "rtc_base/message_handler.h"
26 #include "rtc_base/synchronization/mutex.h"
27 #include "rtc_base/thread.h"
28 #include "rtc_base/thread_message.h"
29 
30 namespace rtc {
31 struct Message;
32 class Thread;
33 }  // namespace rtc
34 
35 namespace webrtc {
36 
37 // This class implements the audio source used by the remote audio track.
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.
40 class RemoteAudioSource : public Notifier<AudioSourceInterface>,
41                           rtc::MessageHandler {
42  public:
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);
58 
59   // Register and unregister remote audio source with the underlying media
60   // engine.
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);
65   void SetState(SourceState new_state);
66 
67   // MediaSourceInterface implementation.
68   MediaSourceInterface::SourceState state() const override;
69   bool remote() const override;
70 
71   // AudioSourceInterface implementation.
72   void SetVolume(double volume) override;
73   void RegisterAudioObserver(AudioObserver* observer) override;
74   void UnregisterAudioObserver(AudioObserver* observer) override;
75 
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;
85 
86   void OnData(const AudioSinkInterface::Data& audio);
87   void OnAudioChannelGone();
88 
89   void OnMessage(rtc::Message* msg) override;
90 
91   rtc::Thread* const main_thread_;
92   rtc::Thread* const worker_thread_;
93   const OnAudioChannelGoneAction on_audio_channel_gone_action_;
94   std::list<AudioObserver*> audio_observers_;
95   Mutex sink_lock_;
96   std::list<AudioTrackSinkInterface*> sinks_;
97   SourceState state_;
98 };
99 
100 }  // namespace webrtc
101 
102 #endif  // PC_REMOTE_AUDIO_SOURCE_H_
103