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_RECORDER_NAPI_H_ 17 #define VIDEO_RECORDER_NAPI_H_ 18 19 #include "recorder.h" 20 #include "av_common.h" 21 #include "media_errors.h" 22 #include "napi/native_api.h" 23 #include "napi/native_node_api.h" 24 #include "common_napi.h" 25 #include "recorder_napi_utils.h" 26 27 namespace OHOS { 28 namespace Media { 29 namespace VideoRecorderState { 30 const std::string STATE_IDLE = "idle"; 31 const std::string STATE_PREPARED = "prepared"; 32 const std::string STATE_PLAYING = "playing"; 33 const std::string STATE_PAUSED = "paused"; 34 const std::string STATE_STOPPED = "stopped"; 35 const std::string STATE_ERROR = "error"; 36 }; 37 38 struct VideoRecorderAsyncContext; 39 40 constexpr int32_t DEFAULT_AUDIO_BIT_RATE = 48000; 41 constexpr int32_t DEFAULT_AUDIO_CHANNELS = 2; 42 constexpr int32_t DEFAULT_AUDIO_SAMPLE_RATE = 48000; 43 constexpr int32_t DEFAULT_DURATION = 5; 44 constexpr int32_t DEFAULT_VIDEO_BIT_RATE = 48000; 45 constexpr int32_t DEFAULT_FRAME_HEIGHT = -1; 46 constexpr int32_t DEFAULT_FRAME_WIDTH = -1; 47 constexpr int32_t DEFAULT_FRAME_RATE = 30; 48 49 class VideoRecorderNapi { 50 public: 51 static napi_value Init(napi_env env, napi_value exports); 52 53 private: 54 static napi_value Constructor(napi_env env, napi_callback_info info); 55 static void Destructor(napi_env env, void *nativeObject, void *finalize); 56 static napi_value CreateVideoRecorder(napi_env env, napi_callback_info info); 57 static napi_value Prepare(napi_env env, napi_callback_info info); 58 static napi_value GetInputSurface(napi_env env, napi_callback_info info); 59 static napi_value Start(napi_env env, napi_callback_info info); 60 static napi_value Pause(napi_env env, napi_callback_info info); 61 static napi_value Resume(napi_env env, napi_callback_info info); 62 static napi_value Stop(napi_env env, napi_callback_info info); 63 static napi_value Release(napi_env env, napi_callback_info info); 64 static napi_value Reset(napi_env env, napi_callback_info info); 65 static napi_value On(napi_env env, napi_callback_info info); 66 static napi_value GetState(napi_env env, napi_callback_info info); 67 void ErrorCallback(MediaServiceExtErrCode errCode); 68 void SetCallbackReference(const std::string &callbackName, std::shared_ptr<AutoRef> ref); 69 void CancelCallback(); 70 VideoRecorderNapi(); 71 ~VideoRecorderNapi(); 72 73 struct VideoRecorderProfile { 74 int32_t audioBitrate = DEFAULT_AUDIO_BIT_RATE; 75 int32_t audioChannels = DEFAULT_AUDIO_CHANNELS; 76 AudioCodecFormat audioCodecFormat = AudioCodecFormat::AUDIO_DEFAULT; 77 int32_t auidoSampleRate = DEFAULT_AUDIO_SAMPLE_RATE; 78 int32_t duration = DEFAULT_DURATION; 79 OutputFormatType outputFormat = OutputFormatType::FORMAT_DEFAULT; 80 int32_t videoBitrate = DEFAULT_VIDEO_BIT_RATE; 81 VideoCodecFormat videoCodecFormat = VideoCodecFormat::VIDEO_DEFAULT; 82 int32_t videoFrameWidth = DEFAULT_FRAME_HEIGHT; 83 int32_t videoFrameHeight = DEFAULT_FRAME_WIDTH; 84 int32_t videoFrameRate = DEFAULT_FRAME_RATE; 85 }; 86 87 struct VideoRecorderProperties { 88 AudioSourceType audioSourceType; // source type; 89 VideoSourceType videoSourceType; 90 VideoRecorderProfile profile; 91 int32_t orientationHint = 0; // Optional 92 Location location; // Optional 93 std::string url; 94 }; 95 96 int32_t GetVideoRecorderProperties(napi_env env, napi_value args, VideoRecorderProperties &properties); 97 int32_t SetVideoRecorderProperties(std::unique_ptr<VideoRecorderAsyncContext> &ctx, 98 const VideoRecorderProperties &properties); 99 void GetConfig(napi_env env, napi_value args, std::unique_ptr<VideoRecorderAsyncContext> &ctx, 100 VideoRecorderProperties &properties); 101 int32_t SetUrl(const std::string &UrlPath); 102 bool IsSurfaceIdVaild(uint64_t surfaceID); 103 104 static thread_local napi_ref constructor_; 105 napi_env env_ = nullptr; 106 std::shared_ptr<Recorder> recorder_ = nullptr; 107 std::shared_ptr<RecorderCallback> callbackNapi_ = nullptr; 108 std::map<std::string, std::shared_ptr<AutoRef>> refMap_; 109 sptr<Surface> surface_; 110 std::string currentStates_ = VideoRecorderState::STATE_IDLE; 111 bool isPureVideo = false; 112 int32_t videoSourceID; 113 int32_t audioSourceID; 114 }; 115 116 struct VideoRecorderAsyncContext : public MediaAsyncContext { VideoRecorderAsyncContextVideoRecorderAsyncContext117 explicit VideoRecorderAsyncContext(napi_env env) : MediaAsyncContext(env) {} 118 ~VideoRecorderAsyncContext() = default; 119 120 VideoRecorderNapi *napi = nullptr; 121 }; 122 } // namespace Media 123 } // namespace OHOS 124 #endif // VIDERECORDER_NAPI_H_ 125