• 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 #include "daudio_source_dev_ctrl_manager.h"
17 
18 #include "audio_ctrl_transport.h"
19 #include "audio_param.h"
20 #include "daudio_constants.h"
21 #include "daudio_errorcode.h"
22 #include "daudio_log.h"
23 #include "daudio_util.h"
24 
25 #undef DH_LOG_TAG
26 #define DH_LOG_TAG "DAudioSourceDevCtrlMgr"
27 
28 namespace OHOS {
29 namespace DistributedHardware {
DAudioSourceDevCtrlMgr(const std::string & devId,std::shared_ptr<IAudioEventCallback> audioEventCallback)30 DAudioSourceDevCtrlMgr::DAudioSourceDevCtrlMgr(const std::string &devId,
31     std::shared_ptr<IAudioEventCallback> audioEventCallback)
32     : devId_(devId), audioEventCallback_(audioEventCallback)
33 {
34     DHLOGD("Control manager constructed.");
35 }
36 
~DAudioSourceDevCtrlMgr()37 DAudioSourceDevCtrlMgr::~DAudioSourceDevCtrlMgr()
38 {
39     DHLOGD("Control manager deconstructed.");
40 }
41 
SetUp()42 int32_t DAudioSourceDevCtrlMgr::SetUp()
43 {
44     DHLOGI("Set up source device control manager.");
45     if (audioCtrlTrans_ == nullptr) {
46         audioCtrlTrans_ = std::make_shared<AudioCtrlTransport>(devId_);
47     }
48     int32_t ret = audioCtrlTrans_->SetUp(shared_from_this());
49     if (ret != DH_SUCCESS) {
50         DHLOGE("Set up ctrl trans failed, ret: %d.", ret);
51         return ret;
52     }
53     return DH_SUCCESS;
54 }
55 
Start()56 int32_t DAudioSourceDevCtrlMgr::Start()
57 {
58     DHLOGI("Start source device control manager.");
59     if (audioCtrlTrans_ == nullptr) {
60         DHLOGE("Audio ctrl trans is null, start failed");
61         return ERR_DH_AUDIO_SA_CTRL_TRANS_NULL;
62     }
63 
64     int32_t ret = audioCtrlTrans_->Start();
65     if (ret != DH_SUCCESS) {
66         DHLOGE("Audio ctrl trans start failed, ret:%d.", ret);
67         return ret;
68     }
69 
70     std::unique_lock<std::mutex> lck(channelWaitMutex_);
71     auto status = channelWaitCond_.wait_for(lck, std::chrono::seconds(CHANNEL_WAIT_SECONDS),
72         [this]() { return isOpened_.load(); });
73     if (!status) {
74         DHLOGE("Wait channel open timeout(%ds).", CHANNEL_WAIT_SECONDS);
75         return ERR_DH_AUDIO_SA_CTRL_CHANNEL_WAIT_TIMEOUT;
76     }
77 
78     return DH_SUCCESS;
79 }
80 
Stop()81 int32_t DAudioSourceDevCtrlMgr::Stop()
82 {
83     isOpened_.store(false);
84     if (audioCtrlTrans_ == nullptr) {
85         DHLOGE("Audio ctrl trans is null, no need stop");
86         return DH_SUCCESS;
87     }
88 
89     return audioCtrlTrans_->Stop();
90 }
91 
Release()92 int32_t DAudioSourceDevCtrlMgr::Release()
93 {
94     DHLOGI("Release source device control manager.");
95     if (audioCtrlTrans_ == nullptr) {
96         DHLOGE("Audio ctrl trans is null, no need release.");
97         return DH_SUCCESS;
98     }
99 
100     int32_t ret = audioCtrlTrans_->Release();
101     if (ret != DH_SUCCESS) {
102         DHLOGE("Release audio ctrl failed.");
103         return ret;
104     }
105     audioCtrlTrans_ = nullptr;
106     return DH_SUCCESS;
107 }
108 
IsOpened()109 bool DAudioSourceDevCtrlMgr::IsOpened()
110 {
111     return isOpened_.load();
112 }
113 
SendAudioEvent(const AudioEvent & event)114 int32_t DAudioSourceDevCtrlMgr::SendAudioEvent(const AudioEvent &event)
115 {
116     DHLOGD("Send audio event.");
117     if (audioCtrlTrans_ == nullptr) {
118         DHLOGE("Send audio event, Audio ctrl trans is null");
119         return ERR_DH_AUDIO_SA_CTRL_TRANS_NULL;
120     }
121     return audioCtrlTrans_->SendAudioEvent(event);
122 }
123 
OnStateChange(int32_t type)124 void DAudioSourceDevCtrlMgr::OnStateChange(int32_t type)
125 {
126     DHLOGD("Daudio source control state change, type: %d.", type);
127     switch (type) {
128         case AudioEventType::CTRL_OPENED:
129             isOpened_.store(true);
130             channelWaitCond_.notify_one();
131             break;
132         case AudioEventType::CTRL_CLOSED:
133             isOpened_.store(false);
134             break;
135         default:
136             DHLOGE("Invalid parameter type, type: %d.", type);
137             return;
138     }
139 
140     auto callback = audioEventCallback_.lock();
141     if (callback == nullptr) {
142         DHLOGE("Callback is nullptr.");
143         return;
144     }
145     AudioEvent event(static_cast<AudioEventType>(type), "");
146     callback->NotifyEvent(event);
147 }
148 
OnEventReceived(const AudioEvent & event)149 void DAudioSourceDevCtrlMgr::OnEventReceived(const AudioEvent &event)
150 {
151     DHLOGD("Received event");
152     auto callback = audioEventCallback_.lock();
153     if (callback == nullptr) {
154         DHLOGE("Callback is nullptr.");
155         return;
156     }
157     callback->NotifyEvent(event);
158 }
159 } // namespace DistributedHardware
160 } // namespace OHOS
161