• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 SLE_AUDIO_DEVICE_MANAGER_H
17 #define SLE_AUDIO_DEVICE_MANAGER_H
18 
19 #include <unordered_set>
20 #include "audio_general_manager.h"
21 #include "audio_stream_descriptor.h"
22 #include "istandard_sle_audio_operation_callback.h"
23 
24 namespace OHOS {
25 namespace AudioStandard {
26 struct SleVolumeConfigInfo {
27     AudioVolumeType volumeType = STREAM_DEFAULT;
28     int32_t volumeLevel = -1;
29     bool isMute = false;
30 
31     SleVolumeConfigInfo() = default;
SleVolumeConfigInfoSleVolumeConfigInfo32     SleVolumeConfigInfo(AudioVolumeType volumeType, int32_t volume) : volumeType(volumeType), volumeLevel(volume) {}
33 };
34 
35 enum SleAudioStreamType : uint32_t {
36     SLE_AUDIO_STREAM_NONE = 0x00000000,
37     SLE_AUDIO_STREAM_UNDEFINED = 0x00000001,
38     SLE_AUDIO_STREAM_MUSIC = 0x00000002,
39     SLE_AUDIO_STREAM_VOICE_CALL = 0x00000004,
40     SLE_AUDIO_STREAM_VOICE_ASSISTANT = 0x00000008,
41     SLE_AUDIO_STREAM_RING = 0x00000010,
42     SLE_AUDIO_STREAM_VOIP = 0x00000020,
43     SLE_AUDIO_STREAM_GAME = 0x00000040,
44     SLE_AUDIO_STREAM_RECORD = 0x00000080,
45     SLE_AUDIO_STREAM_ALERT = 0x00000100,
46     SLE_AUDIO_STREAM_VIDEO = 0x00000200,
47     SLE_AUDIO_STREAM_GUID = 0x00000400,
48 };
49 
50 class SleAudioDeviceManager : public SleAudioOperationCallback {
51 public:
GetInstance()52     static SleAudioDeviceManager &GetInstance()
53     {
54         static SleAudioDeviceManager instance;
55         return instance;
56     }
57 
58     int32_t SetSleAudioOperationCallback(const sptr<IStandardSleAudioOperationCallback> &callback);
59 
60     // Callback Interface Implementations
61     void GetSleAudioDeviceList(std::vector<AudioDeviceDescriptor> &devices) override;
62     void GetSleVirtualAudioDeviceList(std::vector<AudioDeviceDescriptor> &devices) override;
63     bool IsInBandRingOpen(const std::string &device) const override;
64     uint32_t GetSupportStreamType(const std::string &device) const override;
65     int32_t SetActiveSinkDevice(const std::string &device, uint32_t streamType) override;
66     int32_t StartPlaying(const std::string &device, uint32_t streamType) override;
67     int32_t StopPlaying(const std::string &device, uint32_t streamType) override;
68     int32_t ConnectAllowedProfiles(const std::string &remoteAddr) const override;
69     int32_t SetDeviceAbsVolume(const std::string &remoteAddr, uint32_t volume, uint32_t streamType) override;
70     int32_t SendUserSelection(const std::string &device, uint32_t streamType) override;
71     int32_t GetRenderPosition(const std::string &device, uint32_t &delayValue) override;
72 
73     // Parameter Conversion Interface
74     int32_t SetActiveDevice(const AudioDeviceDescriptor &deviceDesc, StreamUsage streamUsage);
75     int32_t SetActiveDevice(const AudioDeviceDescriptor &deviceDesc, SourceType sourceType);
76     int32_t StartPlaying(const AudioDeviceDescriptor &deviceDesc, StreamUsage streamUsage);
77     int32_t StopPlaying(const AudioDeviceDescriptor &deviceDesc, StreamUsage streamUsage);
78     int32_t StartPlaying(const AudioDeviceDescriptor &deviceDesc, SourceType sourceType);
79     int32_t StopPlaying(const AudioDeviceDescriptor &deviceDesc, SourceType sourceType);
80     int32_t SendUserSelection(const AudioDeviceDescriptor &deviceDesc, StreamUsage streamUsage);
81     int32_t SendUserSelection(const AudioDeviceDescriptor &deviceDesc, SourceType sourceType);
82     int32_t SetDeviceAbsVolume(const std::string &device, AudioStreamType streamType, int32_t volume);
83 
84     // Core Device Management Methods
85     int32_t AddNearlinkDevice(const AudioDeviceDescriptor &deviceDesc);
86     int32_t RemoveNearlinkDevice(const AudioDeviceDescriptor &deviceDesc);
87     void UpdateSleStreamTypeCount(const std::shared_ptr<AudioStreamDescriptor> &streamDesc, bool isRemoved = false);
88     void ResetSleStreamTypeCount(const std::shared_ptr<AudioDeviceDescriptor> &deviceDesc);
89     std::unordered_map<uint32_t, std::unordered_set<uint32_t>> GetNearlinkStreamTypeMapByDevice(
90         const std::string &deviceAddr);
91 
92     // Devcice Volume Manager
93     int32_t SetNearlinkDeviceMute(const std::string &device, AudioStreamType streamType, bool isMute);
94     int32_t SetNearlinkDeviceVolumeLevel(const std::string &device, AudioStreamType streamType,
95         const int32_t volumeLevel);
96     int32_t GetVolumeLevelByVolumeType(AudioVolumeType volumeType, const AudioDeviceDescriptor &deviceDesc);
97 
98     uint32_t GetSleStreamTypeByStreamUsage(StreamUsage streamUsage) const;
99     uint32_t GetSleStreamTypeBySourceType(SourceType sourceType) const;
100     std::set<StreamUsage> GetStreamUsagesBySleStreamType(uint32_t streamType) const;
101     std::set<SourceType> GetSourceTypesBySleStreamType(uint32_t streamType) const;
102 private:
103     SleAudioDeviceManager() = default;
104     virtual ~SleAudioDeviceManager() = default;
105 
106     void UpdateStreamTypeMap(const std::string &deviceAddr, uint32_t streamType, uint32_t sessionId, bool isAdd);
107     bool IsNearlinkDevice(DeviceType deviceType);
108     bool IsMoveToNearlinkDevice(const std::shared_ptr<AudioStreamDescriptor> &streamDesc);
109     bool IsNearlinkMoveToOtherDevice(const std::shared_ptr<AudioStreamDescriptor> &streamDesc);
110 
111     sptr<IStandardSleAudioOperationCallback> callback_ = nullptr;
112 
113     std::mutex deviceVolumeConfigMutex_;
114     std::unordered_map<std::string, std::pair<SleVolumeConfigInfo, SleVolumeConfigInfo>> deviceVolumeConfigInfo_;
115 
116     std::mutex startedSleStreamTypeMutex_;
117     // Maps device MAC -> (stream type ->set of session IDs)
118     std::unordered_map<std::string, std::unordered_map<uint32_t, std::unordered_set<uint32_t>>> startedSleStreamType_;
119 };
120 } // namespace AudioStandard
121 } // namespace OHOS
122 #endif // SLE_AUDIO_DEVICE_MANAGER_H
123