• 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 #include "avscreen_capture_ffi.h"
16 #include "cj_avscreen_capture.h"
17 #include "cj_avscreen_capture_callback.h"
18 #include <string>
19 
20 namespace OHOS {
21 namespace Media {
22 using namespace OHOS::FFI;
23 
24 extern "C" {
FfiAVScreenCaptureCreateAVScreenCaptureRecorder(int32_t * errorcode)25     int64_t FfiAVScreenCaptureCreateAVScreenCaptureRecorder(int32_t* errorcode)
26     {
27         return CJAVScreenCapture::CreateAVScreenCaptureRecorder(errorcode);
28     }
29 
FfiAVScreenCaptureinit(int64_t id,CAVScreenCaptureConfig config)30     int32_t FfiAVScreenCaptureinit(int64_t id, CAVScreenCaptureConfig config)
31     {
32         auto cjAVScreenCapture = FFIData::GetData<CJAVScreenCapture>(id);
33         if (!cjAVScreenCapture) {
34             MEDIA_LOGE("[CJAVScreenCapture] instance is nullptr!");
35             return -1;
36         }
37         std::shared_ptr<ScreenCapture> screenCapture = cjAVScreenCapture->screenCapture_;
38         if (!screenCapture) {
39             MEDIA_LOGE("[CJAVScreenCapture] screenCapture_ is nullptr!");
40             return -1;
41         }
42         AVScreenCaptureConfig fconfig;
43         fconfig.captureMode = CaptureMode::CAPTURE_HOME_SCREEN;
44         fconfig.dataType = static_cast<DataType>(config.dataType);
45         //audioInfo
46         fconfig.audioInfo.micCapInfo.audioSampleRate = config.audioInfo.micCapInfo.audioSampleRate;
47         fconfig.audioInfo.innerCapInfo.audioSampleRate = config.audioInfo.innerCapInfo.audioSampleRate;
48         fconfig.audioInfo.micCapInfo.audioChannels = config.audioInfo.micCapInfo.audioChannels;
49         fconfig.audioInfo.innerCapInfo.audioChannels = config.audioInfo.innerCapInfo.audioChannels;
50         fconfig.audioInfo.micCapInfo.audioSource = static_cast<AudioCaptureSourceType>(
51             config.audioInfo.micCapInfo.audioSource);
52         fconfig.audioInfo.innerCapInfo.audioSource = static_cast<AudioCaptureSourceType>(
53             config.audioInfo.innerCapInfo.audioSource);
54         fconfig.audioInfo.audioEncInfo.audioBitrate = config.audioInfo.audioEncInfo.audioBitrate;
55         fconfig.audioInfo.audioEncInfo.audioCodecformat = static_cast<AudioCodecFormat>(
56             config.audioInfo.audioEncInfo.audioCodecformat);
57 
58         int32_t displayId = 0;
59         fconfig.videoInfo.videoCapInfo.displayId = static_cast<uint64_t>(displayId);
60         fconfig.videoInfo.videoCapInfo.videoFrameHeight = config.videoInfo.videoCapInfo.videoFrameHeight;
61         fconfig.videoInfo.videoCapInfo.videoFrameWidth = config.videoInfo.videoCapInfo.videoFrameWidth;
62         fconfig.videoInfo.videoCapInfo.videoSource = static_cast<VideoSourceType>(
63             config.videoInfo.videoCapInfo.videoSource);
64 
65         fconfig.videoInfo.videoEncInfo.videoCodec = static_cast<VideoCodecFormat>(
66             config.videoInfo.videoEncInfo.videoCodec);
67         fconfig.videoInfo.videoEncInfo.videoBitrate = config.videoInfo.videoEncInfo.videoBitrate;
68         int32_t frameRate = 60;
69         fconfig.videoInfo.videoEncInfo.videoFrameRate = frameRate;
70 
71         std::string fileFormat = "mp4";
72         fconfig.recorderInfo.fileFormat = fileFormat;
73         fconfig.recorderInfo.url = config.recorderInfo.url;
74 
75         return cjAVScreenCapture->AVScreenCaptureinit(screenCapture, fconfig);
76     }
77 
FfiAVScreenCaptureStartRecording(int64_t id)78     int32_t FfiAVScreenCaptureStartRecording(int64_t id)
79     {
80         auto cjAVScreenCapture = FFIData::GetData<CJAVScreenCapture>(id);
81         if (!cjAVScreenCapture) {
82             MEDIA_LOGE("[CJAVScreenCapture] instance is nullptr!");
83             return -1;
84         }
85         std::shared_ptr<ScreenCapture> screenCapture = cjAVScreenCapture->screenCapture_;
86         if (!screenCapture) {
87             MEDIA_LOGE("[CJAVScreenCapture] screenCapture_ is nullptr!");
88             return -1;
89         }
90         return cjAVScreenCapture->StartRecording(screenCapture);
91     }
92 
FfiAVScreenCaptureStopRecording(int64_t id)93     int32_t FfiAVScreenCaptureStopRecording(int64_t id)
94     {
95         auto cjAVScreenCapture = FFIData::GetData<CJAVScreenCapture>(id);
96         if (!cjAVScreenCapture) {
97             MEDIA_LOGE("[CJAVScreenCapture] instance is nullptr!");
98             return -1;
99         }
100         std::shared_ptr<ScreenCapture> screenCapture = cjAVScreenCapture->screenCapture_;
101         if (!screenCapture) {
102             MEDIA_LOGE("[CJAVScreenCapture] screenCapture_ is nullptr!");
103             return -1;
104         }
105         return cjAVScreenCapture->StopRecording(screenCapture);
106     }
107 
FfiAVScreenCaptureSkipPrivacyMode(int64_t id,CArrUnit windowIDsVec)108     int32_t FfiAVScreenCaptureSkipPrivacyMode(int64_t id, CArrUnit windowIDsVec)
109     {
110         auto cjAVScreenCapture = FFIData::GetData<CJAVScreenCapture>(id);
111         if (!cjAVScreenCapture) {
112             MEDIA_LOGE("[CJAVScreenCapture] instance is nullptr!");
113             return -1;
114         }
115         std::shared_ptr<ScreenCapture> screenCapture = cjAVScreenCapture->screenCapture_;
116         if (!screenCapture) {
117             MEDIA_LOGE("[CJAVScreenCapture] screenCapture_ is nullptr!");
118             return -1;
119         }
120         std::vector<uint64_t> windowIDsVeclist;
121         uint64_t *tagptr = static_cast<uint64_t *>(windowIDsVec.head);
122         for (int64_t i = 0; i < windowIDsVec.size; i++) {
123             windowIDsVeclist.push_back(tagptr[i]);
124         }
125 
126         return cjAVScreenCapture->SkipPrivacyMode(screenCapture, windowIDsVeclist);
127     }
128 
FfiAVScreenCaptureSetMicEnabled(int64_t id,bool enabled)129     int32_t FfiAVScreenCaptureSetMicEnabled(int64_t id, bool enabled)
130     {
131         auto cjAVScreenCapture = FFIData::GetData<CJAVScreenCapture>(id);
132         if (!cjAVScreenCapture) {
133             MEDIA_LOGE("[CJAVScreenCapture] instance is nullptr!");
134             return -1;
135         }
136         std::shared_ptr<ScreenCapture> screenCapture = cjAVScreenCapture->screenCapture_;
137         if (!screenCapture) {
138             MEDIA_LOGE("[CJAVScreenCapture] screenCapture_ is nullptr!");
139             return -1;
140         }
141         return cjAVScreenCapture->SetMicEnabled(screenCapture, enabled);
142     }
143 
FfiAVScreenCaptureRelease(int64_t id)144     int32_t FfiAVScreenCaptureRelease(int64_t id)
145     {
146         auto cjAVScreenCapture = FFIData::GetData<CJAVScreenCapture>(id);
147         if (!cjAVScreenCapture) {
148             MEDIA_LOGE("[CJAVScreenCapture] instance is nullptr!");
149             return -1;
150         }
151         std::shared_ptr<ScreenCapture> screenCapture = cjAVScreenCapture->screenCapture_;
152         if (!screenCapture) {
153             MEDIA_LOGE("[CJAVScreenCapture] screenCapture_ is nullptr!");
154             return -1;
155         }
156         return cjAVScreenCapture->Release(screenCapture);
157     }
158 
FfiAVScreenCaptureOnStateChange(int64_t id,int64_t callbackId)159     int32_t FfiAVScreenCaptureOnStateChange(int64_t id, int64_t callbackId)
160     {
161         auto cjAVScreenCapture = FFIData::GetData<CJAVScreenCapture>(id);
162         if (!cjAVScreenCapture) {
163             MEDIA_LOGE("[CJAVScreenCapture] instance is nullptr!");
164             return MSERR_EXT_API9_NO_MEMORY;
165         }
166         return cjAVScreenCapture->OnStateChange(callbackId);
167     }
168 
FfiAVScreenCaptureOffStateChange(int64_t id)169     int32_t FfiAVScreenCaptureOffStateChange(int64_t id)
170     {
171         auto cjAVScreenCapture = FFIData::GetData<CJAVScreenCapture>(id);
172         if (!cjAVScreenCapture) {
173             MEDIA_LOGE("[CJAVScreenCapture] instance is nullptr!");
174             return MSERR_EXT_API9_NO_MEMORY;
175         }
176         return cjAVScreenCapture->OffStateChange();
177     }
178 
FfiAVScreenCaptureOnError(int64_t id,int64_t callbackId)179     int32_t FfiAVScreenCaptureOnError(int64_t id, int64_t callbackId)
180     {
181         auto cjAVScreenCapture = FFIData::GetData<CJAVScreenCapture>(id);
182         if (!cjAVScreenCapture) {
183             MEDIA_LOGE("[CJAVScreenCapture] instance is nullptr!");
184             return MSERR_EXT_API9_NO_MEMORY;
185         }
186         return cjAVScreenCapture->OnError(callbackId);
187     }
188 
FfiAVScreenCaptureOffError(int64_t id)189     int32_t FfiAVScreenCaptureOffError(int64_t id)
190     {
191         auto cjAVScreenCapture = FFIData::GetData<CJAVScreenCapture>(id);
192         if (!cjAVScreenCapture) {
193             MEDIA_LOGE("[CJAVScreenCapture] instance is nullptr!");
194             return MSERR_EXT_API9_NO_MEMORY;
195         }
196         return cjAVScreenCapture->OffError();
197     }
198 }
199 }
200 }