• 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 
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     ctrlTransCallback_.reset();
45     return DH_SUCCESS;
46 }
47 
Start()48 int32_t DaudioSinkCtrlTrans::Start()
49 {
50     DHLOGI("Start.");
51     return DH_SUCCESS;
52 }
53 
Stop()54 int32_t DaudioSinkCtrlTrans::Stop()
55 {
56     DHLOGI("Stop.");
57     return DH_SUCCESS;
58 }
59 
SendAudioEvent(uint32_t type,const std::string & content,const std::string & dstDevId)60 int32_t DaudioSinkCtrlTrans::SendAudioEvent(uint32_t type, const std::string &content, const std::string &dstDevId)
61 {
62     DHLOGI("SendAudioEvent, type: %{public}u, content: %{public}s.", type, content.c_str());
63     auto message = std::make_shared<AVTransMessage>(type, content, dstDevId);
64     std::string msgData = message->MarshalMessage();
65     return SoftbusChannelAdapter::GetInstance().SendBytesData(sessionName_, message->dstDevId_, msgData);
66 }
67 
OnChannelEvent(const AVTransEvent & event)68 void DaudioSinkCtrlTrans::OnChannelEvent(const AVTransEvent &event)
69 {
70     DHLOGI("OnChannelEvent, type: %{public}d", event.type);
71     auto sourceDevObj = ctrlTransCallback_.lock();
72     CHECK_NULL_VOID(sourceDevObj);
73     switch (event.type) {
74         case EventType::EVENT_CHANNEL_OPEN_FAIL:
75         case EventType::EVENT_CHANNEL_OPENED:
76         case EventType::EVENT_CHANNEL_CLOSED:
77         case EventType::EVENT_START_FAIL:
78         case EventType::EVENT_START_SUCCESS:
79         case EventType::EVENT_STOP_SUCCESS:
80         case EventType::EVENT_ENGINE_ERROR:
81         case EventType::EVENT_REMOTE_ERROR:
82             sourceDevObj->OnCtrlTransEvent(event);
83             break;
84         case EventType::EVENT_DATA_RECEIVED: {
85             auto avMessage = std::make_shared<AVTransMessage>();
86             CHECK_AND_RETURN_LOG(!avMessage->UnmarshalMessage(event.content,
87                 event.peerDevId), "unmarshal message failed");
88             sourceDevObj->OnCtrlTransMessage(avMessage);
89             break;
90         }
91         default:
92             DHLOGE("Invaild event type.");
93             break;
94     }
95 }
96 
OnStreamReceived(const StreamData * data,const StreamData * ext)97 void DaudioSinkCtrlTrans::OnStreamReceived(const StreamData *data, const StreamData *ext)
98 {
99     (void)data;
100     (void)ext;
101 }
102 } // namespace DistributedHardware
103 } // namespace OHOS