1 /* 2 * Copyright (c) 2021-2022 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_OUTPUT_NAPI_H_ 17 #define VIDEO_OUTPUT_NAPI_H_ 18 #include <securec.h> 19 20 #include "camera_log.h" 21 #include "napi/native_api.h" 22 #include "napi/native_node_api.h" 23 24 #include "output/video_output.h" 25 #include "hilog/log.h" 26 #include "camera_napi_utils.h" 27 #include "input/camera_manager.h" 28 29 #include <cinttypes> 30 #include <fstream> 31 #include <iostream> 32 #include <sstream> 33 #include <vector> 34 35 #include <fcntl.h> 36 #include <sys/stat.h> 37 #include <sys/time.h> 38 #include <sys/types.h> 39 #include "surface_utils.h" 40 41 namespace OHOS { 42 namespace CameraStandard { 43 static const char CAMERA_VIDEO_OUTPUT_NAPI_CLASS_NAME[] = "VideoOutput"; 44 45 class VideoCallbackListener : public VideoStateCallback { 46 public: 47 explicit VideoCallbackListener(napi_env env); 48 ~VideoCallbackListener() = default; 49 50 void OnFrameStarted() const override; 51 void OnFrameEnded(const int32_t frameCount) const override; 52 void OnError(const int32_t errorCode) const override; 53 void SetCallbackRef(const std::string &eventType, const napi_ref &callbackRef); 54 55 private: 56 void UpdateJSCallback(std::string propName, const int32_t value) const; 57 void UpdateJSCallbackAsync(std::string propName, const int32_t value) const; 58 59 napi_env env_; 60 napi_ref frameStartCallbackRef_ = nullptr; 61 napi_ref frameEndCallbackRef_ = nullptr; 62 napi_ref errorCallbackRef_ = nullptr; 63 }; 64 65 struct VideoOutputCallbackInfo { 66 std::string eventName_; 67 int32_t value_; 68 const VideoCallbackListener* listener_; VideoOutputCallbackInfoVideoOutputCallbackInfo69 VideoOutputCallbackInfo(std::string eventName, int32_t value, const VideoCallbackListener* listener) 70 : eventName_(eventName), value_(value), listener_(listener) {} 71 }; 72 73 class VideoOutputNapi { 74 public: 75 static napi_value Init(napi_env env, napi_value exports); 76 static napi_value CreateVideoOutput(napi_env env, VideoProfile &profile, std::string surfaceId); 77 static bool IsVideoOutput(napi_env env, napi_value obj); 78 VideoOutputNapi(); 79 ~VideoOutputNapi(); 80 sptr<VideoOutput> GetVideoOutput(); 81 82 private: 83 static void VideoOutputNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint); 84 static napi_value VideoOutputNapiConstructor(napi_env env, napi_callback_info info); 85 86 static napi_value Start(napi_env env, napi_callback_info info); 87 static napi_value Stop(napi_env env, napi_callback_info info); 88 static napi_value GetFrameRateRange(napi_env env, napi_callback_info info); 89 static napi_value SetFrameRateRange(napi_env env, napi_callback_info info); 90 static napi_value Release(napi_env env, napi_callback_info info); 91 static napi_value On(napi_env env, napi_callback_info info); 92 93 static thread_local napi_ref sConstructor_; 94 static thread_local sptr<VideoOutput> sVideoOutput_; 95 96 napi_env env_; 97 napi_ref wrapper_; 98 sptr<VideoOutput> videoOutput_; 99 std::shared_ptr<VideoCallbackListener> videoCallback_; 100 static thread_local uint32_t videoOutputTaskId; 101 }; 102 103 struct VideoOutputAsyncContext : public AsyncContext { 104 VideoOutputNapi* objectInfo; 105 std::string errorMsg; 106 bool bRetBool; 107 std::vector<int32_t> vecFrameRateRangeList; 108 int32_t minFrameRate; 109 int32_t maxFrameRate; ~VideoOutputAsyncContextVideoOutputAsyncContext110 ~VideoOutputAsyncContext() 111 { 112 objectInfo = nullptr; 113 } 114 }; 115 } // namespace CameraStandard 116 } // namespace OHOS 117 #endif /* VIDEO_OUTPUT_NAPI_H_ */ 118