• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include <memory>
17 #include "avdemuxer.h"
18 #include "native_avmagic.h"
19 #include "avcodec_errors.h"
20 #include "native_object.h"
21 #include "avcodec_log.h"
22 #include "av_common.h"
23 #include "native_avdemuxer.h"
24 
25 namespace {
26     constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "NativeAVDemuxer"};
27 }
28 
29 using namespace OHOS::MediaAVCodec;
30 
31 struct DemuxerObject : public OH_AVDemuxer {
DemuxerObjectDemuxerObject32     explicit DemuxerObject(const std::shared_ptr<AVDemuxer> &demuxer)
33         : OH_AVDemuxer(AVMagic::AVCODEC_MAGIC_AVDEMUXER), demuxer_(demuxer) {}
34     ~DemuxerObject() = default;
35 
36     const std::shared_ptr<AVDemuxer> demuxer_;
37 };
38 
OH_AVDemuxer_CreateWithSource(OH_AVSource * source)39 struct OH_AVDemuxer *OH_AVDemuxer_CreateWithSource(OH_AVSource *source)
40 {
41     CHECK_AND_RETURN_RET_LOG(source != nullptr, nullptr, "Create demuxer failed because input source is nullptr!");
42 
43     struct AVSourceObject *sourceObj = reinterpret_cast<AVSourceObject *>(source);
44     CHECK_AND_RETURN_RET_LOG(sourceObj != nullptr, nullptr,
45         "Create demuxer failed because new sourceObj is nullptr!");
46 
47     std::shared_ptr<AVDemuxer> demuxer = AVDemuxerFactory::CreateWithSource(sourceObj->source_);
48     CHECK_AND_RETURN_RET_LOG(demuxer != nullptr, nullptr, "New demuxer with source by AVDemuxerFactory failed!");
49 
50     struct DemuxerObject *object = new(std::nothrow) DemuxerObject(demuxer);
51     CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "New demuxerObject failed when create demuxer!");
52 
53     return object;
54 }
55 
OH_AVDemuxer_Destroy(OH_AVDemuxer * demuxer)56 OH_AVErrCode OH_AVDemuxer_Destroy(OH_AVDemuxer *demuxer)
57 {
58     CHECK_AND_RETURN_RET_LOG(demuxer != nullptr, AV_ERR_INVALID_VAL,
59         "Destroy demuxer failed because input demuxer is nullptr!");
60     CHECK_AND_RETURN_RET_LOG(demuxer->magic_ == AVMagic::AVCODEC_MAGIC_AVDEMUXER, AV_ERR_INVALID_VAL, "magic error!");
61 
62     delete demuxer;
63     return AV_ERR_OK;
64 }
65 
OH_AVDemuxer_SelectTrackByID(OH_AVDemuxer * demuxer,uint32_t trackIndex)66 OH_AVErrCode OH_AVDemuxer_SelectTrackByID(OH_AVDemuxer *demuxer, uint32_t trackIndex)
67 {
68     CHECK_AND_RETURN_RET_LOG(demuxer != nullptr, AV_ERR_INVALID_VAL,
69         "Select track failed because input demuxer is nullptr!");
70     CHECK_AND_RETURN_RET_LOG(demuxer->magic_ == AVMagic::AVCODEC_MAGIC_AVDEMUXER, AV_ERR_INVALID_VAL, "magic error!");
71 
72     struct DemuxerObject *demuxerObj = reinterpret_cast<DemuxerObject *>(demuxer);
73     CHECK_AND_RETURN_RET_LOG(demuxerObj->demuxer_ != nullptr, AV_ERR_INVALID_VAL,
74         "New DemuxerObject failed when select track!");
75 
76     int32_t ret = demuxerObj->demuxer_->SelectTrackByID(trackIndex);
77     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
78                              "demuxer_ SelectTrackByID failed!");
79 
80     return AV_ERR_OK;
81 }
82 
OH_AVDemuxer_UnselectTrackByID(OH_AVDemuxer * demuxer,uint32_t trackIndex)83 OH_AVErrCode OH_AVDemuxer_UnselectTrackByID(OH_AVDemuxer *demuxer, uint32_t trackIndex)
84 {
85     CHECK_AND_RETURN_RET_LOG(demuxer != nullptr, AV_ERR_INVALID_VAL,
86         "Unselect track failed because input demuxer is nullptr!");
87     CHECK_AND_RETURN_RET_LOG(demuxer->magic_ == AVMagic::AVCODEC_MAGIC_AVDEMUXER, AV_ERR_INVALID_VAL, "magic error!");
88 
89     struct DemuxerObject *demuxerObj = reinterpret_cast<DemuxerObject *>(demuxer);
90     CHECK_AND_RETURN_RET_LOG(demuxerObj->demuxer_ != nullptr, AV_ERR_INVALID_VAL,
91         "New DemuxerObject failed when unselect track!");
92 
93     int32_t ret = demuxerObj->demuxer_->UnselectTrackByID(trackIndex);
94     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
95                              "demuxer_ UnselectTrackByID failed!");
96 
97     return AV_ERR_OK;
98 }
99 
OH_AVDemuxer_ReadSample(OH_AVDemuxer * demuxer,uint32_t trackIndex,OH_AVMemory * sample,OH_AVCodecBufferAttr * info)100 OH_AVErrCode OH_AVDemuxer_ReadSample(OH_AVDemuxer *demuxer, uint32_t trackIndex,
101     OH_AVMemory *sample, OH_AVCodecBufferAttr *info)
102 {
103     CHECK_AND_RETURN_RET_LOG(demuxer != nullptr, AV_ERR_INVALID_VAL,
104         "Read sample failed because input demuxer is nullptr!");
105     CHECK_AND_RETURN_RET_LOG(demuxer->magic_ == AVMagic::AVCODEC_MAGIC_AVDEMUXER, AV_ERR_INVALID_VAL, "magic error!");
106 
107     CHECK_AND_RETURN_RET_LOG(sample != nullptr, AV_ERR_INVALID_VAL,
108         "Read sample failed because input sample is nullptr!");
109     CHECK_AND_RETURN_RET_LOG(info != nullptr, AV_ERR_INVALID_VAL,
110         "Read sample failed because input info is nullptr!");
111 
112     struct DemuxerObject *demuxerObj = reinterpret_cast<DemuxerObject *>(demuxer);
113     CHECK_AND_RETURN_RET_LOG(demuxerObj->demuxer_ != nullptr, AV_ERR_INVALID_VAL,
114         "New DemuxerObject failed when read sample!");
115 
116     struct AVCodecBufferInfo bufferInfoInner;
117     AVCodecBufferFlag bufferFlag = AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_NONE;
118     int32_t ret = demuxerObj->demuxer_->ReadSample(trackIndex, sample->memory_, bufferInfoInner, bufferFlag);
119     info->pts = bufferInfoInner.presentationTimeUs;
120     info->size = bufferInfoInner.size;
121     info->offset = bufferInfoInner.offset;
122     info->flags =static_cast<uint32_t>(bufferFlag);
123 
124     CHECK_AND_RETURN_RET_LOG(ret != AVCS_ERR_NO_MEMORY, AV_ERR_NO_MEMORY,
125         "demuxer_ ReadSample failed! sample size is too small to copy full frame data");
126     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
127                              "demuxer_ ReadSample failed!");
128 
129     return AV_ERR_OK;
130 }
131 
OH_AVDemuxer_SeekToTime(OH_AVDemuxer * demuxer,int64_t millisecond,OH_AVSeekMode mode)132 OH_AVErrCode OH_AVDemuxer_SeekToTime(OH_AVDemuxer *demuxer, int64_t millisecond, OH_AVSeekMode mode)
133 {
134     CHECK_AND_RETURN_RET_LOG(demuxer != nullptr, AV_ERR_INVALID_VAL, "Seek failed because input demuxer is nullptr!");
135     CHECK_AND_RETURN_RET_LOG(demuxer->magic_ == AVMagic::AVCODEC_MAGIC_AVDEMUXER, AV_ERR_INVALID_VAL, "magic error!");
136 
137     CHECK_AND_RETURN_RET_LOG(millisecond >= 0, AV_ERR_INVALID_VAL,
138         "Seek failed because input millisecond is negative!");
139 
140     struct DemuxerObject *demuxerObj = reinterpret_cast<DemuxerObject *>(demuxer);
141     CHECK_AND_RETURN_RET_LOG(demuxerObj->demuxer_ != nullptr, AV_ERR_INVALID_VAL,
142         "New DemuxerObject failed when seek!");
143 
144     int32_t ret = demuxerObj->demuxer_->SeekToTime(millisecond, static_cast<AVSeekMode>(mode));
145     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
146                              "demuxer_ SeekToTime failed!");
147 
148     return AV_ERR_OK;
149 }