• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 OHOS_DAUDIO_SOURCE_DEV_H
17 #define OHOS_DAUDIO_SOURCE_DEV_H
18 
19 #include <mutex>
20 #include <initializer_list>
21 #include "nlohmann/json.hpp"
22 
23 #include "event_handler.h"
24 
25 #include "audio_event.h"
26 #include "daudio_source_dev_ctrl_manager.h"
27 #include "daudio_source_mgr_callback.h"
28 #include "dmic_dev.h"
29 #include "dspeaker_dev.h"
30 #include "iaudio_event_callback.h"
31 #include "iaudio_data_transport.h"
32 #include "iaudio_datatrans_callback.h"
33 #include "idaudio_ipc_callback.h"
34 #include "idaudio_hdi_callback.h"
35 
36 using json = nlohmann::json;
37 
38 namespace OHOS {
39 namespace DistributedHardware {
40 class DAudioSourceDev : public IAudioEventCallback, public std::enable_shared_from_this<DAudioSourceDev> {
41 public:
42     DAudioSourceDev(const std::string &devId, const std::shared_ptr<DAudioSourceMgrCallback> &callback);
43     ~DAudioSourceDev() override = default;
44 
45     int32_t AwakeAudioDev();
46     void SleepAudioDev();
47 
48     int32_t EnableDAudio(const std::string &dhId, const std::string &attrs);
49     int32_t DisableDAudio(const std::string &dhId);
50     void NotifyEvent(const AudioEvent &event) override;
51 
52 private:
53     int32_t EnableDSpeaker(const int32_t dhId, const std::string &attrs);
54     int32_t EnableDMic(const int32_t dhId, const std::string &attrs);
55     int32_t DisableDSpeaker(const int32_t dhId);
56     int32_t DisableDMic(const int32_t dhId);
57 
58     int32_t TaskEnableDAudio(const std::string &args);
59     int32_t TaskDisableDAudio(const std::string &args);
60     int32_t TaskOpenCtrlChannel(const std::string &args);
61     int32_t TaskCloseCtrlChannel(const std::string &args);
62     int32_t TaskOpenDSpeaker(const std::string &args);
63     int32_t TaskCloseDSpeaker(const std::string &args);
64     int32_t TaskOpenDMic(const std::string &args);
65     int32_t TaskCloseDMic(const std::string &args);
66     int32_t TaskSetVolume(const std::string &args);
67     int32_t TaskChangeVolume(const std::string &args);
68     int32_t TaskChangeFocus(const std::string &args);
69     int32_t TaskChangeRenderState(const std::string &args);
70     int32_t TaskPlayStatusChange(const std::string &args);
71     int32_t TaskSpkMmapStart(const std::string &args);
72     int32_t TaskSpkMmapStop(const std::string &args);
73     int32_t TaskMicMmapStart(const std::string &args);
74     int32_t TaskMicMmapStop(const std::string &args);
75 
76     void OnDisableTaskResult(int32_t resultCode, const std::string &result, const std::string &funcName);
77     void OnEnableTaskResult(int32_t resultCode, const std::string &result, const std::string &funcName);
78     void OnTaskResult(int32_t resultCode, const std::string &result, const std::string &funcName);
79 
80     int32_t HandleOpenDSpeaker(const AudioEvent &event);
81     int32_t HandleCloseDSpeaker(const AudioEvent &event);
82     int32_t HandleDSpeakerOpened(const AudioEvent &event);
83     int32_t HandleDSpeakerClosed(const AudioEvent &event);
84     int32_t HandleOpenDMic(const AudioEvent &event);
85     int32_t HandleCloseDMic(const AudioEvent &event);
86     int32_t HandleDMicOpened(const AudioEvent &event);
87     int32_t HandleDMicClosed(const AudioEvent &event);
88     int32_t HandleOpenCtrlTrans(const AudioEvent &event);
89     int32_t HandleCloseCtrlTrans(const AudioEvent &event);
90     int32_t HandleCtrlTransClosed(const AudioEvent &event);
91     int32_t HandleNotifyRPC(const AudioEvent &event);
92     int32_t WaitForRPC(const AudioEventType type);
93     int32_t HandleVolumeSet(const AudioEvent &event);
94     int32_t HandleVolumeChange(const AudioEvent &event);
95     int32_t HandleFocusChange(const AudioEvent &event);
96     int32_t HandleRenderStateChange(const AudioEvent &event);
97     int32_t HandlePlayStatusChange(const AudioEvent &event);
98     int32_t HandleSpkMmapStart(const AudioEvent &event);
99     int32_t HandleSpkMmapStop(const AudioEvent &event);
100     int32_t HandleMicMmapStart(const AudioEvent &event);
101     int32_t HandleMicMmapStop(const AudioEvent &event);
102 
103     int32_t NotifySinkDev(const AudioEventType type, const json Param, const std::string dhId);
104     int32_t NotifyHDF(const AudioEventType type, const std::string result);
105     int32_t OpenCtrlTrans(const AudioEvent &event);
106     int32_t CloseCtrlTrans(const AudioEvent &event, bool isSpk);
107     AudioEventType getEventTypeFromArgs(const std::string &args);
108     void to_json(json &j, const AudioParam &param);
109     int32_t SendAudioEventToRemote(const AudioEvent &event);
110     int32_t CloseSpkOld(const std::string &args);
111     int32_t CloseSpkNew(const std::string &args);
112     int32_t CloseMicOld(const std::string &args);
113     int32_t CloseMicNew(const std::string &args);
114 
115 private:
116     static constexpr uint8_t RPC_WAIT_SECONDS = 2;
117     static constexpr uint8_t TASK_QUEUE_CAPACITY = 20;
118     static constexpr uint8_t EVENT_NOTIFY_OPEN_SPK = 0x01;
119     static constexpr uint8_t EVENT_NOTIFY_CLOSE_SPK = 0x02;
120     static constexpr uint8_t EVENT_NOTIFY_OPEN_MIC = 0x04;
121     static constexpr uint8_t EVENT_NOTIFY_CLOSE_MIC = 0x08;
122     static constexpr uint8_t EVENT_NOTIFY_OPEN_CTRL = 0x10;
123     static constexpr uint8_t EVENT_NOTIFY_CLOSE_CTRL = 0x20;
124 
125     std::string devId_;
126     std::shared_ptr<DAudioSourceMgrCallback> mgrCallback_;
127     std::shared_ptr<DSpeakerDev> speaker_;
128     std::shared_ptr<DMicDev> mic_;
129     std::shared_ptr<DAudioSourceDevCtrlMgr> audioCtrlMgr_;
130 
131     std::mutex rpcWaitMutex_;
132     std::condition_variable rpcWaitCond_;
133     std::atomic<bool> isRpcOpen_ = false;
134     bool rpcResult_ = false;
135     uint8_t rpcNotify_ = 0;
136 
137     class SourceEventHandler : public AppExecFwk::EventHandler {
138     public:
139         SourceEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> &runner,
140             const std::shared_ptr<DAudioSourceDev> &dev);
141         ~SourceEventHandler() override;
142         void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override;
143 
144     private:
145         void EnableDAudioCallback(const AppExecFwk::InnerEvent::Pointer &event);
146         void DisableDAudioCallback(const AppExecFwk::InnerEvent::Pointer &event);
147         void OpenDSpeakerCallback(const AppExecFwk::InnerEvent::Pointer &event);
148         void CloseDSpeakerCallback(const AppExecFwk::InnerEvent::Pointer &event);
149         void OpenDMicCallback(const AppExecFwk::InnerEvent::Pointer &event);
150         void CloseDMicCallback(const AppExecFwk::InnerEvent::Pointer &event);
151         void OpenCtrlCallback(const AppExecFwk::InnerEvent::Pointer &event);
152         void CloseCtrlCallback(const AppExecFwk::InnerEvent::Pointer &event);
153         void SetVolumeCallback(const AppExecFwk::InnerEvent::Pointer &event);
154         void ChangeVolumeCallback(const AppExecFwk::InnerEvent::Pointer &event);
155         void ChangeFocusCallback(const AppExecFwk::InnerEvent::Pointer &event);
156         void ChangeRenderStateCallback(const AppExecFwk::InnerEvent::Pointer &event);
157         void PlayStatusChangeCallback(const AppExecFwk::InnerEvent::Pointer &event);
158         void SpkMmapStartCallback(const AppExecFwk::InnerEvent::Pointer &event);
159         void SpkMmapStopCallback(const AppExecFwk::InnerEvent::Pointer &event);
160         void MicMmapStartCallback(const AppExecFwk::InnerEvent::Pointer &event);
161         void MicMmapStopCallback(const AppExecFwk::InnerEvent::Pointer &event);
162         int32_t GetEventParam(const AppExecFwk::InnerEvent::Pointer &event, std::string &eventParam);
163 
164     private:
165         using SourceEventFunc = void (SourceEventHandler::*)(const AppExecFwk::InnerEvent::Pointer &event);
166         std::map<uint32_t, SourceEventFunc> mapEventFuncs_;
167         std::weak_ptr<DAudioSourceDev> sourceDev_;
168     };
169 
170     using DAudioSourceDevFunc = int32_t (DAudioSourceDev::*)(const AudioEvent &audioEvent);
171     std::map<AudioEventType, DAudioSourceDevFunc> memberFuncMap_;
172     std::map<AudioEventType, uint8_t> eventNotifyMap_;
173     std::shared_ptr<SourceEventHandler> handler_;
174 };
175 } // DistributedHardware
176 } // OHOS
177 #endif // OHOS_DAUDIO_SOURCE_DEV_H