• 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 "media_log.h"
17 #include "video_editor_impl.h"
18 #include "video_editor_manager.h"
19 
20 namespace OHOS {
21 namespace Media {
22 
23 namespace {
24 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_VIDEOEDITOR, "VideoEditor"};
25 }
26 
VideoEditorImpl(uint64_t id)27 VideoEditorImpl::VideoEditorImpl(uint64_t id) : id_(id)
28 {
29     logTag_ = "editorImpl-" + std::to_string(id);
30 }
31 
~VideoEditorImpl()32 VideoEditorImpl::~VideoEditorImpl()
33 {
34     MEDIA_LOGD("[%{public}s] destruct.", logTag_.c_str());
35     if (state_ == VideoEditorState::COMPOSITING) {
36         auto err = compositeEngine_->StopComposite();
37         if (err != VEFError::ERR_OK) {
38             //This branch may failed to accessed, maybe the type of StopComposite could be changed to void.
39             MEDIA_LOGE("[%{public}s] stop composite engine failed.", logTag_.c_str());
40         }
41     }
42     VideoEditorManager::GetInstance().ReleaseVideoEditor(id_);
43 }
44 
GetId() const45 uint64_t VideoEditorImpl::GetId() const
46 {
47     return id_;
48 }
49 
GetState() const50 VideoEditorState VideoEditorImpl::GetState() const
51 {
52     return state_;
53 }
54 
Init()55 VEFError VideoEditorImpl::Init()
56 {
57     MEDIA_LOGI("[%{public}s] init video editor.", logTag_.c_str());
58     std::lock_guard<std::mutex> lock(editorLock_);
59     dataCenter_ = IDataCenter::Create();
60     if (dataCenter_ == nullptr) {
61         MEDIA_LOGE("[%{public}s] create data center failed.", logTag_.c_str());
62         return VEFError::ERR_INTERNAL_ERROR;
63     }
64     compositeEngine_ = ICompositeEngine::CreateCompositeEngine(dataCenter_);
65     if (compositeEngine_ == nullptr) {
66         MEDIA_LOGE("[%{public}s] create coomposite engine failed.", logTag_.c_str());
67         return VEFError::ERR_INTERNAL_ERROR;
68     }
69     state_ = VideoEditorState::IDLE;
70     MEDIA_LOGD("[%{public}s] init video editor finish.", logTag_.c_str());
71     return VEFError::ERR_OK;
72 }
73 
AppendVideoFile(int fileFd,const std::string & effectDescription)74 VEFError VideoEditorImpl::AppendVideoFile(int fileFd, const std::string &effectDescription)
75 {
76     MEDIA_LOGI("[%{public}s] append video file: %{public}d and apply effect[%{public}s].",
77         logTag_.c_str(), fileFd, effectDescription.c_str());
78     std::lock_guard<std::mutex> lock(editorLock_);
79     VEFError err = dataCenter_->AppendVideo(fileFd, effectDescription);
80     if (err != VEFError::ERR_OK) {
81         MEDIA_LOGE("[%{public}s] append file failed, err: %{public}d.", logTag_.c_str(), err);
82         return err;
83     }
84     MEDIA_LOGD("[%{public}s] append video file success.", logTag_.c_str());
85     return VEFError::ERR_OK;
86 }
87 
StartComposite(const std::shared_ptr<CompositionOptions> & options)88 VEFError VideoEditorImpl::StartComposite(const std::shared_ptr<CompositionOptions> &options)
89 {
90     MEDIA_LOGI("[%{public}s] start composite.", logTag_.c_str());
91     std::lock_guard<std::mutex> lock(editorLock_);
92     if (!VideoEditorManager::GetInstance().IsFlowControlPass()) {
93         MEDIA_LOGE("[%{public}s] start composite failed, flow control not pass.", logTag_.c_str());
94         return VEFError::ERR_FLOW_CONTROL_INTERCEPT;
95     }
96     if (state_ != VideoEditorState::IDLE) {
97         MEDIA_LOGE("[%{public}s] start composite failed, editor is busy, state: %{public}d.", logTag_.c_str(), state_);
98         return VEFError::ERR_EDITOR_IS_BUSY;
99     }
100     VEFError err = compositeEngine_->StartComposite(options);
101     if (err != VEFError::ERR_OK) {
102         MEDIA_LOGE("[%{public}s] start composite engine failed, error: %{public}d.", logTag_.c_str(), err);
103         return err;
104     }
105     state_ = VideoEditorState::COMPOSITING;
106     MEDIA_LOGI("[%{public}s] start composite engine success.", logTag_.c_str());
107     return err;
108 }
109 
CancelComposite()110 VEFError VideoEditorImpl::CancelComposite()
111 {
112     MEDIA_LOGI("[%{public}s] cancel composite.", logTag_.c_str());
113     std::lock_guard<std::mutex> lock(editorLock_);
114     if (state_ != VideoEditorState::COMPOSITING) {
115         MEDIA_LOGW("[%{public}s] current state[%{public}d] is not compositing, need not cancel.",
116             logTag_.c_str(), state_);
117         return VEFError::ERR_OK;
118     }
119     state_ = VideoEditorState::CANCELLING;
120     VEFError err = compositeEngine_->StopComposite();
121     if (err != VEFError::ERR_OK) {
122         MEDIA_LOGE("[%{public}s] cancel composite failed with error: %{public}d.", logTag_.c_str(), err);
123         return err;
124     }
125     state_ = VideoEditorState::IDLE;
126     MEDIA_LOGD("[%{public}s] cancel composite success.", logTag_.c_str());
127     return err;
128 }
129 
OnCompositeResult(VEFResult result)130 void VideoEditorImpl::OnCompositeResult(VEFResult result)
131 {
132     MEDIA_LOGI("[%{public}s] composite result: %{public}d.", logTag_.c_str(), result);
133     std::lock_guard<std::mutex> lock(editorLock_);
134     state_ = VideoEditorState::IDLE;
135 }
136 
137 }  // namespace Media
138 }  // namespace OHOS