1 /*
2 * Copyright (c) 2025 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 "cj_avimagegenerator.h"
17 #include "media_log.h"
18 #include "pixel_map_impl.h"
19
20 namespace {
21 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_METADATA, "CJAVImageGeneratorImpl" };
22 }
23
24 namespace OHOS {
25 namespace Media {
26
CJAVImageGeneratorImpl()27 CJAVImageGeneratorImpl::CJAVImageGeneratorImpl()
28 {
29 MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
30 }
31
Create()32 sptr<CJAVImageGeneratorImpl> CJAVImageGeneratorImpl::Create()
33 {
34 auto instance = FFI::FFIData::Create<CJAVImageGeneratorImpl>();
35 if (instance == nullptr) {
36 MEDIA_LOGE("Failed to new CJAVImageGeneratorImpl");
37 return nullptr;
38 }
39 instance->helper_ = AVMetadataHelperFactory::CreateAVMetadataHelper();
40 if (instance->helper_ == nullptr) {
41 MEDIA_LOGE("Failed to CreateMetadataHelper");
42 FFI::FFIData::Release(instance->GetID());
43 return nullptr;
44 }
45 return instance;
46 }
47
FetchFrameAtTime(int64_t timeUs,int32_t option,CPixelMapParams param)48 int64_t CJAVImageGeneratorImpl::FetchFrameAtTime(int64_t timeUs, int32_t option, CPixelMapParams param)
49 {
50 if (state_ != HelperState::HELPER_STATE_RUNNABLE) {
51 MEDIA_LOGE("Current state is not runnable, can't fetchFrame.");
52 return 0;
53 }
54 if (helper_ == nullptr) {
55 MEDIA_LOGE("helper_ is nullptr!");
56 return -1;
57 }
58 auto pixelMap = helper_->FetchFrameYuv(
59 timeUs, option, PixelMapParams{param.width, param.height, PixelFormat::RGBA_8888});
60 auto result = FFI::FFIData::Create<PixelMapImpl>(move(pixelMap));
61 if (result == nullptr) {
62 return 0;
63 }
64 return result->GetID();
65 }
66
SetAVFileDescriptor(CAVFileDescriptor file)67 int32_t CJAVImageGeneratorImpl::SetAVFileDescriptor(CAVFileDescriptor file)
68 {
69 fileDescriptor_.fd = file.fd;
70 fileDescriptor_.offset = file.offset;
71 fileDescriptor_.length = file.length;
72 MEDIA_LOGD("get fd argument, fd = %{public}d, offset = %{public}" PRIi64 ", size = %{public}" PRIi64 "",
73 fileDescriptor_.fd, fileDescriptor_.offset, fileDescriptor_.length);
74 if (helper_ == nullptr) {
75 MEDIA_LOGE("helper_ is nullptr!");
76 return -1;
77 }
78 auto res = helper_->SetSource(fileDescriptor_.fd, fileDescriptor_.offset, fileDescriptor_.length);
79 state_ = res == MSERR_OK ? HelperState::HELPER_STATE_RUNNABLE : HelperState::HELPER_ERROR;
80 return MSERR_OK;
81 }
82
GetAVFileDescriptor(CAVFileDescriptor * data)83 int32_t CJAVImageGeneratorImpl::GetAVFileDescriptor(CAVFileDescriptor* data)
84 {
85 if (data == nullptr) {
86 return MSERR_INVALID_VAL;
87 }
88 data->fd = fileDescriptor_.fd;
89 data->offset = fileDescriptor_.offset;
90 data->length = fileDescriptor_.length;
91 return MSERR_OK;
92 }
93
Release()94 void CJAVImageGeneratorImpl::Release()
95 {
96 if (state_ == HelperState::HELPER_STATE_RELEASED) {
97 MEDIA_LOGE("Has released once, can't release again.");
98 return;
99 }
100 if (helper_ == nullptr) {
101 MEDIA_LOGE("helper_ is nullptr!");
102 return;
103 }
104 helper_->Release();
105 }
106
107 } // namespace Media
108 } // namespace OHOS
109