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 MEDIA_DATA_SOURCE_CALLBACK_H 17 #define MEDIA_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 const std::string READAT_CALLBACK_NAME = "readAt"; 30 31 struct MediaDataSourceJsCallback { MediaDataSourceJsCallbackMediaDataSourceJsCallback32 MediaDataSourceJsCallback(const std::string &callbackName, const std::shared_ptr<AVSharedMemory> &mem, 33 uint32_t length, int64_t pos) 34 :callbackName_(callbackName), memory_(mem), length_(length), pos_(pos) { 35 } 36 ~MediaDataSourceJsCallback(); 37 void WaitResult(); 38 std::weak_ptr<AutoRef> callback_; 39 std::string callbackName_; 40 std::shared_ptr<AVSharedMemory> memory_; 41 uint32_t length_; 42 int64_t pos_; 43 int32_t readSize_ = 0; 44 std::mutex mutexCond_; 45 std::condition_variable cond_; 46 bool setResult_ = false; 47 bool isExit_ = false; 48 }; 49 50 struct MediaDataSourceJsCallbackWraper { 51 std::weak_ptr<MediaDataSourceJsCallback> cb_; 52 }; 53 54 class MediaDataSourceCallback : public IMediaDataSource, public NoCopyable { 55 public: 56 MediaDataSourceCallback(napi_env env, int64_t fileSize); 57 virtual ~MediaDataSourceCallback(); 58 int32_t ReadAt(const std::shared_ptr<AVSharedMemory> &mem, uint32_t length, int64_t pos = -1) override; 59 int32_t GetSize(int64_t &size) override; 60 void SaveCallbackReference(const std::string &name, std::shared_ptr<AutoRef> ref); 61 int32_t GetCallback(const std::string &name, napi_value *callback); 62 void ClearCallbackReference(); 63 static bool AddNapiValueProp(napi_env env, napi_value obj, const std::string &key, napi_value value); 64 65 // This interface has been deprecated 66 int32_t ReadAt(int64_t pos, uint32_t length, const std::shared_ptr<AVSharedMemory> &mem) override; 67 // This interface has been deprecated 68 int32_t ReadAt(uint32_t length, const std::shared_ptr<AVSharedMemory> &mem) override; 69 private: 70 int32_t UvWork(uv_loop_s *loop, uv_work_t *work); 71 std::mutex mutex_; 72 napi_env env_ = nullptr; 73 std::map<std::string, std::shared_ptr<AutoRef>> refMap_; 74 int64_t size_ = -1; 75 std::shared_ptr<MediaDataSourceJsCallback> cb_ = nullptr; 76 }; 77 } // namespace Media 78 } // namespace OHOS 79 #endif // MEDIA_DATA_SOURCE_CALLBACK_H 80