1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef AUDIO_SYSTEM_MANAGER_ADAPTER_IMPL_H
17 #define AUDIO_SYSTEM_MANAGER_ADAPTER_IMPL_H
18 
19 #include "audio_system_manager_adapter.h"
20 
21 #if defined(NWEB_AUDIO_ENABLE)
22 #include "audio_system_manager.h"
23 #endif
24 
25 namespace OHOS::NWeb {
26 #if defined(NWEB_AUDIO_ENABLE)
27 using namespace OHOS::AudioStandard;
28 
29 class AudioManagerCallbackAdapterImpl : public AudioManagerCallback {
30 public:
31     explicit AudioManagerCallbackAdapterImpl(std::shared_ptr<AudioManagerCallbackAdapter> cb);
32 
33     ~AudioManagerCallbackAdapterImpl() override = default;
34 
35     void OnInterrupt(const InterruptAction& interruptAction) override;
36 
37 private:
38     std::shared_ptr<AudioManagerCallbackAdapter> cb_ = nullptr;
39 };
40 
41 class AudioManagerDeviceChangeCallbackAdapterImpl : public AudioManagerDeviceChangeCallback {
42 public:
43     explicit AudioManagerDeviceChangeCallbackAdapterImpl(std::shared_ptr<AudioManagerDeviceChangeCallbackAdapter> cb);
44 
45     ~AudioManagerDeviceChangeCallbackAdapterImpl() override = default;
46 
47     void OnDeviceChange(const DeviceChangeAction& deviceChangeAction) override;
48 
49 private:
50     std::shared_ptr<AudioManagerDeviceChangeCallbackAdapter> cb_ = nullptr;
51 };
52 #endif
53 
54 class AudioSystemManagerAdapterImpl : public AudioSystemManagerAdapter {
55 public:
56     AudioSystemManagerAdapterImpl() = default;
57 
58     static AudioSystemManagerAdapterImpl& GetInstance();
59 
60     ~AudioSystemManagerAdapterImpl() override = default;
61 
62     bool HasAudioOutputDevices() override;
63 
64     bool HasAudioInputDevices() override;
65 
66     int32_t RequestAudioFocus(const std::shared_ptr<AudioInterruptAdapter> audioInterrupt) override;
67 
68     int32_t AbandonAudioFocus(const std::shared_ptr<AudioInterruptAdapter> audioInterrupt) override;
69 
70     int32_t SetAudioManagerInterruptCallback(std::shared_ptr<AudioManagerCallbackAdapter> callback) override;
71 
72     int32_t UnsetAudioManagerInterruptCallback() override;
73 
74     virtual std::vector<std::shared_ptr<AudioDeviceDescAdapter>> GetDevices(AdapterDeviceFlag flag) override;
75 
76     int32_t SelectAudioDeviceById(int32_t deviceId, bool isInput) override;
77 
78     std::shared_ptr<AudioDeviceDescAdapter> GetDefaultOutputDevice() override;
79 
80     int32_t SetDeviceChangeCallback(std::shared_ptr<AudioManagerDeviceChangeCallbackAdapter> callback) override;
81 
82     int32_t UnsetDeviceChangeCallback() override;
83 
84     std::shared_ptr<AudioDeviceDescAdapter> GetDefaultInputDevice() override;
85 
86     bool SetLanguage(const std::string& language) override;
87 
88     std::string GetDeviceName(DeviceType deviceType);
89 
90     std::string language_ = "zh";
91 
92 #if defined(NWEB_AUDIO_ENABLE)
93     static AudioStreamType GetStreamType(AudioAdapterStreamType streamType);
94 
95 private:
96     int32_t SelectAudioOutputDevice(bool isCallDevice, const std::vector<sptr<AudioDeviceDescriptor>>& device) const;
97 
98 private:
99     std::shared_ptr<AudioManagerCallbackAdapterImpl> callback_;
100     std::shared_ptr<AudioManagerDeviceChangeCallbackAdapterImpl> deviceChangeCallback_ = nullptr;
101 #endif
102 };
103 } // namespace OHOS::NWeb
104 
105 #endif // AUDIO_SYSTEM_MANAGER_ADAPTER_IMPL_H
106