• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_texture.h"
17 #include "media_log.h"
18 
19 namespace OHOS {
20 namespace Media {
21 namespace {
22 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_VIDEOEDITOR, "VideoEditorRender"};
23 }
24 const int WAIT_TIMEOUT_MS = 10;
25 
OnFrameAvailable(void * context)26 void SurfaceTexture::OnFrameAvailable(void* context)
27 {
28     if (context == nullptr) {
29         MEDIA_LOGE("OnFrameAvailable failed, context is nullptr.");
30         return;
31     }
32     auto surfaceTexture = static_cast<SurfaceTexture*>(context);
33     std::unique_lock lk(surfaceTexture->frameLock_);
34     surfaceTexture->frameNum_++;
35     surfaceTexture->frameCv_.notify_all();
36 }
37 
AwaitNativeImage()38 void SurfaceTexture::AwaitNativeImage()
39 {
40     std::unique_lock lk(frameLock_);
41     if (frameNum_ > 0) {
42         frameNum_--;
43         return;
44     }
45     frameCv_.wait_for(lk, std::chrono::milliseconds(WAIT_TIMEOUT_MS), [this]() { return frameNum_ > 0; });
46     if (frameNum_ == 0) {
47         MEDIA_LOGW("[Render] Wait for frame available timeout, update directly");
48         return;
49     }
50     frameNum_--;
51 }
52 }
53 }