• 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 #include "cj_avscreen_capture_callback.h"
17 #include "media_core.h"
18 #include "scope_guard.h"
19 #include "cj_lambda.h"
20 
21 namespace OHOS {
22 namespace Media {
CJAVScreenCaptureCallback()23 CJAVScreenCaptureCallback::CJAVScreenCaptureCallback()
24 {
25     MEDIA_LOGI("0x%{public}06" PRIXPTR "Instances create", FAKE_POINTER(this));
26 }
27 
~CJAVScreenCaptureCallback()28 CJAVScreenCaptureCallback::~CJAVScreenCaptureCallback()
29 {
30     MEDIA_LOGI("0x%{public}06" PRIXPTR "Instances destroy", FAKE_POINTER(this));
31 }
32 
SendErrorCallback(int32_t errCode,const std::string & msg)33 void CJAVScreenCaptureCallback::SendErrorCallback(int32_t errCode, const std::string &msg)
34 {
35     std::lock_guard<std::mutex> lock(mutex_);
36     if (onerrorfunc == nullptr) {
37         MEDIA_LOGW("can not find error callback!");
38         return;
39     }
40     onerrorfunc(errCode, msg);
41 }
42 
SendStateCallback(AVScreenCaptureStateCode stateCode)43 void CJAVScreenCaptureCallback::SendStateCallback(AVScreenCaptureStateCode stateCode)
44 {
45     std::lock_guard<std::mutex> lock(mutex_);
46     if (onstatechangefunc == nullptr) {
47         MEDIA_LOGW("can not find stateChange callback!");
48         return;
49     }
50     onstatechangefunc(stateCode);
51 }
52 
SaveCallbackReference(const std::string & name,int64_t callbackId)53 void CJAVScreenCaptureCallback::SaveCallbackReference(const std::string &name, int64_t callbackId)
54 {
55     std::lock_guard<std::mutex> lock(mutex_);
56     if (name == CJAVScreenCaptureEvent::EVENT_ERROR) {
57         auto func = reinterpret_cast<void (*)(int32_t, const std::string &)>(callbackId);
58         onerrorfunc = [lambda = CJLambda::Create(func)](int32_t errCode, const std::string &msg) {
59             lambda(errCode, msg);
60         };
61     } else {
62         auto func = reinterpret_cast<void (*)(AVScreenCaptureStateCode)>(callbackId);
63         onstatechangefunc = [lambda = CJLambda::Create(func)](AVScreenCaptureStateCode stateCode) {
64             lambda(stateCode);
65         };
66     }
67     MEDIA_LOGI("Set callback type: %{public}s", name.c_str());
68 }
69 
CancelCallbackReference(const std::string & name)70 void CJAVScreenCaptureCallback::CancelCallbackReference(const std::string &name)
71 {
72     std::lock_guard<std::mutex> lock(mutex_);
73     if (name == CJAVScreenCaptureEvent::EVENT_ERROR) {
74         onerrorfunc = nullptr;
75     } else {
76         onstatechangefunc = nullptr;
77     }
78     MEDIA_LOGI("Cancel callback type: %{public}s", name.c_str());
79 }
80 
ClearCallbackReference()81 void CJAVScreenCaptureCallback::ClearCallbackReference()
82 {
83     std::lock_guard<std::mutex> lock(mutex_);
84     onerrorfunc = nullptr;
85     onstatechangefunc = nullptr;
86     MEDIA_LOGI("ClearCallback!");
87 }
88 
OnError(ScreenCaptureErrorType errorType,int32_t errorCode)89 void CJAVScreenCaptureCallback::OnError(ScreenCaptureErrorType errorType, int32_t errorCode)
90 {
91     MEDIA_LOGI("OnError is called, name: %{public}d, error message: %{public}d", errorType, errorCode);
92     SendErrorCallback(errorCode, "Screen capture failed.");
93 }
94 
OnStateChange(AVScreenCaptureStateCode stateCode)95 void CJAVScreenCaptureCallback::OnStateChange(AVScreenCaptureStateCode stateCode)
96 {
97     MEDIA_LOGI("OnStateChange() is called, stateCode: %{public}d", stateCode);
98     SendStateCallback(stateCode);
99 }
100 
101 }
102 }
103