• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 <native_buffer.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <securec.h>
20 #include <cstdio>
21 #include <string>
22 #include <map>
23 #include <iostream>
24 #include <thread>
25 #include <cinttypes>
26 #include "napi/native_api.h"
27 #include "native_avscreen_capture.h"
28 #include "native_avscreen_capture_base.h"
29 #include "native_avscreen_capture_errors.h"
30 using namespace std;
31 static char g_filename[100] = {0};
32 static int32_t g_recordTime = 3;
33 static int32_t g_logCount = 30;
34 static int32_t g_aIndex = 0;
35 static int32_t g_vIndex = 0;
36 static int32_t g_aFlag = 0;
37 static int32_t g_vFlag = 0;
38 
39 class ScreenCaptureNdkCallBack {
40   public:
41     virtual ~ScreenCaptureNdkCallBack() = default;
42     virtual void OnError(int32_t errorCode) = 0;
43     virtual void OnAudioBufferAvailable(bool isReady, OH_AudioCaptureSourceType type) = 0;
44     virtual void OnVideoBufferAvailable(bool isReady) = 0;
45 };
46 class ScreenCaptureNdkTestCallback : public ScreenCaptureNdkCallBack {
47 public:
ScreenCaptureNdkTestCallback(OH_AVScreenCapture * ScreenCapture,FILE * audioFile,FILE * iFile,FILE * videoFile)48     ScreenCaptureNdkTestCallback(OH_AVScreenCapture *ScreenCapture, FILE *audioFile, FILE *iFile, FILE *videoFile)
49         : screenCapture_(ScreenCapture), aFile(audioFile), innerFile(iFile), vFile(videoFile) {}
50     ~ScreenCaptureNdkTestCallback() override;
51     void DumpVideoFile(OH_NativeBuffer *nativeBuffer, int32_t length);
52     void OnError(int32_t errorCode) override;
53     void OnAudioBufferAvailable(bool isReady, OH_AudioCaptureSourceType type) override;
54     void OnVideoBufferAvailable(bool isReady) override;
55 
56 private:
57     OH_AVScreenCapture *screenCapture_;
58     FILE *aFile = nullptr;
59     FILE *innerFile = nullptr;
60     FILE *vFile = nullptr;
61 };
62 
63 #define LOG(cond, fmt, ...)           \
64     if (!(cond)) {                                  \
65         (void)printf(fmt, ##__VA_ARGS__);           \
66     }
67 
68 enum AudioSamplingRate {
69     SAMPLE_RATE_8000 = 8000,
70     SAMPLE_RATE_11025 = 11025,
71     SAMPLE_RATE_12000 = 12000,
72     SAMPLE_RATE_16000 = 16000,
73     SAMPLE_RATE_22050 = 22050,
74     SAMPLE_RATE_24000 = 24000,
75     SAMPLE_RATE_32000 = 32000,
76     SAMPLE_RATE_44100 = 44100,
77     SAMPLE_RATE_48000 = 48000,
78     SAMPLE_RATE_64000 = 64000,
79     SAMPLE_RATE_96000 = 96000
80 };
81 
82 enum AudioChannel {
83     MONO = 1,
84     STEREO = 2,
85     CHANNEL_3 = 3,
86     CHANNEL_4 = 4,
87     CHANNEL_5 = 5,
88     CHANNEL_6 = 6
89 };
90 
~ScreenCaptureNdkTestCallback()91 ScreenCaptureNdkTestCallback::~ScreenCaptureNdkTestCallback()
92 {
93     screenCapture_ = nullptr;
94     aFile = nullptr;
95     innerFile = nullptr;
96     vFile = nullptr;
97 }
98 
OnError(int32_t errorCode)99 void ScreenCaptureNdkTestCallback::OnError(int32_t errorCode)
100 {
101     LOG(false, "Error received, errorCode: %d", errorCode);
102 }
OnAudioBufferAvailable(bool isReady,OH_AudioCaptureSourceType type)103 void ScreenCaptureNdkTestCallback::OnAudioBufferAvailable(bool isReady, OH_AudioCaptureSourceType type)
104 {
105     if (isReady == true) {
106         OH_AudioBuffer *audioBuffer = (OH_AudioBuffer *)malloc(sizeof(OH_AudioBuffer));
107         LOG(audioBuffer != nullptr, "audio buffer is nullptr");
108         if (OH_AVScreenCapture_AcquireAudioBuffer(screenCapture_, &audioBuffer, type) == AV_SCREEN_CAPTURE_ERR_OK) {
109             LOG(audioBuffer != nullptr, "AcquireAudioBuffer failed, audio buffer empty");
110             LOG(g_aIndex % g_logCount != 0,
111                 "AcquireAudioBuffer, audioBufferLen: %d, timestampe: %" PRId64", audioSourceType: %d",
112                 audioBuffer->size, audioBuffer->timestamp, audioBuffer->type);
113             if ((aFile != nullptr) && (audioBuffer->buf != nullptr) && (type == OH_MIC)) {
114                 int32_t ret = fwrite(audioBuffer->buf, 1, audioBuffer->size, aFile);
115                 LOG(ret == audioBuffer->size, "error occurred in fwrite audioFile: %s", strerror(errno));
116                 free(audioBuffer->buf);
117                 audioBuffer->buf = nullptr;
118             } else if ((innerFile != nullptr) && (audioBuffer->buf != nullptr) && (type == OH_ALL_PLAYBACK)) {
119                 int32_t ret = fwrite(audioBuffer->buf, 1, audioBuffer->size, innerFile);
120                 LOG(ret == audioBuffer->size, "error occurred in fwrite innerFile_: %s", strerror(errno));
121                 free(audioBuffer->buf);
122                 audioBuffer->buf = nullptr;
123             }
124             free(audioBuffer);
125             audioBuffer = nullptr;
126         }
127         if (g_aFlag == 1) {
128             OH_AVScreenCapture_ReleaseAudioBuffer(screenCapture_, type);
129         }
130         g_aIndex++;
131     } else {
132         LOG(false, "AcquireAudioBuffer failed");
133     }
134 }
135 
DumpVideoFile(OH_NativeBuffer * nativeBuffer,int32_t length)136 void ScreenCaptureNdkTestCallback::DumpVideoFile(OH_NativeBuffer *nativeBuffer, int32_t length)
137 {
138     if (vFile != nullptr) {
139         void *buf = nullptr;
140         OH_NativeBuffer_Map(nativeBuffer, &buf);
141         if (buf != nullptr) {
142             int32_t ret = fwrite(buf, 1, length, vFile);
143             LOG(ret == length, "error occurred in fwrite vFile_: %s", strerror(errno));
144         }
145     }
146 }
147 
OnVideoBufferAvailable(bool isReady)148 void ScreenCaptureNdkTestCallback::OnVideoBufferAvailable(bool isReady)
149 {
150     if (isReady == true) {
151         int32_t fence = 0;
152         int64_t timestamp = 0;
153         int32_t size = 4;
154         OH_Rect damage;
155         OH_NativeBuffer_Config config;
156         OH_NativeBuffer *nativeBuffer =
157             OH_AVScreenCapture_AcquireVideoBuffer(screenCapture_, &fence, &timestamp, &damage);
158         if (nativeBuffer != nullptr) {
159             OH_NativeBuffer_GetConfig(nativeBuffer, &config);
160             int32_t length = config.height * config.width * size;
161             LOG(g_vIndex % g_logCount != 0,
162                 "AcquireVideoBuffer, videoBufferLen: %d, timestamp: %" PRId64", size: %d", length, timestamp, length);
163             DumpVideoFile(nativeBuffer, length);
164             OH_NativeBuffer_Unreference(nativeBuffer);
165             if (g_vFlag == 1) {
166                 OH_AVScreenCapture_ReleaseVideoBuffer(screenCapture_);
167             }
168             g_vIndex++;
169         } else {
170             LOG(false, "AcquireVideoBuffer failed");
171         }
172     }
173 }
174 std::shared_ptr<ScreenCaptureNdkTestCallback> screenCaptureCb = nullptr;
175 std::mutex mutex_;
176 std::map<OH_AVScreenCapture *, std::shared_ptr<ScreenCaptureNdkCallBack>> mockCbMap_;
DelCallback(OH_AVScreenCapture * screenCapture)177 void DelCallback(OH_AVScreenCapture *screenCapture)
178 {
179     std::lock_guard<std::mutex> lock(mutex_);
180     if (mockCbMap_.empty()) {
181         return;
182     }
183     auto it = mockCbMap_.find(screenCapture);
184     if (it != mockCbMap_.end()) {
185         mockCbMap_.erase(it);
186     }
187 }
188 
GetCallback(OH_AVScreenCapture * screenCapture)189 std::shared_ptr<ScreenCaptureNdkCallBack> GetCallback(OH_AVScreenCapture *screenCapture)
190 {
191     std::lock_guard<std::mutex> lock(mutex_);
192     if (mockCbMap_.empty()) {
193         return nullptr;
194     }
195     if (mockCbMap_.find(screenCapture) != mockCbMap_.end()) {
196         return mockCbMap_.at(screenCapture);
197     }
198     return nullptr;
199 }
200 
OnError(OH_AVScreenCapture * screenCapture,int32_t errorCode)201 void OnError(OH_AVScreenCapture *screenCapture, int32_t errorCode)
202 {
203     std::shared_ptr<ScreenCaptureNdkCallBack> mockCb = GetCallback(screenCapture);
204     if (mockCb != nullptr) {
205         mockCb->OnError(errorCode);
206     }
207 }
208 
OnAudioBufferAvailable(OH_AVScreenCapture * screenCapture,bool isReady,OH_AudioCaptureSourceType type)209 void OnAudioBufferAvailable(OH_AVScreenCapture *screenCapture, bool isReady,
210     OH_AudioCaptureSourceType type)
211 {
212     std::shared_ptr<ScreenCaptureNdkCallBack> mockCb = GetCallback(screenCapture);
213     if (mockCb != nullptr) {
214         mockCb->OnAudioBufferAvailable(isReady, type);
215     }
216 }
217 
OnVideoBufferAvailable(OH_AVScreenCapture * screenCapture,bool isReady)218 void OnVideoBufferAvailable(OH_AVScreenCapture *screenCapture, bool isReady)
219 {
220     std::shared_ptr<ScreenCaptureNdkCallBack> mockCb = GetCallback(screenCapture);
221     if (mockCb != nullptr) {
222         mockCb->OnVideoBufferAvailable(isReady);
223     }
224 }
225 
SetScreenCaptureCallback(OH_AVScreenCapture * screenCapture,std::shared_ptr<ScreenCaptureNdkTestCallback> & cb)226 void SetScreenCaptureCallback(OH_AVScreenCapture *screenCapture, std::shared_ptr<ScreenCaptureNdkTestCallback> &cb)
227 {
228     if (cb != nullptr) {
229         std::lock_guard<std::mutex> lock(mutex_);
230         mockCbMap_[screenCapture] = cb;
231         struct OH_AVScreenCaptureCallback callback;
232         callback.onError = OnError;
233         callback.onAudioBufferAvailable = OnAudioBufferAvailable;
234         callback.onVideoBufferAvailable = OnVideoBufferAvailable;
235         OH_AVScreenCapture_SetCallback(screenCapture, callback);
236     }
237 }
238 
CreateScreenCapture()239 OH_AVScreenCapture* CreateScreenCapture()
240 {
241     OH_AVScreenCapture* screenCapture_ = OH_AVScreenCapture_Create();
242     return screenCapture_;
243 }
244 
SetConfig(OH_AVScreenCaptureConfig & config)245 void SetConfig(OH_AVScreenCaptureConfig &config)
246 {
247     char name[30] = "fd://11";
248     int32_t width = 720;
249     int32_t height = 1280;
250     OH_AudioCaptureInfo miccapinfo = {
251         .audioSampleRate = SAMPLE_RATE_16000,
252         .audioChannels = STEREO,
253         .audioSource = OH_SOURCE_DEFAULT
254     };
255     OH_VideoCaptureInfo videocapinfo = {
256         .videoFrameWidth = width,
257         .videoFrameHeight = height,
258         .videoSource = OH_VIDEO_SOURCE_SURFACE_RGBA
259     };
260     OH_AudioInfo audioinfo = {
261         .micCapInfo = miccapinfo
262     };
263     OH_VideoInfo videoinfo = {
264         .videoCapInfo = videocapinfo
265     };
266     OH_RecorderInfo recorderinfo = {
267         .url = name
268     };
269     config = {
270         .captureMode = OH_CAPTURE_HOME_SCREEN,
271         .dataType = OH_ORIGINAL_STREAM,
272         .audioInfo = audioinfo,
273         .videoInfo = videoinfo,
274         .recorderInfo = recorderinfo
275     };
276 }
277 
OpenVFile(FILE * videoFile,string filename)278 FILE* OpenVFile(FILE *videoFile, string filename)
279 {
280     int32_t ret = snprintf_s(g_filename, sizeof(g_filename), sizeof(g_filename) - 1,
281         "data/storage/el2/base/files/%s.yuv", filename.c_str());
282     if (ret >= 0) {
283         videoFile = fopen(g_filename, "w+");
284     }
285     LOG(videoFile != nullptr, "vFile video open failed, %s", strerror(errno));
286     return videoFile;
287 }
288 
OpenAFile(FILE * audioFile,string filename)289 FILE* OpenAFile(FILE *audioFile, string filename)
290 {
291     int32_t ret = snprintf_s(g_filename, sizeof(g_filename), sizeof(g_filename) - 1,
292         "data/storage/el2/base/files/%s.pcm", filename.c_str());
293     if (ret >= 0) {
294         audioFile = fopen(g_filename, "w+");
295     }
296     LOG(audioFile != nullptr, "aFile audio open failed, %s", strerror(errno));
297     return audioFile;
298 }
299 
CloseFile(FILE * audioFile,FILE * videoFile)300 void CloseFile(FILE *audioFile, FILE *videoFile)
301 {
302     if (audioFile != nullptr) {
303         fclose(audioFile);
304         audioFile = nullptr;
305     }
306     if (videoFile != nullptr) {
307         fclose(videoFile);
308         videoFile = nullptr;
309     }
310 }
311 
312 // OH_Media_Screen_Capture_Init_001
InitWidthErr(napi_env env,napi_callback_info info)313 static napi_value InitWidthErr(napi_env env, napi_callback_info info)
314 {
315     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
316     OH_AVScreenCaptureConfig config_;
317     SetConfig(config_);
318     config_.videoInfo.videoCapInfo.videoFrameWidth = -1;
319     config_.videoInfo.videoCapInfo.videoSource = OH_VIDEO_SOURCE_SURFACE_RGBA;
320 
321     bool isMicrophone = false;
322     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
323     OH_AVSCREEN_CAPTURE_ErrCode result = OH_AVScreenCapture_Init(screenCapture, config_);
324     OH_AVScreenCapture_Release(screenCapture);
325     napi_value res;
326     napi_create_int32(env, result, &res);
327     return res;
328 }
329 
330 // OH_Media_Screen_Capture_Init_002
InitHeightErr(napi_env env,napi_callback_info info)331 static napi_value InitHeightErr(napi_env env, napi_callback_info info)
332 {
333     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
334     OH_AVScreenCaptureConfig config_;
335     SetConfig(config_);
336     config_.videoInfo.videoCapInfo.videoFrameHeight = -1;
337     config_.videoInfo.videoCapInfo.videoSource = OH_VIDEO_SOURCE_SURFACE_RGBA;
338 
339     bool isMicrophone = false;
340     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
341     OH_AVSCREEN_CAPTURE_ErrCode result = OH_AVScreenCapture_Init(screenCapture, config_);
342     OH_AVScreenCapture_Release(screenCapture);
343     napi_value res;
344     napi_create_int32(env, result, &res);
345     return res;
346 }
347 
348 // OH_Media_Screen_Capture_Init_003
InitVideoSourceYUV(napi_env env,napi_callback_info info)349 static napi_value InitVideoSourceYUV(napi_env env, napi_callback_info info)
350 {
351     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
352     OH_AVScreenCaptureConfig config_;
353     SetConfig(config_);
354     config_.videoInfo.videoCapInfo.videoSource = OH_VIDEO_SOURCE_SURFACE_YUV;
355 
356     bool isMicrophone = false;
357     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
358     OH_AVSCREEN_CAPTURE_ErrCode result = OH_AVScreenCapture_Init(screenCapture, config_);
359     OH_AVScreenCapture_Release(screenCapture);
360     napi_value res;
361     napi_create_int32(env, result, &res);
362     return res;
363 }
364 
365 // OH_Media_Screen_Capture_Init_004
InitVideoSourceES(napi_env env,napi_callback_info info)366 static napi_value InitVideoSourceES(napi_env env, napi_callback_info info)
367 {
368     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
369     OH_AVScreenCaptureConfig config_;
370     SetConfig(config_);
371     config_.videoInfo.videoCapInfo.videoSource = OH_VIDEO_SOURCE_SURFACE_ES;
372 
373     bool isMicrophone = false;
374     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
375     OH_AVSCREEN_CAPTURE_ErrCode result = OH_AVScreenCapture_Init(screenCapture, config_);
376     OH_AVScreenCapture_Release(screenCapture);
377     napi_value res;
378     napi_create_int32(env, result, &res);
379     return res;
380 }
381 
382 // OH_Media_Screen_Capture_Without_AudioData
WithoutAudioData(napi_env env,napi_callback_info info)383 static napi_value WithoutAudioData(napi_env env, napi_callback_info info)
384 {
385     g_aFlag = 1;
386     g_vFlag = 1;
387     FILE *audioFile = nullptr;
388     FILE *videoFile = nullptr;
389     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
390     OH_AVScreenCaptureConfig config_;
391     SetConfig(config_);
392     audioFile = OpenAFile(audioFile, "SUB_MULTIMEDIA_SCREEN_CAPTURE_WITHOUT_SOUND_DATA");
393     videoFile = OpenVFile(videoFile, "SUB_MULTIMEDIA_SCREEN_CAPTURE_WITHOUT_SOUND_DATA");
394     screenCaptureCb = std::make_shared<ScreenCaptureNdkTestCallback>(screenCapture, audioFile, nullptr, videoFile);
395 
396     bool isMicrophone = false;
397     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
398     SetScreenCaptureCallback(screenCapture, screenCaptureCb);
399     OH_AVSCREEN_CAPTURE_ErrCode result1 = OH_AVScreenCapture_Init(screenCapture, config_);
400     OH_AVSCREEN_CAPTURE_ErrCode result2 = OH_AVScreenCapture_StartScreenCapture(screenCapture);
401     sleep(g_recordTime);
402     OH_AVSCREEN_CAPTURE_ErrCode result3 = OH_AVScreenCapture_StopScreenCapture(screenCapture);
403     DelCallback(screenCapture);
404     OH_AVScreenCapture_Release(screenCapture);
405     CloseFile(audioFile, videoFile);
406     screenCaptureCb = nullptr;
407     napi_value res;
408     OH_AVSCREEN_CAPTURE_ErrCode result;
409     if (result1 == AV_SCREEN_CAPTURE_ERR_OK && result2 == AV_SCREEN_CAPTURE_ERR_OK
410         && result3 == AV_SCREEN_CAPTURE_ERR_OK) {
411         result = AV_SCREEN_CAPTURE_ERR_OK;
412     } else {
413         LOG(false, "init/start/stop failed, init: %d, start: %d, stop: %d", result1, result2, result3);
414         result = AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT;
415     }
416     napi_create_int32(env, result, &res);
417     return res;
418 }
419 
420 // OH_Media_Screen_Capture_Init_005
InitAudioSampleRate_01(napi_env env,napi_callback_info info)421 static napi_value InitAudioSampleRate_01(napi_env env, napi_callback_info info)
422 {
423     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
424     OH_AVScreenCaptureConfig config_;
425     SetConfig(config_);
426     config_.audioInfo.micCapInfo.audioSampleRate = -1;
427     config_.videoInfo.videoCapInfo.videoSource = OH_VIDEO_SOURCE_SURFACE_RGBA;
428 
429     bool isMicrophone = true;
430     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
431     OH_AVSCREEN_CAPTURE_ErrCode result = OH_AVScreenCapture_Init(screenCapture, config_);
432     OH_AVScreenCapture_Release(screenCapture);
433     napi_value res;
434     napi_create_int32(env, result, &res);
435     return res;
436 }
437 
438 // OH_Media_Screen_Capture_Init_006
InitAudioSampleRate_02(napi_env env,napi_callback_info info)439 static napi_value InitAudioSampleRate_02(napi_env env, napi_callback_info info)
440 {
441     int32_t invalidRate = 98000;
442     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
443     OH_AVScreenCaptureConfig config_;
444     SetConfig(config_);
445     config_.audioInfo.micCapInfo.audioSampleRate = invalidRate;
446     config_.videoInfo.videoCapInfo.videoSource = OH_VIDEO_SOURCE_SURFACE_RGBA;
447 
448     bool isMicrophone = true;
449     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
450     OH_AVSCREEN_CAPTURE_ErrCode result = OH_AVScreenCapture_Init(screenCapture, config_);
451     OH_AVScreenCapture_Release(screenCapture);
452     napi_value res;
453     napi_create_int32(env, result, &res);
454     return res;
455 }
456 
457 // OH_Media_Screen_Capture_Init_007
InitAudioSampleRate_03(napi_env env,napi_callback_info info)458 static napi_value InitAudioSampleRate_03(napi_env env, napi_callback_info info)
459 {
460     int32_t invalidRate = 30000;
461     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
462     OH_AVScreenCaptureConfig config_;
463     SetConfig(config_);
464     config_.audioInfo.micCapInfo.audioSampleRate = invalidRate;
465     config_.videoInfo.videoCapInfo.videoSource = OH_VIDEO_SOURCE_SURFACE_RGBA;
466 
467     bool isMicrophone = true;
468     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
469     OH_AVSCREEN_CAPTURE_ErrCode result = OH_AVScreenCapture_Init(screenCapture, config_);
470     OH_AVScreenCapture_Release(screenCapture);
471     napi_value res;
472     napi_create_int32(env, result, &res);
473     return res;
474 }
475 
476 // OH_Media_Screen_Capture_Init_008
InitAudioChannels_01(napi_env env,napi_callback_info info)477 static napi_value InitAudioChannels_01(napi_env env, napi_callback_info info)
478 {
479     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
480     OH_AVScreenCaptureConfig config_;
481     SetConfig(config_);
482     config_.audioInfo.micCapInfo.audioChannels = -1;
483     config_.videoInfo.videoCapInfo.videoSource = OH_VIDEO_SOURCE_SURFACE_RGBA;
484 
485     bool isMicrophone = true;
486     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
487     OH_AVSCREEN_CAPTURE_ErrCode result = OH_AVScreenCapture_Init(screenCapture, config_);
488     OH_AVScreenCapture_Release(screenCapture);
489     napi_value res;
490     napi_create_int32(env, result, &res);
491     return res;
492 }
493 
494 // OH_Media_Screen_Capture_Init_009
InitAudioChannels_02(napi_env env,napi_callback_info info)495 static napi_value InitAudioChannels_02(napi_env env, napi_callback_info info)
496 {
497     int32_t invalidChannels = 7;
498     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
499     OH_AVScreenCaptureConfig config_;
500     SetConfig(config_);
501     config_.audioInfo.micCapInfo.audioChannels = invalidChannels;
502     config_.videoInfo.videoCapInfo.videoSource = OH_VIDEO_SOURCE_SURFACE_RGBA;
503 
504     bool isMicrophone = true;
505     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
506     OH_AVSCREEN_CAPTURE_ErrCode result = OH_AVScreenCapture_Init(screenCapture, config_);
507     OH_AVScreenCapture_Release(screenCapture);
508     napi_value res;
509     napi_create_int32(env, result, &res);
510     return res;
511 }
512 
513 // OH_Media_Screen_Capture_Init_010
InitAudioSourceErr(napi_env env,napi_callback_info info)514 static napi_value InitAudioSourceErr(napi_env env, napi_callback_info info)
515 {
516     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
517     OH_AVScreenCaptureConfig config_;
518     SetConfig(config_);
519     config_.audioInfo.micCapInfo.audioSource = OH_SOURCE_INVALID;
520     config_.videoInfo.videoCapInfo.videoSource = OH_VIDEO_SOURCE_SURFACE_RGBA;
521 
522     bool isMicrophone = true;
523     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
524     OH_AVSCREEN_CAPTURE_ErrCode result = OH_AVScreenCapture_Init(screenCapture, config_);
525     OH_AVScreenCapture_Release(screenCapture);
526     napi_value res;
527     napi_create_int32(env, result, &res);
528     return res;
529 }
530 
531 // OH_Media_Screen_Capture_Init_011
ScreenCaptureInitErr(napi_env env,napi_callback_info info)532 static napi_value ScreenCaptureInitErr(napi_env env, napi_callback_info info)
533 {
534     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
535     OH_AVScreenCaptureConfig config_;
536     SetConfig(config_);
537     config_.audioInfo.micCapInfo.audioSource = OH_SOURCE_INVALID;
538     config_.videoInfo.videoCapInfo.videoSource = OH_VIDEO_SOURCE_SURFACE_YUV;
539 
540     bool isMicrophone = true;
541     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
542     OH_AVSCREEN_CAPTURE_ErrCode result = OH_AVScreenCapture_Init(screenCapture, config_);
543     OH_AVScreenCapture_Release(screenCapture);
544     napi_value res;
545     napi_create_int32(env, result, &res);
546     return res;
547 }
548 
549 // OH_Media_Screen_Capture_With_AudioData
WithAudioData(napi_env env,napi_callback_info info)550 static napi_value WithAudioData(napi_env env, napi_callback_info info)
551 {
552     g_aFlag = 1;
553     g_vFlag = 1;
554     FILE *audioFile = nullptr;
555     FILE *videoFile = nullptr;
556     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
557     OH_AVScreenCaptureConfig config_;
558     SetConfig(config_);
559     audioFile = OpenAFile(audioFile, "SUB_MULTIMEDIA_SCREEN_CAPTURE_WITH_SOUND_DATA");
560     videoFile = OpenVFile(videoFile, "SUB_MULTIMEDIA_SCREEN_CAPTURE_WITH_SOUND_DATA");
561     screenCaptureCb = std::make_shared<ScreenCaptureNdkTestCallback>(screenCapture, audioFile, nullptr, videoFile);
562 
563     bool isMicrophone = true;
564     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
565     SetScreenCaptureCallback(screenCapture, screenCaptureCb);
566     OH_AVSCREEN_CAPTURE_ErrCode result1 = OH_AVScreenCapture_Init(screenCapture, config_);
567     OH_AVSCREEN_CAPTURE_ErrCode result2 = OH_AVScreenCapture_StartScreenCapture(screenCapture);
568     sleep(g_recordTime);
569     OH_AVSCREEN_CAPTURE_ErrCode result3 = OH_AVScreenCapture_StopScreenCapture(screenCapture);
570     DelCallback(screenCapture);
571     OH_AVScreenCapture_Release(screenCapture);
572     CloseFile(audioFile, videoFile);
573     screenCaptureCb = nullptr;
574     napi_value res;
575     OH_AVSCREEN_CAPTURE_ErrCode result;
576     if (result1 == AV_SCREEN_CAPTURE_ERR_OK && result2 == AV_SCREEN_CAPTURE_ERR_OK
577         && result3 == AV_SCREEN_CAPTURE_ERR_OK) {
578         result = AV_SCREEN_CAPTURE_ERR_OK;
579     } else {
580         LOG(false, "init/start/stop failed, init: %d, start: %d, stop: %d", result1, result2, result3);
581         result = AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT;
582     }
583     napi_create_int32(env, result, &res);
584     return res;
585 }
586 
587 // OH_Media_Screen_Capture_Init_012
InitCaptureMode_01(napi_env env,napi_callback_info info)588 static napi_value InitCaptureMode_01(napi_env env, napi_callback_info info)
589 {
590     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
591     OH_AVScreenCaptureConfig config_;
592     SetConfig(config_);
593     config_.captureMode = static_cast<OH_CaptureMode>(-1);
594     config_.videoInfo.videoCapInfo.videoSource = OH_VIDEO_SOURCE_SURFACE_RGBA;
595 
596     bool isMicrophone = true;
597     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
598     OH_AVSCREEN_CAPTURE_ErrCode result = OH_AVScreenCapture_Init(screenCapture, config_);
599     OH_AVScreenCapture_Release(screenCapture);
600     napi_value res;
601     napi_create_int32(env, result, &res);
602     return res;
603 }
604 
605 // OH_Media_Screen_Capture_Init_013
InitCaptureMode_02(napi_env env,napi_callback_info info)606 static napi_value InitCaptureMode_02(napi_env env, napi_callback_info info)
607 {
608     int32_t invalidMode = 3;
609     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
610     OH_AVScreenCaptureConfig config_;
611     SetConfig(config_);
612     config_.captureMode = static_cast<OH_CaptureMode>(invalidMode);
613     config_.videoInfo.videoCapInfo.videoSource = OH_VIDEO_SOURCE_SURFACE_RGBA;
614 
615     bool isMicrophone = true;
616     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
617     OH_AVSCREEN_CAPTURE_ErrCode result = OH_AVScreenCapture_Init(screenCapture, config_);
618     OH_AVScreenCapture_Release(screenCapture);
619     napi_value res;
620     napi_create_int32(env, result, &res);
621     return res;
622 }
623 
624 // OH_Media_Screen_Capture_Init_014
InitDataType_01(napi_env env,napi_callback_info info)625 static napi_value InitDataType_01(napi_env env, napi_callback_info info)
626 {
627     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
628     OH_AVScreenCaptureConfig config_;
629     SetConfig(config_);
630     config_.videoInfo.videoCapInfo.videoSource = OH_VIDEO_SOURCE_SURFACE_RGBA;
631     config_.dataType = OH_ENCODED_STREAM;
632 
633     bool isMicrophone = true;
634     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
635     OH_AVSCREEN_CAPTURE_ErrCode result = OH_AVScreenCapture_Init(screenCapture, config_);
636     OH_AVScreenCapture_Release(screenCapture);
637     napi_value res;
638     napi_create_int32(env, result, &res);
639     return res;
640 }
641 
642 // OH_Media_Screen_Capture_Init_015
InitDataType_02(napi_env env,napi_callback_info info)643 static napi_value InitDataType_02(napi_env env, napi_callback_info info)
644 {
645     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
646     OH_AVScreenCaptureConfig config_;
647     SetConfig(config_);
648     config_.videoInfo.videoCapInfo.videoSource = OH_VIDEO_SOURCE_SURFACE_RGBA;
649     config_.dataType = OH_CAPTURE_FILE;
650 
651     bool isMicrophone = true;
652     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
653     OH_AVSCREEN_CAPTURE_ErrCode result = OH_AVScreenCapture_Init(screenCapture, config_);
654     OH_AVScreenCapture_Release(screenCapture);
655     napi_value res;
656     napi_create_int32(env, result, &res);
657     return res;
658 }
659 
660 // OH_Media_Screen_Capture_Init_016
InitDataType_03(napi_env env,napi_callback_info info)661 static napi_value InitDataType_03(napi_env env, napi_callback_info info)
662 {
663     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
664     OH_AVScreenCaptureConfig config_;
665     SetConfig(config_);
666     config_.videoInfo.videoCapInfo.videoSource = OH_VIDEO_SOURCE_SURFACE_RGBA;
667     config_.dataType = OH_INVAILD;
668 
669     bool isMicrophone = true;
670     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
671     OH_AVSCREEN_CAPTURE_ErrCode result = OH_AVScreenCapture_Init(screenCapture, config_);
672     OH_AVScreenCapture_Release(screenCapture);
673     napi_value res;
674     napi_create_int32(env, result, &res);
675     return res;
676 }
677 
678 // OH_Media_Screen_Capture_AudioSampleRate_8000
ChangeAudioSampleRate_01(napi_env env,napi_callback_info info)679 static napi_value ChangeAudioSampleRate_01(napi_env env, napi_callback_info info)
680 {
681     g_aFlag = 1;
682     g_vFlag = 1;
683     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
684     OH_AVScreenCaptureConfig config_;
685     SetConfig(config_);
686     config_.audioInfo.micCapInfo.audioSampleRate = SAMPLE_RATE_8000;
687     screenCaptureCb = std::make_shared<ScreenCaptureNdkTestCallback>(screenCapture, nullptr, nullptr, nullptr);
688 
689     bool isMicrophone = true;
690     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
691     SetScreenCaptureCallback(screenCapture, screenCaptureCb);
692     OH_AVSCREEN_CAPTURE_ErrCode result1 = OH_AVScreenCapture_Init(screenCapture, config_);
693     OH_AVSCREEN_CAPTURE_ErrCode result2 = OH_AVScreenCapture_StartScreenCapture(screenCapture);
694     sleep(g_recordTime);
695     OH_AVSCREEN_CAPTURE_ErrCode result3 = OH_AVScreenCapture_StopScreenCapture(screenCapture);
696     DelCallback(screenCapture);
697     OH_AVScreenCapture_Release(screenCapture);
698     screenCaptureCb = nullptr;
699     napi_value res;
700     OH_AVSCREEN_CAPTURE_ErrCode result;
701     if (result1 == AV_SCREEN_CAPTURE_ERR_OK && result2 == AV_SCREEN_CAPTURE_ERR_OK
702         && result3 == AV_SCREEN_CAPTURE_ERR_OK) {
703         result = AV_SCREEN_CAPTURE_ERR_OK;
704     } else {
705         LOG(false, "init/start/stop failed, init: %d, start: %d, stop: %d", result1, result2, result3);
706         result = AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT;
707     }
708     napi_create_int32(env, result, &res);
709     return res;
710 }
711 
712 // OH_Media_Screen_Capture_AudioSampleRate_96000
ChangeAudioSampleRate_02(napi_env env,napi_callback_info info)713 static napi_value ChangeAudioSampleRate_02(napi_env env, napi_callback_info info)
714 {
715     g_aFlag = 1;
716     g_vFlag = 1;
717     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
718     OH_AVScreenCaptureConfig config_;
719     SetConfig(config_);
720     config_.audioInfo.micCapInfo.audioSampleRate = SAMPLE_RATE_96000;
721     screenCaptureCb = std::make_shared<ScreenCaptureNdkTestCallback>(screenCapture, nullptr, nullptr, nullptr);
722 
723     bool isMicrophone = true;
724     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
725     SetScreenCaptureCallback(screenCapture, screenCaptureCb);
726     OH_AVSCREEN_CAPTURE_ErrCode result1 = OH_AVScreenCapture_Init(screenCapture, config_);
727     OH_AVSCREEN_CAPTURE_ErrCode result2 = OH_AVScreenCapture_StartScreenCapture(screenCapture);
728     sleep(g_recordTime);
729     OH_AVSCREEN_CAPTURE_ErrCode result3 = OH_AVScreenCapture_StopScreenCapture(screenCapture);
730     DelCallback(screenCapture);
731     OH_AVScreenCapture_Release(screenCapture);
732     screenCaptureCb = nullptr;
733     napi_value res;
734     OH_AVSCREEN_CAPTURE_ErrCode result;
735     if (result1 == AV_SCREEN_CAPTURE_ERR_OK && result2 == AV_SCREEN_CAPTURE_ERR_OK
736         && result3 == AV_SCREEN_CAPTURE_ERR_OK) {
737         result = AV_SCREEN_CAPTURE_ERR_OK;
738     } else {
739         LOG(false, "init/start/stop failed, init: %d, start: %d, stop: %d", result1, result2, result3);
740         result = AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT;
741     }
742     napi_create_int32(env, result, &res);
743     return res;
744 }
745 
746 // OH_Media_Screen_Capture_AudioChannels_001
ChangeAudioChannels_01(napi_env env,napi_callback_info info)747 static napi_value ChangeAudioChannels_01(napi_env env, napi_callback_info info)
748 {
749     g_aFlag = 1;
750     g_vFlag = 1;
751     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
752     OH_AVScreenCaptureConfig config_;
753     SetConfig(config_);
754     config_.audioInfo.micCapInfo.audioChannels = CHANNEL_3;
755     screenCaptureCb = std::make_shared<ScreenCaptureNdkTestCallback>(screenCapture, nullptr, nullptr, nullptr);
756 
757     bool isMicrophone = true;
758     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
759     SetScreenCaptureCallback(screenCapture, screenCaptureCb);
760     OH_AVSCREEN_CAPTURE_ErrCode result1 = OH_AVScreenCapture_Init(screenCapture, config_);
761     OH_AVSCREEN_CAPTURE_ErrCode result2 = OH_AVScreenCapture_StartScreenCapture(screenCapture);
762     sleep(g_recordTime);
763     OH_AVSCREEN_CAPTURE_ErrCode result3 = OH_AVScreenCapture_StopScreenCapture(screenCapture);
764     DelCallback(screenCapture);
765     OH_AVScreenCapture_Release(screenCapture);
766     screenCaptureCb = nullptr;
767     napi_value res;
768     OH_AVSCREEN_CAPTURE_ErrCode result;
769     if (result1 == AV_SCREEN_CAPTURE_ERR_OK && result2 == AV_SCREEN_CAPTURE_ERR_OK
770         && result3 == AV_SCREEN_CAPTURE_ERR_OK) {
771         result = AV_SCREEN_CAPTURE_ERR_OK;
772     } else {
773         LOG(false, "init/start/stop failed, init: %d, start: %d, stop: %d", result1, result2, result3);
774         result = AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT;
775     }
776     napi_create_int32(env, result, &res);
777     return res;
778 }
779 
780 // OH_Media_Screen_Capture_AudioChannels_002
ChangeAudioChannels_02(napi_env env,napi_callback_info info)781 static napi_value ChangeAudioChannels_02(napi_env env, napi_callback_info info)
782 {
783     g_aFlag = 1;
784     g_vFlag = 1;
785     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
786     OH_AVScreenCaptureConfig config_;
787     SetConfig(config_);
788     config_.audioInfo.micCapInfo.audioChannels = CHANNEL_4;
789     screenCaptureCb = std::make_shared<ScreenCaptureNdkTestCallback>(screenCapture, nullptr, nullptr, nullptr);
790 
791     bool isMicrophone = true;
792     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
793     SetScreenCaptureCallback(screenCapture, screenCaptureCb);
794     OH_AVSCREEN_CAPTURE_ErrCode result1 = OH_AVScreenCapture_Init(screenCapture, config_);
795     OH_AVSCREEN_CAPTURE_ErrCode result2 = OH_AVScreenCapture_StartScreenCapture(screenCapture);
796     sleep(g_recordTime);
797     OH_AVSCREEN_CAPTURE_ErrCode result3 = OH_AVScreenCapture_StopScreenCapture(screenCapture);
798     DelCallback(screenCapture);
799     OH_AVScreenCapture_Release(screenCapture);
800     screenCaptureCb = nullptr;
801     napi_value res;
802     OH_AVSCREEN_CAPTURE_ErrCode result;
803     if (result1 == AV_SCREEN_CAPTURE_ERR_OK && result2 == AV_SCREEN_CAPTURE_ERR_OK
804         && result3 == AV_SCREEN_CAPTURE_ERR_OK) {
805         result = AV_SCREEN_CAPTURE_ERR_OK;
806     } else {
807         LOG(false, "init/start/stop failed, init: %d, start: %d, stop: %d", result1, result2, result3);
808         result = AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT;
809     }
810     napi_create_int32(env, result, &res);
811     return res;
812 }
813 
814 // OH_Media_Screen_Capture_AudioChannels_003
ChangeAudioChannels_03(napi_env env,napi_callback_info info)815 static napi_value ChangeAudioChannels_03(napi_env env, napi_callback_info info)
816 {
817     g_aFlag = 1;
818     g_vFlag = 1;
819     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
820     OH_AVScreenCaptureConfig config_;
821     SetConfig(config_);
822     config_.audioInfo.micCapInfo.audioChannels = CHANNEL_5;
823     screenCaptureCb = std::make_shared<ScreenCaptureNdkTestCallback>(screenCapture, nullptr, nullptr, nullptr);
824 
825     bool isMicrophone = true;
826     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
827     SetScreenCaptureCallback(screenCapture, screenCaptureCb);
828     OH_AVSCREEN_CAPTURE_ErrCode result1 = OH_AVScreenCapture_Init(screenCapture, config_);
829     OH_AVSCREEN_CAPTURE_ErrCode result2 = OH_AVScreenCapture_StartScreenCapture(screenCapture);
830     sleep(g_recordTime);
831     OH_AVSCREEN_CAPTURE_ErrCode result3 = OH_AVScreenCapture_StopScreenCapture(screenCapture);
832     DelCallback(screenCapture);
833     OH_AVScreenCapture_Release(screenCapture);
834     screenCaptureCb = nullptr;
835     napi_value res;
836     OH_AVSCREEN_CAPTURE_ErrCode result;
837     if (result1 == AV_SCREEN_CAPTURE_ERR_OK && result2 == AV_SCREEN_CAPTURE_ERR_OK
838         && result3 == AV_SCREEN_CAPTURE_ERR_OK) {
839         result = AV_SCREEN_CAPTURE_ERR_OK;
840     } else {
841         LOG(false, "init/start/stop failed, init: %d, start: %d, stop: %d", result1, result2, result3);
842         result = AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT;
843     }
844     napi_create_int32(env, result, &res);
845     return res;
846 }
847 
848 // OH_Media_Screen_Capture_AudioChannels_004
ChangeAudioChannels_04(napi_env env,napi_callback_info info)849 static napi_value ChangeAudioChannels_04(napi_env env, napi_callback_info info)
850 {
851     g_aFlag = 1;
852     g_vFlag = 1;
853     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
854     OH_AVScreenCaptureConfig config_;
855     SetConfig(config_);
856     config_.audioInfo.micCapInfo.audioChannels = CHANNEL_6;
857     screenCaptureCb = std::make_shared<ScreenCaptureNdkTestCallback>(screenCapture, nullptr, nullptr, nullptr);
858 
859     bool isMicrophone = true;
860     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
861     SetScreenCaptureCallback(screenCapture, screenCaptureCb);
862     OH_AVSCREEN_CAPTURE_ErrCode result1 = OH_AVScreenCapture_Init(screenCapture, config_);
863     OH_AVSCREEN_CAPTURE_ErrCode result2 = OH_AVScreenCapture_StartScreenCapture(screenCapture);
864     sleep(g_recordTime);
865     OH_AVSCREEN_CAPTURE_ErrCode result3 = OH_AVScreenCapture_StopScreenCapture(screenCapture);
866     DelCallback(screenCapture);
867     OH_AVScreenCapture_Release(screenCapture);
868     screenCaptureCb = nullptr;
869     napi_value res;
870     OH_AVSCREEN_CAPTURE_ErrCode result;
871     if (result1 == AV_SCREEN_CAPTURE_ERR_OK && result2 == AV_SCREEN_CAPTURE_ERR_OK
872         && result3 == AV_SCREEN_CAPTURE_ERR_OK) {
873         result = AV_SCREEN_CAPTURE_ERR_OK;
874     } else {
875         LOG(false, "init/start/stop failed, init: %d, start: %d, stop: %d", result1, result2, result3);
876         result = AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT;
877     }
878     napi_create_int32(env, result, &res);
879     return res;
880 }
881 
882 // OH_Media_Screen_Capture_VideoSize_001
ChangeVideoSize_01(napi_env env,napi_callback_info info)883 static napi_value ChangeVideoSize_01(napi_env env, napi_callback_info info)
884 {
885     g_aFlag = 1;
886     g_vFlag = 1;
887     int32_t width = 160;
888     int32_t height = 160;
889     FILE *videoFile = nullptr;
890     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
891     OH_AVScreenCaptureConfig config_;
892     SetConfig(config_);
893     config_.videoInfo.videoCapInfo.videoFrameWidth = width;
894     config_.videoInfo.videoCapInfo.videoFrameHeight = height;
895     config_.videoInfo.videoCapInfo.videoSource = OH_VIDEO_SOURCE_SURFACE_RGBA;
896     videoFile = OpenVFile(videoFile, "SUB_MULTIMEDIA_SCREEN_CAPTURE_VIDEOSIZE_0001");
897     screenCaptureCb = std::make_shared<ScreenCaptureNdkTestCallback>(screenCapture, nullptr, nullptr, videoFile);
898 
899     bool isMicrophone = true;
900     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
901     SetScreenCaptureCallback(screenCapture, screenCaptureCb);
902     OH_AVSCREEN_CAPTURE_ErrCode result1 = OH_AVScreenCapture_Init(screenCapture, config_);
903     OH_AVSCREEN_CAPTURE_ErrCode result2 = OH_AVScreenCapture_StartScreenCapture(screenCapture);
904     sleep(g_recordTime);
905     OH_AVSCREEN_CAPTURE_ErrCode result3 = OH_AVScreenCapture_StopScreenCapture(screenCapture);
906     DelCallback(screenCapture);
907     OH_AVScreenCapture_Release(screenCapture);
908     CloseFile(nullptr, videoFile);
909     screenCaptureCb = nullptr;
910     napi_value res;
911     OH_AVSCREEN_CAPTURE_ErrCode result;
912     if (result1 == AV_SCREEN_CAPTURE_ERR_OK && result2 == AV_SCREEN_CAPTURE_ERR_OK
913         && result3 == AV_SCREEN_CAPTURE_ERR_OK) {
914         result = AV_SCREEN_CAPTURE_ERR_OK;
915     } else {
916         LOG(false, "init/start/stop failed, init: %d, start: %d, stop: %d", result1, result2, result3);
917         result = AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT;
918     }
919     napi_create_int32(env, result, &res);
920     return res;
921 }
922 
923 // OH_Media_Screen_Capture_VideoSize_002
ChangeVideoSize_02(napi_env env,napi_callback_info info)924 static napi_value ChangeVideoSize_02(napi_env env, napi_callback_info info)
925 {
926     g_aFlag = 1;
927     g_vFlag = 1;
928     int32_t width = 640;
929     int32_t height = 480;
930     FILE *videoFile = nullptr;
931     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
932     OH_AVScreenCaptureConfig config_;
933     SetConfig(config_);
934     config_.videoInfo.videoCapInfo.videoFrameWidth = width;
935     config_.videoInfo.videoCapInfo.videoFrameHeight = height;
936     config_.videoInfo.videoCapInfo.videoSource = OH_VIDEO_SOURCE_SURFACE_RGBA;
937     videoFile = OpenVFile(videoFile, "SUB_MULTIMEDIA_SCREEN_CAPTURE_VIDEOSIZE_0002");
938     screenCaptureCb = std::make_shared<ScreenCaptureNdkTestCallback>(screenCapture, nullptr, nullptr, videoFile);
939 
940     bool isMicrophone = true;
941     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
942     SetScreenCaptureCallback(screenCapture, screenCaptureCb);
943     OH_AVSCREEN_CAPTURE_ErrCode result1 = OH_AVScreenCapture_Init(screenCapture, config_);
944     OH_AVSCREEN_CAPTURE_ErrCode result2 = OH_AVScreenCapture_StartScreenCapture(screenCapture);
945     sleep(g_recordTime);
946     OH_AVSCREEN_CAPTURE_ErrCode result3 = OH_AVScreenCapture_StopScreenCapture(screenCapture);
947     DelCallback(screenCapture);
948     OH_AVScreenCapture_Release(screenCapture);
949     CloseFile(nullptr, videoFile);
950     screenCaptureCb = nullptr;
951     napi_value res;
952     OH_AVSCREEN_CAPTURE_ErrCode result;
953     if (result1 == AV_SCREEN_CAPTURE_ERR_OK && result2 == AV_SCREEN_CAPTURE_ERR_OK
954         && result3 == AV_SCREEN_CAPTURE_ERR_OK) {
955         result = AV_SCREEN_CAPTURE_ERR_OK;
956     } else {
957         LOG(false, "init/start/stop failed, init: %d, start: %d, stop: %d", result1, result2, result3);
958         result = AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT;
959     }
960     napi_create_int32(env, result, &res);
961     return res;
962 }
963 
964 // OH_Media_Screen_Capture_VideoSize_003
ChangeVideoSize_03(napi_env env,napi_callback_info info)965 static napi_value ChangeVideoSize_03(napi_env env, napi_callback_info info)
966 {
967     g_aFlag = 1;
968     g_vFlag = 1;
969     int32_t width = 1920;
970     int32_t height = 1080;
971     FILE *videoFile = nullptr;
972     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
973     OH_AVScreenCaptureConfig config_;
974     SetConfig(config_);
975     config_.videoInfo.videoCapInfo.videoFrameWidth = width;
976     config_.videoInfo.videoCapInfo.videoFrameHeight = height;
977     config_.videoInfo.videoCapInfo.videoSource = OH_VIDEO_SOURCE_SURFACE_RGBA;
978     videoFile = OpenVFile(videoFile, "SUB_MULTIMEDIA_SCREEN_CAPTURE_VIDEOSIZE_0003");
979     screenCaptureCb = std::make_shared<ScreenCaptureNdkTestCallback>(screenCapture, nullptr, nullptr, videoFile);
980 
981     bool isMicrophone = true;
982     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
983     SetScreenCaptureCallback(screenCapture, screenCaptureCb);
984     OH_AVSCREEN_CAPTURE_ErrCode result1 = OH_AVScreenCapture_Init(screenCapture, config_);
985     OH_AVSCREEN_CAPTURE_ErrCode result2 = OH_AVScreenCapture_StartScreenCapture(screenCapture);
986     sleep(g_recordTime);
987     OH_AVSCREEN_CAPTURE_ErrCode result3 = OH_AVScreenCapture_StopScreenCapture(screenCapture);
988     DelCallback(screenCapture);
989     OH_AVScreenCapture_Release(screenCapture);
990     CloseFile(nullptr, videoFile);
991     screenCaptureCb = nullptr;
992     napi_value res;
993     OH_AVSCREEN_CAPTURE_ErrCode result;
994     if (result1 == AV_SCREEN_CAPTURE_ERR_OK && result2 == AV_SCREEN_CAPTURE_ERR_OK
995         && result3 == AV_SCREEN_CAPTURE_ERR_OK) {
996         result = AV_SCREEN_CAPTURE_ERR_OK;
997     } else {
998         LOG(false, "init/start/stop failed, init: %d, start: %d, stop: %d", result1, result2, result3);
999         result = AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT;
1000     }
1001     napi_create_int32(env, result, &res);
1002     return res;
1003 }
1004 
1005 // OH_Media_Screen_Capture_Display
ScreenCaptureFromDisplay(napi_env env,napi_callback_info info)1006 static napi_value ScreenCaptureFromDisplay(napi_env env, napi_callback_info info)
1007 {
1008     g_aFlag = 1;
1009     g_vFlag = 1;
1010     size_t argc = 3;
1011     int32_t num = 2;
1012     napi_value args[3] = {nullptr};
1013     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
1014     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
1015     OH_AVScreenCaptureConfig config_;
1016     SetConfig(config_);
1017     int32_t width;
1018     napi_get_value_int32(env, args[0], &width);
1019     int32_t height;
1020     napi_get_value_int32(env, args[1], &height);
1021     int32_t frameRate;
1022     napi_get_value_int32(env, args[num], &frameRate);
1023     LOG(false, "Get display info: width: %d, height: %d", width, height);
1024     config_.videoInfo.videoCapInfo.videoFrameWidth = width;
1025     config_.videoInfo.videoCapInfo.videoFrameHeight = height;
1026     config_.videoInfo.videoCapInfo.videoSource = OH_VIDEO_SOURCE_SURFACE_RGBA;
1027     screenCaptureCb = std::make_shared<ScreenCaptureNdkTestCallback>(screenCapture, nullptr, nullptr, nullptr);
1028 
1029     bool isMicrophone = true;
1030     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
1031     SetScreenCaptureCallback(screenCapture, screenCaptureCb);
1032     OH_AVSCREEN_CAPTURE_ErrCode result1 = OH_AVScreenCapture_Init(screenCapture, config_);
1033     OH_AVSCREEN_CAPTURE_ErrCode result2 = OH_AVScreenCapture_StartScreenCapture(screenCapture);
1034     sleep(g_recordTime);
1035     OH_AVSCREEN_CAPTURE_ErrCode result3 = OH_AVScreenCapture_StopScreenCapture(screenCapture);
1036     DelCallback(screenCapture);
1037     OH_AVScreenCapture_Release(screenCapture);
1038     screenCaptureCb = nullptr;
1039     napi_value res;
1040     OH_AVSCREEN_CAPTURE_ErrCode result;
1041     if (result1 == AV_SCREEN_CAPTURE_ERR_OK && result2 == AV_SCREEN_CAPTURE_ERR_OK
1042         && result3 == AV_SCREEN_CAPTURE_ERR_OK) {
1043         result = AV_SCREEN_CAPTURE_ERR_OK;
1044     } else {
1045         LOG(false, "init/start/stop failed, init: %d, start: %d, stop: %d", result1, result2, result3);
1046         result = AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT;
1047     }
1048     napi_create_int32(env, result, &res);
1049     return res;
1050 }
1051 
1052 // OH_Media_Screen_Capture_Buffer_001
BufferAndRelease_01(napi_env env,napi_callback_info info)1053 static napi_value BufferAndRelease_01(napi_env env, napi_callback_info info)
1054 {
1055     g_aFlag = 0;
1056     g_vFlag = 1;
1057     int32_t time = 15;
1058     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
1059     OH_AVScreenCaptureConfig config_;
1060     SetConfig(config_);
1061     config_.videoInfo.videoCapInfo.videoSource = OH_VIDEO_SOURCE_SURFACE_RGBA;
1062     screenCaptureCb = std::make_shared<ScreenCaptureNdkTestCallback>(screenCapture, nullptr, nullptr, nullptr);
1063 
1064     bool isMicrophone = true;
1065     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
1066     SetScreenCaptureCallback(screenCapture, screenCaptureCb);
1067     OH_AVSCREEN_CAPTURE_ErrCode result1 = OH_AVScreenCapture_Init(screenCapture, config_);
1068     OH_AVSCREEN_CAPTURE_ErrCode result2 = OH_AVScreenCapture_StartScreenCapture(screenCapture);
1069     sleep(time);
1070     OH_AVSCREEN_CAPTURE_ErrCode result3 = OH_AVScreenCapture_StopScreenCapture(screenCapture);
1071     DelCallback(screenCapture);
1072     OH_AVScreenCapture_Release(screenCapture);
1073     screenCaptureCb = nullptr;
1074     napi_value res;
1075     OH_AVSCREEN_CAPTURE_ErrCode result;
1076     if (result1 == AV_SCREEN_CAPTURE_ERR_OK && result2 == AV_SCREEN_CAPTURE_ERR_OK
1077         && result3 == AV_SCREEN_CAPTURE_ERR_OK) {
1078         result = AV_SCREEN_CAPTURE_ERR_OK;
1079     } else {
1080         LOG(false, "init/start/stop failed, init: %d, start: %d, stop: %d", result1, result2, result3);
1081         result = AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT;
1082     }
1083     napi_create_int32(env, result, &res);
1084     return res;
1085 }
1086 
1087 // OH_Media_Screen_Capture_Buffer_002
BufferAndRelease_02(napi_env env,napi_callback_info info)1088 static napi_value BufferAndRelease_02(napi_env env, napi_callback_info info)
1089 {
1090     g_aFlag = 1;
1091     g_vFlag = 0;
1092     int32_t time = 10;
1093     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
1094     OH_AVScreenCaptureConfig config_;
1095     SetConfig(config_);
1096     config_.videoInfo.videoCapInfo.videoSource = OH_VIDEO_SOURCE_SURFACE_RGBA;
1097     screenCaptureCb = std::make_shared<ScreenCaptureNdkTestCallback>(screenCapture, nullptr, nullptr, nullptr);
1098 
1099     bool isMicrophone = true;
1100     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
1101     SetScreenCaptureCallback(screenCapture, screenCaptureCb);
1102     OH_AVSCREEN_CAPTURE_ErrCode result1 = OH_AVScreenCapture_Init(screenCapture, config_);
1103     OH_AVSCREEN_CAPTURE_ErrCode result2 = OH_AVScreenCapture_StartScreenCapture(screenCapture);
1104     sleep(time);
1105     OH_AVSCREEN_CAPTURE_ErrCode result3 = OH_AVScreenCapture_StopScreenCapture(screenCapture);
1106     DelCallback(screenCapture);
1107     OH_AVScreenCapture_Release(screenCapture);
1108     screenCaptureCb = nullptr;
1109     napi_value res;
1110     OH_AVSCREEN_CAPTURE_ErrCode result;
1111     if (result1 == AV_SCREEN_CAPTURE_ERR_OK && result2 == AV_SCREEN_CAPTURE_ERR_OK
1112         && result3 == AV_SCREEN_CAPTURE_ERR_OK) {
1113         result = AV_SCREEN_CAPTURE_ERR_OK;
1114     } else {
1115         LOG(false, "init/start/stop failed, init: %d, start: %d, stop: %d", result1, result2, result3);
1116         result = AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT;
1117     }
1118     napi_create_int32(env, result, &res);
1119     return res;
1120 }
1121 
1122 // OH_Media_Screen_Capture_Buffer_003
BufferAndRelease_03(napi_env env,napi_callback_info info)1123 static napi_value BufferAndRelease_03(napi_env env, napi_callback_info info)
1124 {
1125     g_aFlag = 0;
1126     g_vFlag = 0;
1127     int32_t time = 10;
1128     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
1129     OH_AVScreenCaptureConfig config_;
1130     SetConfig(config_);
1131     config_.videoInfo.videoCapInfo.videoSource = OH_VIDEO_SOURCE_SURFACE_RGBA;
1132     screenCaptureCb = std::make_shared<ScreenCaptureNdkTestCallback>(screenCapture, nullptr, nullptr, nullptr);
1133 
1134     bool isMicrophone = true;
1135     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
1136     SetScreenCaptureCallback(screenCapture, screenCaptureCb);
1137     OH_AVSCREEN_CAPTURE_ErrCode result1 = OH_AVScreenCapture_Init(screenCapture, config_);
1138     OH_AVSCREEN_CAPTURE_ErrCode result2 = OH_AVScreenCapture_StartScreenCapture(screenCapture);
1139     sleep(time);
1140     OH_AVSCREEN_CAPTURE_ErrCode result3 = OH_AVScreenCapture_StopScreenCapture(screenCapture);
1141     DelCallback(screenCapture);
1142     OH_AVScreenCapture_Release(screenCapture);
1143     screenCaptureCb = nullptr;
1144     napi_value res;
1145     OH_AVSCREEN_CAPTURE_ErrCode result;
1146     if (result1 == AV_SCREEN_CAPTURE_ERR_OK && result2 == AV_SCREEN_CAPTURE_ERR_OK
1147         && result3 == AV_SCREEN_CAPTURE_ERR_OK) {
1148         result = AV_SCREEN_CAPTURE_ERR_OK;
1149     } else {
1150         LOG(false, "init/start/stop failed, init: %d, start: %d, stop: %d", result1, result2, result3);
1151         result = AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT;
1152     }
1153     napi_create_int32(env, result, &res);
1154     return res;
1155 }
1156 
1157 // OH_Media_Screen_Capture_Mic_001
SetMicrophoneEnabled_01(napi_env env,napi_callback_info info)1158 static napi_value SetMicrophoneEnabled_01(napi_env env, napi_callback_info info)
1159 {
1160     g_aFlag = 1;
1161     g_vFlag = 1;
1162     int32_t time = 5;
1163     FILE *audioFile = nullptr;
1164     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
1165     OH_AVScreenCaptureConfig config_;
1166     SetConfig(config_);
1167     config_.videoInfo.videoCapInfo.videoSource = OH_VIDEO_SOURCE_SURFACE_RGBA;
1168     audioFile = OpenAFile(audioFile, "SUB_MULTIMEDIA_SCREEN_CAPTURE_MICTEST_0001");
1169     screenCaptureCb = std::make_shared<ScreenCaptureNdkTestCallback>(screenCapture, audioFile, nullptr, nullptr);
1170 
1171     bool isMicrophone = true;
1172     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
1173     SetScreenCaptureCallback(screenCapture, screenCaptureCb);
1174     OH_AVSCREEN_CAPTURE_ErrCode result1 = OH_AVScreenCapture_Init(screenCapture, config_);
1175     OH_AVSCREEN_CAPTURE_ErrCode result2 = OH_AVScreenCapture_StartScreenCapture(screenCapture);
1176     sleep(time);
1177     isMicrophone = false;
1178     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
1179     sleep(g_recordTime);
1180     isMicrophone = true;
1181     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
1182     sleep(g_recordTime);
1183     OH_AVSCREEN_CAPTURE_ErrCode result3 = OH_AVScreenCapture_StopScreenCapture(screenCapture);
1184     DelCallback(screenCapture);
1185     OH_AVScreenCapture_Release(screenCapture);
1186     CloseFile(audioFile, nullptr);
1187     screenCaptureCb = nullptr;
1188     napi_value res;
1189     OH_AVSCREEN_CAPTURE_ErrCode result;
1190     if (result1 == AV_SCREEN_CAPTURE_ERR_OK && result2 == AV_SCREEN_CAPTURE_ERR_OK
1191         && result3 == AV_SCREEN_CAPTURE_ERR_OK) {
1192         result = AV_SCREEN_CAPTURE_ERR_OK;
1193     } else {
1194         LOG(false, "init/start/stop failed, init: %d, start: %d, stop: %d", result1, result2, result3);
1195         result = AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT;
1196     }
1197     napi_create_int32(env, result, &res);
1198     return res;
1199 }
1200 
1201 // OH_Media_Screen_Capture_Mic_002
SetMicrophoneEnabled_02(napi_env env,napi_callback_info info)1202 static napi_value SetMicrophoneEnabled_02(napi_env env, napi_callback_info info)
1203 {
1204     g_aFlag = 1;
1205     g_vFlag = 1;
1206     int32_t time = 5;
1207     FILE *audioFile = nullptr;
1208     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
1209     OH_AVScreenCaptureConfig config_;
1210     SetConfig(config_);
1211     config_.videoInfo.videoCapInfo.videoSource = OH_VIDEO_SOURCE_SURFACE_RGBA;
1212     audioFile = OpenAFile(audioFile, "SUB_MULTIMEDIA_SCREEN_CAPTURE_MICTEST_0002");
1213     screenCaptureCb = std::make_shared<ScreenCaptureNdkTestCallback>(screenCapture, audioFile, nullptr, nullptr);
1214 
1215     bool isMicrophone = false;
1216     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
1217     SetScreenCaptureCallback(screenCapture, screenCaptureCb);
1218     OH_AVSCREEN_CAPTURE_ErrCode result1 = OH_AVScreenCapture_Init(screenCapture, config_);
1219     OH_AVSCREEN_CAPTURE_ErrCode result2 = OH_AVScreenCapture_StartScreenCapture(screenCapture);
1220     sleep(time);
1221     isMicrophone = true;
1222     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
1223     sleep(g_recordTime);
1224     isMicrophone = false;
1225     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
1226     sleep(g_recordTime);
1227     OH_AVSCREEN_CAPTURE_ErrCode result3 = OH_AVScreenCapture_StopScreenCapture(screenCapture);
1228     DelCallback(screenCapture);
1229     OH_AVScreenCapture_Release(screenCapture);
1230     CloseFile(audioFile, nullptr);
1231     screenCaptureCb = nullptr;
1232     napi_value res;
1233     OH_AVSCREEN_CAPTURE_ErrCode result;
1234     if (result1 == AV_SCREEN_CAPTURE_ERR_OK && result2 == AV_SCREEN_CAPTURE_ERR_OK
1235         && result3 == AV_SCREEN_CAPTURE_ERR_OK) {
1236         result = AV_SCREEN_CAPTURE_ERR_OK;
1237     } else {
1238         LOG(false, "init/start/stop failed, init: %d, start: %d, stop: %d", result1, result2, result3);
1239         result = AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT;
1240     }
1241     napi_create_int32(env, result, &res);
1242     return res;
1243 }
1244 
1245 // OH_Media_Screen_Capture_Configure_001
ConfigureCombination_01(napi_env env,napi_callback_info info)1246 static napi_value ConfigureCombination_01(napi_env env, napi_callback_info info)
1247 {
1248     g_aFlag = 1;
1249     g_vFlag = 1;
1250     int32_t width = 640;
1251     int32_t height = 480;
1252     FILE *videoFile = nullptr;
1253     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
1254     OH_AVScreenCaptureConfig config_;
1255     SetConfig(config_);
1256     config_.captureMode = OH_CAPTURE_SPECIFIED_SCREEN;
1257     config_.videoInfo.videoCapInfo.videoFrameWidth = width;
1258     config_.videoInfo.videoCapInfo.videoFrameHeight = height;
1259     config_.videoInfo.videoCapInfo.videoSource = OH_VIDEO_SOURCE_SURFACE_RGBA;
1260     videoFile = OpenVFile(videoFile, "SUB_MULTIMEDIA_SCREEN_CAPTURE_CONFIG_COMBINATION_0001");
1261     screenCaptureCb = std::make_shared<ScreenCaptureNdkTestCallback>(screenCapture, nullptr, nullptr, videoFile);
1262 
1263     bool isMicrophone = true;
1264     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
1265     SetScreenCaptureCallback(screenCapture, screenCaptureCb);
1266     OH_AVSCREEN_CAPTURE_ErrCode result1 = OH_AVScreenCapture_Init(screenCapture, config_);
1267     OH_AVSCREEN_CAPTURE_ErrCode result2 = OH_AVScreenCapture_StartScreenCapture(screenCapture);
1268     sleep(g_recordTime);
1269     OH_AVSCREEN_CAPTURE_ErrCode result3 = OH_AVScreenCapture_StopScreenCapture(screenCapture);
1270     DelCallback(screenCapture);
1271     OH_AVScreenCapture_Release(screenCapture);
1272     CloseFile(nullptr, videoFile);
1273     screenCaptureCb = nullptr;
1274     OH_AVSCREEN_CAPTURE_ErrCode result;
1275     if (result1 == AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT && result2 == AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT
1276         && result3 == AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT) {
1277         result = AV_SCREEN_CAPTURE_ERR_OK;
1278     } else {
1279         LOG(false, "init/start/stop failed, init: %d, start: %d, stop: %d", result1, result2, result3);
1280         result = AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT;
1281     }
1282     napi_value res;
1283     napi_create_int32(env, result, &res);
1284     return res;
1285 }
1286 
1287 // OH_Media_Screen_Capture_Configure_002
ConfigureCombination_02(napi_env env,napi_callback_info info)1288 static napi_value ConfigureCombination_02(napi_env env, napi_callback_info info)
1289 {
1290     g_aFlag = 1;
1291     g_vFlag = 1;
1292     int32_t width = 1920;
1293     int32_t height = 1080;
1294     FILE *videoFile = nullptr;
1295     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
1296     OH_AVScreenCaptureConfig config_;
1297     SetConfig(config_);
1298     config_.captureMode = OH_CAPTURE_SPECIFIED_SCREEN;
1299     config_.videoInfo.videoCapInfo.videoFrameWidth = width;
1300     config_.videoInfo.videoCapInfo.videoFrameHeight = height;
1301     config_.videoInfo.videoCapInfo.videoSource = OH_VIDEO_SOURCE_SURFACE_RGBA;
1302     videoFile = OpenVFile(videoFile, "SUB_MULTIMEDIA_SCREEN_CAPTURE_CONFIG_COMBINATION_0002");
1303     screenCaptureCb = std::make_shared<ScreenCaptureNdkTestCallback>(screenCapture, nullptr, nullptr, videoFile);
1304 
1305     bool isMicrophone = true;
1306     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
1307     SetScreenCaptureCallback(screenCapture, screenCaptureCb);
1308     OH_AVSCREEN_CAPTURE_ErrCode result1 = OH_AVScreenCapture_Init(screenCapture, config_);
1309     OH_AVSCREEN_CAPTURE_ErrCode result2 = OH_AVScreenCapture_StartScreenCapture(screenCapture);
1310     sleep(g_recordTime);
1311     OH_AVSCREEN_CAPTURE_ErrCode result3 = OH_AVScreenCapture_StopScreenCapture(screenCapture);
1312     DelCallback(screenCapture);
1313     OH_AVScreenCapture_Release(screenCapture);
1314     CloseFile(nullptr, videoFile);
1315     screenCaptureCb = nullptr;
1316     OH_AVSCREEN_CAPTURE_ErrCode result;
1317     if (result1 == AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT && result2 == AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT
1318         && result3 == AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT) {
1319         result = AV_SCREEN_CAPTURE_ERR_OK;
1320     } else {
1321         LOG(false, "init/start/stop failed, init: %d, start: %d, stop: %d", result1, result2, result3);
1322         result = AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT;
1323     }
1324     napi_value res;
1325     napi_create_int32(env, result, &res);
1326     return res;
1327 }
1328 
1329 // OH_Media_Screen_Capture_Configure_003
ConfigureCombination_03(napi_env env,napi_callback_info info)1330 static napi_value ConfigureCombination_03(napi_env env, napi_callback_info info)
1331 {
1332     g_aFlag = 1;
1333     g_vFlag = 1;
1334     int32_t width = 640;
1335     int32_t height = 480;
1336     FILE *videoFile = nullptr;
1337     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
1338     OH_AVScreenCaptureConfig config_;
1339     SetConfig(config_);
1340     config_.captureMode = OH_CAPTURE_SPECIFIED_WINDOW;
1341     config_.videoInfo.videoCapInfo.videoFrameWidth = width;
1342     config_.videoInfo.videoCapInfo.videoFrameHeight = height;
1343     config_.videoInfo.videoCapInfo.videoSource = OH_VIDEO_SOURCE_SURFACE_RGBA;
1344     videoFile = OpenVFile(videoFile, "SUB_MULTIMEDIA_SCREEN_CAPTURE_CONFIG_COMBINATION_0003");
1345     screenCaptureCb = std::make_shared<ScreenCaptureNdkTestCallback>(screenCapture, nullptr, nullptr, videoFile);
1346 
1347     bool isMicrophone = true;
1348     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
1349     SetScreenCaptureCallback(screenCapture, screenCaptureCb);
1350     OH_AVSCREEN_CAPTURE_ErrCode result1 = OH_AVScreenCapture_Init(screenCapture, config_);
1351     OH_AVSCREEN_CAPTURE_ErrCode result2 = OH_AVScreenCapture_StartScreenCapture(screenCapture);
1352     sleep(g_recordTime);
1353     OH_AVSCREEN_CAPTURE_ErrCode result3 = OH_AVScreenCapture_StopScreenCapture(screenCapture);
1354     DelCallback(screenCapture);
1355     OH_AVScreenCapture_Release(screenCapture);
1356     CloseFile(nullptr, videoFile);
1357     screenCaptureCb = nullptr;
1358     OH_AVSCREEN_CAPTURE_ErrCode result;
1359     if (result1 == AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT && result2 == AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT
1360         && result3 == AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT) {
1361         result = AV_SCREEN_CAPTURE_ERR_OK;
1362     } else {
1363         LOG(false, "init/start/stop failed, init: %d, start: %d, stop: %d", result1, result2, result3);
1364         result = AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT;
1365     }
1366     napi_value res;
1367     napi_create_int32(env, result, &res);
1368     return res;
1369 }
1370 
1371 // OH_Media_Screen_Capture_Configure_004
ConfigureCombination_04(napi_env env,napi_callback_info info)1372 static napi_value ConfigureCombination_04(napi_env env, napi_callback_info info)
1373 {
1374     g_aFlag = 1;
1375     g_vFlag = 1;
1376     int32_t width = 1920;
1377     int32_t height = 1080;
1378     FILE *videoFile = nullptr;
1379     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
1380     OH_AVScreenCaptureConfig config_;
1381     SetConfig(config_);
1382     config_.captureMode = OH_CAPTURE_SPECIFIED_WINDOW;
1383     config_.videoInfo.videoCapInfo.videoFrameWidth = width;
1384     config_.videoInfo.videoCapInfo.videoFrameHeight = height;
1385     config_.videoInfo.videoCapInfo.videoSource = OH_VIDEO_SOURCE_SURFACE_RGBA;
1386     videoFile = OpenVFile(videoFile, "SUB_MULTIMEDIA_SCREEN_CAPTURE_CONFIG_COMBINATION_0004");
1387     screenCaptureCb = std::make_shared<ScreenCaptureNdkTestCallback>(screenCapture, nullptr, nullptr, videoFile);
1388 
1389     bool isMicrophone = true;
1390     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
1391     SetScreenCaptureCallback(screenCapture, screenCaptureCb);
1392     OH_AVSCREEN_CAPTURE_ErrCode result1 = OH_AVScreenCapture_Init(screenCapture, config_);
1393     OH_AVSCREEN_CAPTURE_ErrCode result2 = OH_AVScreenCapture_StartScreenCapture(screenCapture);
1394     sleep(g_recordTime);
1395     OH_AVSCREEN_CAPTURE_ErrCode result3 = OH_AVScreenCapture_StopScreenCapture(screenCapture);
1396     DelCallback(screenCapture);
1397     OH_AVScreenCapture_Release(screenCapture);
1398     CloseFile(nullptr, videoFile);
1399     screenCaptureCb = nullptr;
1400     OH_AVSCREEN_CAPTURE_ErrCode result;
1401     if (result1 == AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT && result2 == AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT
1402         && result3 == AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT) {
1403         result = AV_SCREEN_CAPTURE_ERR_OK;
1404     } else {
1405         LOG(false, "init/start/stop failed, init: %d, start: %d, stop: %d", result1, result2, result3);
1406         result = AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT;
1407     }
1408     napi_value res;
1409     napi_create_int32(env, result, &res);
1410     return res;
1411 }
1412 
1413 // OH_Media_Screen_Capture_MultiInstance
MultiInstance(void * arg)1414 static void *MultiInstance(void *arg)
1415 {
1416     g_aFlag = 1;
1417     g_vFlag = 1;
1418     int32_t width = 1920;
1419     int32_t height = 1080;
1420     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
1421     OH_AVScreenCaptureConfig config_;
1422     SetConfig(config_);
1423     config_.audioInfo.micCapInfo.audioSampleRate = SAMPLE_RATE_48000;
1424     config_.videoInfo.videoCapInfo.videoFrameWidth = width;
1425     config_.videoInfo.videoCapInfo.videoFrameHeight = height;
1426     config_.videoInfo.videoCapInfo.videoSource = OH_VIDEO_SOURCE_SURFACE_RGBA;
1427     FILE *audioFileMulti = nullptr;
1428     FILE *videoFileMulti = nullptr;
1429     audioFileMulti = OpenAFile(audioFileMulti, "SUB_MULTIMEDIA_SCREEN_CAPTURE_MULTI_INSTANCE_2");
1430     videoFileMulti = OpenVFile(videoFileMulti, "SUB_MULTIMEDIA_SCREEN_CAPTURE_MULTI_INSTANCE_2");
1431     screenCaptureCb = std::make_shared<ScreenCaptureNdkTestCallback>(screenCapture, audioFileMulti, nullptr,
1432         videoFileMulti);
1433 
1434     bool isMicrophone = true;
1435     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
1436     SetScreenCaptureCallback(screenCapture, screenCaptureCb);
1437     OH_AVSCREEN_CAPTURE_ErrCode result1 = OH_AVScreenCapture_Init(screenCapture, config_);
1438     OH_AVSCREEN_CAPTURE_ErrCode result2 = OH_AVScreenCapture_StartScreenCapture(screenCapture);
1439     sleep(g_recordTime);
1440     OH_AVSCREEN_CAPTURE_ErrCode result3 = OH_AVScreenCapture_StopScreenCapture(screenCapture);
1441     DelCallback(screenCapture);
1442     OH_AVScreenCapture_Release(screenCapture);
1443     CloseFile(audioFileMulti, videoFileMulti);
1444     screenCaptureCb = nullptr;
1445     if (result1 == AV_SCREEN_CAPTURE_ERR_OK && result2 == AV_SCREEN_CAPTURE_ERR_OK
1446         && result3 == AV_SCREEN_CAPTURE_ERR_OK) {
1447         LOG(false, "MultiInstance init/start/stop success");
1448     } else {
1449         LOG(false, "MultiInstance init/start/stop failed, init: %d, start: %d, stop: %d", result1, result2, result3);
1450     }
1451     return arg;
1452 }
1453 
ScreenCaptureMultiInstance(napi_env env,napi_callback_info info)1454 static napi_value ScreenCaptureMultiInstance(napi_env env, napi_callback_info info)
1455 {
1456     g_aFlag = 1;
1457     g_vFlag = 1;
1458     FILE *audioFile = nullptr;
1459     FILE *videoFile = nullptr;
1460     pthread_t tid;
1461     pthread_create(&tid, nullptr, MultiInstance, nullptr);
1462     struct OH_AVScreenCapture* screenCaptureMulti = CreateScreenCapture();
1463     OH_AVScreenCaptureConfig config_;
1464     SetConfig(config_);
1465     config_.videoInfo.videoCapInfo.videoSource = OH_VIDEO_SOURCE_SURFACE_RGBA;
1466     audioFile = OpenAFile(audioFile, "SUB_MULTIMEDIA_SCREEN_CAPTURE_MULTI_INSTANCE");
1467     videoFile = OpenVFile(videoFile, "SUB_MULTIMEDIA_SCREEN_CAPTURE_MULTI_INSTANCE");
1468     std::shared_ptr<ScreenCaptureNdkTestCallback> screenCaptureCb_ =
1469         std::make_shared<ScreenCaptureNdkTestCallback>(screenCaptureMulti, audioFile, nullptr, videoFile);
1470 
1471     bool isMicrophone = true;
1472     OH_AVScreenCapture_SetMicrophoneEnabled(screenCaptureMulti, isMicrophone);
1473     SetScreenCaptureCallback(screenCaptureMulti, screenCaptureCb_);
1474     OH_AVSCREEN_CAPTURE_ErrCode result1 = OH_AVScreenCapture_Init(screenCaptureMulti, config_);
1475     OH_AVSCREEN_CAPTURE_ErrCode result2 = OH_AVScreenCapture_StartScreenCapture(screenCaptureMulti);
1476     sleep(g_recordTime);
1477     OH_AVSCREEN_CAPTURE_ErrCode result3 = OH_AVScreenCapture_StopScreenCapture(screenCaptureMulti);
1478     DelCallback(screenCaptureMulti);
1479     OH_AVScreenCapture_Release(screenCaptureMulti);
1480     CloseFile(audioFile, videoFile);
1481     screenCaptureCb = nullptr;
1482     napi_value res;
1483     OH_AVSCREEN_CAPTURE_ErrCode result;
1484     if (result1 == AV_SCREEN_CAPTURE_ERR_OK && result2 == AV_SCREEN_CAPTURE_ERR_OK
1485         && result3 == AV_SCREEN_CAPTURE_ERR_OK) {
1486         result = AV_SCREEN_CAPTURE_ERR_OK;
1487     } else {
1488         LOG(false, "init/start/stop failed, init: %d, start: %d, stop: %d", result1, result2, result3);
1489         result = AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT;
1490     }
1491     pthread_join(tid, nullptr);
1492     napi_create_int32(env, result, &res);
1493     return res;
1494 }
1495 
1496 // OH_Media_Screen_Capture_InnerAudio
ScreenCaptureInnerAudio(napi_env env,napi_callback_info info)1497 static napi_value ScreenCaptureInnerAudio(napi_env env, napi_callback_info info)
1498 {
1499     g_aFlag = 1;
1500     g_vFlag = 1;
1501     int32_t time = 5;
1502     FILE *audioFile = nullptr;
1503     FILE *videoFile = nullptr;
1504     OH_AVScreenCapture* screenCapture = CreateScreenCapture();
1505     OH_AVScreenCaptureConfig config_;
1506     SetConfig(config_);
1507     config_.audioInfo.innerCapInfo.audioSampleRate = SAMPLE_RATE_48000;
1508     config_.audioInfo.innerCapInfo.audioChannels = STEREO;
1509     config_.audioInfo.innerCapInfo.audioSource = OH_ALL_PLAYBACK;
1510     config_.videoInfo.videoCapInfo.videoSource = OH_VIDEO_SOURCE_SURFACE_RGBA;
1511     audioFile = OpenAFile(audioFile, "SUB_MULTIMEDIA_SCREEN_CAPTURE_INNER_AUDIO(mic)");
1512     videoFile = OpenVFile(videoFile, "SUB_MULTIMEDIA_SCREEN_CAPTURE_INNER_AUDIO(mic)");
1513     FILE *iFile = fopen("data/storage/el2/base/files/SUB_MULTIMEDIA_SCREEN_CAPTURE_INNER_AUDIO(inner).pcm", "w+");
1514     LOG(iFile != nullptr, "inner file audio inner open failed, %s", strerror(errno));
1515     screenCaptureCb = std::make_shared<ScreenCaptureNdkTestCallback>(screenCapture, audioFile, iFile, videoFile);
1516 
1517     bool isMicrophone = true;
1518     OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture, isMicrophone);
1519     SetScreenCaptureCallback(screenCapture, screenCaptureCb);
1520     OH_AVSCREEN_CAPTURE_ErrCode result1 = OH_AVScreenCapture_Init(screenCapture, config_);
1521     OH_AVSCREEN_CAPTURE_ErrCode result2 = OH_AVScreenCapture_StartScreenCapture(screenCapture);
1522     sleep(time);
1523     OH_AVSCREEN_CAPTURE_ErrCode result3 = OH_AVScreenCapture_StopScreenCapture(screenCapture);
1524     DelCallback(screenCapture);
1525     OH_AVScreenCapture_Release(screenCapture);
1526     CloseFile(audioFile, videoFile);
1527     if (iFile != nullptr) {
1528         fclose(iFile);
1529         iFile = nullptr;
1530     }
1531     screenCaptureCb = nullptr;
1532     napi_value res;
1533     OH_AVSCREEN_CAPTURE_ErrCode result;
1534     if (result1 == AV_SCREEN_CAPTURE_ERR_OK && result2 == AV_SCREEN_CAPTURE_ERR_OK
1535         && result3 == AV_SCREEN_CAPTURE_ERR_OK) {
1536         result = AV_SCREEN_CAPTURE_ERR_OK;
1537     } else {
1538         LOG(false, "init/start/stop failed, init: %d, start: %d, stop: %d", result1, result2, result3);
1539         result = AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT;
1540     }
1541     napi_create_int32(env, result, &res);
1542     return res;
1543 }
1544 
1545 EXTERN_C_START
Init(napi_env env,napi_value exports)1546 static napi_value Init(napi_env env, napi_value exports)
1547 {
1548     napi_property_descriptor desc[] = {
1549         {"initVideoWidthErr", nullptr, InitWidthErr, nullptr, nullptr, nullptr, napi_default, nullptr},
1550         {"initVideoHeightErr", nullptr, InitHeightErr, nullptr, nullptr, nullptr, napi_default, nullptr},
1551         {"initVideoSourceErr_YUV", nullptr, InitVideoSourceYUV, nullptr, nullptr, nullptr, napi_default, nullptr},
1552         {"initVideoSourceErr_ES", nullptr, InitVideoSourceES, nullptr, nullptr, nullptr, napi_default, nullptr},
1553         {"screenCaptureWithoutAudioData", nullptr, WithoutAudioData, nullptr, nullptr, nullptr, napi_default, nullptr},
1554         {"initAudioSampleErr_01", nullptr, InitAudioSampleRate_01, nullptr, nullptr, nullptr, napi_default, nullptr},
1555         {"initAudioSampleErr_02", nullptr, InitAudioSampleRate_02, nullptr, nullptr, nullptr, napi_default, nullptr},
1556         {"initAudioSampleErr_03", nullptr, InitAudioSampleRate_03, nullptr, nullptr, nullptr, napi_default, nullptr},
1557         {"initAudioChannelsErr_01", nullptr, InitAudioChannels_01, nullptr, nullptr, nullptr, napi_default, nullptr},
1558         {"initAudioChannelsErr_02", nullptr, InitAudioChannels_02, nullptr, nullptr, nullptr, napi_default, nullptr},
1559         {"initAudioSourceErr", nullptr, InitAudioSourceErr, nullptr, nullptr, nullptr, napi_default, nullptr},
1560         {"initAVErr", nullptr, ScreenCaptureInitErr, nullptr, nullptr, nullptr, napi_default, nullptr},
1561         {"screenCaptureWithAudioData", nullptr, WithAudioData, nullptr, nullptr, nullptr, napi_default, nullptr},
1562         {"initCaptureMode_01", nullptr, InitCaptureMode_01, nullptr, nullptr, nullptr, napi_default, nullptr},
1563         {"initCaptureMode_02", nullptr, InitCaptureMode_02, nullptr, nullptr, nullptr, napi_default, nullptr},
1564         {"initDataTypeErr_01", nullptr, InitDataType_01, nullptr, nullptr, nullptr, napi_default, nullptr},
1565         {"initDataTypeErr_02", nullptr, InitDataType_02, nullptr, nullptr, nullptr, napi_default, nullptr},
1566         {"initDataTypeErr_03", nullptr, InitDataType_03, nullptr, nullptr, nullptr, napi_default, nullptr},
1567         {"changeAudioSample_01", nullptr, ChangeAudioSampleRate_01, nullptr, nullptr, nullptr, napi_default, nullptr},
1568         {"changeAudioSample_02", nullptr, ChangeAudioSampleRate_02, nullptr, nullptr, nullptr, napi_default, nullptr},
1569         {"changeAudioChannels_01", nullptr, ChangeAudioChannels_01, nullptr, nullptr, nullptr, napi_default, nullptr},
1570         {"changeAudioChannels_02", nullptr, ChangeAudioChannels_02, nullptr, nullptr, nullptr, napi_default, nullptr},
1571         {"changeAudioChannels_03", nullptr, ChangeAudioChannels_03, nullptr, nullptr, nullptr, napi_default, nullptr},
1572         {"changeAudioChannels_04", nullptr, ChangeAudioChannels_04, nullptr, nullptr, nullptr, napi_default, nullptr},
1573         {"captureChangeVideoSize_01", nullptr, ChangeVideoSize_01, nullptr, nullptr, nullptr, napi_default, nullptr},
1574         {"captureChangeVideoSize_02", nullptr, ChangeVideoSize_02, nullptr, nullptr, nullptr, napi_default, nullptr},
1575         {"captureChangeVideoSize_03", nullptr, ChangeVideoSize_03, nullptr, nullptr, nullptr, napi_default, nullptr},
1576         {"screenCaptureFromDisplay", nullptr, ScreenCaptureFromDisplay, nullptr, nullptr, nullptr, napi_default,
1577             nullptr},
1578         {"setCallbackAcquireBuffer_01", nullptr, BufferAndRelease_01, nullptr, nullptr, nullptr, napi_default, nullptr},
1579         {"setCallbackAcquireBuffer_02", nullptr, BufferAndRelease_02, nullptr, nullptr, nullptr, napi_default, nullptr},
1580         {"setCallbackAcquireBuffer_03", nullptr, BufferAndRelease_03, nullptr, nullptr, nullptr, napi_default, nullptr},
1581         {"setMicrophoneOpenCloseOpen", nullptr, SetMicrophoneEnabled_01, nullptr, nullptr, nullptr, napi_default,
1582             nullptr},
1583         {"setMicrophoneCloseOpenClose", nullptr, SetMicrophoneEnabled_02, nullptr, nullptr, nullptr, napi_default,
1584             nullptr},
1585         {"configCombination_01", nullptr, ConfigureCombination_01, nullptr, nullptr, nullptr, napi_default, nullptr},
1586         {"configCombination_02", nullptr, ConfigureCombination_02, nullptr, nullptr, nullptr, napi_default, nullptr},
1587         {"configCombination_03", nullptr, ConfigureCombination_03, nullptr, nullptr, nullptr, napi_default, nullptr},
1588         {"configCombination_04", nullptr, ConfigureCombination_04, nullptr, nullptr, nullptr, napi_default, nullptr},
1589         {"multiInstance", nullptr, ScreenCaptureMultiInstance, nullptr, nullptr, nullptr, napi_default, nullptr},
1590         {"innerAudioAndMicAudio", nullptr, ScreenCaptureInnerAudio, nullptr, nullptr, nullptr, napi_default, nullptr},
1591     };
1592     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
1593     return exports;
1594 }
1595 EXTERN_C_END
1596 
1597 static napi_module demoModule = {
1598     .nm_version = 1,
1599     .nm_flags = 0,
1600     .nm_filename = nullptr,
1601     .nm_register_func = Init,
1602     .nm_modname = "entry",
1603     .nm_priv = ((void*)0),
1604     .reserved = { 0 },
1605 };
1606 
RegisterEntryModule(void)1607 extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
1608 {
1609     napi_module_register(&demoModule);
1610 }
1611