• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-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 #ifndef AUDIO_USB_MANAGER_H
16 #define AUDIO_USB_MANAGER_H
17 
18 #include <mutex>
19 #include <map>
20 #include <vector>
21 #include "common_event_subscriber.h"
22 #include "idevice_status_observer.h"
23 
24 namespace OHOS {
25 namespace AudioStandard {
26 
27 using namespace std;
28 
29 string EncUsbAddr(const string &src);
30 
31 struct UsbAddr {
32     uint8_t busNum_{0};
33     uint8_t devAddr_{0};
34     inline bool operator==(const UsbAddr &o) const
35     {
36         return busNum_ == o.busNum_ && devAddr_ == o.devAddr_;
37     }
38     inline bool operator<(const UsbAddr &o) const
39     {
40         return busNum_ < o.busNum_ || (busNum_ == o.busNum_ && devAddr_ < o.devAddr_);
41     }
42 };
43 
44 struct SoundCard {
45     uint32_t cardNum_{0};
46     string usbBus_;
47     bool isCapturer_{false};
48     bool isPlayer_{false};
49 };
50 
51 struct UsbAudioDevice {
52     UsbAddr usbAddr_;
53     string name_;
54     uint32_t cardNum_{0};
55     bool isCapturer_{false};
56     bool isPlayer_{false};
57     inline bool operator==(const UsbAudioDevice &o) const
58     {
59         return usbAddr_ == o.usbAddr_;
60     }
61 };
62 
63 class AudioUsbManager {
64 public:
65     class EventSubscriber : public EventFwk::CommonEventSubscriber {
66     public:
EventSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo)67         explicit EventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo)
68             : EventFwk::CommonEventSubscriber(subscribeInfo) {}
69         void OnReceiveEvent(const EventFwk::CommonEventData &data) override;
70     };
71 
72     static AudioUsbManager &GetInstance();
73     static map<UsbAddr, SoundCard> GetUsbSoundCardMap();
74     static int32_t GetUsbAudioDevices(vector<UsbAudioDevice> &result);
75 
76     void Init(std::shared_ptr<IDeviceStatusObserver> observer);
77     void Deinit();
78     void SubscribeEvent();
79 
80 private:
81     AudioUsbManager() = default;
82     void RefreshUsbAudioDevices();
83     void NotifyDevice(const UsbAudioDevice &device, const bool isConnected);
84     void HandleAudioDeviceEvent(pair<UsbAudioDevice, bool> &&p);
85     bool FillUsbAudioDevice(UsbAudioDevice &device);
86     // must be called in mutex_ lock
87     void UpdateDevice(const UsbAudioDevice &dev, std::__wrap_iter<UsbAudioDevice *> &it);
88 
89     std::shared_ptr<IDeviceStatusObserver> observer_{nullptr};
90     std::shared_ptr<EventSubscriber> eventSubscriber_{nullptr};
91     vector<UsbAudioDevice> audioDevices_;
92     map<UsbAddr, SoundCard> soundCardMap_;
93 
94     bool initialized_{false};
95     mutex mutex_;
96 };
97 
98 } // namespace AudioStandard
99 } // namespace OHOS
100 
101 #endif