1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <mutex>
20 #include <set>
21 
22 #include <api/media_stream_interface.h>
23 
24 #include "host/frontend/webrtc/libdevice/audio_sink.h"
25 
26 namespace cuttlefish {
27 namespace webrtc_streaming {
28 
29 class AudioTrackSourceImpl : public webrtc::AudioSourceInterface {
30  public:
31   AudioTrackSourceImpl() = default;
32 
33   // Sets the volume of the source. |volume| is in  the range of [0, 10].
34   void SetVolume(double volume) override;
35 
36   void RegisterAudioObserver(AudioObserver* observer) override;
37   void UnregisterAudioObserver(AudioObserver* observer) override;
38 
39   void AddSink(webrtc::AudioTrackSinkInterface* sink) override;
40   void RemoveSink(webrtc::AudioTrackSinkInterface* sink) override;
41 
42   // Returns options for the AudioSource.
43   // (for some of the settings this approach is broken, e.g. setting
44   // audio network adaptation on the source is the wrong layer of abstraction).
45   virtual const cricket::AudioOptions options() const;
46 
47   void OnFrame(const AudioFrameBuffer& frame, int64_t timestamp_ms);
48 
49   // MediaSourceInterface implementation
50   SourceState state() const override;
51   bool remote() const override;
52 
53   // NotifierInterface implementation
54   void RegisterObserver(webrtc::ObserverInterface* observer) override;
55   void UnregisterObserver(webrtc::ObserverInterface* observer) override;
56 
57  private:
58   std::set<AudioObserver*> audio_observers_;
59   std::mutex observers_mutex_;
60   std::set<webrtc::AudioTrackSinkInterface*> sinks_;
61   std::mutex sinks_mutex_;
62 };
63 
64 // Wraps an AudioTrackSourceImpl as an implementation of the AudioSink
65 // interface. This is needed as the AudioTrackSourceImpl is a reference counted
66 // object that should only be referenced by rtc::scoped_refptr pointers, but the
67 // AudioSink interface is not a reference counted object and therefore not
68 // compatible with that kind of pointers. This class can be referenced by a
69 // shared pointer and it in turn holds a scoped_refptr to the wrapped object.
70 class AudioTrackSourceImplSinkWrapper : public AudioSink {
71  public:
72   virtual ~AudioTrackSourceImplSinkWrapper() = default;
73 
AudioTrackSourceImplSinkWrapper(rtc::scoped_refptr<AudioTrackSourceImpl> obj)74   AudioTrackSourceImplSinkWrapper(rtc::scoped_refptr<AudioTrackSourceImpl> obj)
75       : track_source_impl_(obj) {}
76 
OnFrame(const AudioFrameBuffer & frame,int64_t timestamp_ms)77   void OnFrame(const AudioFrameBuffer& frame,
78                int64_t timestamp_ms) override {
79     track_source_impl_->OnFrame(frame, timestamp_ms);
80   }
81 
82  private:
83   rtc::scoped_refptr<AudioTrackSourceImpl> track_source_impl_;
84 };
85 
86 }  // namespace webrtc_streaming
87 }  // namespace cuttlefish
88