1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved. 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_APPSRC_ENGINE_H 17 #define GST_APPSRC_ENGINE_H 18 19 #include <vector> 20 #include <gst/gst.h> 21 #include <vector> 22 #include "task_queue.h" 23 #include "media_data_source.h" 24 #include "appsrc_memory.h" 25 #include "inner_msg_define.h" 26 #include "gst/app/gstappsrc.h" 27 #include "gst_shmemory_wrap_allocator.h" 28 29 namespace OHOS { 30 namespace Media { 31 class GstAppsrcEngine : public std::enable_shared_from_this<GstAppsrcEngine>, public NoCopyable { 32 public: 33 using AppsrcErrorNotifier = std::function<bool(const InnerMessage &msg)>; 34 static std::shared_ptr<GstAppsrcEngine> Create(const std::shared_ptr<IMediaDataSource> &dataSrc); 35 GstAppsrcEngine(const std::shared_ptr<IMediaDataSource> &dataSrc, const int64_t size); 36 ~GstAppsrcEngine(); 37 int32_t Init(); 38 int32_t Prepare(); 39 void Stop(); 40 int32_t SetAppsrc(GstElement *appSrc); 41 int32_t SetCallback(AppsrcErrorNotifier notifier); 42 bool IsLiveMode() const; 43 void SetVideoMode(); 44 void SetPushBufferMode(); 45 void DecoderSwitch(); 46 void RecoverParamFromDecSwitch(); 47 private: 48 void SetCallBackForAppSrc(); 49 void ClearAppsrc(); 50 void ResetConfig(); 51 static void NeedData(const GstElement *appSrc, uint32_t size, gpointer self); 52 void NeedDataInner(uint32_t size); 53 static gboolean SeekData(const GstElement *appSrc, uint64_t seekPos, gpointer self); 54 gboolean SeekDataInner(uint64_t pos); 55 void PullTask(); 56 int32_t PullBuffer(); 57 void PushTask(); 58 int32_t PushEos(); 59 int32_t PushBuffer(uint32_t pushSize); 60 int32_t PushBufferWithCopy(uint32_t pushSize); 61 int32_t AddSrcMem(uint32_t bufferSize); 62 void OnError(int32_t errorCode, const std::string &message); 63 bool OnBufferReport(int32_t percent); 64 bool ReportMessage(const InnerMessage &msg); 65 void FreePointerMemory(uint32_t offset, uint32_t length, uint32_t subscript); 66 bool IsConnectTimeout(); 67 std::shared_ptr<IMediaDataSource> dataSrc_ = nullptr; 68 const int64_t size_ = 0; 69 std::mutex mutex_; 70 std::mutex freeMutex_; 71 std::mutex pullMutex_; 72 std::condition_variable pullCond_; 73 std::condition_variable pushCond_; 74 GstElement *appSrc_ = nullptr; 75 TaskQueue pullTaskQue_; 76 TaskQueue pushTaskQue_; 77 GstAppStreamType streamType_ = GST_APP_STREAM_TYPE_STREAM; 78 AppsrcErrorNotifier notifier_; 79 std::vector<gulong> callbackIds_; 80 bool atEos_ = false; 81 bool needData_ = false; 82 uint32_t needDataSize_ = 0; 83 bool isExit_ = false; 84 std::vector<std::shared_ptr<AppsrcMemory>> appSrcMemVec_; 85 std::shared_ptr<AppsrcMemory> appSrcMem_; 86 GstShMemoryWrapAllocator *allocator_ = nullptr; 87 int64_t timer_ = 0; 88 bool copyMode_ = false; 89 bool videoMode_ = false; 90 uint32_t curSubscript_ = 0; 91 bool decoderSwitch_ = false; 92 bool playState_ = true; 93 }; 94 } // namespace Media 95 } // namespace OHOS 96 #endif /* GST_APPSRC_ENGINE_H */ 97