1 /*
2 * Copyright (c) 2021 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 "audio_transport_context.h"
17 #include "audio_transport_status_factory.h"
18
19 #include "daudio_errorcode.h"
20 #include "daudio_log.h"
21
22 #undef DH_LOG_TAG
23 #define DH_LOG_TAG "AudioTransportContext"
24
25 namespace OHOS {
26 namespace DistributedHardware {
SetTransportStatus(TransportStateType stateType)27 void AudioTransportContext::SetTransportStatus(TransportStateType stateType)
28 {
29 DHLOGD("Set transport status in state %d", stateType);
30 auto stateContext = std::shared_ptr<AudioTransportContext>(shared_from_this());
31 currentState_ = AudioTransportStatusFactory::GetInstance().CreateState(stateType, stateContext);
32 }
33
GetTransportStatusType()34 int32_t AudioTransportContext::GetTransportStatusType()
35 {
36 if (currentState_ == nullptr) {
37 DHLOGI("CurrentState is nullptr.");
38 return ERR_DH_AUDIO_NULLPTR;
39 }
40 DHLOGI("Get transport status in state %d", currentState_->GetStateType());
41 return currentState_->GetStateType();
42 }
43
Start()44 int32_t AudioTransportContext::Start()
45 {
46 if (currentState_ == nullptr) {
47 DHLOGD("CurrentState is nullptr.");
48 return ERR_DH_AUDIO_NULLPTR;
49 }
50 DHLOGI("Audio transport context start.");
51 return currentState_->Start(audioChannel_, processor_);
52 }
53
Stop()54 int32_t AudioTransportContext::Stop()
55 {
56 if (currentState_ == nullptr) {
57 DHLOGD("CurrentState is nullptr.");
58 return ERR_DH_AUDIO_NULLPTR;
59 }
60 DHLOGI("Audio transport context stop.");
61 return currentState_->Stop(audioChannel_, processor_);
62 }
63
Pause()64 int32_t AudioTransportContext::Pause()
65 {
66 if (currentState_ == nullptr) {
67 DHLOGD("CurrentState is nullptr.");
68 return ERR_DH_AUDIO_NULLPTR;
69 }
70 DHLOGI("Audio transport context pause.");
71 return currentState_->Pause(processor_);
72 }
73
Restart(const AudioParam & localParam,const AudioParam & remoteParam)74 int32_t AudioTransportContext::Restart(const AudioParam &localParam, const AudioParam &remoteParam)
75 {
76 if (currentState_ == nullptr) {
77 DHLOGD("CurrentState is nullptr.");
78 return ERR_DH_AUDIO_NULLPTR;
79 }
80 DHLOGI("Audio transport context restart.");
81 return currentState_->Restart(localParam, remoteParam, processor_);
82 }
83
SetAudioChannel(std::shared_ptr<IAudioChannel> & audioChannel)84 void AudioTransportContext::SetAudioChannel(std::shared_ptr<IAudioChannel> &audioChannel)
85 {
86 audioChannel_ = audioChannel;
87 }
88
SetAudioProcessor(std::shared_ptr<IAudioProcessor> & processor)89 void AudioTransportContext::SetAudioProcessor(std::shared_ptr<IAudioProcessor> &processor)
90 {
91 processor_ = processor;
92 }
93 } // namespace DistributedHardware
94 } // namespace OHOS
95