1 /* 2 * Copyright (c) 2025 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 CJ_HELPERDATASOURCECALLBACK_H 17 #define CJ_HELPERDATASOURCECALLBACK_H 18 19 #include <cstdint> 20 #include <functional> 21 #include <map> 22 #include <memory> 23 #include <mutex> 24 #include "media_data_source.h" 25 #include "nocopyable.h" 26 27 namespace OHOS { 28 namespace Media { 29 30 struct HelperDataSourceCJCallback { HelperDataSourceCJCallbackHelperDataSourceCJCallback31 HelperDataSourceCJCallback( 32 const std::string& callbackName, 33 const std::shared_ptr<AVSharedMemory>& mem, 34 uint32_t length, 35 int64_t pos) 36 : callbackName_(callbackName), memory_(mem), length_(length), pos_(pos) { 37 } 38 ~HelperDataSourceCJCallback(); 39 void WaitResult(); 40 std::function<int32_t(uint8_t *, uint32_t, int64_t)> callback_; 41 std::string callbackName_; 42 std::shared_ptr<AVSharedMemory> memory_; 43 uint32_t length_; 44 int64_t pos_; 45 int32_t readSize_ = 0; 46 std::mutex mutexCond_; 47 std::condition_variable cond_; 48 bool setResult_ = false; 49 bool isExit_ = false; 50 }; 51 52 class CJHelperDataSourceCallback : public IMediaDataSource, public NoCopyable { 53 public: 54 CJHelperDataSourceCallback(int64_t fileSize); 55 virtual ~CJHelperDataSourceCallback(); 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 int32_t GetCallbackId(int64_t &id); 59 60 // This interface has been deprecated 61 int32_t ReadAt(int64_t pos, uint32_t length, const std::shared_ptr<AVSharedMemory> &mem) override; 62 // This interface has been deprecated 63 int32_t ReadAt(uint32_t length, const std::shared_ptr<AVSharedMemory> &mem) override; 64 int32_t SaveCallbackReference(const std::string &name, int64_t id); 65 void ClearCallbackReference(); 66 private: 67 std::mutex mutex_; 68 std::map<std::string, std::pair<int64_t, std::function<int32_t(uint8_t *, uint32_t, int64_t)>>> funcMap_; 69 int64_t size_ = -1; 70 std::shared_ptr<HelperDataSourceCJCallback> cb_ = nullptr; 71 }; 72 73 } // namespace Media 74 } // namespace OHOS 75 76 #endif // CJ_HELPERDATASOURCECALLBACK_H 77