1 /*
2  * Copyright (c) 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 "i_engine_factory.h"
17 #include "media_errors.h"
18 #include "media_utils.h"
19 #include "common/log.h"
20 #include "avmetadatahelper_impl.h"
21 #ifdef SUPPORT_RECORDER
22 #include "hirecorder_impl.h"
23 #endif
24 #ifdef SUPPORT_TRANSCODER
25 #include "hitranscoder_impl.h"
26 #endif
27 #ifdef SUPPORT_PLAYER
28 #include "hiplayer_impl.h"
29 #endif
30 
31 namespace {
32 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_SYSTEM_PLAYER, "HstEngineFactory" };
33 }
34 
35 namespace OHOS {
36 namespace Media {
37 class HstEngineFactory : public IEngineFactory {
38 public:
39     HstEngineFactory() = default;
40     ~HstEngineFactory() override = default;
41 
42     int32_t Score(Scene scene, const int32_t& appUid, const std::string& uri) override;
43 #ifdef SUPPORT_PLAYER
44     std::unique_ptr<IPlayerEngine> CreatePlayerEngine(int32_t uid = 0, int32_t pid = 0, uint32_t tokenId = 0) override;
45 #endif
46 #ifdef SUPPORT_RECORDER
47     std::unique_ptr<IRecorderEngine> CreateRecorderEngine(int32_t appUid, int32_t appPid, uint32_t appTokenId,
48         uint64_t appFullTokenId) override;
49 #endif
50 #ifdef SUPPORT_TRANSCODER
51     std::unique_ptr<ITransCoderEngine> CreateTransCoderEngine(int32_t appUid, int32_t appPid, uint32_t appTokenId,
52         uint64_t appFullTokenId) override;
53 #endif
54 #ifdef SUPPORT_METADATA
55     std::unique_ptr<IAVMetadataHelperEngine> CreateAVMetadataHelperEngine() override;
56 #endif
57 };
58 
Score(Scene scene,const int32_t & appUid,const std::string & uri)59 int32_t HstEngineFactory::Score(Scene scene, const int32_t& appUid, const std::string& uri)
60 {
61     MEDIA_LOG_E("Score in");
62     (void)scene;
63     (void)uri;
64     (void)appUid;
65 
66     return MAX_SCORE;
67 }
68 
69 #ifdef SUPPORT_RECORDER
CreateRecorderEngine(int32_t appUid,int32_t appPid,uint32_t appTokenId,uint64_t appFullTokenId)70 std::unique_ptr<IRecorderEngine> HstEngineFactory::CreateRecorderEngine(
71     int32_t appUid, int32_t appPid, uint32_t appTokenId, uint64_t appFullTokenId)
72 {
73     MEDIA_LOG_E("CreateRecorderEngine enter.");
74     auto recorder = std::unique_ptr<HiRecorderImpl>(new (std::nothrow) HiRecorderImpl(
75         appUid, appPid, appTokenId, appFullTokenId));
76     if (recorder && recorder->Init() == 0) {
77         return recorder;
78     }
79     MEDIA_LOG_E("create recorder failed or recorder init failed");
80     return nullptr;
81 }
82 #endif
83 
84 #ifdef SUPPORT_TRANSCODER
CreateTransCoderEngine(int32_t appUid,int32_t appPid,uint32_t appTokenId,uint64_t appFullTokenId)85 std::unique_ptr<ITransCoderEngine> HstEngineFactory::CreateTransCoderEngine(
86     int32_t appUid, int32_t appPid, uint32_t appTokenId, uint64_t appFullTokenId)
87 {
88     MEDIA_LOG_E("CreateTransCoderEngine enter.");
89     auto transCoder = std::make_unique<HiTransCoderImpl>(appUid, appPid, appTokenId, appFullTokenId);
90     if (transCoder && transCoder->Init() == 0) {
91         return transCoder;
92     }
93     MEDIA_LOG_E("create transCoder failed or transCoder init failed");
94     return nullptr;
95 }
96 #endif
97 
98 #ifdef SUPPORT_PLAYER
CreatePlayerEngine(int32_t uid,int32_t pid,uint32_t tokenId)99 std::unique_ptr<IPlayerEngine> HstEngineFactory::CreatePlayerEngine(int32_t uid, int32_t pid, uint32_t tokenId)
100 {
101     MEDIA_LOG_I("Hst CreatePlayerEngine enter.");
102     auto player = std::unique_ptr<HiPlayerImpl>(new (std::nothrow) HiPlayerImpl(
103         uid, pid, tokenId, 0));
104     if (player) {
105         return player;
106     }
107     MEDIA_LOG_E("create player failed");
108     return nullptr;
109 }
110 #endif
111 
112 #ifdef SUPPORT_METADATA
CreateAVMetadataHelperEngine()113 std::unique_ptr<IAVMetadataHelperEngine> HstEngineFactory::CreateAVMetadataHelperEngine()
114 {
115     MEDIA_LOG_I("CreateAVMetadataHelperEngine enter.");
116     auto helper = std::make_unique<AVMetadataHelperImpl>();
117     if (helper == nullptr) {
118         MEDIA_LOG_E("create AVMetadataHelperImpl failed");
119         return nullptr;
120     }
121     return helper;
122 }
123 #endif
124 } // namespace Media
125 } // namespace OHOS
126 
127 #ifdef __cplusplus
128 extern "C" {
129 #endif
CreateEngineFactory()130 __attribute__((visibility("default"))) OHOS::Media::IEngineFactory *CreateEngineFactory()
131 {
132     return new (std::nothrow) OHOS::Media::HstEngineFactory();
133 }
134 #ifdef __cplusplus
135 }
136 #endif