1 /*
2  * Copyright (c) 2023 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 ST_AUDIO_PNP_SERVER_H
17 #define ST_AUDIO_PNP_SERVER_H
18 
19 #include <functional>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 #include <thread>
24 
25 #include "hdf_types.h"
26 #include "audio_info.h"
27 #include "audio_pnp_param.h"
28 
29 namespace OHOS {
30 namespace AudioStandard {
31 using namespace std;
32 class MicrophoneBlocked;
33 
34 class AudioPnpServer {
35 public:
GetAudioPnpServer()36     static AudioPnpServer& GetAudioPnpServer()
37     {
38         static AudioPnpServer audioPnpServer;
39         return audioPnpServer;
40     }
41     ~AudioPnpServer();
42     bool init(void);
43     int32_t RegisterPnpStatusListener(std::shared_ptr<AudioPnpDeviceChangeCallback> callback);
44     int32_t UnRegisterPnpStatusListener();
45     void OnPnpDeviceStatusChanged(const std::string &info);
46     void StopPnpServer();
47     friend class MicrophoneBlocked;
48     void OnMicrophoneBlocked(const std::string &info, AudioPnpServer &audioPnpServer);
49 
50 private:
51     std::string eventInfo_;
52     std::mutex pnpMutex_;
53     std::shared_ptr<AudioPnpDeviceChangeCallback> pnpCallback_ = nullptr;
54     std::unique_ptr<std::thread> socketThread_ = nullptr;
55     std::unique_ptr<std::thread> inputThread_ = nullptr;
56     void OpenAndReadWithSocket();
57     void OpenAndReadInput();
58     void DetectAudioDevice();
59 #ifdef AUDIO_DOUBLE_PNP_DETECT
60     void UpdateUsbHeadset();
61 #endif
62 };
63 
64 class MicrophoneBlocked {
65 public:
GetInstance()66     static MicrophoneBlocked &GetInstance()
67     {
68         static MicrophoneBlocked instance_;
69         return instance_;
70     }
MicrophoneBlocked()71     MicrophoneBlocked() {}
~MicrophoneBlocked()72     ~MicrophoneBlocked() {}
73     void OnMicrophoneBlocked(const std::string &info, AudioPnpServer &audioPnpServer);
74 };
75 
76 enum PnpEventType {
77     PNP_EVENT_DEVICE_ADD = 1,
78     PNP_EVENT_DEVICE_REMOVE = 2,
79     PNP_EVENT_LOAD_SUCCESS = 3,
80     PNP_EVENT_LOAD_FAILURE = 4,
81     PNP_EVENT_UNLOAD = 5,
82     PNP_EVENT_SERVICE_VALID = 7,
83     PNP_EVENT_SERVICE_INVALID  = 8,
84     PNP_EVENT_CAPTURE_THRESHOLD = 9,
85     PNP_EVENT_UNKNOWN = 10,
86     PNP_EVENT_MIC_BLOCKED = 11,
87     PNP_EVENT_MIC_UNBLOCKED = 12,
88 };
89 
90 enum PnpDeviceType {
91     PNP_DEVICE_LINEOUT = 1 << 0,
92     PNP_DEVICE_HEADPHONE = 1 << 1,
93     PNP_DEVICE_HEADSET = 1 << 2,
94     PNP_DEVICE_USB_HEADSET = 1 << 3,
95     PNP_DEVICE_USB_HEADPHONE = 1 << 4,
96     PNP_DEVICE_USBA_HEADSET = 1 << 5,
97     PNP_DEVICE_USBA_HEADPHONE = 1 << 6,
98     PNP_DEVICE_PRIMARY_DEVICE = 1 << 7,
99     PNP_DEVICE_USB_DEVICE = 1 << 8,
100     PNP_DEVICE_A2DP_DEVICE = 1 << 9,
101     PNP_DEVICE_HDMI_DEVICE = 1 << 10,
102     PNP_DEVICE_ADAPTER_DEVICE = 1 << 11,
103     PNP_DEVICE_DP_DEVICE = 1 << 12,
104     PNP_DEVICE_MIC = 1 << 13,
105     PNP_DEVICE_UNKNOWN,
106 };
107 
108 } // namespace AudioStandard
109 } // namespace OHOS
110 #endif // ST_AUDIO_PNP_SERVER_H