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 #define MLOG_TAG "MediaCallTranscode"
17
18 #include "media_asset_manager_callback.h"
19 #include "media_call_transcode.h"
20 #include "medialibrary_napi_log.h"
21 #include "medialibrary_errno.h"
22 #include "unique_fd.h"
23
24 namespace OHOS {
25 namespace Media {
26
27 static MediaCallTranscode::CallbackType callback_;
28 static std::mutex transCoderMapMutex_;
29 static std::map<std::string, std::shared_ptr<TransCoder>> transCoderMap_;
30 static const int32_t INFO_TYPE_ERROR = 2;
31
CallTranscodeHandle(napi_env env,int srcFd,int destFd,napi_value & result,off_t & size,std::string requestId)32 void MediaCallTranscode::CallTranscodeHandle(napi_env env, int srcFd, int destFd,
33 napi_value &result, off_t &size, std::string requestId)
34 {
35 NAPI_INFO_LOG("CallTranscodeHandle start");
36 bool ret = DoTranscode(srcFd, destFd, size, requestId, 0);
37 napi_get_boolean(env, ret, &result);
38 }
39
DoTranscode(int srcFd,int destFd,int64_t & size,std::string requestId,int64_t offset)40 bool MediaCallTranscode::DoTranscode(int srcFd, int destFd, int64_t &size, std::string requestId, int64_t offset)
41 {
42 UniqueFd uniqueSrcFd(srcFd);
43 UniqueFd uniqueDestFd(destFd);
44 auto transCoder = TransCoderFactory::CreateTransCoder();
45 if (transCoder == nullptr) {
46 NAPI_ERR_LOG("Failed to create TransCoder");
47 return false;
48 }
49 {
50 std::lock_guard<std::mutex> lock(transCoderMapMutex_);
51 transCoderMap_.insert(std::pair<std::string, std::shared_ptr<TransCoder>>(requestId, transCoder));
52 }
53 auto transCoderCb = std::make_shared<OHOS::Media::MediaAssetManagerCallback>();
54 if (transCoderCb == nullptr) {
55 NAPI_ERR_LOG("Failed to create TransCoder");
56 return false;
57 }
58 transCoderCb->SetRequestId(requestId);
59 if (transCoder->SetTransCoderCallback(transCoderCb) != E_OK) {
60 NAPI_ERR_LOG("Failed to set TransCoder callback");
61 return false;
62 }
63 if (transCoder->SetInputFile(uniqueSrcFd.Get(), offset, size) != E_OK) {
64 NAPI_ERR_LOG("Failed to set input file for TransCoder");
65 return false;
66 }
67 if (transCoder->SetOutputFile(uniqueDestFd.Get()) != E_OK) {
68 NAPI_ERR_LOG("Failed to set output file for TransCoder");
69 return false;
70 }
71 if (transCoder->SetOutputFormat(FORMAT_MPEG_4) != E_OK) {
72 NAPI_ERR_LOG("Failed to SetOutputFormat");
73 return false;
74 }
75 if (transCoder->SetColorSpace(TRANSCODER_COLORSPACE_BT709_LIMIT) != E_OK) {
76 NAPI_ERR_LOG("Failed to SetColorSpace");
77 return false;
78 }
79 if (transCoder->Prepare() != E_OK) {
80 NAPI_ERR_LOG("Failed to prepare TransCoder");
81 return false;
82 }
83 if (transCoder->Start() != E_OK) {
84 NAPI_ERR_LOG("Failed to TransCoder Start");
85 return false;
86 }
87 NAPI_INFO_LOG("DoTranscode success requestId:%{public}s", requestId.c_str());
88 return true;
89 }
90
CallTranscodeRelease(const std::string & requestId)91 void MediaCallTranscode::CallTranscodeRelease(const std::string& requestId)
92 {
93 std::lock_guard<std::mutex> lock(transCoderMapMutex_);
94 auto tcm = transCoderMap_.find(requestId);
95 if (tcm == transCoderMap_.end()) {
96 return;
97 }
98 tcm->second->Release();
99 transCoderMap_.erase(tcm);
100 }
101
RegisterCallback(const CallbackType & cb)102 void MediaCallTranscode::RegisterCallback(const CallbackType &cb)
103 {
104 callback_ = cb;
105 }
106
OnInfo(int32_t type,int32_t extra)107 void MediaAssetManagerCallback::OnInfo(int32_t type, int32_t extra)
108 {
109 NAPI_INFO_LOG("MediaAssetManagerCallback OnInfo type:%{public}d extra:%{public}d", type, extra);
110 if (callback_) {
111 callback_(type, extra, requestId_);
112 }
113 }
114
OnError(int32_t errCode,const std::string & errorMsg)115 void MediaAssetManagerCallback::OnError(int32_t errCode, const std::string &errorMsg)
116 {
117 std::lock_guard<std::mutex> lock(transCoderMapMutex_);
118 auto tcm = transCoderMap_.find(requestId_);
119 if (tcm != transCoderMap_.end()) {
120 transCoderMap_.erase(tcm);
121 }
122 NAPI_ERR_LOG("MediaAssetManagerCallback OnInfo errorMsg:%{public}s", errorMsg.c_str());
123 int32_t type = INFO_TYPE_ERROR;
124 if (callback_) {
125 callback_(type, 0, requestId_);
126 }
127 }
128
SetRequestId(std::string requestId)129 void MediaAssetManagerCallback::SetRequestId(std::string requestId)
130 {
131 requestId_ = requestId;
132 }
133 } // namespace Media
134 } // namespace OHOS
135