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 "gst_msg_converter.h"
17 #include <functional>
18 #include <unordered_map>
19 #include "media_errors.h"
20 #include "media_log.h"
21 #include "gst_utils.h"
22
23 namespace {
24 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "GstMsgConvDefault"};
25 }
26
27 namespace OHOS {
28 namespace Media {
ConvertErrorMessage(GstMessage & gstMsg,InnerMessage & innerMsg)29 static int32_t ConvertErrorMessage(GstMessage &gstMsg, InnerMessage &innerMsg)
30 {
31 GError *error = nullptr;
32 gchar *debug = nullptr;
33 gst_message_parse_error(const_cast<GstMessage *>(&gstMsg), &error, &debug);
34 if (error == nullptr || debug == nullptr) {
35 return MSERR_UNKNOWN;
36 }
37 MEDIA_LOGE("[ERROR] %{public}s, %{public}s", error->message, debug);
38
39 innerMsg.type = INNER_MSG_ERROR;
40 innerMsg.detail1 = MSERR_UNKNOWN;
41
42 // need to add more detail error msg convert
43 g_error_free(error);
44 g_free(debug);
45 return MSERR_OK;
46 }
47
ConvertWarningMessage(GstMessage & gstMsg,InnerMessage & innerMsg)48 static int32_t ConvertWarningMessage(GstMessage &gstMsg, InnerMessage &innerMsg)
49 {
50 GError *error = nullptr;
51 gchar *debug = nullptr;
52 gst_message_parse_warning(const_cast<GstMessage *>(&gstMsg), &error, &debug);
53 if (error == nullptr || debug == nullptr) {
54 return MSERR_UNKNOWN;
55 }
56 MEDIA_LOGW("[WARNING] %{public}s, %{public}s", error->message, debug);
57
58 innerMsg.type = INNER_MSG_WARNING;
59 innerMsg.detail1 = MSERR_UNKNOWN;
60
61 g_error_free(error);
62 g_free(debug);
63 return MSERR_OK;
64 }
65
ConvertInfoMessage(GstMessage & gstMsg,InnerMessage & innerMsg)66 static int32_t ConvertInfoMessage(GstMessage &gstMsg, InnerMessage &innerMsg)
67 {
68 GError *error = nullptr;
69 gchar *debug = nullptr;
70 gst_message_parse_info(const_cast<GstMessage *>(&gstMsg), &error, &debug);
71 if (error == nullptr || debug == nullptr) {
72 return MSERR_UNKNOWN;
73 }
74 MEDIA_LOGI("[INFO] %{public}s, %{public}s", error->message, debug);
75
76 innerMsg.type = INNER_MSG_INFO;
77 innerMsg.detail1 = MSERR_UNKNOWN;
78
79 g_error_free(error);
80 g_free(debug);
81 return MSERR_OK;
82 }
83
ConvertStateChangedMessage(GstMessage & gstMsg,InnerMessage & innerMsg)84 static int32_t ConvertStateChangedMessage(GstMessage &gstMsg, InnerMessage &innerMsg)
85 {
86 GstState oldState = GST_STATE_VOID_PENDING;
87 GstState newState = GST_STATE_VOID_PENDING;
88 GstState pendingState = GST_STATE_VOID_PENDING;
89 gst_message_parse_state_changed(const_cast<GstMessage *>(&gstMsg), &oldState, &newState, &pendingState);
90 MEDIA_LOGI("%{public}s change state from %{public}s to %{public}s", ELEM_NAME(GST_MESSAGE_SRC(&gstMsg)),
91 gst_element_state_get_name(oldState), gst_element_state_get_name(newState));
92
93 innerMsg.type = INNER_MSG_STATE_CHANGED;
94 innerMsg.detail1 = static_cast<int32_t>(oldState);
95 innerMsg.detail2 = static_cast<int32_t>(newState);
96 innerMsg.extend = gstMsg.src;
97
98 return MSERR_OK;
99 }
100
101 static const std::unordered_map<GstMessageType, InnerMsgType> SIMPLE_MSG_TYPE_MAPPING = {
102 { GST_MESSAGE_DURATION_CHANGED, INNER_MSG_DURATION_CHANGED },
103 { GST_MESSAGE_EOS, INNER_MSG_EOS },
104 { GST_MESSAGE_ASYNC_DONE, INNER_MSG_ASYNC_DONE },
105 };
106
107 using MsgConvFunc = std::function<int32_t(GstMessage&, InnerMessage&)>;
108 static const std::unordered_map<GstMessageType, MsgConvFunc> MSG_CONV_FUNC_TABLE = {
109 { GST_MESSAGE_ERROR, ConvertErrorMessage },
110 { GST_MESSAGE_WARNING, ConvertWarningMessage },
111 { GST_MESSAGE_INFO, ConvertInfoMessage },
112 { GST_MESSAGE_STATE_CHANGED, ConvertStateChangedMessage },
113 };
114
ConvertToInnerMsg(GstMessage & gstMsg,InnerMessage & innerMsg) const115 int32_t GstMsgConverterDefault::ConvertToInnerMsg(GstMessage &gstMsg, InnerMessage &innerMsg) const
116 {
117 innerMsg.type = INNER_MSG_UNKNOWN;
118
119 if (SIMPLE_MSG_TYPE_MAPPING.count(gstMsg.type) != 0) {
120 innerMsg.type = SIMPLE_MSG_TYPE_MAPPING.at(gstMsg.type);
121 MEDIA_LOGI("convert gst msg type: %{public}s", GST_MESSAGE_TYPE_NAME(&gstMsg));
122 return MSERR_OK;
123 }
124
125 if (MSG_CONV_FUNC_TABLE.count(gstMsg.type) == 0) {
126 return MSERR_OK;
127 }
128
129 return MSG_CONV_FUNC_TABLE.at(gstMsg.type)(gstMsg, innerMsg);
130 }
131 } // namespace Media
132 } // namespace OHOS