• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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     inline bool operator==(const UsbAudioDevice &o) const
55     {
56         return usbAddr_ == o.usbAddr_;
57     }
58 };
59 
60 class AudioUsbManager {
61 public:
62     class EventSubscriber : public EventFwk::CommonEventSubscriber {
63     public:
EventSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo)64         explicit EventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo)
65             : EventFwk::CommonEventSubscriber(subscribeInfo) {}
66         void OnReceiveEvent(const EventFwk::CommonEventData &data) override;
67     };
68 
69     static AudioUsbManager& GetInstance();
70     static map<UsbAddr, SoundCard> GetUsbSoundCardMap();
71     static vector<UsbAudioDevice> GetUsbAudioDevices();
72     static vector<UsbAudioDevice> GetPlayerDevices();
73     static vector<UsbAudioDevice> GetCapturerDevices();
74 
75     void Init(IDeviceStatusObserver *observer);
76     void Deinit();
77     void SubscribeEvent();
78 
79 private:
80     void HandleUsbAudioDeviceAttach(const UsbAudioDevice &device);
81     void HandleUsbAudioDeviceDetach(const UsbAudioDevice &device);
82     void RefreshUsbAudioDevices();
83     void NotifyDevice(const UsbAudioDevice &device, const bool isConnected);
84 
85     vector<UsbAudioDevice> audioDevices_;
86     map<UsbAddr, SoundCard> soundCardMap_;
87     shared_ptr<EventSubscriber> eventSubscriber_{nullptr};
88     bool initialized_{false};
89 
90     mutex mutex_;
91     IDeviceStatusObserver *observer_{nullptr};
92 };
93 
94 } // namespace AudioStandard
95 } // namespace OHOS
96 
97 #endif