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 "src_surface_impl.h"
17 #include "common_utils.h"
18 #include "media_log.h"
19
20 namespace {
21 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "SrcSurfaceImpl"};
22 }
23
24 namespace OHOS {
25 namespace Media {
SrcSurfaceImpl()26 SrcSurfaceImpl::SrcSurfaceImpl()
27 {
28 }
29
~SrcSurfaceImpl()30 SrcSurfaceImpl::~SrcSurfaceImpl()
31 {
32 if (src_ != nullptr) {
33 gst_object_unref(src_);
34 src_ = nullptr;
35 }
36 }
37
Init()38 int32_t SrcSurfaceImpl::Init()
39 {
40 src_ = GST_ELEMENT_CAST(gst_object_ref(gst_element_factory_make("surfacesrc", "src")));
41 CHECK_AND_RETURN_RET_LOG(src_ != nullptr, MSERR_UNKNOWN, "Failed to make gstsurfacevideosrc");
42 return MSERR_OK;
43 }
44
Configure(const std::shared_ptr<ProcessorConfig> & config)45 int32_t SrcSurfaceImpl::Configure(const std::shared_ptr<ProcessorConfig> &config)
46 {
47 CHECK_AND_RETURN_RET(config != nullptr && config->caps_ != nullptr, MSERR_UNKNOWN);
48 g_object_set(G_OBJECT(src_), "caps", config->caps_, nullptr);
49 return MSERR_OK;
50 }
51
CreateInputSurface(const std::shared_ptr<ProcessorConfig> & inputConfig)52 sptr<Surface> SrcSurfaceImpl::CreateInputSurface(const std::shared_ptr<ProcessorConfig> &inputConfig)
53 {
54 CHECK_AND_RETURN_RET(Configure(inputConfig) == MSERR_OK, nullptr);
55
56 GstStateChangeReturn ret = gst_element_set_state(src_, GST_STATE_READY);
57 CHECK_AND_RETURN_RET(ret != GST_STATE_CHANGE_FAILURE, nullptr);
58
59 GValue val = G_VALUE_INIT;
60 g_object_get_property(G_OBJECT(src_), "surface", &val);
61 gpointer surfaceObj = g_value_get_pointer(&val);
62 CHECK_AND_RETURN_RET_LOG(surfaceObj != nullptr, nullptr, "Failed to get surface");
63 sptr<Surface> surface = reinterpret_cast<Surface *>(surfaceObj);
64 return surface;
65 }
66
SetParameter(const Format & format)67 int32_t SrcSurfaceImpl::SetParameter(const Format &format)
68 {
69 int32_t value = 0;
70 if (format.GetValueType(std::string_view("suspend_input_surface")) == FORMAT_TYPE_INT32) {
71 if (format.GetIntValue("suspend_input_surface", value) && (value == 0 || value == 1)) {
72 g_object_set(src_, "suspend", static_cast<gboolean>(value), nullptr);
73 }
74 }
75
76 if (format.GetValueType(std::string_view("max_encoder_fps")) == FORMAT_TYPE_INT32) {
77 if (format.GetIntValue("max_encoder_fps", value) && value >= 0) {
78 g_object_set(src_, "max-framerate", static_cast<uint32_t>(value), nullptr);
79 }
80 }
81
82 if (format.GetValueType(std::string_view("repeat_frame_after")) == FORMAT_TYPE_INT32) {
83 if (format.GetIntValue("repeat_frame_after", value) && value >= 0) {
84 g_object_set(src_, "repeat", static_cast<uint32_t>(value), nullptr);
85 }
86 }
87
88 return MSERR_OK;
89 }
90
NotifyEos()91 int32_t SrcSurfaceImpl::NotifyEos()
92 {
93 gboolean value = TRUE;
94 g_object_set(src_, "notify-eos", value, nullptr);
95 return MSERR_OK;
96 }
97 } // namespace Media
98 } // namespace OHOS
99