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 <vector>
16 #include <unistd.h>
17 #include <fcntl.h>
18 #include "i_avcodec_service.h"
19 #include "avcodec_errors.h"
20 #include "avcodec_log.h"
21 #include "avcodec_trace.h"
22 #include "avsource_impl.h"
23 #include "common/media_source.h"
24 #include "common/status.h"
25
26 namespace {
27 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AVSourceImpl"};
28 }
29
30 namespace OHOS {
31 namespace MediaAVCodec {
32 using namespace Media;
33 using namespace Media::Plugins;
CreateWithURI(const std::string & uri)34 std::shared_ptr<AVSource> AVSourceFactory::CreateWithURI(const std::string &uri)
35 {
36 AVCODEC_SYNC_TRACE;
37
38 AVCODEC_LOGD("create source with uri: uri=%{private}s", uri.c_str());
39
40 std::shared_ptr<AVSourceImpl> sourceImpl = std::make_shared<AVSourceImpl>();
41 CHECK_AND_RETURN_RET_LOG(sourceImpl != nullptr, nullptr, "New AVSourceImpl failed");
42
43 int32_t ret = sourceImpl->InitWithURI(uri);
44
45 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "Init AVSourceImpl failed");
46
47 return sourceImpl;
48 }
49
CreateWithFD(int32_t fd,int64_t offset,int64_t size)50 std::shared_ptr<AVSource> AVSourceFactory::CreateWithFD(int32_t fd, int64_t offset, int64_t size)
51 {
52 AVCODEC_SYNC_TRACE;
53
54 AVCODEC_LOGD("create source with fd: fd=%{private}d, offset=%{public}" PRId64 ", size=%{public}" PRId64,
55 fd, offset, size);
56
57 std::shared_ptr<AVSourceImpl> sourceImpl = std::make_shared<AVSourceImpl>();
58 CHECK_AND_RETURN_RET_LOG(sourceImpl != nullptr, nullptr, "New AVSourceImpl failed");
59
60 int32_t ret = sourceImpl->InitWithFD(fd, offset, size);
61
62 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "Init AVSourceImpl failed");
63
64 return sourceImpl;
65 }
66
InitWithURI(const std::string & uri)67 int32_t AVSourceImpl::InitWithURI(const std::string &uri)
68 {
69 AVCODEC_SYNC_TRACE;
70
71 CHECK_AND_RETURN_RET_LOG(demuxerEngine == nullptr, AVCS_ERR_INVALID_OPERATION,
72 "Create source failed due to has been used by demuxer.");
73 demuxerEngine = std::make_shared<MediaDemuxer>();
74 CHECK_AND_RETURN_RET_LOG(demuxerEngine != nullptr, AVCS_ERR_INVALID_OPERATION,
75 "Init AVSource with uri failed due to create demuxer engine failed.");
76
77 std::shared_ptr<MediaSource> mediaSource = std::make_shared<MediaSource>(uri);
78 Status ret = demuxerEngine->SetDataSource(mediaSource);
79 CHECK_AND_RETURN_RET_LOG(ret == Status::OK, StatusToAVCodecServiceErrCode(ret),
80 "Init AVSource with uri failed due to set data source for demuxer engine failed.");
81
82 sourceUri = uri;
83 return AVCS_ERR_OK;
84 }
85
InitWithFD(int32_t fd,int64_t offset,int64_t size)86 int32_t AVSourceImpl::InitWithFD(int32_t fd, int64_t offset, int64_t size)
87 {
88 AVCODEC_SYNC_TRACE;
89
90 CHECK_AND_RETURN_RET_LOG(demuxerEngine == nullptr, AVCS_ERR_INVALID_OPERATION,
91 "Create source failed due to has been used by demuxer.");
92 CHECK_AND_RETURN_RET_LOG(fd > STDERR_FILENO, AVCS_ERR_INVALID_VAL,
93 "Create source with uri failed because input fd is illegal, fd must be greater than 2!");
94 CHECK_AND_RETURN_RET_LOG(offset >= 0, AVCS_ERR_INVALID_VAL,
95 "Create source with fd failed because input offset is negative");
96 CHECK_AND_RETURN_RET_LOG(size > 0, AVCS_ERR_INVALID_VAL,
97 "Create source with fd failed because input size must be greater than zero");
98 int32_t flag = fcntl(fd, F_GETFL, 0);
99 CHECK_AND_RETURN_RET_LOG(flag >= 0, AVCS_ERR_INVALID_VAL, "get fd status failed");
100 CHECK_AND_RETURN_RET_LOG(
101 (static_cast<uint32_t>(flag) & static_cast<uint32_t>(O_WRONLY)) != static_cast<uint32_t>(O_WRONLY),
102 AVCS_ERR_INVALID_VAL, "Fd not be permitted to read ");
103 CHECK_AND_RETURN_RET_LOG(lseek(fd, 0, SEEK_CUR) != -1, AVCS_ERR_INVALID_VAL, "Fd is not seekable");
104
105 std::string uri = "fd://" + std::to_string(fd) + "?offset=" + \
106 std::to_string(offset) + "&size=" + std::to_string(size);
107
108 return InitWithURI(uri);
109 }
110
AVSourceImpl()111 AVSourceImpl::AVSourceImpl()
112 {
113 AVCODEC_LOGD("AVSourceImpl:0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
114 }
115
~AVSourceImpl()116 AVSourceImpl::~AVSourceImpl()
117 {
118 AVCODEC_LOGD("Destroy AVSourceImpl for source %{private}s", sourceUri.c_str());
119 if (demuxerEngine != nullptr) {
120 demuxerEngine = nullptr;
121 }
122 AVCODEC_LOGD("AVSourceImpl:0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
123 }
124
GetSourceFormat(OHOS::Media::Format & format)125 int32_t AVSourceImpl::GetSourceFormat(OHOS::Media::Format &format)
126 {
127 AVCODEC_SYNC_TRACE;
128 AVCODEC_LOGD("get source format.");
129
130 CHECK_AND_RETURN_RET_LOG(demuxerEngine != nullptr, AVCS_ERR_INVALID_OPERATION, "Demuxer engine does not exist.");
131
132 std::shared_ptr<OHOS::Media::Meta> mediaInfo = demuxerEngine->GetGlobalMetaInfo();
133 CHECK_AND_RETURN_RET_LOG(mediaInfo != nullptr, AVCS_ERR_INVALID_OPERATION,
134 "Get source format failed due to parse media info failed.");
135
136 bool set = format.SetMeta(mediaInfo);
137 CHECK_AND_RETURN_RET_LOG(set, AVCS_ERR_INVALID_OPERATION, "Get source format failed due to convert meta failed.");
138
139 return AVCS_ERR_OK;
140 }
141
GetTrackFormat(OHOS::Media::Format & format,uint32_t trackIndex)142 int32_t AVSourceImpl::GetTrackFormat(OHOS::Media::Format &format, uint32_t trackIndex)
143 {
144 AVCODEC_SYNC_TRACE;
145 AVCODEC_LOGD("get track format: trackIndex=%{public}u", trackIndex);
146
147 CHECK_AND_RETURN_RET_LOG(demuxerEngine != nullptr, AVCS_ERR_INVALID_OPERATION, "Demuxer engine does not exist.");
148
149 std::vector<std::shared_ptr<Meta>> streamsInfo = demuxerEngine->GetStreamMetaInfo();
150 CHECK_AND_RETURN_RET_LOG(trackIndex < streamsInfo.size(), AVCS_ERR_INVALID_VAL,
151 "Just have %{public}zu tracks. index is out of range.", streamsInfo.size());
152
153 bool set = format.SetMeta(streamsInfo[trackIndex]);
154 CHECK_AND_RETURN_RET_LOG(set, AVCS_ERR_INVALID_OPERATION, "Get track format failed due to convert meta failed.");
155
156 return AVCS_ERR_OK;
157 }
158 } // namespace MediaAVCodec
159 } // namespace OHOS