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_DEMUXER, "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 %{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 avsource failed");
42 
43     int32_t ret = sourceImpl->InitWithURI(uri);
44 
45     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "Init avsource 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 %{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 avsource failed");
59 
60     int32_t ret = sourceImpl->InitWithFD(fd, offset, size);
61 
62     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "Init avsource failed");
63 
64     return sourceImpl;
65 }
66 
CreateWithDataSource(const std::shared_ptr<Media::IMediaDataSource> & dataSource)67 std::shared_ptr<AVSource> AVSourceFactory::CreateWithDataSource(
68     const std::shared_ptr<Media::IMediaDataSource> &dataSource)
69 {
70     AVCODEC_SYNC_TRACE;
71 
72     std::shared_ptr<AVSourceImpl> sourceImpl = std::make_shared<AVSourceImpl>();
73     CHECK_AND_RETURN_RET_LOG(sourceImpl != nullptr, nullptr, "New avsource failed");
74 
75     int32_t ret = sourceImpl->InitWithDataSource(dataSource);
76 
77     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "Init avsource failed");
78 
79     return sourceImpl;
80 }
81 
InitWithURI(const std::string & uri)82 int32_t AVSourceImpl::InitWithURI(const std::string &uri)
83 {
84     AVCODEC_SYNC_TRACE;
85 
86     CHECK_AND_RETURN_RET_LOG(demuxerEngine == nullptr, AVCS_ERR_INVALID_OPERATION, "Has been used by demuxer");
87     demuxerEngine = std::make_shared<MediaDemuxer>();
88     CHECK_AND_RETURN_RET_LOG(demuxerEngine != nullptr, AVCS_ERR_INVALID_OPERATION, "Create mediaDemuxer failed");
89 
90     std::shared_ptr<MediaSource> mediaSource = std::make_shared<MediaSource>(uri);
91     Status ret = demuxerEngine->SetDataSource(mediaSource);
92     CHECK_AND_RETURN_RET_LOG(ret == Status::OK, StatusToAVCodecServiceErrCode(ret), "Set data source failed");
93 
94     sourceUri = uri;
95     return AVCS_ERR_OK;
96 }
97 
InitWithFD(int32_t fd,int64_t offset,int64_t size)98 int32_t AVSourceImpl::InitWithFD(int32_t fd, int64_t offset, int64_t size)
99 {
100     AVCODEC_SYNC_TRACE;
101 
102     CHECK_AND_RETURN_RET_LOG(demuxerEngine == nullptr, AVCS_ERR_INVALID_OPERATION, "Has been used by demuxer");
103     CHECK_AND_RETURN_RET_LOG(fd >= 0, AVCS_ERR_INVALID_VAL, "Input fd is negative");
104     if (fd <= STDERR_FILENO) {
105         AVCODEC_LOGW("Using special fd: %{public}d, isatty: %{public}d", fd, isatty(fd));
106     }
107     CHECK_AND_RETURN_RET_LOG(offset >= 0, AVCS_ERR_INVALID_VAL, "Input offset is negative");
108     CHECK_AND_RETURN_RET_LOG(size > 0, AVCS_ERR_INVALID_VAL, "Input size must be greater than zero");
109     int32_t flag = fcntl(fd, F_GETFL, 0);
110     CHECK_AND_RETURN_RET_LOG(flag >= 0, AVCS_ERR_INVALID_VAL, "Get fd status failed");
111     CHECK_AND_RETURN_RET_LOG(
112         (static_cast<uint32_t>(flag) & static_cast<uint32_t>(O_WRONLY)) != static_cast<uint32_t>(O_WRONLY),
113         AVCS_ERR_INVALID_VAL, "Fd is not permitted to read");
114     CHECK_AND_RETURN_RET_LOG(lseek(fd, 0, SEEK_CUR) != -1, AVCS_ERR_INVALID_VAL, "Fd unseekable");
115 
116     std::string uri = "fd://" + std::to_string(fd) + "?offset=" + \
117         std::to_string(offset) + "&size=" + std::to_string(size);
118 
119     return InitWithURI(uri);
120 }
121 
InitWithDataSource(const std::shared_ptr<Media::IMediaDataSource> & dataSource)122 int32_t AVSourceImpl::InitWithDataSource(const std::shared_ptr<Media::IMediaDataSource> &dataSource)
123 {
124     AVCODEC_SYNC_TRACE;
125 
126     CHECK_AND_RETURN_RET_LOG(demuxerEngine == nullptr, AVCS_ERR_INVALID_OPERATION, "Has been used by demuxer");
127     demuxerEngine = std::make_shared<MediaDemuxer>();
128     CHECK_AND_RETURN_RET_LOG(demuxerEngine != nullptr, AVCS_ERR_INVALID_OPERATION, "Create mediaDemuxer failed");
129 
130     std::shared_ptr<MediaSource> mediaSource = std::make_shared<MediaSource>(dataSource);
131     Status ret = demuxerEngine->SetDataSource(mediaSource);
132     CHECK_AND_RETURN_RET_LOG(ret == Status::OK, StatusToAVCodecServiceErrCode(ret), "Set data source failed");
133 
134     return AVCS_ERR_OK;
135 }
136 
AVSourceImpl()137 AVSourceImpl::AVSourceImpl()
138 {
139     AVCODEC_LOGD("Create instances 0x%{public}06" PRIXPTR, FAKE_POINTER(this));
140 }
141 
~AVSourceImpl()142 AVSourceImpl::~AVSourceImpl()
143 {
144     if (demuxerEngine != nullptr) {
145         demuxerEngine = nullptr;
146     }
147     AVCODEC_LOGD("Destroy instances 0x%{public}06" PRIXPTR, FAKE_POINTER(this));
148 }
149 
GetSourceFormat(OHOS::Media::Format & format)150 int32_t AVSourceImpl::GetSourceFormat(OHOS::Media::Format &format)
151 {
152     AVCODEC_SYNC_TRACE;
153     AVCODEC_LOGD("Get source format");
154 
155     CHECK_AND_RETURN_RET_LOG(demuxerEngine != nullptr, AVCS_ERR_INVALID_OPERATION, "MediaDemuxer does not exist");
156 
157     std::shared_ptr<OHOS::Media::Meta> mediaInfo = demuxerEngine->GetGlobalMetaInfo();
158     CHECK_AND_RETURN_RET_LOG(mediaInfo != nullptr, AVCS_ERR_INVALID_OPERATION, "Parse media info failed");
159 
160     bool set = format.SetMeta(mediaInfo);
161     CHECK_AND_RETURN_RET_LOG(set, AVCS_ERR_INVALID_OPERATION, "Convert meta failed");
162 
163     return AVCS_ERR_OK;
164 }
165 
GetTrackFormat(OHOS::Media::Format & format,uint32_t trackIndex)166 int32_t AVSourceImpl::GetTrackFormat(OHOS::Media::Format &format, uint32_t trackIndex)
167 {
168     AVCODEC_SYNC_TRACE;
169     AVCODEC_LOGD("Get track %{public}u format", trackIndex);
170 
171     CHECK_AND_RETURN_RET_LOG(demuxerEngine != nullptr, AVCS_ERR_INVALID_OPERATION, "MediaDemuxer does not exist");
172 
173     std::vector<std::shared_ptr<Meta>> streamsInfo = demuxerEngine->GetStreamMetaInfo();
174     CHECK_AND_RETURN_RET_LOG(trackIndex < streamsInfo.size(), AVCS_ERR_INVALID_VAL,
175         "Just have %{public}zu tracks. index is out of range", streamsInfo.size());
176 
177     bool set = format.SetMeta(streamsInfo[trackIndex]);
178     CHECK_AND_RETURN_RET_LOG(set, AVCS_ERR_INVALID_OPERATION, "Convert meta failed");
179 
180     return AVCS_ERR_OK;
181 }
182 
GetUserMeta(OHOS::Media::Format & format)183 int32_t AVSourceImpl::GetUserMeta(OHOS::Media::Format &format)
184 {
185     AVCODEC_SYNC_TRACE;
186     AVCODEC_LOGD("get user meta");
187 
188     CHECK_AND_RETURN_RET_LOG(demuxerEngine != nullptr, AVCS_ERR_INVALID_OPERATION, "MediaDemuxer does not exist");
189 
190     std::shared_ptr<OHOS::Media::Meta> userDataMeta = demuxerEngine->GetUserMeta();
191     CHECK_AND_RETURN_RET_LOG(userDataMeta != nullptr, AVCS_ERR_INVALID_OPERATION, "Parse user info failed");
192 
193     bool set = format.SetMeta(userDataMeta);
194     CHECK_AND_RETURN_RET_LOG(set, AVCS_ERR_INVALID_OPERATION, "Convert meta failed");
195 
196     return AVCS_ERR_OK;
197 }
198 } // namespace MediaAVCodec
199 } // namespace OHOS