1 /* 2 * Copyright (c) 2024 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 DRAGGING_PLAYER_AGENT_H 17 #define DRAGGING_PLAYER_AGENT_H 18 19 #include <atomic> 20 #include "dragging_player.h" 21 #include "common/status.h" 22 #include "osal/task/task.h" 23 #include "pipeline/pipeline.h" 24 25 namespace OHOS { 26 namespace Media { 27 using namespace std; 28 using namespace Pipeline; 29 30 enum class DraggingMode : uint8_t { 31 DRAGGING_NONE = 0, 32 DRAGGING_CLOSEST = 1, 33 DRAGGING_CONTINUOUS = 2, 34 }; 35 36 class DraggingDelegator : public std::enable_shared_from_this<DraggingDelegator> { 37 public: DraggingDelegator(const shared_ptr<OHOS::Media::Pipeline::Pipeline> pipeline,const shared_ptr<DemuxerFilter> demuxer,const shared_ptr<DecoderSurfaceFilter> decoder,const string & playerId)38 DraggingDelegator( 39 const shared_ptr<OHOS::Media::Pipeline::Pipeline> pipeline, 40 const shared_ptr<DemuxerFilter> demuxer, 41 const shared_ptr<DecoderSurfaceFilter> decoder, 42 const string &playerId) : pipeline_(pipeline), demuxer_(demuxer), decoder_(decoder), playerId_(playerId) {}; ~DraggingDelegator()43 virtual ~DraggingDelegator() {}; 44 virtual Status Init() = 0; 45 virtual void UpdateSeekPos(int64_t seekMs) = 0; 46 virtual void Release() = 0; 47 ConsumeVideoFrame(const shared_ptr<AVBuffer> avBuffer,uint32_t bufferIndex)48 virtual void ConsumeVideoFrame(const shared_ptr<AVBuffer> avBuffer, uint32_t bufferIndex) 49 { 50 if (decoder_ != nullptr) { 51 return decoder_->ConsumeVideoFrame(bufferIndex, true, -1); 52 } 53 } 54 IsVideoStreamDiscardable(const shared_ptr<AVBuffer> avBuffer)55 virtual bool IsVideoStreamDiscardable(const shared_ptr<AVBuffer> avBuffer) 56 { 57 (void)avBuffer; 58 return false; 59 } 60 61 protected: 62 shared_ptr<OHOS::Media::Pipeline::Pipeline> pipeline_ {nullptr}; 63 shared_ptr<DemuxerFilter> demuxer_ {nullptr}; 64 shared_ptr<DecoderSurfaceFilter> decoder_ {nullptr}; 65 string playerId_ {}; 66 }; 67 68 class DraggingPlayerAgent : public enable_shared_from_this<DraggingPlayerAgent> { 69 public: 70 static shared_ptr<DraggingPlayerAgent> Create( 71 const shared_ptr<OHOS::Media::Pipeline::Pipeline> pipeline, 72 const shared_ptr<DemuxerFilter> demuxer, 73 const shared_ptr<DecoderSurfaceFilter> decoder, 74 const string &playerId); 75 static bool IsDraggingSupported(const shared_ptr<DemuxerFilter> demuxer, 76 const shared_ptr<DecoderSurfaceFilter> decoder); 77 78 using CreateFunc = DraggingPlayer *(*)(); 79 using DestroyFunc = void (*)(DraggingPlayer *); 80 using CheckSupportedFunc = bool (*) (DemuxerFilter *, DecoderSurfaceFilter *); 81 static CreateFunc createFunc_; 82 static DestroyFunc destroyFunc_; 83 static CheckSupportedFunc checkSupportedFunc_; DraggingPlayerAgent()84 DraggingPlayerAgent() {}; 85 DraggingPlayerAgent(const DraggingPlayerAgent &) = delete; 86 DraggingPlayerAgent( 87 const shared_ptr<OHOS::Media::Pipeline::Pipeline> pipeline, 88 const shared_ptr<DemuxerFilter> demuxer, 89 const shared_ptr<DecoderSurfaceFilter> decoder, 90 const string &playerId); 91 DraggingPlayerAgent operator=(const DraggingPlayerAgent &) = delete; 92 ~DraggingPlayerAgent(); 93 Status Init(); 94 void UpdateSeekPos(int64_t seekMs); 95 void Release(); 96 mutex draggingMutex_ {}; 97 DraggingMode GetDraggingMode(); 98 99 private: 100 static bool loaded_; 101 static bool LoadSymbol(); 102 static void *LoadLibrary(); 103 static bool CheckSymbol(void *handler); 104 static mutex mtx_; 105 static void *handler_; 106 shared_ptr<OHOS::Media::Pipeline::Pipeline> pipeline_ {nullptr}; 107 shared_ptr<DemuxerFilter> demuxer_ {nullptr}; 108 shared_ptr<DecoderSurfaceFilter> decoder_ {nullptr}; 109 string playerId_ {}; 110 shared_ptr<DraggingDelegator> delegator_ {nullptr}; 111 bool isReleased_ {false}; 112 DraggingMode draggingMode_ {DraggingMode::DRAGGING_CLOSEST}; 113 }; 114 115 class SeekContinuousDelegator : public DraggingDelegator { 116 public: 117 static shared_ptr<SeekContinuousDelegator> Create( 118 const shared_ptr<OHOS::Media::Pipeline::Pipeline> pipeline, 119 const shared_ptr<DemuxerFilter> demuxer, 120 const shared_ptr<DecoderSurfaceFilter> decoder, 121 const string &playerId); 122 explicit SeekContinuousDelegator( 123 const shared_ptr<OHOS::Media::Pipeline::Pipeline> pipeline, 124 const shared_ptr<DemuxerFilter> demuxer, 125 const shared_ptr<DecoderSurfaceFilter> decoder, 126 const string &playerId); 127 ~SeekContinuousDelegator() override; 128 Status Init() override; 129 void UpdateSeekPos(int64_t seekMs) override; 130 void Release() override; 131 132 void ConsumeVideoFrame(const shared_ptr<AVBuffer> avBuffer, uint32_t bufferIndex) override; 133 bool IsVideoStreamDiscardable(const shared_ptr<AVBuffer> avBuffer) override; 134 135 private: 136 void StopDragging(int64_t seekCnt); 137 138 DraggingPlayer *draggingPlayer_ {nullptr}; 139 shared_ptr<VideoStreamReadyCallback> videoStreamReadyCb_ {nullptr}; 140 shared_ptr<VideoFrameReadyCallback> videoFrameReadyCb_ {nullptr}; 141 bool isReleased_ {false}; 142 unique_ptr<OHOS::Media::Task> monitorTask_; 143 atomic<int64_t> seekCnt_ {0}; 144 mutex draggingMutex_ {}; 145 string threadName_ {}; 146 }; 147 148 class SeekClosestDelegator : public DraggingDelegator { 149 public: 150 static shared_ptr<SeekClosestDelegator> Create( 151 const shared_ptr<OHOS::Media::Pipeline::Pipeline> pipeline, 152 const shared_ptr<DemuxerFilter> demuxer, 153 const shared_ptr<DecoderSurfaceFilter> decoder, 154 const string &playerId); 155 explicit SeekClosestDelegator( 156 const shared_ptr<OHOS::Media::Pipeline::Pipeline> pipeline, 157 const shared_ptr<DemuxerFilter> demuxer, 158 const shared_ptr<DecoderSurfaceFilter> decoder, 159 const string &playerId); 160 ~SeekClosestDelegator() override; 161 Status Init() override; 162 void UpdateSeekPos(int64_t seekMs) override; 163 void Release() override; 164 165 private: 166 void SeekJob(); 167 void DoSeek(int64_t seekTimeMs); 168 bool isReleased_ {false}; 169 string threadName_ {}; 170 unique_ptr<Task> seekTask_ {nullptr}; 171 mutex seekClosestMutex_ {}; 172 mutex queueMutex_ {}; 173 vector<int64_t> seekTimeMsQue_ {}; 174 int64_t curSeekTimeMs_ {-1}; 175 }; 176 177 class DraggingDelegatorFactory { 178 public: 179 static shared_ptr<DraggingDelegator> CreateDelegator( 180 const shared_ptr<OHOS::Media::Pipeline::Pipeline> pipeline, 181 const shared_ptr<DemuxerFilter> demuxer, 182 const shared_ptr<DecoderSurfaceFilter> decoder, 183 const string &playerId, 184 DraggingMode &draggingMode); 185 }; 186 } // namespace Media 187 } // namespace OHOS 188 #endif // DRAGGING_PLAYER_AGENT_H