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 GST_UTILS_H 17 #define GST_UTILS_H 18 19 #include <memory> 20 #include <vector> 21 #include <string> 22 #include <string_view> 23 #include <unordered_map> 24 #include <glib/glib.h> 25 #include <gst/gst.h> 26 #include "nocopyable.h" 27 28 namespace OHOS { 29 namespace Media { 30 #define EXPORT_API __attribute__((visibility("default"))) 31 32 #define ELEM_NAME(elem) (GST_ELEMENT_NAME(elem) != nullptr) ? GST_ELEMENT_NAME(elem) : "unknown" 33 34 #define PAD_NAME(pad) (GST_PAD_NAME(pad) != nullptr) ? GST_PAD_NAME(pad) : "unknown" 35 36 #define PAD_PARENT_NAME(pad) (GST_PAD_PARENT(pad) != nullptr) ? \ 37 ((GST_ELEMENT_NAME(GST_PAD_PARENT(pad)) != nullptr) ? \ 38 (GST_ELEMENT_NAME(GST_PAD_PARENT(pad))) : "unknown") : "unknown" 39 40 #define STRUCTURE_NAME(struc) (gst_structure_get_name(struc) != nullptr) ? gst_structure_get_name(struc) : "" 41 42 #define GST_OBJECT_UNREF_IF_NOT_NULL(obj) \ 43 do { \ 44 if (obj != nullptr) { \ 45 gst_object_unref(obj); \ 46 obj = nullptr; \ 47 } \ 48 } while (0) 49 50 EXPORT_API bool MatchElementByMeta( 51 const GstElement &elem, const std::string_view &metaKey, const std::vector<std::string_view> &expectedMetaFields); 52 53 template <typename T> 54 class ThizWrapper : public NoCopyable { 55 public: ThizWrapper(std::weak_ptr<T> thiz)56 explicit ThizWrapper(std::weak_ptr<T> thiz) : thiz_(thiz) {} 57 ~ThizWrapper() = default; 58 TakeStrongThiz(gpointer userData)59 static std::shared_ptr<T> TakeStrongThiz(gpointer userData) 60 { 61 if (userData == nullptr) { 62 return nullptr; 63 } 64 65 ThizWrapper<T> *wrapper = reinterpret_cast<ThizWrapper<T> *>(userData); 66 return wrapper->thiz_.lock(); 67 } 68 OnDestory(gpointer wrapper)69 static void OnDestory(gpointer wrapper) 70 { 71 if (wrapper == nullptr) { 72 return; 73 } 74 delete reinterpret_cast<ThizWrapper<T> *>(wrapper); 75 } 76 77 private: 78 std::weak_ptr<T> thiz_; 79 }; 80 } // namespace Media 81 } // namespace OHOS 82 #endif // GST_UTILS_H 83