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
16 #include "daudio_sink_ctrl_trans.h"
17
18 #include <dlfcn.h>
19
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 "SinkCtrlTrans"
27
28 namespace OHOS {
29 namespace DistributedHardware {
SetUp(const std::shared_ptr<IAudioCtrlTransCallback> & callback)30 int32_t DaudioSinkCtrlTrans::SetUp(const std::shared_ptr<IAudioCtrlTransCallback> &callback)
31 {
32 DHLOGI("SetUp.");
33 CHECK_NULL_RETURN(callback, ERR_DH_AUDIO_NULLPTR);
34 ctrlTransCallback_ = callback;
35 SoftbusChannelAdapter::GetInstance().RegisterChannelListener(sessionName_, devId_, this);
36 return DH_SUCCESS;
37 }
38
Release()39 int32_t DaudioSinkCtrlTrans::Release()
40 {
41 DHLOGI("Release.");
42 SoftbusChannelAdapter::GetInstance().CloseSoftbusChannel(sessionName_, devId_);
43 SoftbusChannelAdapter::GetInstance().UnRegisterChannelListener(sessionName_, devId_);
44 return DH_SUCCESS;
45 }
46
Start()47 int32_t DaudioSinkCtrlTrans::Start()
48 {
49 DHLOGI("Start.");
50 return DH_SUCCESS;
51 }
52
Stop()53 int32_t DaudioSinkCtrlTrans::Stop()
54 {
55 DHLOGI("Stop.");
56 return DH_SUCCESS;
57 }
58
SendAudioEvent(uint32_t type,const std::string & content,const std::string & dstDevId)59 int32_t DaudioSinkCtrlTrans::SendAudioEvent(uint32_t type, const std::string &content, const std::string &dstDevId)
60 {
61 DHLOGI("SendAudioEvent, type: %{public}u, content: %{public}s.", type, content.c_str());
62 auto message = std::make_shared<AVTransMessage>(type, content, dstDevId);
63 std::string msgData = message->MarshalMessage();
64 return SoftbusChannelAdapter::GetInstance().SendBytesData(sessionName_, message->dstDevId_, msgData);
65 }
66
OnChannelEvent(const AVTransEvent & event)67 void DaudioSinkCtrlTrans::OnChannelEvent(const AVTransEvent &event)
68 {
69 DHLOGI("OnChannelEvent, type: %{public}d", event.type);
70 auto sourceDevObj = ctrlTransCallback_.lock();
71 CHECK_NULL_VOID(sourceDevObj);
72 switch (event.type) {
73 case EventType::EVENT_CHANNEL_OPEN_FAIL:
74 case EventType::EVENT_CHANNEL_OPENED:
75 case EventType::EVENT_CHANNEL_CLOSED:
76 case EventType::EVENT_START_FAIL:
77 case EventType::EVENT_START_SUCCESS:
78 case EventType::EVENT_STOP_SUCCESS:
79 case EventType::EVENT_ENGINE_ERROR:
80 case EventType::EVENT_REMOTE_ERROR:
81 sourceDevObj->OnCtrlTransEvent(event);
82 break;
83 case EventType::EVENT_DATA_RECEIVED: {
84 auto avMessage = std::make_shared<AVTransMessage>();
85 CHECK_AND_RETURN_LOG(!avMessage->UnmarshalMessage(event.content,
86 event.peerDevId), "unmarshal message failed");
87 sourceDevObj->OnCtrlTransMessage(avMessage);
88 break;
89 }
90 default:
91 DHLOGE("Invaild event type.");
92 break;
93 }
94 }
95
OnStreamReceived(const StreamData * data,const StreamData * ext)96 void DaudioSinkCtrlTrans::OnStreamReceived(const StreamData *data, const StreamData *ext)
97 {
98 (void)data;
99 (void)ext;
100 }
101 } // namespace DistributedHardware
102 } // namespace OHOS