1 /* 2 * Copyright (C) 2023 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 HELPER_DATA_SOURCE_CALLBACK_H 17 #define HELPER_DATA_SOURCE_CALLBACK_H 18 19 #include <mutex> 20 #include <uv.h> 21 #include "media_data_source.h" 22 #include "common_napi.h" 23 #include "napi/native_api.h" 24 #include "napi/native_node_api.h" 25 #include "nocopyable.h" 26 27 namespace OHOS { 28 namespace Media { 29 struct HelperDataSourceJsCallback { HelperDataSourceJsCallbackHelperDataSourceJsCallback30 HelperDataSourceJsCallback(const std::string &callbackName, const std::shared_ptr<AVSharedMemory> &mem, 31 uint32_t length, int64_t pos) 32 :callbackName_(callbackName), memory_(mem), length_(length), pos_(pos) { 33 } 34 ~HelperDataSourceJsCallback(); 35 void WaitResult(); 36 std::weak_ptr<AutoRef> callback_; 37 std::string callbackName_; 38 std::shared_ptr<AVSharedMemory> memory_; 39 uint32_t length_; 40 int64_t pos_; 41 int32_t readSize_ = 0; 42 std::mutex mutexCond_; 43 std::condition_variable cond_; 44 bool setResult_ = false; 45 bool isExit_ = false; 46 }; 47 48 struct HelperDataSourceJsCallbackWraper { 49 std::weak_ptr<HelperDataSourceJsCallback> cb_; 50 }; 51 52 class HelperDataSourceCallback : public IMediaDataSource, public NoCopyable { 53 public: 54 HelperDataSourceCallback(napi_env env, int64_t fileSize); 55 virtual ~HelperDataSourceCallback(); 56 int32_t ReadAt(const std::shared_ptr<AVSharedMemory> &mem, uint32_t length, int64_t pos = -1) override; 57 int32_t GetSize(int64_t &size) override; 58 void SaveCallbackReference(const std::string &name, std::shared_ptr<AutoRef> ref); 59 int32_t GetCallback(const std::string &name, napi_value *callback); 60 void ClearCallbackReference(); 61 static bool AddNapiValueProp(napi_env env, napi_value obj, const std::string &key, napi_value value); 62 63 // This interface has been deprecated 64 int32_t ReadAt(int64_t pos, uint32_t length, const std::shared_ptr<AVSharedMemory> &mem) override; 65 // This interface has been deprecated 66 int32_t ReadAt(uint32_t length, const std::shared_ptr<AVSharedMemory> &mem) override; 67 private: 68 napi_status UvWork(HelperDataSourceJsCallbackWraper *cbWrap); 69 std::mutex mutex_; 70 napi_env env_ = nullptr; 71 std::map<std::string, std::shared_ptr<AutoRef>> refMap_; 72 int64_t size_ = -1; 73 std::shared_ptr<HelperDataSourceJsCallback> cb_ = nullptr; 74 }; 75 } // namespace Media 76 } // namespace OHOS 77 #endif // HELPER_DATA_SOURCE_CALLBACK_H 78