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 "video_editor.h"
17 #include "video_editor_impl.h"
18 #include "video_editor_manager.h"
19 #include "media_log.h"
20
21 namespace OHOS {
22 namespace Media {
23
24 namespace {
25 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_VIDEOEDITOR, "VideoEditor"};
26 }
27
28 constexpr uint64_t FLOW_CONTROL_MAX_EDITOR_THRESHOLD = 5;
29
CreateVideoEditor()30 std::shared_ptr<VideoEditor> VideoEditorFactory::CreateVideoEditor()
31 {
32 return VideoEditorManager::GetInstance().CreateVideoEditor();
33 }
34
GetInstance()35 VideoEditorManager& VideoEditorManager::GetInstance()
36 {
37 static VideoEditorManager instance;
38 return instance;
39 }
40
CreateVideoEditor()41 std::shared_ptr<VideoEditor> VideoEditorManager::CreateVideoEditor()
42 {
43 auto videoEditor = std::make_shared<VideoEditorImpl>(id_.fetch_add(1));
44 uint64_t id = videoEditor->GetId();
45 MEDIA_LOGD("create VideoEditor[id = %{public}" PRIu64 "] object success.", id);
46
47 VEFError error = videoEditor->Init();
48 if (error != VEFError::ERR_OK) {
49 MEDIA_LOGE("init VideoEditor[id = %{public}" PRIu64 "] failed, error: %{public}d.", id, error);
50 return nullptr;
51 }
52
53 editorMapMutex_.lock();
54 editorMap_[id] = std::weak_ptr<VideoEditorImpl>(videoEditor);
55 editorMapMutex_.unlock();
56
57 MEDIA_LOGI("create VideoEditor[id = %{public}" PRIu64 "] success.", id);
58 return videoEditor;
59 }
60
ReleaseVideoEditor(uint64_t id)61 void VideoEditorManager::ReleaseVideoEditor(uint64_t id)
62 {
63 MEDIA_LOGI("release VideoEditor[id = %{public}" PRIu64 "] start.", id);
64 editorMapMutex_.lock();
65 editorMap_.erase(id);
66 editorMapMutex_.unlock();
67 MEDIA_LOGI("release VideoEditor[id = %{public}" PRIu64 "] finish.", id);
68 }
69
IsFlowControlPass() const70 bool VideoEditorManager::IsFlowControlPass() const
71 {
72 uint32_t compositingCount = 0;
73 std::shared_lock<std::shared_mutex> lock(editorMapMutex_);
74 for (auto it = editorMap_.begin(); it != editorMap_.end(); ++it) {
75 auto editor = it->second.lock();
76 if (editor == nullptr) {
77 continue;
78 }
79 if (editor->GetState() == VideoEditorState::COMPOSITING) {
80 compositingCount++;
81 }
82 if (compositingCount >= FLOW_CONTROL_MAX_EDITOR_THRESHOLD) {
83 return false;
84 }
85 }
86 return true;
87 }
88
89 } // namespace Media
90 } // namespace OHOS
91