1 /*
2 * Copyright (C) 2023 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 #include "native_avsource.h"
16 #include <unistd.h>
17 #include "avcodec_errors.h"
18 #include "avcodec_log.h"
19 #include "avsource.h"
20 #include "common/native_mfmagic.h"
21 #include "native_avformat.h"
22 #include "native_avmagic.h"
23 #include "native_object.h"
24
25 namespace {
26 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "NativeAVSource"};
27 }
28
29 using namespace OHOS::MediaAVCodec;
30
OH_AVSource_CreateWithURI(char * uri)31 struct OH_AVSource *OH_AVSource_CreateWithURI(char *uri)
32 {
33 CHECK_AND_RETURN_RET_LOG(uri != nullptr, nullptr, "Create source with uri failed because input uri is nullptr!");
34
35 std::shared_ptr<AVSource> source = AVSourceFactory::CreateWithURI(uri);
36 CHECK_AND_RETURN_RET_LOG(source != nullptr, nullptr, "New source with uri failed by AVSourceFactory!");
37
38 struct AVSourceObject *object = new(std::nothrow) AVSourceObject(source);
39 CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "New AVSourceObject failed when create source with uri!");
40
41 return object;
42 }
43
OH_AVSource_CreateWithFD(int32_t fd,int64_t offset,int64_t size)44 struct OH_AVSource *OH_AVSource_CreateWithFD(int32_t fd, int64_t offset, int64_t size)
45 {
46 CHECK_AND_RETURN_RET_LOG(fd > STDERR_FILENO, nullptr,
47 "Create source with fd failed because input fd is illegal, fd must be greater than 2!");
48 CHECK_AND_RETURN_RET_LOG(offset >= 0, nullptr,
49 "Create source with fd failed because input offset is negative");
50 CHECK_AND_RETURN_RET_LOG(size > 0, nullptr,
51 "Create source with fd failed because input size must be greater than zero");
52
53 std::shared_ptr<AVSource> source = AVSourceFactory::CreateWithFD(fd, offset, size);
54 CHECK_AND_RETURN_RET_LOG(source != nullptr, nullptr, "New source with fd failed by AVSourceFactory!");
55
56 struct AVSourceObject *object = new(std::nothrow) AVSourceObject(source);
57 CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "New AVSourceObject failed when create source with fd!");
58
59 return object;
60 }
61
OH_AVSource_Destroy(OH_AVSource * source)62 OH_AVErrCode OH_AVSource_Destroy(OH_AVSource *source)
63 {
64 CHECK_AND_RETURN_RET_LOG(source != nullptr, AV_ERR_INVALID_VAL,
65 "Destroy source failed because input source is nullptr!");
66 CHECK_AND_RETURN_RET_LOG(source->magic_ == AVMagic::AVCODEC_MAGIC_AVSOURCE, AV_ERR_INVALID_VAL, "magic error!");
67
68 delete source;
69 return AV_ERR_OK;
70 }
71
OH_AVSource_GetSourceFormat(OH_AVSource * source)72 OH_AVFormat *OH_AVSource_GetSourceFormat(OH_AVSource *source)
73 {
74 CHECK_AND_RETURN_RET_LOG(source != nullptr, nullptr, "Get source format failed because input source is nullptr!");
75 CHECK_AND_RETURN_RET_LOG(source->magic_ == AVMagic::AVCODEC_MAGIC_AVSOURCE, nullptr, "magic error!");
76
77 struct AVSourceObject *sourceObj = reinterpret_cast<AVSourceObject *>(source);
78 CHECK_AND_RETURN_RET_LOG(sourceObj->source_ != nullptr, nullptr,
79 "New AVSourceObject failed when get source format!");
80
81 Format format;
82 int32_t ret = sourceObj->source_->GetSourceFormat(format);
83 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "source_ GetSourceFormat failed!");
84
85 OH_AVFormat *avFormat = OH_AVFormat_Create();
86 avFormat->format_ = format;
87
88 return avFormat;
89 }
90
OH_AVSource_GetTrackFormat(OH_AVSource * source,uint32_t trackIndex)91 OH_AVFormat *OH_AVSource_GetTrackFormat(OH_AVSource *source, uint32_t trackIndex)
92 {
93 CHECK_AND_RETURN_RET_LOG(source != nullptr, nullptr, "Get format failed because input source is nullptr!");
94 CHECK_AND_RETURN_RET_LOG(source->magic_ == AVMagic::AVCODEC_MAGIC_AVSOURCE, nullptr, "magic error!");
95
96 struct AVSourceObject *sourceObj = reinterpret_cast<AVSourceObject *>(source);
97 CHECK_AND_RETURN_RET_LOG(sourceObj->source_ != nullptr, nullptr,
98 "New AVSourceObject failed when get track format!");
99
100 Format format;
101 int32_t ret = sourceObj->source_->GetTrackFormat(format, trackIndex);
102 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "Source GetTrackFormat failed!");
103
104 OH_AVFormat *avFormat = OH_AVFormat_Create();
105 avFormat->format_ = format;
106
107 return avFormat;
108 }