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 #ifndef OHOS_MEDIA_RECORDER_MESSAGE_HANDLER_H 17 #define OHOS_MEDIA_RECORDER_MESSAGE_HANDLER_H 18 19 #include <gst/gstmessage.h> 20 #include "nocopyable.h" 21 #include "recorder_inner_defines.h" 22 23 namespace OHOS { 24 namespace Media { 25 /** 26 * @brief Recorder engine internal message type. 27 */ 28 enum RecorderMessageType : int32_t { 29 REC_MSG_ERROR, // see IRecorderEngineObs::ErrorType in i_recorder_engine.h 30 REC_MSG_INFO, // see IRecorderEngineObs::InfoType in i_recorder_engine.h 31 REC_MSG_FEATURE, // see RecorderMessageFeature 32 }; 33 34 /** 35 * @brief Recorder engine internal feature message type. 36 */ 37 enum RecorderMessageFeature : int32_t { 38 REC_MSG_FEATURE_ASYNC_DONE, 39 REC_MSG_FEATURE_EOS_DONE, 40 REC_MSG_FEATURE_STATE_CHANGE_DONE, 41 }; 42 43 /** 44 * @brief Recorder engine internal message structure define. 45 */ 46 struct RecorderMessage { 47 int32_t type; // see RecorderMessageType 48 int32_t code; 49 int32_t detail; 50 int32_t sourceId = INVALID_SOURCE_ID; 51 }; 52 53 /** 54 * @brief Recorder engine internal process raw message(GstMessage) result value. 55 */ 56 enum class RecorderMsgProcResult : uint8_t { 57 REC_MSG_PROC_OK, 58 REC_MSG_PROC_IGNORE, 59 REC_MSG_PROC_FAILED, 60 }; 61 62 /** 63 * @brief Recorder message handler base type, the subclass inherits to it to implement the message proccess. 64 * 65 * If the gst message's format is ohos custom, the engine transparently transmits the encoding value to the user. 66 * If the gst message's source is's not gstpipeline, the engine will deliver the message to RecorderMsgHandlers. 67 * If there no RecorderMsgHandler to process the gst error messages, the engine will take over. For other kind 68 * of messages, the engine will ignore it directly. 69 * 70 * The RecorderMsgHandlers should translate all the messages it receives as much as possible to give the 71 * user a more friendly message prompt. 72 */ 73 class RecorderMsgHandler : public NoCopyable { 74 public: 75 RecorderMsgHandler() = default; 76 virtual ~RecorderMsgHandler() = default; 77 78 virtual RecorderMsgProcResult OnMessageReceived(GstMessage &rawMsg, RecorderMessage &prettyMsg) = 0; 79 80 static RecorderMsgProcResult ProcessInfoMsgDefault(GstMessage &msg, RecorderMessage &prettyMsg); 81 static RecorderMsgProcResult ProcessWarningMsgDefault(GstMessage &msg, RecorderMessage &prettyMsg); 82 static RecorderMsgProcResult ProcessErrorMsgDefault(GstMessage &msg, RecorderMessage &prettyMsg); 83 }; 84 85 // not MT-safe 86 #define GST_MSG_PARSER_DEFINE(type, Type) \ 87 class Gst##Type##MsgParser { \ 88 public: \ 89 explicit Gst##Type##MsgParser(GstMessage &msg) : msg_(msg) {} \ 90 bool InitCheck() \ 91 { \ 92 FreeErrAndDbg(); \ 93 gst_message_parse_##type(&msg_, &error_, &debug_); \ 94 if (error_ == nullptr || debug_ == nullptr) { \ 95 return false; \ 96 } \ 97 return true; \ 98 } \ 99 ~Gst##Type##MsgParser() \ 100 { \ 101 FreeErrAndDbg(); \ 102 } \ 103 const GError *GetErr() const \ 104 { \ 105 return error_; \ 106 } \ 107 const gchar *GetDbg() const \ 108 { \ 109 return debug_; \ 110 } \ 111 \ 112 DISALLOW_COPY_AND_MOVE(Gst##Type##MsgParser); \ 113 \ 114 private: \ 115 GError *error_ = nullptr; \ 116 gchar *debug_ = nullptr; \ 117 GstMessage &msg_; \ 118 \ 119 void FreeErrAndDbg() \ 120 { \ 121 if (error_ != nullptr) { \ 122 g_error_free(error_); \ 123 } \ 124 if (debug_ != nullptr) { \ 125 g_free(debug_); \ 126 } \ 127 } \ 128 } 129 130 GST_MSG_PARSER_DEFINE(info, Info); 131 GST_MSG_PARSER_DEFINE(warning, Warning); 132 GST_MSG_PARSER_DEFINE(error, Error); 133 } // namespace Media 134 } // namespace OHOS 135 #endif