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 VIDEO_CALLBACK_NAPI_H_ 17 #define VIDEO_CALLBACK_NAPI_H_ 18 19 #include <queue> 20 #include <uv.h> 21 #include "player_callback_napi.h" 22 #include "video_player_napi.h" 23 24 namespace OHOS { 25 namespace Media { 26 enum class AsyncWorkType : int32_t { 27 ASYNC_WORK_PREPARE = 0, 28 ASYNC_WORK_PLAY, 29 ASYNC_WORK_PAUSE, 30 ASYNC_WORK_STOP, 31 ASYNC_WORK_RESET, 32 ASYNC_WORK_SEEK, 33 ASYNC_WORK_SPEED, 34 ASYNC_WORK_VOLUME, 35 ASYNC_WORK_BITRATE, 36 ASYNC_WORK_INVALID, 37 }; 38 39 struct VideoPlayerAsyncContext : public MediaAsyncContext { VideoPlayerAsyncContextVideoPlayerAsyncContext40 explicit VideoPlayerAsyncContext(napi_env env) : MediaAsyncContext(env) {} 41 ~VideoPlayerAsyncContext() = default; 42 VideoPlayerNapi *jsPlayer = nullptr; 43 AsyncWorkType asyncWorkType = AsyncWorkType::ASYNC_WORK_INVALID; 44 double volume = 1.0f; // default volume level 45 int32_t seekPosition = 0; 46 int32_t seekMode = SEEK_PREVIOUS_SYNC; 47 int32_t speedMode = SPEED_FORWARD_1_00_X; 48 int32_t bitRate = 0; 49 std::string surface = ""; 50 }; 51 52 class VideoCallbackNapi : public PlayerCallbackNapi { 53 public: 54 explicit VideoCallbackNapi(napi_env env); 55 ~VideoCallbackNapi() override; 56 57 void OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody) override; 58 void OnError(int32_t errorCode, const std::string &errorMsg) override; 59 PlayerStates GetCurrentState() const override; GetVideoWidth()60 int32_t GetVideoWidth() const 61 { 62 return width_; 63 } GetVideoHeight()64 int32_t GetVideoHeight() const 65 { 66 return height_; 67 } 68 void QueueAsyncWork(VideoPlayerAsyncContext *context); 69 void ClearAsyncWork(bool error, const std::string &msg); 70 71 protected: 72 void OnStateChangeCb(PlayerStates state) override; 73 74 private: 75 void OnStartRenderFrameCb() const; 76 void OnPlaybackCompleteCb() const; 77 void OnVideoSizeChangedCb(const Format &infoBody); 78 void OnSeekDoneCb(int32_t position); 79 void OnSpeedDoneCb(int32_t speedMode); 80 void OnVolumeDoneCb(); 81 void OnBitRateDoneCb(int32_t bitRate); 82 void OnBitRateCollectedCb(const Format &infoBody) const; 83 void DequeueAsyncWork(); 84 static void UvWorkCallBack(uv_work_t *work, int status); 85 void OnJsCallBack(VideoPlayerAsyncContext *context) const; 86 void ClearAsyncWorkWithErrorCode(MediaServiceExtErrCode errorCode, bool error, const std::string &msg); 87 88 std::mutex mutex_; 89 napi_env env_ = nullptr; 90 PlayerStates currentState_ = PLAYER_IDLE; 91 std::map<AsyncWorkType, std::queue<VideoPlayerAsyncContext *>> contextMap_; 92 int32_t width_ = 0; 93 int32_t height_ = 0; 94 }; 95 } // namespace Media 96 } // namespace OHOS 97 #endif // VIDEO_CALLBACK_NAPI_H_