• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 MEDIA_SOURCE_LOADER_CALLBACK_H
17 #define MEDIA_SOURCE_LOADER_CALLBACK_H
18 
19 #include <mutex>
20 #include <uv.h>
21 #include "media_source.h"
22 #include "common_napi.h"
23 #include "napi/native_api.h"
24 #include "napi/native_node_api.h"
25 #include "media_source_loading_request_napi.h"
26 #include "nocopyable.h"
27 
28 namespace OHOS {
29 namespace Media {
30 namespace FunctionName {
31 const std::string SOURCE_OPEN = "open";
32 const std::string SOURCE_READ = "read";
33 const std::string SOURCE_CLOSE = "close";
34 }
35 struct MediaDataSourceLoaderJsCallback {
36     ~MediaDataSourceLoaderJsCallback();
37     void WaitResult();
38     std::weak_ptr<AutoRef> autoRef_;
39     std::string callbackName_ = "unknown";
40     std::shared_ptr<LoadingRequest> request_;
41     int64_t uuid_ = 0; // The default value is 0, indicating retry upon failure.
42     int64_t requestedOffset_ = -1;
43     int64_t requestedLength_ = -1;
44     std::mutex mutexCond_;
45     std::condition_variable cond_;
46     bool setResult_ = false;
47     bool isExit_ = false;
48 };
49 
50 struct MediaDataSourceLoaderJsCallbackWraper {
51     std::weak_ptr<MediaDataSourceLoaderJsCallback> cb_;
52 };
53 
54 class MediaSourceLoaderCallback : public LoaderCallback, public NoCopyable {
55 public:
56     explicit MediaSourceLoaderCallback(napi_env env);
57     virtual ~MediaSourceLoaderCallback();
58 
59     int64_t Open(std::shared_ptr<LoadingRequest> &request) override;
60     void Read(int64_t uuid, int64_t requestedOffset, int64_t requestedLength) override;
61     void Close(int64_t uuid) override;
62     void SaveCallbackReference(const std::string &name, std::shared_ptr<AutoRef> ref);
63     void ClearCallbackReference();
64     void WaitResult();
65 
66 private:
67     std::mutex mutex_;
68     napi_env env_ = nullptr;
69     std::map<std::string, std::shared_ptr<AutoRef>> refMap_;
70     std::shared_ptr<MediaDataSourceLoaderJsCallback> jsCb_ = nullptr;
71 };
72 } // namespace Media
73 } // namespace OHOS
74 #endif // MEDIA_SOURCE_LOADER_CALLBACK_H
75