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 "avdemuxer_impl.h"
17 #include <fcntl.h>
18 #include <unistd.h>
19 #include <functional>
20 #include <sys/types.h>
21 #include "securec.h"
22 #include "avcodec_log.h"
23 #include "avsharedmemorybase.h"
24 #include "avcodec_dfx.h"
25 #include "i_avcodec_service.h"
26 #include "avcodec_errors.h"
27
28 namespace {
29 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AVDemuxerImpl"};
30 }
31
32 namespace OHOS {
33 namespace MediaAVCodec {
CreateWithSource(std::shared_ptr<AVSource> source)34 std::shared_ptr<AVDemuxer> AVDemuxerFactory::CreateWithSource(std::shared_ptr<AVSource> source)
35 {
36 AVCodecTrace trace("AVDemuxerFactory::CreateWithSource");
37
38 std::shared_ptr<AVDemuxerImpl> demuxerImpl = std::make_shared<AVDemuxerImpl>();
39 CHECK_AND_RETURN_RET_LOG(demuxerImpl != nullptr, nullptr, "New AVDemuxerImpl failed");
40
41 int32_t ret = demuxerImpl->Init(source);
42 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "Init AVDemuxerImpl failed");
43
44 return demuxerImpl;
45 }
46
Init(std::shared_ptr<AVSource> source)47 int32_t AVDemuxerImpl::Init(std::shared_ptr<AVSource> source)
48 {
49 AVCodecTrace trace("AVDemuxer::Init");
50
51 CHECK_AND_RETURN_RET_LOG(source != nullptr, AVCS_ERR_INVALID_VAL,
52 "Init AVDemuxerImpl failed because source is nullptr");
53 AVCODEC_LOGI("Init AVDemuxerImpl for source %{private}s", source->sourceUri.c_str());
54
55 uintptr_t sourceAddr;
56 int32_t ret = source->GetSourceAddr(sourceAddr);
57 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_INVALID_OPERATION,
58 "Init AVDemuxerImpl failed because get source address failed");
59 sourceUri_ = source->sourceUri;
60
61 demuxerEngine_ = IDemuxerEngineFactory::CreateDemuxerEngine(sourceAddr);
62 CHECK_AND_RETURN_RET_LOG(demuxerEngine_ != nullptr, AVCS_ERR_INVALID_OPERATION, "Create demuxer engine failed");
63 return AVCS_ERR_OK;
64 }
65
AVDemuxerImpl()66 AVDemuxerImpl::AVDemuxerImpl()
67 {
68 AVCODEC_LOGD("AVDemuxerImpl:0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
69 }
70
~AVDemuxerImpl()71 AVDemuxerImpl::~AVDemuxerImpl()
72 {
73 AVCODEC_LOGI("Destroy AVDemuxerImpl for source %{private}s", sourceUri_.c_str());
74 if (demuxerEngine_ != nullptr) {
75 demuxerEngine_ = nullptr;
76 }
77 AVCODEC_LOGD("AVDemuxerImpl:0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
78 }
79
SelectTrackByID(uint32_t trackIndex)80 int32_t AVDemuxerImpl::SelectTrackByID(uint32_t trackIndex)
81 {
82 AVCodecTrace trace("AVDemuxer::SelectTrackByID");
83
84 AVCODEC_LOGI("select track: trackIndex=%{public}u", trackIndex);
85
86 CHECK_AND_RETURN_RET_LOG(demuxerEngine_ != nullptr, AVCS_ERR_INVALID_OPERATION, "Demuxer engine does not exist");
87 return demuxerEngine_->SelectTrackByID(trackIndex);
88 }
89
UnselectTrackByID(uint32_t trackIndex)90 int32_t AVDemuxerImpl::UnselectTrackByID(uint32_t trackIndex)
91 {
92 AVCodecTrace trace("AVDemuxer::UnselectTrackByID");
93
94 AVCODEC_LOGI("unselect track: trackIndex=%{public}u", trackIndex);
95
96 CHECK_AND_RETURN_RET_LOG(demuxerEngine_ != nullptr, AVCS_ERR_INVALID_OPERATION, "Demuxer engine does not exist");
97 return demuxerEngine_->UnselectTrackByID(trackIndex);
98 }
99
ReadSample(uint32_t trackIndex,std::shared_ptr<AVSharedMemory> sample,AVCodecBufferInfo & info,AVCodecBufferFlag & flag)100 int32_t AVDemuxerImpl::ReadSample(uint32_t trackIndex, std::shared_ptr<AVSharedMemory> sample,
101 AVCodecBufferInfo &info, AVCodecBufferFlag &flag)
102 {
103 AVCodecTrace trace("AVDemuxer::ReadSample");
104
105 AVCODEC_LOGD("ReadSample: trackIndex=%{public}u", trackIndex);
106
107 CHECK_AND_RETURN_RET_LOG(demuxerEngine_ != nullptr, AVCS_ERR_INVALID_OPERATION, "Demuxer engine does not exist");
108
109 CHECK_AND_RETURN_RET_LOG(sample != nullptr, AVCS_ERR_INVALID_VAL,
110 "Copy sample failed because sample buffer is nullptr!");
111
112 return demuxerEngine_->ReadSample(trackIndex, sample, info, flag);
113 }
114
SeekToTime(int64_t millisecond,AVSeekMode mode)115 int32_t AVDemuxerImpl::SeekToTime(int64_t millisecond, AVSeekMode mode)
116 {
117 AVCodecTrace trace("AVDemuxer::SeekToTime");
118
119 AVCODEC_LOGI("seek to time: millisecond=%{public}" PRId64 "; mode=%{public}d", millisecond, mode);
120
121 CHECK_AND_RETURN_RET_LOG(demuxerEngine_ != nullptr, AVCS_ERR_INVALID_OPERATION, "Demuxer engine does not exist");
122
123 CHECK_AND_RETURN_RET_LOG(millisecond >= 0, AVCS_ERR_INVALID_VAL,
124 "Seek failed because input millisecond is negative!");
125
126 return demuxerEngine_->SeekToTime(millisecond, mode);
127 }
128 } // namespace MediaAVCodec
129 } // namespace OHOS