1 /*
2 * Copyright (c) 2024 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 "surface_decoder_adapter.h"
17 #include <ctime>
18 #include "avcodec_info.h"
19 #include "avcodec_common.h"
20 #include "codec_server.h"
21 #include "meta/format.h"
22 #include "media_description.h"
23 #include "avcodec_trace.h"
24
25 namespace {
26 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_ONLY_PRERELEASE, LOG_DOMAIN_SYSTEM_PLAYER,
27 "SurfaceDecoderAdapter" };
28 constexpr uint32_t BUFFER_IS_EOS = 1;
29 }
30
31 namespace OHOS {
32 namespace Media {
33
34 constexpr int64_t VARIABLE_INCREMENT_INTERVAL = 1;
35
36 class SurfaceDecoderAdapterCallback : public MediaAVCodec::MediaCodecCallback {
37 public:
SurfaceDecoderAdapterCallback(std::shared_ptr<SurfaceDecoderAdapter> surfaceDecoderAdapter)38 explicit SurfaceDecoderAdapterCallback(std::shared_ptr<SurfaceDecoderAdapter> surfaceDecoderAdapter)
39 : surfaceDecoderAdapter_(std::move(surfaceDecoderAdapter))
40 {
41 }
42
OnError(MediaAVCodec::AVCodecErrorType errorType,int32_t errorCode)43 void OnError(MediaAVCodec::AVCodecErrorType errorType, int32_t errorCode) override
44 {
45 if (auto surfaceDecoderAdapter = surfaceDecoderAdapter_.lock()) {
46 surfaceDecoderAdapter->decoderAdapterCallback_->OnError(errorType, errorCode);
47 } else {
48 MEDIA_LOG_I("invalid surfaceEncoderAdapter");
49 }
50 }
51
OnOutputFormatChanged(const MediaAVCodec::Format & format)52 void OnOutputFormatChanged(const MediaAVCodec::Format &format) override
53 {
54 }
55
OnInputBufferAvailable(uint32_t index,std::shared_ptr<AVBuffer> buffer)56 void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override
57 {
58 if (auto surfaceDecoderAdapter = surfaceDecoderAdapter_.lock()) {
59 surfaceDecoderAdapter->OnInputBufferAvailable(index, buffer);
60 } else {
61 MEDIA_LOG_I("invalid surfaceDecoderAdapter");
62 }
63 }
64
OnOutputBufferAvailable(uint32_t index,std::shared_ptr<AVBuffer> buffer)65 void OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override
66 {
67 if (auto surfaceDecoderAdapter = surfaceDecoderAdapter_.lock()) {
68 MEDIA_LOG_D("OnOutputBuffer flag " PUBLIC_LOG_D32, buffer->flag_);
69 surfaceDecoderAdapter->OnOutputBufferAvailable(index, buffer);
70 if ((buffer->flag_ & BUFFER_IS_EOS) == 1) {
71 int64_t lastBufferPts = surfaceDecoderAdapter->GetLastBufferPts();
72 int64_t frameNum = surfaceDecoderAdapter->GetFrameNum();
73 MEDIA_LOG_I("lastBuffer PTS: " PUBLIC_LOG_D64 " frameNum: " PUBLIC_LOG_D64,
74 lastBufferPts, frameNum);
75 surfaceDecoderAdapter->decoderAdapterCallback_->OnBufferEos(lastBufferPts, frameNum);
76 }
77 } else {
78 MEDIA_LOG_I("invalid surfaceDecoderAdapter");
79 }
80 }
81
82 private:
83 std::weak_ptr<SurfaceDecoderAdapter> surfaceDecoderAdapter_;
84 };
85
86 class AVBufferAvailableListener : public OHOS::Media::IConsumerListener {
87 public:
AVBufferAvailableListener(std::shared_ptr<SurfaceDecoderAdapter> surfaceDecoderAdapter)88 explicit AVBufferAvailableListener(std::shared_ptr<SurfaceDecoderAdapter> surfaceDecoderAdapter)
89 : surfaceDecoderAdapter_(std::move(surfaceDecoderAdapter))
90 {
91 }
92
OnBufferAvailable()93 void OnBufferAvailable() override
94 {
95 if (auto surfaceDecoderAdapter = surfaceDecoderAdapter_.lock()) {
96 surfaceDecoderAdapter->AcquireAvailableInputBuffer();
97 } else {
98 MEDIA_LOG_I("invalid surfaceDecoderAdapter");
99 }
100 }
101 private:
102 std::weak_ptr<SurfaceDecoderAdapter> surfaceDecoderAdapter_;
103 };
104
SurfaceDecoderAdapter()105 SurfaceDecoderAdapter::SurfaceDecoderAdapter()
106 {
107 MEDIA_LOG_I("encoder adapter create");
108 }
109
~SurfaceDecoderAdapter()110 SurfaceDecoderAdapter::~SurfaceDecoderAdapter()
111 {
112 MEDIA_LOG_I("encoder adapter destroy");
113 if (codecServer_) {
114 codecServer_->Release();
115 }
116 codecServer_ = nullptr;
117 }
118
Init(const std::string & mime)119 Status SurfaceDecoderAdapter::Init(const std::string &mime)
120 {
121 MEDIA_LOG_I("Init mime: " PUBLIC_LOG_S, mime.c_str());
122 codecServer_ = MediaAVCodec::VideoDecoderFactory::CreateByMime(mime);
123 if (!codecServer_) {
124 MEDIA_LOG_I("Create codecServer failed");
125 return Status::ERROR_UNKNOWN;
126 }
127 if (!releaseBufferTask_) {
128 releaseBufferTask_ = std::make_shared<Task>("SurfaceDecoder");
129 releaseBufferTask_->RegisterJob([this] {
130 ReleaseBuffer();
131 return 0;
132 });
133 }
134 return Status::OK;
135 }
136
Configure(const Format & format)137 Status SurfaceDecoderAdapter::Configure(const Format &format)
138 {
139 MEDIA_LOG_I("Configure");
140 if (!codecServer_) {
141 return Status::ERROR_UNKNOWN;
142 }
143 int32_t ret = codecServer_->Configure(format);
144 return ret == 0 ? Status::OK : Status::ERROR_UNKNOWN;
145 }
146
GetFrameNum()147 int64_t SurfaceDecoderAdapter::GetFrameNum()
148 {
149 return frameNum_.load();
150 }
151
GetLastBufferPts()152 int64_t SurfaceDecoderAdapter::GetLastBufferPts()
153 {
154 return lastBufferPts_.load();
155 }
156
GetInputBufferQueue()157 sptr<OHOS::Media::AVBufferQueueProducer> SurfaceDecoderAdapter::GetInputBufferQueue()
158 {
159 MEDIA_LOG_I("GetInputBufferQueue");
160 if (inputBufferQueue_ != nullptr && inputBufferQueue_-> GetQueueSize() > 0) {
161 MEDIA_LOG_I("InputBufferQueue already create");
162 return inputBufferQueueProducer_;
163 }
164 inputBufferQueue_ = AVBufferQueue::Create(0,
165 MemoryType::UNKNOWN_MEMORY, "inputBufferQueue", true);
166 inputBufferQueueProducer_ = inputBufferQueue_->GetProducer();
167 inputBufferQueueConsumer_ = inputBufferQueue_->GetConsumer();
168 sptr<IConsumerListener> listener = new AVBufferAvailableListener(shared_from_this());
169 inputBufferQueueConsumer_->SetBufferAvailableListener(listener);
170 return inputBufferQueueProducer_;
171 }
172
SetDecoderAdapterCallback(const std::shared_ptr<DecoderAdapterCallback> & decoderAdapterCallback)173 Status SurfaceDecoderAdapter::SetDecoderAdapterCallback(
174 const std::shared_ptr<DecoderAdapterCallback> &decoderAdapterCallback)
175 {
176 MEDIA_LOG_I("SetDecoderAdapterCallback");
177 std::shared_ptr<MediaAVCodec::MediaCodecCallback> surfaceDecoderAdapterCallback =
178 std::make_shared<SurfaceDecoderAdapterCallback>(shared_from_this());
179 decoderAdapterCallback_ = decoderAdapterCallback;
180 if (!codecServer_) {
181 return Status::ERROR_UNKNOWN;
182 }
183 int32_t ret = codecServer_->SetCallback(surfaceDecoderAdapterCallback);
184 if (ret == 0) {
185 return Status::OK;
186 } else {
187 return Status::ERROR_UNKNOWN;
188 }
189 }
190
SetOutputSurface(sptr<Surface> surface)191 Status SurfaceDecoderAdapter::SetOutputSurface(sptr<Surface> surface)
192 {
193 MEDIA_LOG_I("SetOutputSurface");
194 if (!codecServer_) {
195 return Status::ERROR_UNKNOWN;
196 }
197 int32_t ret = codecServer_->SetOutputSurface(surface);
198 if (ret == 0) {
199 MEDIA_LOG_I("SetOutputSurface success");
200 return Status::OK;
201 } else {
202 MEDIA_LOG_I("SetOutputSurface fail");
203 return Status::ERROR_UNKNOWN;
204 }
205 }
206
Start()207 Status SurfaceDecoderAdapter::Start()
208 {
209 MEDIA_LOG_I("Start");
210 if (!codecServer_) {
211 return Status::ERROR_UNKNOWN;
212 }
213 int32_t ret;
214 isThreadExit_ = false;
215 if (releaseBufferTask_) {
216 releaseBufferTask_->Start();
217 }
218 ret = codecServer_->Prepare();
219 if (ret == 0) {
220 MEDIA_LOG_I("Prepare success");
221 } else {
222 MEDIA_LOG_I("Prepare fail");
223 return Status::ERROR_UNKNOWN;
224 }
225 ret = codecServer_->Start();
226 if (ret == 0) {
227 MEDIA_LOG_I("Start success");
228 return Status::OK;
229 } else {
230 MEDIA_LOG_I("Start fail");
231 return Status::ERROR_UNKNOWN;
232 }
233 }
234
Stop()235 Status SurfaceDecoderAdapter::Stop()
236 {
237 MEDIA_LOG_I("Stop");
238 if (releaseBufferTask_) {
239 {
240 std::unique_lock<std::mutex> lock(releaseBufferMutex_);
241 isThreadExit_ = true;
242 }
243 releaseBufferCondition_.notify_all();
244 releaseBufferTask_->Stop();
245 MEDIA_LOG_I("releaseBufferTask_ Stop");
246 }
247 if (!codecServer_) {
248 return Status::OK;
249 }
250 int32_t ret = codecServer_->Stop();
251 MEDIA_LOG_I("codecServer_ Stop");
252 if (ret == 0) {
253 return Status::OK;
254 } else {
255 return Status::ERROR_UNKNOWN;
256 }
257 }
258
Pause()259 Status SurfaceDecoderAdapter::Pause()
260 {
261 MEDIA_LOG_I("Pause");
262 return Status::OK;
263 }
264
Resume()265 Status SurfaceDecoderAdapter::Resume()
266 {
267 MEDIA_LOG_I("Resume");
268 return Status::OK;
269 }
270
Flush()271 Status SurfaceDecoderAdapter::Flush()
272 {
273 MEDIA_LOG_I("Flush");
274 if (!codecServer_) {
275 return Status::ERROR_UNKNOWN;
276 }
277 int32_t ret = codecServer_->Flush();
278 if (ret == 0) {
279 return Status::OK;
280 } else {
281 return Status::ERROR_UNKNOWN;
282 }
283 }
284
Release()285 Status SurfaceDecoderAdapter::Release()
286 {
287 MEDIA_LOG_I("Release");
288 if (!codecServer_) {
289 return Status::OK;
290 }
291 int32_t ret = codecServer_->Release();
292 if (ret == 0) {
293 return Status::OK;
294 } else {
295 return Status::ERROR_UNKNOWN;
296 }
297 }
298
SetParameter(const Format & format)299 Status SurfaceDecoderAdapter::SetParameter(const Format &format)
300 {
301 MEDIA_LOG_I("SetParameter");
302 if (!codecServer_) {
303 return Status::ERROR_UNKNOWN;
304 }
305 int32_t ret = codecServer_->SetParameter(format);
306 if (ret == 0) {
307 return Status::OK;
308 } else {
309 return Status::ERROR_UNKNOWN;
310 }
311 }
312
OnInputBufferAvailable(uint32_t index,std::shared_ptr<AVBuffer> buffer)313 void SurfaceDecoderAdapter::OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer)
314 {
315 FALSE_RETURN_MSG(buffer != nullptr && buffer->meta_ != nullptr, "meta_ is nullptr.");
316 MEDIA_LOG_D("OnInputBufferAvailable enter. index: %{public}u, bufferid: %{public}" PRIu64", pts: %{public}" PRIu64
317 ", flag: %{public}u", index, buffer->GetUniqueId(), buffer->pts_, buffer->flag_);
318 buffer->meta_->SetData(Tag::REGULAR_TRACK_ID, index);
319 if (inputBufferQueueConsumer_ == nullptr) {
320 MEDIA_LOG_E("inputBufferQueueConsumer_ is null");
321 return;
322 }
323 if (inputBufferQueueConsumer_->IsBufferInQueue(buffer)) {
324 if (inputBufferQueueConsumer_->ReleaseBuffer(buffer) != Status::OK) {
325 MEDIA_LOG_E("IsBufferInQueue ReleaseBuffer failed. index: %{public}u, bufferid: %{public}" PRIu64
326 ", pts: %{public}" PRIu64", flag: %{public}u", index, buffer->GetUniqueId(),
327 buffer->pts_, buffer->flag_);
328 } else {
329 MEDIA_LOG_D("IsBufferInQueue ReleaseBuffer success. index: %{public}u, bufferid: %{public}" PRIu64
330 ", pts: %{public}" PRIu64", flag: %{public}u", index, buffer->GetUniqueId(),
331 buffer->pts_, buffer->flag_);
332 }
333 } else {
334 uint32_t size = inputBufferQueueConsumer_->GetQueueSize() + 1;
335 MEDIA_LOG_I("AttachBuffer enter. index: %{public}u, size: %{public}u , bufferid: %{public}" PRIu64,
336 index, size, buffer->GetUniqueId());
337 inputBufferQueueConsumer_->SetQueueSize(size);
338 inputBufferQueueConsumer_->AttachBuffer(buffer, false);
339 }
340 }
341
OnOutputBufferAvailable(uint32_t index,std::shared_ptr<AVBuffer> buffer)342 void SurfaceDecoderAdapter::OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer)
343 {
344 MediaAVCodec::AVCodecTrace trace("OnOutputBufferAvailable " + std::to_string(index) + " " +
345 std::to_string(buffer->pts_));
346 {
347 std::lock_guard<std::mutex> lock(releaseBufferMutex_);
348 if ((buffer->flag_ & BUFFER_IS_EOS) == 1) {
349 MEDIA_LOG_I("Buffer index: %{public}u" PRIu32 " flag: %{public}u" PRIu32, index, buffer->flag_);
350 dropIndexs_.push_back(index);
351 } else if (buffer->pts_ > lastBufferPts_.load()) {
352 lastBufferPts_ = buffer->pts_;
353 frameNum_.fetch_add(VARIABLE_INCREMENT_INTERVAL, std::memory_order_relaxed);
354 indexs_.push_back(index);
355 } else {
356 MEDIA_LOG_W("Buffer drop index: " PUBLIC_LOG_U32 " pts: " PUBLIC_LOG_D64, index, buffer->pts_);
357 dropIndexs_.push_back(index);
358 }
359 }
360 releaseBufferCondition_.notify_all();
361 MEDIA_LOG_D("OnOutputBufferAvailable end, index: " PUBLIC_LOG_U32 " pts: " PUBLIC_LOG_D64, index, buffer->pts_);
362 }
363
AcquireAvailableInputBuffer()364 void SurfaceDecoderAdapter::AcquireAvailableInputBuffer()
365 {
366 std::shared_ptr<AVBuffer> filledInputBuffer;
367 Status ret = inputBufferQueueConsumer_->AcquireBuffer(filledInputBuffer);
368 if (ret != Status::OK) {
369 MEDIA_LOG_E("AcquireBuffer fail");
370 return;
371 }
372 FALSE_RETURN_MSG(filledInputBuffer->meta_ != nullptr, "filledInputBuffer meta is nullptr.");
373 uint32_t index;
374 FALSE_RETURN_MSG(filledInputBuffer->meta_->GetData(Tag::REGULAR_TRACK_ID, index), "get index failed.");
375 FALSE_RETURN_MSG(codecServer_ != nullptr, "codecServer_ is nullptr.");
376 if (codecServer_->QueueInputBuffer(index) != ERR_OK) {
377 MEDIA_LOG_E("QueueInputBuffer failed, index: %{public}u, bufferid: %{public}" PRIu64
378 ", pts: %{public}" PRIu64", flag: %{public}u", index, filledInputBuffer->GetUniqueId(),
379 filledInputBuffer->pts_, filledInputBuffer->flag_);
380 } else {
381 MEDIA_LOG_D("QueueInputBuffer success, index: %{public}u, bufferid: %{public}" PRIu64
382 ", pts: %{public}" PRIu64", flag: %{public}u", index, filledInputBuffer->GetUniqueId(),
383 filledInputBuffer->pts_, filledInputBuffer->flag_);
384 }
385 }
386
ReleaseBuffer()387 void SurfaceDecoderAdapter::ReleaseBuffer()
388 {
389 MEDIA_LOG_I("ReleaseBuffer");
390 while (!isThreadExit_) {
391 std::vector<uint32_t> indexs;
392 std::vector<uint32_t> dropIndexs;
393 {
394 std::unique_lock<std::mutex> lock(releaseBufferMutex_);
395 releaseBufferCondition_.wait(lock, [this] {
396 return isThreadExit_ || !indexs_.empty() || !dropIndexs_.empty();
397 });
398 indexs = indexs_;
399 indexs_.clear();
400 dropIndexs = dropIndexs_;
401 dropIndexs_.clear();
402 }
403 for (auto &index : indexs) {
404 MEDIA_LOG_D("Release buffer, index: " PUBLIC_LOG_U32, index);
405 codecServer_->ReleaseOutputBuffer(index, true);
406 }
407 for (auto &dropIndex : dropIndexs) {
408 MediaAVCodec::AVCodecTrace trace("ReleaseBuffer drop " + std::to_string(dropIndex));
409 MEDIA_LOG_D("Drop buffer, index: " PUBLIC_LOG_U32, dropIndex);
410 codecServer_->ReleaseOutputBuffer(dropIndex, false);
411 }
412 }
413 MEDIA_LOG_I("ReleaseBuffer end");
414 }
415 } // namespace MEDIA
416 } // namespace OHOS
417