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 #ifndef LOG_TAG
16 #define LOG_TAG "OfflineStreamInServer"
17 #endif
18
19 #include "offline_stream_in_server.h"
20 #include "audio_service_log.h"
21 #include "audio_errors.h"
22 #include "audio_utils.h"
23
24 namespace OHOS {
25 namespace AudioStandard {
26 namespace {
27 static const std::string OFFLINE_SERVER_BUFFER_IN = "offline_server_buffer_in";
28 static const std::string OFFLINE_SERVER_BUFFER_OUT = "offline_server_buffer_out";
29 static constexpr int32_t MAXIMUM_BUFFER_SIZE = 1000000; // 1,000,000
30 }
31 // static method
GetOfflineStream(int32_t & errCode)32 sptr<OfflineStreamInServer> OfflineStreamInServer::GetOfflineStream(int32_t &errCode)
33 {
34 sptr<OfflineStreamInServer> streamInServer = sptr<OfflineStreamInServer>::MakeSptr();
35 CHECK_AND_RETURN_RET_LOG(streamInServer, nullptr, "Create OfflineStream failed, errCode: %{public}d", errCode);
36 return streamInServer;
37 }
38
39 // static method
40 #ifdef FEATURE_OFFLINE_EFFECT
GetOfflineAudioEffectChains(std::vector<std::string> & effectChains)41 int32_t OfflineStreamInServer::GetOfflineAudioEffectChains(std::vector<std::string> &effectChains)
42 {
43 return OfflineAudioEffectServerChain::GetOfflineAudioEffectChains(effectChains);
44 }
45
CreateOfflineEffectChain(const std::string & chainName)46 int32_t OfflineStreamInServer::CreateOfflineEffectChain(const std::string &chainName)
47 {
48 effectChain_ = std::make_shared<OfflineAudioEffectServerChain>(chainName);
49 return effectChain_->Create();
50 }
51
ConfigureOfflineEffectChain(const AudioStreamInfo & inInfo,const AudioStreamInfo & outInfo)52 int32_t OfflineStreamInServer::ConfigureOfflineEffectChain(const AudioStreamInfo &inInfo,
53 const AudioStreamInfo &outInfo)
54 {
55 CHECK_AND_RETURN_RET_LOG(CheckSupportedParams(inInfo) == SUCCESS, ERR_INVALID_PARAM, "inInfo do not support");
56 CHECK_AND_RETURN_RET_LOG(CheckSupportedParams(outInfo) == SUCCESS, ERR_INVALID_PARAM, "outInfo do not support");
57 CHECK_AND_RETURN_RET_LOG(effectChain_, ERR_ILLEGAL_STATE, "effectChain not init");
58 return effectChain_->SetConfig(inInfo, outInfo);
59 }
60
SetParamOfflineEffectChain(const std::vector<uint8_t> & param)61 int32_t OfflineStreamInServer::SetParamOfflineEffectChain(const std::vector<uint8_t> ¶m)
62 {
63 CHECK_AND_RETURN_RET_LOG(effectChain_, ERR_ILLEGAL_STATE, "effectChain not init");
64 return effectChain_->SetParam(param);
65 }
66
PrepareOfflineEffectChain(std::shared_ptr<AudioSharedMemory> & inBuffer,std::shared_ptr<AudioSharedMemory> & outBuffer)67 int32_t OfflineStreamInServer::PrepareOfflineEffectChain(std::shared_ptr<AudioSharedMemory> &inBuffer,
68 std::shared_ptr<AudioSharedMemory> &outBuffer)
69 {
70 CHECK_AND_RETURN_RET_LOG(effectChain_, ERR_ILLEGAL_STATE, "effectChain not init");
71 if (serverBufferIn_ == nullptr || serverBufferOut_ == nullptr) {
72 uint32_t inSize = 0;
73 uint32_t outSize = 0;
74 effectChain_->GetEffectBufferSize(inSize, outSize);
75 CHECK_AND_RETURN_RET_LOG(AllocSharedMemory(inSize, outSize) == SUCCESS, ERR_OPERATION_FAILED,
76 "AllocSharedMemory failed");
77 }
78 inBuffer = serverBufferIn_;
79 outBuffer = serverBufferOut_;
80 return effectChain_->Prepare(serverBufferIn_, serverBufferOut_);
81 }
82
ProcessOfflineEffectChain(uint32_t inSize,uint32_t outSize)83 int32_t OfflineStreamInServer::ProcessOfflineEffectChain(uint32_t inSize, uint32_t outSize)
84 {
85 CHECK_AND_RETURN_RET_LOG(effectChain_, ERR_ILLEGAL_STATE, "effectChain not init");
86 return effectChain_->Process(inSize, outSize);
87 }
88
ReleaseOfflineEffectChain()89 int32_t OfflineStreamInServer::ReleaseOfflineEffectChain()
90 {
91 CHECK_AND_RETURN_RET_LOG(effectChain_, ERR_ILLEGAL_STATE, "effectChain not init");
92 effectChain_->Release();
93 return SUCCESS;
94 }
95 #endif
96
AllocSharedMemory(uint32_t inSize,uint32_t outSize)97 int32_t OfflineStreamInServer::AllocSharedMemory(uint32_t inSize, uint32_t outSize)
98 {
99 CHECK_AND_RETURN_RET_LOG(inSize < MAXIMUM_BUFFER_SIZE && outSize < MAXIMUM_BUFFER_SIZE,
100 ERR_INVALID_PARAM, "alloc %{public}u inBuf or %{public}u outBuf out of range", inSize, outSize);
101 serverBufferIn_ = AudioSharedMemory::CreateFormLocal(inSize, OFFLINE_SERVER_BUFFER_IN);
102 CHECK_AND_RETURN_RET_LOG(serverBufferIn_ != nullptr, ERR_OPERATION_FAILED, "serverBufferIn_ mmap failed!");
103 serverBufferOut_ = AudioSharedMemory::CreateFormLocal(outSize, OFFLINE_SERVER_BUFFER_OUT);
104 CHECK_AND_RETURN_RET_LOG(serverBufferOut_ != nullptr, ERR_OPERATION_FAILED, "serverBufferOut_ mmap failed!");
105 return SUCCESS;
106 }
107 } // namespace AudioStandard
108 } // namespace OHOS
109