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 AUDIO_RECORDER_NAPI_H_ 17 #define AUDIO_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 "task_queue.h" 25 #include "common_napi.h" 26 27 namespace OHOS { 28 namespace Media { 29 enum JSAudioEncoder : int32_t { 30 JS_DEFAULT_ENCORD_TYPE = 0, 31 JS_AAC_LC = 3, 32 }; 33 34 enum JSFileFormat : int32_t { 35 JS_DEFAULT_FILE_FORMAT = 0, 36 JS_MPEG_4 = 2, 37 JS_AAC_ADTS = 6, 38 }; 39 40 class AudioRecorderNapi { 41 public: 42 static napi_value Init(napi_env env, napi_value exports); 43 44 private: 45 static napi_value Constructor(napi_env env, napi_callback_info info); 46 static void Destructor(napi_env env, void *nativeObject, void *finalize); 47 static napi_value CreateAudioRecorder(napi_env env, napi_callback_info info); 48 static napi_value CreateAudioRecorderAsync(napi_env env, napi_callback_info info); 49 static napi_value Prepare(napi_env env, napi_callback_info info); 50 static napi_value Start(napi_env env, napi_callback_info info); 51 static napi_value Pause(napi_env env, napi_callback_info info); 52 static napi_value Resume(napi_env env, napi_callback_info info); 53 static napi_value Stop(napi_env env, napi_callback_info info); 54 static napi_value Reset(napi_env env, napi_callback_info info); 55 static napi_value Release(napi_env env, napi_callback_info info); 56 static napi_value On(napi_env env, napi_callback_info info); 57 void ErrorCallback(MediaServiceExtErrCode errCode); 58 void StateCallback(const std::string &callbackName); 59 void SetCallbackReference(const std::string &callbackName, std::shared_ptr<AutoRef> ref); 60 void CancelCallback(); 61 struct AudioRecorderProperties { 62 AudioRecorderProperties(); 63 ~AudioRecorderProperties(); 64 AudioSourceType sourceType; 65 OutputFormatType outputFormatType; 66 AudioCodecFormat audioCodecFormat; 67 int32_t encodeBitRate; 68 int32_t audioSampleRate; 69 int32_t numberOfChannels; 70 Location location; 71 }; 72 int32_t GetAudioProperties(napi_env env, napi_value args, AudioRecorderProperties &properties); 73 bool GetAudioEncAndFileFormat(napi_env env, napi_value args, AudioRecorderProperties &properties); 74 int32_t GetAudioUriPath(napi_env env, napi_value args, std::string &uriPath); 75 int32_t OnPrepare(const std::string &uriPath, const AudioRecorderProperties &properties); 76 int32_t SetUri(const std::string &uriPath); 77 int32_t CheckValidPath(const std::string &filePath, std::string &realPath); 78 AudioRecorderNapi(); 79 ~AudioRecorderNapi(); 80 81 static thread_local napi_ref constructor_; 82 napi_env env_ = nullptr; 83 std::shared_ptr<Recorder> recorderImpl_ = nullptr; 84 std::shared_ptr<RecorderCallback> callbackNapi_ = nullptr; 85 std::unique_ptr<TaskQueue> taskQue_; 86 std::map<std::string, std::shared_ptr<AutoRef>> refMap_; 87 }; 88 } // namespace Media 89 } // namespace OHOS 90 #endif // AUDIO_RECORDER_NAPI_H_ 91