1 /*
2  * Copyright (C) 2024 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 AV_SCREEN_CAPTURE_CALLBACK_H
17 #define AV_SCREEN_CAPTURE_CALLBACK_H
18 
19 #include <uv.h>
20 #include "screen_capture.h"
21 #include "avscreen_capture_napi.h"
22 
23 namespace OHOS {
24 namespace Media {
25 class AVScreenCaptureCallback : public ScreenCaptureCallBack {
26 public:
27     explicit AVScreenCaptureCallback(napi_env env);
28     virtual ~AVScreenCaptureCallback();
29 
30     void SendErrorCallback(int32_t errCode, const std::string &msg);
31     void SendStateCallback(AVScreenCaptureStateCode stateCode);
32     void SaveCallbackReference(const std::string &name, std::weak_ptr<AutoRef> ref);
33     void CancelCallbackReference(const std::string &name);
34     void ClearCallbackReference();
35 protected:
36     void OnError(ScreenCaptureErrorType errorType, int32_t errorCode) override;
37     void OnStateChange(AVScreenCaptureStateCode stateCode) override;
OnAudioBufferAvailable(bool isReady,AudioCaptureSourceType type)38     void OnAudioBufferAvailable(bool isReady, AudioCaptureSourceType type) override
39     {
40         (void)isReady;
41         (void)type;
42     }
OnVideoBufferAvailable(bool isReady)43     void OnVideoBufferAvailable(bool isReady) override
44     {
45         (void)isReady;
46     }
47 private:
48     struct AVScreenCaptureJsCallback {
49         std::weak_ptr<AutoRef> autoRef;
50         std::string callbackName = "unknown";
51         std::string errorMsg = "unknown";
52         int32_t errorCode = MSERR_EXT_UNKNOWN;
53         AVScreenCaptureStateCode stateCode;
54     };
55     static void OnJsErrorCallBackWork(uv_work_t *work, int status);
56     void OnJsErrorCallBack(AVScreenCaptureJsCallback *jsCb) const;
57     void OnJsStateChangeCallBack(AVScreenCaptureJsCallback *jsCb) const;
58     napi_env env_ = nullptr;
59     std::mutex mutex_;
60     std::map<std::string, std::weak_ptr<AutoRef>> refMap_;
61 };
62 } // namespace Media
63 } // namespace OHOS
64 #endif