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