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