1 /*
2 * Copyright (c) 2021-2021 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 #define HST_LOG_TAG "PluginCoreBase"
17
18 #include "base.h"
19 #include "foundation/log.h"
20 #include "foundation/osal/thread/scoped_lock.h"
21 #include "plugin/interface/plugin_base.h"
22 #include "plugin_core_utils.h"
23
24 using namespace OHOS::Media::Plugin;
25
Base(uint32_t pkgVer,uint32_t apiVer,std::shared_ptr<PluginBase> plugin)26 Base::Base(uint32_t pkgVer, uint32_t apiVer, std::shared_ptr<PluginBase> plugin)
27 : pkgVersion_(pkgVer), apiVersion_(apiVer), plugin_(std::move(plugin)) {}
28
Init()29 Status Base::Init()
30 {
31 MEDIA_LOG_D("%" PUBLIC_LOG "s Enter.", __FUNCTION__);
32 OSAL::ScopedLock lock(stateChangeMutex_);
33 RETURN_WRONG_STATE_IF_CON_TRUE(pluginState_ == State::DESTROYED, plugin_, pluginState_.load());
34 if (pluginState_ != State::CREATED) {
35 MEDIA_LOG_I("plugin %" PUBLIC_LOG "s has been already inited", plugin_->GetName().c_str());
36 return Status::OK;
37 }
38 auto ret = plugin_->Init();
39 LOG_WARN_IF_NOT_OK(plugin_, ret);
40 if (ret == Status::OK) {
41 pluginState_ = State::INITIALIZED;
42 }
43 MEDIA_LOG_D("%" PUBLIC_LOG "s Exit.", __FUNCTION__);
44 return ret;
45 }
46
Deinit()47 Status Base::Deinit()
48 {
49 MEDIA_LOG_D("%" PUBLIC_LOG "s Enter.", __FUNCTION__);
50 OSAL::ScopedLock lock(stateChangeMutex_);
51 if (pluginState_ == State::DESTROYED) {
52 MEDIA_LOG_I("plugin %" PUBLIC_LOG "s already deinited, no need to destroy any more",
53 plugin_->GetName().c_str());
54 return Status::OK;
55 }
56 auto ret = plugin_->Deinit();
57 LOG_WARN_IF_NOT_OK(plugin_, ret);
58 pluginState_ = State::DESTROYED;
59 MEDIA_LOG_D("%" PUBLIC_LOG "s Exit.", __FUNCTION__);
60 return ret;
61 }
62
Prepare()63 Status Base::Prepare()
64 {
65 MEDIA_LOG_D("%" PUBLIC_LOG "s Enter.", __FUNCTION__);
66 OSAL::ScopedLock lock(stateChangeMutex_);
67 RETURN_WRONG_STATE_IF_CON_TRUE(pluginState_ != State::PREPARED && pluginState_ != State::INITIALIZED,
68 plugin_, pluginState_.load());
69 if (pluginState_ == State::PREPARED) {
70 MEDIA_LOG_I("plugin %" PUBLIC_LOG "s already prepared, ignore this prepare", plugin_->GetName().c_str());
71 return Status::OK;
72 }
73 auto ret = plugin_->Prepare();
74 LOG_WARN_IF_NOT_OK(plugin_, ret);
75 if (ret == Status::OK || ret == Status::ERROR_DELAY_READY) {
76 pluginState_ = State::PREPARED;
77 }
78 MEDIA_LOG_D("%" PUBLIC_LOG "s Exit.", __FUNCTION__);
79 return ret;
80 }
81
Reset()82 Status Base::Reset()
83 {
84 MEDIA_LOG_D("%" PUBLIC_LOG "s Enter.", __FUNCTION__);
85 OSAL::ScopedLock lock(stateChangeMutex_);
86 RETURN_WRONG_STATE_IF_CON_TRUE(pluginState_ == State::RUNNING || pluginState_ == State::PAUSED ||
87 pluginState_ == State::DESTROYED, plugin_, pluginState_.load());
88 if (pluginState_ == State::CREATED || pluginState_ == State::INITIALIZED) {
89 MEDIA_LOG_I("plugin %" PUBLIC_LOG "s no need to reset in state %" PUBLIC_LOG "s",
90 plugin_->GetName().c_str(), GetStateString(pluginState_.load()));
91 return Status::OK;
92 }
93 auto ret = plugin_->Reset();
94 LOG_WARN_IF_NOT_OK(plugin_, ret);
95 pluginState_ = State::INITIALIZED;
96 MEDIA_LOG_D("%" PUBLIC_LOG "s Exit.", __FUNCTION__);
97 return ret;
98 }
99
Start()100 Status Base::Start()
101 {
102 MEDIA_LOG_D("%" PUBLIC_LOG "s Enter.", __FUNCTION__);
103 OSAL::ScopedLock lock(stateChangeMutex_);
104 RETURN_WRONG_STATE_IF_CON_TRUE(pluginState_ != State::PREPARED && pluginState_ != State::RUNNING, plugin_,
105 pluginState_.load());
106 if (pluginState_ == State::RUNNING) {
107 MEDIA_LOG_I("plugin %" PUBLIC_LOG "s already in running state, ignore start", plugin_->GetName().c_str());
108 return Status::OK;
109 }
110 auto ret = plugin_->Start();
111 LOG_WARN_IF_NOT_OK(plugin_, ret);
112 if (ret == Status::OK) {
113 pluginState_ = State::RUNNING;
114 }
115 MEDIA_LOG_D("%" PUBLIC_LOG "s Exit.", __FUNCTION__);
116 return ret;
117 }
118
Stop()119 Status Base::Stop()
120 {
121 MEDIA_LOG_D("%" PUBLIC_LOG "s Enter.", __FUNCTION__);
122 OSAL::ScopedLock lock(stateChangeMutex_);
123 if (pluginState_ != State::RUNNING && pluginState_ != State::PAUSED) {
124 MEDIA_LOG_I("plugin %" PUBLIC_LOG "s not running or paused, no need to stop", plugin_->GetName().c_str());
125 return Status::OK;
126 }
127 auto ret = plugin_->Stop();
128 LOG_WARN_IF_NOT_OK(plugin_, ret);
129 if (ret == Status::OK) {
130 pluginState_ = State::PREPARED;
131 }
132 MEDIA_LOG_D("%" PUBLIC_LOG "s Exit.", __FUNCTION__);
133 return ret;
134 }
135
IsParameterSupported(Tag tag)136 bool Base::IsParameterSupported(Tag tag)
137 {
138 return plugin_->IsParameterSupported(tag);
139 }
140
GetParameter(Tag tag,ValueType & value)141 Status Base::GetParameter(Tag tag, ValueType& value)
142 {
143 return plugin_->GetParameter(tag, value);
144 }
145
SetParameter(Tag tag,const ValueType & value)146 Status Base::SetParameter(Tag tag, const ValueType& value)
147 {
148 return plugin_->SetParameter(tag, value);
149 }
150
GetState(State & state)151 Status Base::GetState(State &state)
152 {
153 state = pluginState_;
154 return Status::OK;
155 }
156
GetAllocator()157 std::shared_ptr<Allocator> Base::GetAllocator()
158 {
159 return plugin_->GetAllocator();
160 }
161
SetCallback(CallbackWrap * cb)162 Status Base::SetCallback(CallbackWrap* cb)
163 {
164 pluginCallback_.SetCallbackWrap(cb);
165 return plugin_->SetCallback(&pluginCallback_);
166 }