1 /*
2 * Copyright (C) 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 #include "i_engine_factory.h"
17 #include "avcodec_engine_gst_impl.h"
18 #include "avcodeclist_engine_gst_impl.h"
19 #include "avmetadatahelper_engine_gst_impl.h"
20 #include "gst_loader.h"
21 #include "media_errors.h"
22 #include "media_log.h"
23 #include "nocopyable.h"
24 #include "player_engine_gst_impl.h"
25 #include "recorder_engine_gst_impl.h"
26 #include "avmuxer_engine_gst_impl.h"
27
28 namespace {
29 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "GstEngineFactory"};
30 }
31
32 namespace OHOS {
33 namespace Media {
34 class GstEngineFactory : public IEngineFactory, public NoCopyable {
35 public:
36 GstEngineFactory() = default;
37 ~GstEngineFactory() = default;
38
39 int32_t Score(Scene scene, const std::string &uri) override;
40 #ifdef SUPPORT_PLAYER
41 std::unique_ptr<IPlayerEngine> CreatePlayerEngine(int32_t uid = 0, int32_t pid = 0) override;
42 #endif
43 #ifdef SUPPORT_RECORDER
44 std::unique_ptr<IRecorderEngine> CreateRecorderEngine(int32_t appUid, int32_t appPid, uint32_t appTokenId) override;
45 #endif
46 #ifdef SUPPORT_METADATA
47 std::unique_ptr<IAVMetadataHelperEngine> CreateAVMetadataHelperEngine() override;
48 #endif
49 #ifdef SUPPORT_CODEC
50 std::unique_ptr<IAVCodecEngine> CreateAVCodecEngine() override;
51 std::unique_ptr<IAVCodecListEngine> CreateAVCodecListEngine() override;
52 #endif
53 #ifdef SUPPORT_MUXER
54 std::unique_ptr<IAVMuxerEngine> CreateAVMuxerEngine() override;
55 #endif
56 };
57
Score(Scene scene,const std::string & uri)58 int32_t GstEngineFactory::Score(Scene scene, const std::string &uri)
59 {
60 (void)uri;
61 (void)scene;
62 return MIN_SCORE + 1;
63 }
64
65 #ifdef SUPPORT_PLAYER
CreatePlayerEngine(int32_t uid,int32_t pid)66 std::unique_ptr<IPlayerEngine> GstEngineFactory::CreatePlayerEngine(int32_t uid, int32_t pid)
67 {
68 GstLoader::Instance().UpdateLogLevel();
69 return std::make_unique<PlayerEngineGstImpl>(uid, pid);
70 }
71 #endif
72
73 #ifdef SUPPORT_METADATA
CreateAVMetadataHelperEngine()74 std::unique_ptr<IAVMetadataHelperEngine> GstEngineFactory::CreateAVMetadataHelperEngine()
75 {
76 GstLoader::Instance().UpdateLogLevel();
77 return std::make_unique<AVMetadataHelperEngineGstImpl>();
78 }
79 #endif
80
81 #ifdef SUPPORT_RECORDER
CreateRecorderEngine(int32_t appUid,int32_t appPid,uint32_t appTokenId)82 std::unique_ptr<IRecorderEngine> GstEngineFactory::CreateRecorderEngine(
83 int32_t appUid, int32_t appPid, uint32_t appTokenId)
84 {
85 GstLoader::Instance().UpdateLogLevel();
86 auto engine = std::make_unique<RecorderEngineGstImpl>(appUid, appPid, appTokenId);
87 int32_t ret = engine->Init();
88 if (ret != MSERR_OK) {
89 MEDIA_LOGE("recorder engine init failed, ret = %{public}d", ret);
90 return nullptr;
91 }
92 return engine;
93 }
94 #endif
95
96 #ifdef SUPPORT_CODEC
CreateAVCodecEngine()97 std::unique_ptr<IAVCodecEngine> GstEngineFactory::CreateAVCodecEngine()
98 {
99 GstLoader::Instance().UpdateLogLevel();
100 return std::make_unique<AVCodecEngineGstImpl>();
101 }
102
CreateAVCodecListEngine()103 std::unique_ptr<IAVCodecListEngine> GstEngineFactory::CreateAVCodecListEngine()
104 {
105 GstLoader::Instance().UpdateLogLevel();
106 return std::make_unique<AVCodecListEngineGstImpl>();
107 }
108 #endif
109
110 #ifdef SUPPORT_MUXER
CreateAVMuxerEngine()111 std::unique_ptr<IAVMuxerEngine> GstEngineFactory::CreateAVMuxerEngine()
112 {
113 GstLoader::Instance().UpdateLogLevel();
114 return std::make_unique<AVMuxerEngineGstImpl>();
115 }
116 #endif
117 } // namespace Media
118 } // namespace OHOS
119
120 #ifdef __cplusplus
121 extern "C" {
122 #endif
CreateEngineFactory()123 __attribute__((visibility("default"))) OHOS::Media::IEngineFactory *CreateEngineFactory()
124 {
125 int32_t ret = OHOS::Media::GstLoader::Instance().SetUp();
126 if (ret != OHOS::Media::MSERR_OK) {
127 MEDIA_LOGE("Gst Engine setup failed, ret = %{public}d", ret);
128 return nullptr;
129 }
130 return new (std::nothrow) OHOS::Media::GstEngineFactory();
131 }
132 #ifdef __cplusplus
133 }
134 #endif