1 /*
2  * Copyright (c) 2023-2023 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 "plugin/plugin_manager_v2.h"
17 #include "common/log.h"
18 
19 namespace {
20 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_FOUNDATION, "PluginManagerV2" };
21 }
22 
23 namespace OHOS {
24 namespace Media {
25 namespace Plugins {
PluginManagerV2()26 PluginManagerV2::PluginManagerV2()
27 {
28     MEDIA_LOG_D("PluginManagerV2");
29     cachedPluginPackage_ = std::make_shared<CachedPluginPackage>();
30 }
31 
CreatePluginByMime(PluginType pluginType, std::string mime)32 std::shared_ptr<PluginBase> PluginManagerV2::CreatePluginByMime(PluginType pluginType, std::string mime)
33 {
34     MEDIA_LOG_D("CreatePluginByMime pluginType: " PUBLIC_LOG_D32 " mime: " PUBLIC_LOG_S, pluginType, mime.c_str());
35     PluginDescription pluginDescription = PluginList::GetInstance().GetPluginByCap(pluginType, mime);
36     return cachedPluginPackage_->CreatePlugin(pluginDescription);
37 }
38 
CreatePluginByName(std::string name)39 std::shared_ptr<PluginBase> PluginManagerV2::CreatePluginByName(std::string name)
40 {
41     MEDIA_LOG_D("CreatePluginByName pluginName: " PUBLIC_LOG_S, name.c_str());
42     PluginDescription pluginDescription = PluginList::GetInstance().GetPluginByName(name);
43     return cachedPluginPackage_->CreatePlugin(pluginDescription);
44 }
45 
SnifferPlugin(PluginType pluginType, std::shared_ptr<DataSource> dataSource)46 std::string PluginManagerV2::SnifferPlugin(PluginType pluginType, std::shared_ptr<DataSource> dataSource)
47 {
48     MEDIA_LOG_D("SnifferPlugin pluginType: " PUBLIC_LOG_D32, pluginType);
49     std::vector<PluginDescription> matchedPluginsDescriptions =
50         PluginList::GetInstance().GetPluginsByType(pluginType);
51     int maxProb = 0;
52     std::vector<PluginDescription>::iterator it;
53     PluginDescription bestMatchedPlugin;
54     for (it = matchedPluginsDescriptions.begin(); it != matchedPluginsDescriptions.end(); it++) {
55         std::shared_ptr<PluginDefBase> pluginDef = cachedPluginPackage_->GetPluginDef(*it);
56         if (pluginDef != nullptr) {
57             auto prob = pluginDef->GetSniffer()(pluginDef->name, dataSource);
58             if (prob > maxProb) {
59                 maxProb = prob;
60                 bestMatchedPlugin = (*it);
61             }
62         }
63     }
64     return bestMatchedPlugin.pluginName;
65 }
66 } // namespace Plugins
67 } // namespace Media
68 } // namespace OHOS