1 /* 2 * Copyright (C) 2025 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 LPP_VIDEO_STREAMER_SERVICE_SERVER_H 17 #define LPP_VIDEO_STREAMER_SERVICE_SERVER_H 18 19 #include <atomic> 20 #include <mutex> 21 22 #include "i_lpp_video_streamer_service.h" 23 #include "i_lpp_video_streamer.h" 24 #include "nocopyable.h" 25 26 namespace OHOS { 27 namespace Media { 28 29 enum class VideoState : int32_t { 30 CREATED = 0, 31 INITIALIZED = 1, 32 READY = 2, 33 DECODING = 3, 34 RENDERING = 4, 35 PAUSED = 5, 36 EOS = 6, 37 STOPPED = 7, 38 RELEASED = 8, 39 ERROR = 32, 40 }; 41 42 class LppLinkCallbackLooper; 43 class LppVideoStreamerServer : public ILppVideoStreamerService, public NoCopyable, public ILppVideoStreamerEngineObs { 44 public: 45 static std::shared_ptr<ILppVideoStreamerService> Create(); 46 LppVideoStreamerServer(); 47 virtual ~LppVideoStreamerServer(); 48 49 int32_t Init(const std::string &mime) override; 50 51 int32_t SetParameter(const Format ¶m) override; 52 53 int32_t Configure(const Format ¶m) override; 54 55 int32_t Prepare() override; 56 57 int32_t Start() override; 58 59 int32_t Pause() override; 60 61 int32_t Resume() override; 62 63 int32_t Flush() override; 64 65 int32_t Stop() override; 66 67 int32_t Reset() override; 68 69 int32_t Release() override; 70 71 int32_t StartDecode() override; 72 73 int32_t StartRender() override; 74 75 int32_t SetOutputSurface(sptr<Surface> surface) override; 76 77 int32_t SetSyncAudioStreamer(AudioStreamer *audioStreamer) override; 78 79 int32_t SetTargetStartFrame(const int64_t targetPts, const int timeoutMs) override; 80 81 int32_t SetVolume(float volume) override; 82 83 int32_t SetPlaybackSpeed(float speed) override; 84 85 int32_t ReturnFrames(sptr<LppDataPacket> framePacket) override; 86 87 int32_t RegisterCallback() override; 88 89 int32_t SetLppVideoStreamerCallback(const std::shared_ptr<VideoStreamerCallback> &callback) override; 90 91 int32_t SetLppAudioStreamerId(const std::string audioStreamId) override; 92 93 std::string GetStreamerId() override; 94 95 int32_t RenderFirstFrame() override; 96 97 void OnDataNeeded(const int32_t maxBufferSize, const int32_t maxFrameNum) override; 98 bool OnAnchorUpdateNeeded(int64_t &anchorPts, int64_t &anchorClk) override; 99 void OnError(const MediaServiceErrCode errCode, const std::string &errMsg) override; 100 void OnEos() override; 101 void OnRenderStarted() override; 102 void OnTargetArrived(const int64_t targetPts, const bool isTimeout) override; 103 void OnFirstFrameReady() override; 104 void OnStreamChanged(Format &format) override; 105 106 private: 107 int32_t CreateStreamerEngine(); 108 bool StateEnter(VideoState targetState, const std::string funcName = ""); 109 bool StateCheck(VideoState curState); 110 bool ErrorCheck(int32_t errorCode); 111 112 std::shared_ptr<VideoStreamerCallback> callback_ = nullptr; 113 114 std::string mime_ {}; 115 std::shared_ptr<ILppVideoStreamerEngine> streamerEngine_ = nullptr; 116 uint32_t appTokenId_ = 0; 117 int32_t appUid_ = 0; 118 int32_t appPid_ = 0; 119 std::string appName_ {}; 120 121 VideoState state_ {VideoState::CREATED}; 122 std::mutex stateMutex_ {}; 123 std::atomic<bool> isFirstFrameDecoded_ {false}; 124 std::atomic<bool> isFirstFrameRendered_ {false}; 125 }; 126 } // namespace Media 127 } // namespace OHOS 128 #endif // LPP_VIDEO_STREAMER_SERVICE_SERVER_H 129