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 RECORDER_PIPELINE_H 17 #define RECORDER_PIPELINE_H 18 19 #include <cstdint> 20 #include <list> 21 #include <memory> 22 #include <string> 23 #include <condition_variable> 24 #include <mutex> 25 #include <map> 26 #include <atomic> 27 #include <gst/gst.h> 28 #include "nocopyable.h" 29 #include "recorder.h" 30 #include "recorder_inner_defines.h" 31 #include "recorder_param.h" 32 #include "recorder_element.h" 33 #include "recorder_message_processor.h" 34 35 namespace OHOS { 36 namespace Media { 37 using RecorderMsgNotifier = std::function<void(const RecorderMessage &)>; 38 using RecorderExecuteInCmdQ = std::function<int32_t(const std::shared_ptr<TaskHandler<int32_t>> &, const bool &)>; 39 40 struct RecorderPipelineDesc { 41 struct LinkDesc { 42 std::shared_ptr<RecorderElement> dstElem; 43 std::string srcPad; 44 std::string sinkPad; 45 bool isSrcPadStatic; 46 bool isSinkPadStatic; 47 }; 48 49 std::list<std::shared_ptr<RecorderElement>> allElems; 50 std::map<int32_t, std::shared_ptr<RecorderElement>> srcElems; 51 std::shared_ptr<RecorderElement> muxerSinkBin; 52 std::map<std::shared_ptr<RecorderElement>, LinkDesc> allLinkDescs; 53 }; 54 55 class RecorderPipeline : public NoCopyable { 56 public: 57 explicit RecorderPipeline(std::shared_ptr<RecorderPipelineDesc> desc); 58 ~RecorderPipeline(); 59 60 int32_t Init(); 61 int32_t Prepare(); 62 int32_t Start(); 63 int32_t Pause(); 64 int32_t Resume(); 65 int32_t Stop(bool isDrainAll); 66 int32_t Reset(); 67 int32_t SetParameter(int32_t sourceId, const RecorderParam &recParam); 68 int32_t GetParameter(int32_t sourceId, RecorderParam &recParam); 69 void SetNotifier(RecorderMsgNotifier notifier); 70 void SetExecuteInCmdQ(RecorderExecuteInCmdQ executeInCmdQ); 71 void Dump(); 72 73 private: 74 using ElemAction = std::function<int32_t(RecorderElement &)>; 75 int32_t DoElemAction(const ElemAction &action, bool needAllSucc = true); 76 int32_t SyncWaitChangeState(GstState targetState); 77 int32_t PostAndSyncWaitEOS(); 78 bool SyncWaitEOS(); 79 void DrainBuffer(bool isDrainAll); 80 void ClearResource(); 81 void OnNotifyMsgProcResult(const RecorderMessage &msg); 82 void ProcessInfoMessage(const RecorderMessage &msg); 83 void ProcessErrorMessage(const RecorderMessage &msg); 84 void ProcessFeatureMessage(const RecorderMessage &msg); 85 void NotifyMessage(const RecorderMessage &msg); 86 bool CheckStopForError(const RecorderMessage &msg); 87 void StopForError(const RecorderMessage &msg); 88 int32_t BypassOneSource(int32_t sourceId); 89 90 friend class RecorderPipelineLinkHelper; 91 92 std::shared_ptr<RecorderPipelineDesc> desc_; 93 RecorderMsgNotifier notifier_; 94 RecorderExecuteInCmdQ executeInCmdQ_; 95 std::unique_ptr<RecorderMsgProcessor> msgProcessor_; 96 97 GstPipeline *gstPipeline_ = nullptr; 98 std::condition_variable gstPipeCond_; 99 std::mutex gstPipeMutex_; 100 std::mutex cmdQMutex_; 101 bool asyncDone_ = false; 102 bool eosDone_ = false; 103 bool isStarted_ = false; 104 GstState currState_ = GST_STATE_NULL; 105 std::atomic<bool> errorState_ { false }; 106 std::set<bool> errorSources_; 107 }; 108 } // namespace Media 109 } // namespace OHOS 110 #endif 111