• 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_avscreen_capture.h"
17 
18 #include <mutex>
19 #include <queue>
20 #include <shared_mutex>
21 #include <set>
22 
23 #include "buffer/avbuffer.h"
24 #include "common/native_mfmagic.h"
25 #include "media_log.h"
26 #include "media_errors.h"
27 #include "native_avbuffer.h"
28 #include "native_player_magic.h"
29 #include "surface_buffer_impl.h"
30 #include "native_window.h"
31 
32 namespace {
33 constexpr int MAX_WINDOWS_LEN = 1000;
34 constexpr int VIRTUAL_DISPLAY_ID_START = 1000;
35 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_SCREENCAPTURE, "NativeScreenCapture"};
36 }
37 
38 typedef struct NativeWindow OHNativeWindow;
39 
40 using namespace OHOS::Media;
41 static std::queue<OH_NativeBuffer*> referencedBuffer_;
42 class NativeScreenCaptureCallback;
43 struct ScreenCaptureUserSelectionObject;
44 
45 struct ScreenCaptureObject : public OH_AVScreenCapture {
ScreenCaptureObjectScreenCaptureObject46     explicit ScreenCaptureObject(const std::shared_ptr<ScreenCapture> &capture)
47         : screenCapture_(capture) {}
48     ~ScreenCaptureObject() = default;
49 
50     const std::shared_ptr<ScreenCapture> screenCapture_ = nullptr;
51     std::shared_ptr<NativeScreenCaptureCallback> callback_ = nullptr;
52     bool isStart = false;
53 };
54 
55 struct ScreenCaptureUserSelectionObject : public OH_AVScreenCapture_UserSelectionInfo {
ScreenCaptureUserSelectionObjectScreenCaptureUserSelectionObject56     explicit ScreenCaptureUserSelectionObject(ScreenCaptureUserSelectionInfo selectionInfo)
57         : userSelectionInfo_(selectionInfo) {}
58     ~ScreenCaptureUserSelectionObject() = default;
59     ScreenCaptureUserSelectionInfo userSelectionInfo_;
60 };
61 
62 class NativeScreenCaptureStateChangeCallback {
63 public:
NativeScreenCaptureStateChangeCallback(OH_AVScreenCapture_OnStateChange callback,void * userData)64     NativeScreenCaptureStateChangeCallback(OH_AVScreenCapture_OnStateChange callback, void *userData)
65         : callback_(callback), userData_(userData) {}
66     virtual ~NativeScreenCaptureStateChangeCallback() = default;
67 
OnStateChange(struct OH_AVScreenCapture * capture,AVScreenCaptureStateCode infoType)68     void OnStateChange(struct OH_AVScreenCapture *capture, AVScreenCaptureStateCode infoType)
69     {
70         CHECK_AND_RETURN(capture != nullptr && callback_ != nullptr);
71         callback_(capture, static_cast<OH_AVScreenCaptureStateCode>(infoType), userData_);
72     }
73 
74 private:
75     OH_AVScreenCapture_OnStateChange callback_;
76     void *userData_;
77 };
78 
79 class NativeScreenCaptureContentChangedCallback {
80 public:
NativeScreenCaptureContentChangedCallback(OH_AVScreenCapture_OnCaptureContentChanged callback,void * userData)81     NativeScreenCaptureContentChangedCallback(OH_AVScreenCapture_OnCaptureContentChanged callback, void *userData)
82         : callback_(callback), userData_(userData) {}
83     virtual ~NativeScreenCaptureContentChangedCallback() = default;
84 
OnCaptureContentChanged(struct OH_AVScreenCapture * capture,AVScreenCaptureContentChangedEvent event,ScreenCaptureRect * area)85     void OnCaptureContentChanged(struct OH_AVScreenCapture *capture, AVScreenCaptureContentChangedEvent event,
86         ScreenCaptureRect* area)
87     {
88         MEDIA_LOGD("NativeScreenCaptureContentChangedCallback OnCaptureContentChanged");
89         CHECK_AND_RETURN(capture != nullptr && callback_ != nullptr);
90         callback_(capture, static_cast<OH_AVScreenCaptureContentChangedEvent>(event),
91             area == nullptr ? nullptr : reinterpret_cast<OH_Rect*>(area), userData_);
92     }
93 
94 private:
95     OH_AVScreenCapture_OnCaptureContentChanged callback_;
96     void *userData_;
97 };
98 
99 class NativeScreenCaptureDisplaySelectedCallback {
100 public:
NativeScreenCaptureDisplaySelectedCallback(OH_AVScreenCapture_OnDisplaySelected callback,void * userData)101     NativeScreenCaptureDisplaySelectedCallback(OH_AVScreenCapture_OnDisplaySelected callback, void *userData)
102         : callback_(callback), userData_(userData) {}
103     virtual ~NativeScreenCaptureDisplaySelectedCallback() = default;
104 
OnDisplaySelected(struct OH_AVScreenCapture * capture,uint64_t displayId)105     void OnDisplaySelected(struct OH_AVScreenCapture *capture, uint64_t displayId)
106     {
107         CHECK_AND_RETURN(capture != nullptr && callback_ != nullptr);
108         callback_(capture, displayId, userData_);
109     }
110 
111 private:
112     OH_AVScreenCapture_OnDisplaySelected callback_;
113     void *userData_;
114 };
115 
116 class NativeScreenCaptureUserSelectedCallback {
117 public:
NativeScreenCaptureUserSelectedCallback(OH_AVScreenCapture_OnUserSelected callback,void * userData)118     NativeScreenCaptureUserSelectedCallback(OH_AVScreenCapture_OnUserSelected callback, void *userData)
119         : callback_(callback), userData_(userData) {}
120     virtual ~NativeScreenCaptureUserSelectedCallback() = default;
121 
OnUserSelected(struct OH_AVScreenCapture * capture,ScreenCaptureUserSelectionInfo selectionInfo)122     void OnUserSelected(struct OH_AVScreenCapture *capture, ScreenCaptureUserSelectionInfo selectionInfo)
123     {
124         CHECK_AND_RETURN(capture != nullptr && callback_ != nullptr);
125         struct ScreenCaptureUserSelectionObject *object =
126             new(std::nothrow) ScreenCaptureUserSelectionObject(selectionInfo);
127         CHECK_AND_RETURN_LOG(object != nullptr, "failed to new ScreenCaptureUserSelectionObject");
128         callback_(capture, reinterpret_cast<OH_AVScreenCapture_UserSelectionInfo*>(object), userData_);
129         delete object;
130         object = nullptr;
131     }
132 
133 private:
134     OH_AVScreenCapture_OnUserSelected callback_;
135     void *userData_;
136 };
137 
138 class NativeScreenCaptureErrorCallback {
139 public:
NativeScreenCaptureErrorCallback(OH_AVScreenCapture_OnError callback,void * userData)140     NativeScreenCaptureErrorCallback(OH_AVScreenCapture_OnError callback, void *userData)
141         : callback_(callback), userData_(userData) {}
142     virtual ~NativeScreenCaptureErrorCallback() = default;
143 
OnError(struct OH_AVScreenCapture * capture,int32_t errorCode)144     void OnError(struct OH_AVScreenCapture *capture, int32_t errorCode)
145     {
146         CHECK_AND_RETURN(capture != nullptr && callback_ != nullptr);
147         callback_(capture, errorCode, userData_);
148     }
149 
150 private:
151     OH_AVScreenCapture_OnError callback_;
152     void *userData_;
153 };
154 
155 class NativeScreenCaptureDataCallback {
156 public:
NativeScreenCaptureDataCallback(OH_AVScreenCapture_OnBufferAvailable callback,void * userData)157     NativeScreenCaptureDataCallback(OH_AVScreenCapture_OnBufferAvailable callback, void *userData)
158         : callback_(callback), userData_(userData) {}
159     virtual ~NativeScreenCaptureDataCallback() = default;
160 
OnBufferAvailable(struct OH_AVScreenCapture * capture,OH_AVScreenCaptureBufferType bufferType)161     void OnBufferAvailable(struct OH_AVScreenCapture *capture, OH_AVScreenCaptureBufferType bufferType)
162     {
163         CHECK_AND_RETURN(capture != nullptr && callback_ != nullptr);
164         switch (bufferType) {
165             case OH_AVScreenCaptureBufferType::OH_SCREEN_CAPTURE_BUFFERTYPE_VIDEO:
166                 OnProcessVideoBuffer(capture);
167                 return;
168             case OH_AVScreenCaptureBufferType::OH_SCREEN_CAPTURE_BUFFERTYPE_AUDIO_INNER: // fall-through
169             case OH_AVScreenCaptureBufferType::OH_SCREEN_CAPTURE_BUFFERTYPE_AUDIO_MIC:
170                 OnProcessAudioBuffer(capture, bufferType);
171                 return;
172             default:
173                 MEDIA_LOGD("OnBufferAvailable() is called, invalid bufferType:%{public}d", bufferType);
174                 return;
175         }
176     }
177 
178 private:
AcquireAudioBuffer(const std::shared_ptr<ScreenCapture> & screenCapture,OHOS::sptr<OH_AVBuffer> & ohAvBuffer,int64_t & timestamp,AudioCaptureSourceType type)179     static OH_AVSCREEN_CAPTURE_ErrCode AcquireAudioBuffer(const std::shared_ptr<ScreenCapture> &screenCapture,
180         OHOS::sptr<OH_AVBuffer> &ohAvBuffer, int64_t &timestamp, AudioCaptureSourceType type)
181     {
182         std::shared_ptr<AudioBuffer> aBuffer;
183         int32_t ret = screenCapture->AcquireAudioBuffer(aBuffer, type);
184         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT,
185             "AcquireAudioBuffer failed not permit! ret:%{public}d", ret);
186         CHECK_AND_RETURN_RET_LOG(aBuffer != nullptr && aBuffer->buffer != nullptr, AV_SCREEN_CAPTURE_ERR_NO_MEMORY,
187             "AcquireAudioBuffer failed aBuffer no memory!");
188         std::shared_ptr<AVBuffer> avBuffer = AVBuffer::CreateAVBuffer(aBuffer->buffer, aBuffer->length,
189             aBuffer->length);
190         CHECK_AND_RETURN_RET_LOG(avBuffer != nullptr, AV_SCREEN_CAPTURE_ERR_NO_MEMORY,
191             "AcquireAudioBuffer failed avBuffer no memory!");
192         aBuffer->buffer = nullptr; // memory control has transfered to AVBuffer
193         timestamp = aBuffer->timestamp;
194         ohAvBuffer = new(std::nothrow) OH_AVBuffer(avBuffer);
195 
196         CHECK_AND_RETURN_RET_LOG(ohAvBuffer != nullptr, AV_SCREEN_CAPTURE_ERR_NO_MEMORY,
197             "AcquireAudioBuffer failed ohAvBuffer no memory!");
198         return AV_SCREEN_CAPTURE_ERR_OK;
199     }
200 
ReleaseAudioBuffer(const std::shared_ptr<ScreenCapture> & screenCapture,AudioCaptureSourceType type)201     static OH_AVSCREEN_CAPTURE_ErrCode ReleaseAudioBuffer(const std::shared_ptr<ScreenCapture> &screenCapture,
202         AudioCaptureSourceType type)
203     {
204         int32_t ret = screenCapture->ReleaseAudioBuffer(type);
205         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT,
206             "ReleaseAudioBuffer failed! ret:%{public}d", ret);
207         return AV_SCREEN_CAPTURE_ERR_OK;
208     }
209 
OnProcessAudioBuffer(struct OH_AVScreenCapture * capture,OH_AVScreenCaptureBufferType bufferType)210     OH_AVSCREEN_CAPTURE_ErrCode OnProcessAudioBuffer(struct OH_AVScreenCapture *capture,
211         OH_AVScreenCaptureBufferType bufferType)
212     {
213         CHECK_AND_RETURN_RET_LOG(capture != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input capture is nullptr!");
214         struct ScreenCaptureObject *screenCaptureObj = reinterpret_cast<ScreenCaptureObject *>(capture);
215         CHECK_AND_RETURN_RET_LOG(screenCaptureObj->screenCapture_ != nullptr,
216             AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "screenCapture is null");
217 
218         MEDIA_LOGD("OnProcessAudioBuffer() is called, bufferType %{public}d", bufferType);
219         AudioCaptureSourceType audioSourceType;
220         if (bufferType == OH_AVScreenCaptureBufferType::OH_SCREEN_CAPTURE_BUFFERTYPE_AUDIO_INNER) {
221             audioSourceType = AudioCaptureSourceType::ALL_PLAYBACK;
222         } else if (bufferType == OH_AVScreenCaptureBufferType::OH_SCREEN_CAPTURE_BUFFERTYPE_AUDIO_MIC) {
223             audioSourceType = AudioCaptureSourceType::MIC;
224         } else {
225             return AV_SCREEN_CAPTURE_ERR_INVALID_VAL;
226         }
227         OHOS::sptr<OH_AVBuffer> ohAvBuffer;
228         int64_t timestamp = 0;
229         OH_AVSCREEN_CAPTURE_ErrCode errCode =
230             AcquireAudioBuffer(screenCaptureObj->screenCapture_, ohAvBuffer, timestamp, audioSourceType);
231         if (errCode == AV_SCREEN_CAPTURE_ERR_OK) {
232             MEDIA_LOGD("OnProcessAudioBuffer: 0x%{public}06" PRIXPTR " In", FAKE_POINTER(capture));
233             callback_(capture, reinterpret_cast<OH_AVBuffer *>(ohAvBuffer.GetRefPtr()), bufferType, timestamp,
234                 userData_);
235             MEDIA_LOGD("OnProcessAudioBuffer: 0x%{public}06" PRIXPTR " Out", FAKE_POINTER(capture));
236             if (ohAvBuffer->buffer_ == nullptr || ohAvBuffer->buffer_->memory_ == nullptr) {
237                 return AV_SCREEN_CAPTURE_ERR_INVALID_VAL;
238             }
239             free(ohAvBuffer->buffer_->memory_->GetAddr());
240         }
241         errCode = ReleaseAudioBuffer(screenCaptureObj->screenCapture_, audioSourceType);
242         return errCode;
243     }
AcquireVideoBuffer(const std::shared_ptr<ScreenCapture> & screenCapture,OHOS::sptr<OH_AVBuffer> & ohAvBuffer,int64_t & timestamp)244     static OH_AVSCREEN_CAPTURE_ErrCode AcquireVideoBuffer(const std::shared_ptr<ScreenCapture> &screenCapture,
245         OHOS::sptr<OH_AVBuffer> &ohAvBuffer, int64_t &timestamp)
246     {
247         int32_t fence = -1;
248         OHOS::Rect damage;
249         OHOS::sptr<OHOS::SurfaceBuffer> surfaceBuffer =
250             screenCapture->AcquireVideoBuffer(fence, timestamp, damage);
251         CHECK_AND_RETURN_RET_LOG(surfaceBuffer != nullptr, AV_SCREEN_CAPTURE_ERR_NO_MEMORY,
252             "AcquireVideoBuffer failed surfaceBuffer no memory!");
253         std::shared_ptr<AVBuffer> avBuffer = AVBuffer::CreateAVBuffer(surfaceBuffer);
254         CHECK_AND_RETURN_RET_LOG(avBuffer != nullptr && avBuffer->memory_ != nullptr, AV_SCREEN_CAPTURE_ERR_NO_MEMORY,
255             "AcquireVideoBuffer failed avBuffer no memory!");
256         MEDIA_LOGD("AcquireVideoBuffer Size %{public}d", static_cast<int32_t>(surfaceBuffer->GetSize()));
257         avBuffer->memory_->SetSize(static_cast<int32_t>(surfaceBuffer->GetSize()));
258         ohAvBuffer = new(std::nothrow) OH_AVBuffer(avBuffer);
259         CHECK_AND_RETURN_RET_LOG(ohAvBuffer != nullptr, AV_SCREEN_CAPTURE_ERR_NO_MEMORY,
260             "AcquireVideoBuffer failed ohAvBuffer no memory!");
261         return AV_SCREEN_CAPTURE_ERR_OK;
262     }
263 
ReleaseVideoBuffer(const std::shared_ptr<ScreenCapture> & screenCapture)264     static OH_AVSCREEN_CAPTURE_ErrCode ReleaseVideoBuffer(const std::shared_ptr<ScreenCapture> &screenCapture)
265     {
266         int32_t ret = screenCapture->ReleaseVideoBuffer();
267         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT,
268             "ReleaseVideoBuffer failed! ret:%{public}d", ret);
269         return AV_SCREEN_CAPTURE_ERR_OK;
270     }
271 
OnProcessVideoBuffer(struct OH_AVScreenCapture * capture)272     OH_AVSCREEN_CAPTURE_ErrCode OnProcessVideoBuffer(struct OH_AVScreenCapture *capture)
273     {
274         CHECK_AND_RETURN_RET_LOG(capture != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input capture is nullptr!");
275         struct ScreenCaptureObject *screenCaptureObj = reinterpret_cast<ScreenCaptureObject *>(capture);
276         CHECK_AND_RETURN_RET_LOG(screenCaptureObj->screenCapture_ != nullptr,
277             AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "screenCapture is null");
278 
279         OHOS::sptr<OH_AVBuffer> ohAvBuffer;
280         int64_t timestamp = 0;
281         OH_AVSCREEN_CAPTURE_ErrCode errCode =
282             AcquireVideoBuffer(screenCaptureObj->screenCapture_, ohAvBuffer, timestamp);
283         if (errCode == AV_SCREEN_CAPTURE_ERR_OK) {
284             MEDIA_LOGD("OnProcessVideoBuffer: 0x%{public}06" PRIXPTR " In", FAKE_POINTER(capture));
285             callback_(capture, reinterpret_cast<OH_AVBuffer *>(ohAvBuffer.GetRefPtr()),
286                 OH_AVScreenCaptureBufferType::OH_SCREEN_CAPTURE_BUFFERTYPE_VIDEO, timestamp, userData_);
287             MEDIA_LOGD("OnProcessVideoBuffer: 0x%{public}06" PRIXPTR " Out", FAKE_POINTER(capture));
288         }
289         errCode = ReleaseVideoBuffer(screenCaptureObj->screenCapture_);
290         return errCode;
291     }
292 
293 private:
294     OH_AVScreenCapture_OnBufferAvailable callback_;
295     void *userData_;
296 };
297 
298 class NativeScreenCaptureCallback : public ScreenCaptureCallBack {
299 public:
NativeScreenCaptureCallback(struct OH_AVScreenCapture * capture,struct OH_AVScreenCaptureCallback callback)300     NativeScreenCaptureCallback(struct OH_AVScreenCapture *capture, struct OH_AVScreenCaptureCallback callback)
301         : capture_(capture), callback_(callback) {}
302     virtual ~NativeScreenCaptureCallback() = default;
303 
OnError(ScreenCaptureErrorType errorType,int32_t errorCode)304     void OnError(ScreenCaptureErrorType errorType, int32_t errorCode) override
305     {
306         MEDIA_LOGE("OnError() is called, errorType %{public}d, errorCode %{public}d", errorType, errorCode);
307         std::shared_lock<std::shared_mutex> lock(mutex_);
308         CHECK_AND_RETURN(capture_ != nullptr);
309 
310         if (errorCallback_ != nullptr) {
311             errorCallback_->OnError(capture_, errorCode);
312             return;
313         }
314 
315         if (callback_.onError != nullptr) {
316             callback_.onError(capture_, errorCode);
317             return;
318         }
319     }
320 
OnStateChange(AVScreenCaptureStateCode stateCode)321     void OnStateChange(AVScreenCaptureStateCode stateCode) override
322     {
323         MEDIA_LOGI("OnStateChange() is called, stateCode %{public}d", stateCode);
324         std::shared_lock<std::shared_mutex> lock(mutex_);
325         CHECK_AND_RETURN(capture_ != nullptr);
326 
327         if (stateChangeCallback_ != nullptr) {
328             stateChangeCallback_->OnStateChange(capture_, stateCode);
329             return;
330         }
331     }
332 
OnCaptureContentChanged(AVScreenCaptureContentChangedEvent event,ScreenCaptureRect * area)333     void OnCaptureContentChanged(AVScreenCaptureContentChangedEvent event, ScreenCaptureRect* area) override
334     {
335         MEDIA_LOGD("OnCaptureContentChanged() is called, event: %{public}d", event);
336         std::shared_lock<std::shared_mutex> lock(mutex_);
337         CHECK_AND_RETURN(capture_ != nullptr && contentChangedCallback_ != nullptr);
338         contentChangedCallback_->OnCaptureContentChanged(capture_, event, area);
339     }
340 
OnDisplaySelected(uint64_t displayId)341     void OnDisplaySelected(uint64_t displayId) override
342     {
343         MEDIA_LOGI("OnDisplaySelected() is called, displayId (%{public}" PRIu64 ")", displayId);
344         std::shared_lock<std::shared_mutex> lock(mutex_);
345         CHECK_AND_RETURN(capture_ != nullptr);
346 
347         if (displaySelectedCallback_ != nullptr) {
348             displaySelectedCallback_->OnDisplaySelected(capture_, displayId);
349             return;
350         }
351     }
352 
OnUserSelected(ScreenCaptureUserSelectionInfo selectionInfo)353     void OnUserSelected(ScreenCaptureUserSelectionInfo selectionInfo) override
354     {
355         MEDIA_LOGI("OnUserSelected() is called");
356         std::shared_lock<std::shared_mutex> lock(mutex_);
357         CHECK_AND_RETURN(capture_ != nullptr);
358 
359         if (userSelectedCallback_ != nullptr) {
360             userSelectedCallback_->OnUserSelected(capture_, selectionInfo);
361             return;
362         }
363     }
364 
OnAudioBufferAvailable(bool isReady,AudioCaptureSourceType type)365     void OnAudioBufferAvailable(bool isReady, AudioCaptureSourceType type) override
366     {
367         CHECK_AND_RETURN(!isBufferAvailableCallbackStop_.load());
368         MEDIA_LOGD("OnAudioBufferAvailable() is called, isReady:%{public}d, type:%{public}d", isReady, type);
369         std::shared_lock<std::shared_mutex> lock(mutex_);
370         CHECK_AND_RETURN(capture_ != nullptr);
371 
372         if (dataCallback_ != nullptr && hasDataCallback_.load()) {
373             if (!isReady) {
374                 MEDIA_LOGD("OnAudioBufferAvailable not ready");
375                 return;
376             }
377             if (type == AudioCaptureSourceType::SOURCE_DEFAULT || type == AudioCaptureSourceType::MIC) {
378                 dataCallback_->OnBufferAvailable(capture_,
379                     OH_AVScreenCaptureBufferType::OH_SCREEN_CAPTURE_BUFFERTYPE_AUDIO_MIC);
380             } else if (type == AudioCaptureSourceType::ALL_PLAYBACK || type == AudioCaptureSourceType::APP_PLAYBACK) {
381                 dataCallback_->OnBufferAvailable(capture_,
382                     OH_AVScreenCaptureBufferType::OH_SCREEN_CAPTURE_BUFFERTYPE_AUDIO_INNER);
383             } else {
384                 MEDIA_LOGD("OnAudioBufferAvailable() is called, invalid audio source type:%{public}d", type);
385             }
386             MEDIA_LOGD("OnAudioBufferAvailable finished");
387             return;
388         }
389         if (callback_.onAudioBufferAvailable != nullptr) {
390             callback_.onAudioBufferAvailable(capture_, isReady, static_cast<OH_AudioCaptureSourceType>(type));
391             MEDIA_LOGD("OnAudioBufferAvailable finished");
392             return;
393         }
394         MEDIA_LOGD("OnAudioBufferAvailable finished");
395     }
396 
OnVideoBufferAvailable(bool isReady)397     void OnVideoBufferAvailable(bool isReady) override
398     {
399         CHECK_AND_RETURN(!isBufferAvailableCallbackStop_.load());
400         MEDIA_LOGD("OnVideoBufferAvailable() is called, isReady:%{public}d", isReady);
401         std::shared_lock<std::shared_mutex> lock(mutex_);
402         CHECK_AND_RETURN(capture_ != nullptr);
403 
404         if (dataCallback_ != nullptr && hasDataCallback_.load()) {
405             if (!isReady) {
406                 MEDIA_LOGD("OnVideoBufferAvailable not ready");
407                 return;
408             }
409             dataCallback_->OnBufferAvailable(capture_,
410                 OH_AVScreenCaptureBufferType::OH_SCREEN_CAPTURE_BUFFERTYPE_VIDEO);
411             MEDIA_LOGD("OnVideoBufferAvailable finished");
412             return;
413         }
414         if (capture_ != nullptr && callback_.onVideoBufferAvailable != nullptr) {
415             callback_.onVideoBufferAvailable(capture_, isReady);
416         }
417         MEDIA_LOGD("OnVideoBufferAvailable finished");
418     }
419 
StopCallback()420     void StopCallback()
421     {
422         isBufferAvailableCallbackStop_.store(true);
423         MEDIA_LOGD("StopCallback before lock"); // waiting for OnBufferAvailable
424         std::unique_lock<std::shared_mutex> lock(mutex_);
425         MEDIA_LOGD("StopCallback after lock");
426     }
427 
SetCallback(struct OH_AVScreenCaptureCallback callback)428     void SetCallback(struct OH_AVScreenCaptureCallback callback)
429     {
430         std::unique_lock<std::shared_mutex> lock(mutex_);
431         callback_ = callback;
432     }
433 
IsDataCallbackEnabled()434     bool IsDataCallbackEnabled()
435     {
436         return hasDataCallback_.load();
437     }
438 
IsStateChangeCallbackEnabled()439     bool IsStateChangeCallbackEnabled()
440     {
441         std::shared_lock<std::shared_mutex> lock(mutex_);
442         return stateChangeCallback_ != nullptr;
443     }
444 
SetStateChangeCallback(OH_AVScreenCapture_OnStateChange callback,void * userData)445     bool SetStateChangeCallback(OH_AVScreenCapture_OnStateChange callback, void *userData)
446     {
447         std::unique_lock<std::shared_mutex> lock(mutex_);
448         stateChangeCallback_ = std::make_shared<NativeScreenCaptureStateChangeCallback>(callback, userData);
449         return stateChangeCallback_ != nullptr;
450     }
451 
SetCaptureContentChangedCallback(OH_AVScreenCapture_OnCaptureContentChanged callback,void * userData)452     bool SetCaptureContentChangedCallback(OH_AVScreenCapture_OnCaptureContentChanged callback, void *userData)
453     {
454         std::unique_lock<std::shared_mutex> lock(mutex_);
455         contentChangedCallback_ = std::make_shared<NativeScreenCaptureContentChangedCallback>(callback, userData);
456         return contentChangedCallback_ != nullptr;
457     }
458 
SetUserSelectedCallback(OH_AVScreenCapture_OnUserSelected callback,void * userData)459     bool SetUserSelectedCallback(OH_AVScreenCapture_OnUserSelected callback, void *userData)
460     {
461         std::unique_lock<std::shared_mutex> lock(mutex_);
462         userSelectedCallback_ = std::make_shared<NativeScreenCaptureUserSelectedCallback>(callback, userData);
463         return userSelectedCallback_ != nullptr;
464     }
465 
SetErrorCallback(OH_AVScreenCapture_OnError callback,void * userData)466     bool SetErrorCallback(OH_AVScreenCapture_OnError callback, void *userData)
467     {
468         std::unique_lock<std::shared_mutex> lock(mutex_);
469         errorCallback_ = std::make_shared<NativeScreenCaptureErrorCallback>(callback, userData);
470         return errorCallback_ != nullptr;
471     }
472 
SetDataCallback(OH_AVScreenCapture_OnBufferAvailable callback,void * userData)473     bool SetDataCallback(OH_AVScreenCapture_OnBufferAvailable callback, void *userData)
474     {
475         std::unique_lock<std::shared_mutex> lock(mutex_);
476         dataCallback_ = std::make_shared<NativeScreenCaptureDataCallback>(callback, userData);
477         hasDataCallback_.store(true);
478         return dataCallback_ != nullptr;
479     }
480 
SetDisplayCallback(OH_AVScreenCapture_OnDisplaySelected callback,void * userData)481     bool SetDisplayCallback(OH_AVScreenCapture_OnDisplaySelected callback, void *userData)
482     {
483         std::unique_lock<std::shared_mutex> lock(mutex_);
484         displaySelectedCallback_ = std::make_shared<NativeScreenCaptureDisplaySelectedCallback>(callback, userData);
485         return displaySelectedCallback_ != nullptr;
486     }
487 
488 private:
489     std::shared_mutex mutex_;
490     struct OH_AVScreenCapture *capture_ = nullptr;
491     struct OH_AVScreenCaptureCallback callback_;
492     std::atomic<bool> isBufferAvailableCallbackStop_ = false;
493     std::atomic<bool> hasDataCallback_ = false;
494     std::shared_ptr<NativeScreenCaptureStateChangeCallback> stateChangeCallback_ = nullptr;
495     std::shared_ptr<NativeScreenCaptureErrorCallback> errorCallback_ = nullptr;
496     std::shared_ptr<NativeScreenCaptureDataCallback> dataCallback_ = nullptr;
497     std::shared_ptr<NativeScreenCaptureDisplaySelectedCallback> displaySelectedCallback_ = nullptr;
498     std::shared_ptr<NativeScreenCaptureContentChangedCallback> contentChangedCallback_ = nullptr;
499     std::shared_ptr<NativeScreenCaptureUserSelectedCallback> userSelectedCallback_ = nullptr;
500 };
501 
502 struct ScreenCaptureContentFilterObject : public OH_AVScreenCapture_ContentFilter {
503     ScreenCaptureContentFilterObject() = default;
504     ~ScreenCaptureContentFilterObject() = default;
505 
506     ScreenCaptureContentFilter screenCaptureContentFilter;
507 };
508 
OH_AVScreenCapture_CreateContentFilter(void)509 struct OH_AVScreenCapture_ContentFilter *OH_AVScreenCapture_CreateContentFilter(void)
510 {
511     struct ScreenCaptureContentFilterObject *object = new(std::nothrow) ScreenCaptureContentFilterObject();
512     CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to new ScreenCaptureContentFilterObject");
513     return object;
514 }
515 
OH_AVScreenCapture_ReleaseContentFilter(struct OH_AVScreenCapture_ContentFilter * filter)516 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_ReleaseContentFilter(struct OH_AVScreenCapture_ContentFilter *filter)
517 {
518     CHECK_AND_RETURN_RET_LOG(filter != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input filter is nullptr!");
519     struct ScreenCaptureContentFilterObject *contentFilterObj =
520         reinterpret_cast<ScreenCaptureContentFilterObject *>(filter);
521     delete contentFilterObj;
522     return AV_SCREEN_CAPTURE_ERR_OK;
523 }
524 
OH_AVScreenCapture_ContentFilter_AddAudioContent(struct OH_AVScreenCapture_ContentFilter * filter,OH_AVScreenCaptureFilterableAudioContent content)525 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_ContentFilter_AddAudioContent(
526     struct OH_AVScreenCapture_ContentFilter *filter, OH_AVScreenCaptureFilterableAudioContent content)
527 {
528     CHECK_AND_RETURN_RET_LOG(filter != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input filter is nullptr!");
529     struct ScreenCaptureContentFilterObject *contentFilterObj =
530         reinterpret_cast<ScreenCaptureContentFilterObject *>(filter);
531 
532     CHECK_AND_RETURN_RET_LOG(
533         content >= OH_AVScreenCaptureFilterableAudioContent::OH_SCREEN_CAPTURE_NOTIFICATION_AUDIO ||
534         content <= OH_AVScreenCaptureFilterableAudioContent::OH_SCREEN_CAPTURE_CURRENT_APP_AUDIO,
535         AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input content invalid!");
536     contentFilterObj->screenCaptureContentFilter.filteredAudioContents.insert(
537         static_cast<AVScreenCaptureFilterableAudioContent>(content));
538     return AV_SCREEN_CAPTURE_ERR_OK;
539 }
540 
OH_AVScreenCapture_ExcludeContent(struct OH_AVScreenCapture * capture,struct OH_AVScreenCapture_ContentFilter * filter)541 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_ExcludeContent(struct OH_AVScreenCapture *capture,
542     struct OH_AVScreenCapture_ContentFilter *filter)
543 {
544     CHECK_AND_RETURN_RET_LOG(capture != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input capture is nullptr!");
545     struct ScreenCaptureObject *screenCaptureObj = reinterpret_cast<ScreenCaptureObject *>(capture);
546     CHECK_AND_RETURN_RET_LOG(screenCaptureObj->screenCapture_ != nullptr,
547         AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "screenCapture is null");
548 
549     CHECK_AND_RETURN_RET_LOG(filter != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input filter is nullptr!");
550     struct ScreenCaptureContentFilterObject *contentFilterObj =
551         reinterpret_cast<ScreenCaptureContentFilterObject *>(filter);
552 
553     int32_t ret = screenCaptureObj->screenCapture_->ExcludeContent(contentFilterObj->screenCaptureContentFilter);
554     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_SCREEN_CAPTURE_ERR_UNSUPPORT, "StartScreenCapture failed!");
555     return AV_SCREEN_CAPTURE_ERR_OK;
556 }
557 
OH_AVScreenCapture_Create(void)558 struct OH_AVScreenCapture *OH_AVScreenCapture_Create(void)
559 {
560     std::shared_ptr<ScreenCapture> screenCapture = ScreenCaptureFactory::CreateScreenCapture();
561     CHECK_AND_RETURN_RET_LOG(screenCapture != nullptr, nullptr, "failed to ScreenCaptureFactory::CreateScreenCapture");
562 
563     struct ScreenCaptureObject *object = new(std::nothrow) ScreenCaptureObject(screenCapture);
564     CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to new ScreenCaptureObject");
565 
566     return object;
567 }
568 
569 namespace {
SetVideoCapInfo(OH_AVScreenCaptureConfig config,AVScreenCaptureConfig & config_)570 void SetVideoCapInfo(OH_AVScreenCaptureConfig config, AVScreenCaptureConfig& config_)
571 {
572     config_.videoInfo.videoCapInfo.displayId = config.videoInfo.videoCapInfo.displayId;
573     int32_t *taskIds = config.videoInfo.videoCapInfo.missionIDs;
574     int32_t size = config.videoInfo.videoCapInfo.missionIDsLen;
575     size = size >= MAX_WINDOWS_LEN ? MAX_WINDOWS_LEN : size;
576     config_.videoInfo.videoCapInfo.taskIDs = {};
577     while (size > 0) {
578         if (taskIds == nullptr) {
579             break;
580         }
581         if (*(taskIds) >= 0) {
582             config_.videoInfo.videoCapInfo.taskIDs.push_back(*(taskIds));
583         }
584         taskIds++;
585         size--;
586     }
587     config_.videoInfo.videoCapInfo.videoFrameWidth = config.videoInfo.videoCapInfo.videoFrameWidth;
588     config_.videoInfo.videoCapInfo.videoFrameHeight = config.videoInfo.videoCapInfo.videoFrameHeight;
589     config_.videoInfo.videoCapInfo.videoSource =
590         static_cast<VideoSourceType>(config.videoInfo.videoCapInfo.videoSource);
591 }
592 
ConvertOHVideoCodecFormat(OH_VideoCodecFormat ohVideoCodec)593 VideoCodecFormat ConvertOHVideoCodecFormat(OH_VideoCodecFormat ohVideoCodec)
594 {
595     static const std::map<OH_VideoCodecFormat, VideoCodecFormat> codecFormatMap = {
596         {OH_VideoCodecFormat::OH_VIDEO_DEFAULT, VideoCodecFormat::H264},
597         {OH_VideoCodecFormat::OH_H264, VideoCodecFormat::H264},
598         {OH_VideoCodecFormat::OH_H265, VideoCodecFormat::H265},
599         {OH_VideoCodecFormat::OH_MPEG4, VideoCodecFormat::MPEG4},
600     };
601     if (codecFormatMap.find(ohVideoCodec) != codecFormatMap.end()) {
602         return codecFormatMap.at(ohVideoCodec);
603     }
604     MEDIA_LOGE("videoCodec is invalid, value is: %{public}d", static_cast<int32_t>(ohVideoCodec));
605     return VideoCodecFormat::VIDEO_CODEC_FORMAT_BUTT;
606 }
607 }
608 
OH_AVScreenCapture_Convert(OH_AVScreenCaptureConfig config)609 AVScreenCaptureConfig OH_AVScreenCapture_Convert(OH_AVScreenCaptureConfig config)
610 {
611     AVScreenCaptureConfig config_;
612     config_.captureMode = static_cast<CaptureMode>(config.captureMode);
613     config_.dataType = static_cast<DataType>(config.dataType);
614     config_.audioInfo.micCapInfo = {
615         .audioSampleRate = config.audioInfo.micCapInfo.audioSampleRate,
616         .audioChannels = config.audioInfo.micCapInfo.audioChannels,
617         .audioSource = static_cast<AudioCaptureSourceType>(config.audioInfo.micCapInfo.audioSource)
618     };
619     config_.audioInfo.innerCapInfo = {
620         .audioSampleRate = config.audioInfo.innerCapInfo.audioSampleRate,
621         .audioChannels = config.audioInfo.innerCapInfo.audioChannels,
622         .audioSource = static_cast<AudioCaptureSourceType>(config.audioInfo.innerCapInfo.audioSource)
623     };
624     config_.audioInfo.audioEncInfo.audioBitrate = config.audioInfo.audioEncInfo.audioBitrate;
625     config_.audioInfo.audioEncInfo.audioCodecformat =
626         static_cast<AudioCodecFormat>(config.audioInfo.audioEncInfo.audioCodecformat);
627     SetVideoCapInfo(config, config_);
628     config_.videoInfo.videoEncInfo = {
629         .videoCodec = ConvertOHVideoCodecFormat(config.videoInfo.videoEncInfo.videoCodec),
630         .videoBitrate = config.videoInfo.videoEncInfo. videoBitrate,
631         .videoFrameRate = config.videoInfo.videoEncInfo.videoFrameRate
632     };
633     if (config.recorderInfo.url != nullptr) {
634         config_.recorderInfo.url = config.recorderInfo.url;
635     }
636     if (config.recorderInfo.fileFormat == CFT_MPEG_4A) {
637         config_.recorderInfo.fileFormat = ContainerFormatType::CFT_MPEG_4A;
638     } else if (config.recorderInfo.fileFormat == CFT_MPEG_4) {
639         config_.recorderInfo.fileFormat = ContainerFormatType::CFT_MPEG_4;
640     }
641     return config_;
642 }
643 
OH_AVScreenCapture_Init(struct OH_AVScreenCapture * capture,OH_AVScreenCaptureConfig config)644 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_Init(struct OH_AVScreenCapture *capture, OH_AVScreenCaptureConfig config)
645 {
646     CHECK_AND_RETURN_RET_LOG(capture != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input capture is nullptr!");
647 
648     struct ScreenCaptureObject *screenCaptureObj = reinterpret_cast<ScreenCaptureObject *>(capture);
649     CHECK_AND_RETURN_RET_LOG(screenCaptureObj->screenCapture_ != nullptr,
650         AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "screenCapture_ is null");
651 
652     AVScreenCaptureConfig config_ = OH_AVScreenCapture_Convert(config);
653     int32_t ret = screenCaptureObj->screenCapture_->Init(config_);
654     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT, "screenCapture init failed!");
655 
656     return AV_SCREEN_CAPTURE_ERR_OK;
657 }
658 
SetPrivacyAuthorityEnabled(struct ScreenCaptureObject * screenCaptureObj)659 static int32_t SetPrivacyAuthorityEnabled(struct ScreenCaptureObject *screenCaptureObj)
660 {
661     if (screenCaptureObj->callback_ != nullptr && screenCaptureObj->callback_->IsStateChangeCallbackEnabled()) {
662         int32_t ret = screenCaptureObj->screenCapture_->SetPrivacyAuthorityEnabled();
663         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "SetPrivacyAuthorityEnabled failed!");
664     }
665     return MSERR_OK;
666 }
667 
OH_AVScreenCapture_StartScreenCapture(struct OH_AVScreenCapture * capture)668 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_StartScreenCapture(struct OH_AVScreenCapture *capture)
669 {
670     CHECK_AND_RETURN_RET_LOG(capture != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input capture is nullptr!");
671 
672     struct ScreenCaptureObject *screenCaptureObj = reinterpret_cast<ScreenCaptureObject *>(capture);
673     CHECK_AND_RETURN_RET_LOG(screenCaptureObj->screenCapture_ != nullptr,
674         AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "screenCapture_ is null");
675 
676     int32_t ret = SetPrivacyAuthorityEnabled(screenCaptureObj);
677     CHECK_AND_RETURN_RET(ret == AV_SCREEN_CAPTURE_ERR_OK, AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT);
678     ret = screenCaptureObj->screenCapture_->StartScreenCapture();
679     if (ret == MSERR_UNSUPPORT) {
680         MEDIA_LOGE("StartScreenCapture failed, ret: %{public}d", ret);
681         return AV_SCREEN_CAPTURE_ERR_UNSUPPORT;
682     }
683     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT, "StartScreenCapture failed!");
684     screenCaptureObj->isStart = true;
685     return AV_SCREEN_CAPTURE_ERR_OK;
686 }
687 
OH_AVScreenCapture_StartScreenCaptureWithSurface(struct OH_AVScreenCapture * capture,OHNativeWindow * window)688 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_StartScreenCaptureWithSurface(struct OH_AVScreenCapture *capture,
689     OHNativeWindow* window)
690 {
691     CHECK_AND_RETURN_RET_LOG(capture != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input capture is nullptr!");
692     CHECK_AND_RETURN_RET_LOG(window != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input window is nullptr!");
693     CHECK_AND_RETURN_RET_LOG(window->surface != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL,
694         "Input window surface is nullptr!");
695 
696     struct ScreenCaptureObject *screenCaptureObj = reinterpret_cast<ScreenCaptureObject *>(capture);
697     CHECK_AND_RETURN_RET_LOG(screenCaptureObj->screenCapture_ != nullptr,
698         AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "screenCapture_ is null");
699 
700     int32_t ret = SetPrivacyAuthorityEnabled(screenCaptureObj);
701     CHECK_AND_RETURN_RET(ret == AV_SCREEN_CAPTURE_ERR_OK, AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT);
702     ret = screenCaptureObj->screenCapture_->StartScreenCaptureWithSurface(window->surface);
703     if (ret == MSERR_UNSUPPORT) {
704         MEDIA_LOGE("StartScreenCaptureWithSurface failed, ret: %{public}d", ret);
705         return AV_SCREEN_CAPTURE_ERR_UNSUPPORT;
706     }
707     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT, "StartScreenCapture failed!");
708     screenCaptureObj->isStart = true;
709     return AV_SCREEN_CAPTURE_ERR_OK;
710 }
711 
OH_AVScreenCapture_StopScreenCapture(struct OH_AVScreenCapture * capture)712 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_StopScreenCapture(struct OH_AVScreenCapture *capture)
713 {
714     MEDIA_LOGI("OH_AVScreenCapture_StopScreenCapture In: 0x%{public}06" PRIXPTR, FAKE_POINTER(capture));
715     CHECK_AND_RETURN_RET_LOG(capture != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input capture is nullptr!");
716 
717     struct ScreenCaptureObject *screenCaptureObj = reinterpret_cast<ScreenCaptureObject *>(capture);
718     CHECK_AND_RETURN_RET_LOG(screenCaptureObj->screenCapture_ != nullptr,
719         AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "screenCapture_ is null");
720 
721     int32_t ret = screenCaptureObj->screenCapture_->StopScreenCapture();
722     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT, "StopScreenCapture failed!");
723     screenCaptureObj->isStart = false;
724     return AV_SCREEN_CAPTURE_ERR_OK;
725 }
726 
OH_AVScreenCapture_StartScreenRecording(struct OH_AVScreenCapture * capture)727 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_StartScreenRecording(struct OH_AVScreenCapture *capture)
728 {
729     CHECK_AND_RETURN_RET_LOG(capture != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input capture is nullptr!");
730 
731     struct ScreenCaptureObject *screenCaptureObj = reinterpret_cast<ScreenCaptureObject *>(capture);
732     CHECK_AND_RETURN_RET_LOG(screenCaptureObj->screenCapture_ != nullptr,
733         AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "screenCapture_ is null");
734 
735     int32_t ret = SetPrivacyAuthorityEnabled(screenCaptureObj);
736     CHECK_AND_RETURN_RET(ret == MSERR_OK, AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT);
737     ret = screenCaptureObj->screenCapture_->StartScreenRecording();
738     if (ret == MSERR_UNSUPPORT) {
739         MEDIA_LOGE("StartScreenRecording failed, ret: %{public}d", ret);
740         return AV_SCREEN_CAPTURE_ERR_UNSUPPORT;
741     }
742     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT, "StartScreenRecording failed!");
743     screenCaptureObj->isStart = true;
744     return AV_SCREEN_CAPTURE_ERR_OK;
745 }
746 
OH_AVScreenCapture_StopScreenRecording(struct OH_AVScreenCapture * capture)747 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_StopScreenRecording(struct OH_AVScreenCapture *capture)
748 {
749     MEDIA_LOGI("OH_AVScreenCapture_StopScreenRecording In: 0x%{public}06" PRIXPTR, FAKE_POINTER(capture));
750     CHECK_AND_RETURN_RET_LOG(capture != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input capture is nullptr!");
751 
752     struct ScreenCaptureObject *screenCaptureObj = reinterpret_cast<ScreenCaptureObject *>(capture);
753     CHECK_AND_RETURN_RET_LOG(screenCaptureObj->screenCapture_ != nullptr,
754         AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "screenCapture_ is null");
755 
756     int32_t ret = screenCaptureObj->screenCapture_->StopScreenRecording();
757     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT, "StopScreenRecording failed!");
758     screenCaptureObj->isStart = false;
759     return AV_SCREEN_CAPTURE_ERR_OK;
760 }
761 
OH_AVScreenCapture_AcquireAudioBuffer(struct OH_AVScreenCapture * capture,OH_AudioBuffer ** audiobuffer,OH_AudioCaptureSourceType type)762 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_AcquireAudioBuffer(struct OH_AVScreenCapture *capture,
763     OH_AudioBuffer **audiobuffer, OH_AudioCaptureSourceType type)
764 {
765     CHECK_AND_RETURN_RET_LOG(capture != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input capture is nullptr!");
766     CHECK_AND_RETURN_RET_LOG(audiobuffer != nullptr && (*audiobuffer != nullptr), AV_SCREEN_CAPTURE_ERR_INVALID_VAL,
767         "input OH_AudioBuffer **audiobuffer is nullptr!");
768 
769     struct ScreenCaptureObject *screenCaptureObj = reinterpret_cast<ScreenCaptureObject *>(capture);
770     CHECK_AND_RETURN_RET_LOG(screenCaptureObj->screenCapture_ != nullptr,
771         AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "screenCapture_ is null");
772 
773     if (screenCaptureObj->callback_ != nullptr && screenCaptureObj->callback_->IsDataCallbackEnabled()) {
774         MEDIA_LOGE("AcquireAudioBuffer() not permit for has set DataCallback");
775         return AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT;
776     }
777     std::shared_ptr<AudioBuffer> aBuffer;
778     int32_t ret =
779         screenCaptureObj->screenCapture_->AcquireAudioBuffer(aBuffer, static_cast<AudioCaptureSourceType>(type));
780     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT, "AcquireAudioBuffer failed!");
781     if ((aBuffer == nullptr) || (audiobuffer == nullptr)) {
782         return AV_SCREEN_CAPTURE_ERR_NO_MEMORY;
783     }
784     if (aBuffer->buffer != nullptr) {
785         (*audiobuffer)->buf = std::move(aBuffer->buffer);
786         aBuffer->buffer = nullptr;
787     }
788     (*audiobuffer)->size = aBuffer->length;
789     (*audiobuffer)->timestamp = aBuffer->timestamp;
790     (*audiobuffer)->type = static_cast<OH_AudioCaptureSourceType>(aBuffer->sourcetype);
791     return AV_SCREEN_CAPTURE_ERR_OK;
792 }
793 
OH_AVScreenCapture_AcquireVideoBuffer(struct OH_AVScreenCapture * capture,int32_t * fence,int64_t * timestamp,struct OH_Rect * region)794 OH_NativeBuffer* OH_AVScreenCapture_AcquireVideoBuffer(struct OH_AVScreenCapture *capture,
795     int32_t *fence, int64_t *timestamp, struct OH_Rect *region)
796 {
797     CHECK_AND_RETURN_RET_LOG(capture != nullptr, nullptr, "input capture is nullptr!");
798 
799     struct ScreenCaptureObject *screenCaptureObj = reinterpret_cast<ScreenCaptureObject *>(capture);
800     CHECK_AND_RETURN_RET_LOG(screenCaptureObj->screenCapture_ != nullptr, nullptr, "screenCapture_ is null");
801 
802     if (screenCaptureObj->callback_ != nullptr && screenCaptureObj->callback_->IsDataCallbackEnabled()) {
803         MEDIA_LOGE("AcquireVideoBuffer() not permit for has set DataCallback");
804         return nullptr;
805     }
806     OHOS::Rect damage;
807     OHOS::sptr<OHOS::SurfaceBuffer> sufacebuffer =
808         screenCaptureObj->screenCapture_->AcquireVideoBuffer(*fence, *timestamp, damage);
809     region->x = damage.x;
810     region->y = damage.y;
811     region->width = damage.w;
812     region->height = damage.h;
813     CHECK_AND_RETURN_RET_LOG(sufacebuffer != nullptr, nullptr, "AcquireVideoBuffer failed!");
814 
815     OH_NativeBuffer* nativebuffer = sufacebuffer->SurfaceBufferToNativeBuffer();
816     OH_NativeBuffer_Reference(nativebuffer);
817     referencedBuffer_.push(nativebuffer);
818     MEDIA_LOGD("return and reference the nativebuffer");
819 
820     return nativebuffer;
821 }
822 
OH_AVScreenCapture_ReleaseVideoBuffer(struct OH_AVScreenCapture * capture)823 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_ReleaseVideoBuffer(struct OH_AVScreenCapture *capture)
824 {
825     CHECK_AND_RETURN_RET_LOG(capture != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input capture is nullptr!");
826 
827     struct ScreenCaptureObject *screenCaptureObj = reinterpret_cast<ScreenCaptureObject *>(capture);
828     CHECK_AND_RETURN_RET_LOG(screenCaptureObj->screenCapture_ != nullptr,
829         AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "screenCapture_ is null");
830 
831     if (screenCaptureObj->callback_ != nullptr && screenCaptureObj->callback_->IsDataCallbackEnabled()) {
832         MEDIA_LOGE("ReleaseVideoBuffer() not permit for has set DataCallback");
833         return AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT;
834     }
835 
836     if (!referencedBuffer_.empty()) {
837         OH_NativeBuffer* nativebuffer = referencedBuffer_.front();
838         OH_NativeBuffer_Unreference(nativebuffer);
839         referencedBuffer_.pop();
840         MEDIA_LOGD("unreference the front nativebuffer");
841     }
842 
843     int32_t ret = screenCaptureObj->screenCapture_->ReleaseVideoBuffer();
844     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT, "ReleaseVideoBuffer failed!");
845 
846     return AV_SCREEN_CAPTURE_ERR_OK;
847 }
848 
OH_AVScreenCapture_ReleaseAudioBuffer(struct OH_AVScreenCapture * capture,OH_AudioCaptureSourceType type)849 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_ReleaseAudioBuffer(struct OH_AVScreenCapture *capture,
850     OH_AudioCaptureSourceType type)
851 {
852     CHECK_AND_RETURN_RET_LOG(capture != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input capture is nullptr!");
853 
854     struct ScreenCaptureObject *screenCaptureObj = reinterpret_cast<ScreenCaptureObject *>(capture);
855     CHECK_AND_RETURN_RET_LOG(screenCaptureObj->screenCapture_ != nullptr,
856         AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "screenCapture_ is null");
857 
858     if (screenCaptureObj->callback_ != nullptr && screenCaptureObj->callback_->IsDataCallbackEnabled()) {
859         MEDIA_LOGE("ReleaseAudioBuffer() not permit for has set DataCallback");
860         return AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT;
861     }
862     int32_t ret = screenCaptureObj->screenCapture_->ReleaseAudioBuffer(static_cast<AudioCaptureSourceType>(type));
863     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT, "ReleaseSurfaceBuffer failed!");
864 
865     return AV_SCREEN_CAPTURE_ERR_OK;
866 }
867 
OH_AVScreenCapture_SetCallback(struct OH_AVScreenCapture * capture,struct OH_AVScreenCaptureCallback callback)868 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_SetCallback(struct OH_AVScreenCapture *capture,
869     struct OH_AVScreenCaptureCallback callback)
870 {
871     CHECK_AND_RETURN_RET_LOG(capture != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input capture is nullptr!");
872 
873     struct ScreenCaptureObject *screenCaptureObj = reinterpret_cast<ScreenCaptureObject *>(capture);
874     CHECK_AND_RETURN_RET_LOG(screenCaptureObj->screenCapture_ != nullptr,
875         AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "screenCapture_ is null");
876 
877     if (screenCaptureObj->callback_ == nullptr) {
878         screenCaptureObj->callback_ = std::make_shared<NativeScreenCaptureCallback>(capture, callback);
879         CHECK_AND_RETURN_RET_LOG(screenCaptureObj->callback_ != nullptr,
880             AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "callback_ is nullptr!");
881     } else {
882         screenCaptureObj->callback_->SetCallback(callback);
883     }
884 
885     int32_t ret = screenCaptureObj->screenCapture_->SetScreenCaptureCallback(screenCaptureObj->callback_);
886     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT,
887         "SetScreenCaptureCallback failed!");
888     return AV_SCREEN_CAPTURE_ERR_OK;
889 }
890 
OH_AVScreenCapture_Release(struct OH_AVScreenCapture * capture)891 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_Release(struct OH_AVScreenCapture *capture)
892 {
893     MEDIA_LOGI("OH_AVScreenCapture_Release In: 0x%{public}06" PRIXPTR, FAKE_POINTER(capture));
894     CHECK_AND_RETURN_RET_LOG(capture != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input capture is nullptr!");
895 
896     struct ScreenCaptureObject *screenCaptureObj = reinterpret_cast<ScreenCaptureObject *>(capture);
897 
898     if (screenCaptureObj != nullptr && screenCaptureObj->screenCapture_ != nullptr) {
899         if (screenCaptureObj->callback_ != nullptr) {
900             screenCaptureObj->callback_->StopCallback();
901         }
902         int32_t ret = screenCaptureObj->screenCapture_->Release();
903         delete capture;
904         capture = nullptr;
905         if (ret != MSERR_OK) {
906             MEDIA_LOGE("screen capture Release failed!");
907             return AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT;
908         }
909     } else {
910         MEDIA_LOGD("screen capture is nullptr!");
911     }
912     return AV_SCREEN_CAPTURE_ERR_OK;
913 }
914 
OH_AVScreenCapture_SetMicrophoneEnabled(struct OH_AVScreenCapture * capture,bool isMicrophone)915 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_SetMicrophoneEnabled(struct OH_AVScreenCapture *capture,
916     bool isMicrophone)
917 {
918     CHECK_AND_RETURN_RET_LOG(capture != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input capture is nullptr!");
919 
920     struct ScreenCaptureObject *screenCaptureObj = reinterpret_cast<ScreenCaptureObject *>(capture);
921     CHECK_AND_RETURN_RET_LOG(screenCaptureObj->screenCapture_ != nullptr,
922         AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "screenCapture_ is null");
923 
924     int32_t ret = screenCaptureObj->screenCapture_->SetMicrophoneEnabled(isMicrophone);
925     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT, "setMicrophoneEnable failed!");
926 
927     return AV_SCREEN_CAPTURE_ERR_OK;
928 }
929 
AVScreenCaptureSetCallback(struct OH_AVScreenCapture * capture,struct ScreenCaptureObject * screenCaptureObj)930 static OH_AVSCREEN_CAPTURE_ErrCode AVScreenCaptureSetCallback(struct OH_AVScreenCapture *capture,
931     struct ScreenCaptureObject *screenCaptureObj)
932 {
933     MEDIA_LOGD("AVScreenCaptureSetCallback S");
934     CHECK_AND_RETURN_RET_LOG(capture != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input capture is nullptr!");
935     CHECK_AND_RETURN_RET_LOG(screenCaptureObj != nullptr && screenCaptureObj->screenCapture_ != nullptr,
936         AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "screenCapture_ is nullptr!");
937     if (screenCaptureObj->callback_ == nullptr) {
938         MEDIA_LOGD("AVScreenCaptureSetCallback new NativeScreenCaptureCallback");
939         OH_AVScreenCaptureCallback dummyCallback = {
940             .onError = nullptr,
941             .onAudioBufferAvailable = nullptr,
942             .onVideoBufferAvailable = nullptr
943         };
944         screenCaptureObj->callback_ = std::make_shared<NativeScreenCaptureCallback>(capture, dummyCallback);
945         CHECK_AND_RETURN_RET_LOG(screenCaptureObj->callback_ != nullptr,
946             AV_SCREEN_CAPTURE_ERR_NO_MEMORY, "callback_ is nullptr!");
947 
948         int32_t ret = screenCaptureObj->screenCapture_->SetScreenCaptureCallback(screenCaptureObj->callback_);
949         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT,
950             "SetScreenCaptureCallback failed!");
951     }
952     MEDIA_LOGD("AVScreenCaptureSetCallback E");
953     return AV_SCREEN_CAPTURE_ERR_OK;
954 }
955 
OH_AVScreenCapture_SetStateCallback(struct OH_AVScreenCapture * capture,OH_AVScreenCapture_OnStateChange callback,void * userData)956 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_SetStateCallback(struct OH_AVScreenCapture *capture,
957     OH_AVScreenCapture_OnStateChange callback, void *userData)
958 {
959     MEDIA_LOGD("OH_AVScreenCapture_SetStateCallback S");
960     CHECK_AND_RETURN_RET_LOG(capture != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input capture is nullptr!");
961     CHECK_AND_RETURN_RET_LOG(callback != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input stateCallback is nullptr!");
962     struct ScreenCaptureObject *screenCaptureObj = reinterpret_cast<ScreenCaptureObject *>(capture);
963     CHECK_AND_RETURN_RET_LOG(screenCaptureObj->screenCapture_ != nullptr,
964         AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "screenCapture_ is null");
965 
966     OH_AVSCREEN_CAPTURE_ErrCode errCode = AVScreenCaptureSetCallback(capture, screenCaptureObj);
967     CHECK_AND_RETURN_RET_LOG(errCode == AV_SCREEN_CAPTURE_ERR_OK, errCode, "SetStateCallback is null");
968 
969     if (screenCaptureObj->callback_ == nullptr ||
970         !screenCaptureObj->callback_->SetStateChangeCallback(callback, userData)) {
971         MEDIA_LOGE("OH_AVScreenCapture_SetStateCallback error");
972         return AV_SCREEN_CAPTURE_ERR_NO_MEMORY;
973     }
974     MEDIA_LOGD("OH_AVScreenCapture_SetStateCallback E");
975     return AV_SCREEN_CAPTURE_ERR_OK;
976 }
977 
OH_AVScreenCapture_SetCaptureContentChangedCallback(struct OH_AVScreenCapture * capture,OH_AVScreenCapture_OnCaptureContentChanged callback,void * userData)978 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_SetCaptureContentChangedCallback(struct OH_AVScreenCapture *capture,
979     OH_AVScreenCapture_OnCaptureContentChanged callback, void *userData)
980 {
981     MEDIA_LOGD("OH_AVScreenCapture_SetCaptureContentChangedCallback S");
982     CHECK_AND_RETURN_RET_LOG(capture != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input capture is nullptr!");
983     CHECK_AND_RETURN_RET_LOG(callback != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL,
984         "input contentChangedCallback is nullptr!");
985     struct ScreenCaptureObject *screenCaptureObj = reinterpret_cast<ScreenCaptureObject *>(capture);
986     CHECK_AND_RETURN_RET_LOG(screenCaptureObj->screenCapture_ != nullptr,
987         AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "screenCapture_ is null");
988 
989     OH_AVSCREEN_CAPTURE_ErrCode errCode = AVScreenCaptureSetCallback(capture, screenCaptureObj);
990     CHECK_AND_RETURN_RET_LOG(errCode == AV_SCREEN_CAPTURE_ERR_OK, errCode, "SetCaptureContentChangedCallback is null");
991 
992     if (screenCaptureObj->callback_ == nullptr ||
993         !screenCaptureObj->callback_->SetCaptureContentChangedCallback(callback, userData)) {
994         MEDIA_LOGE("OH_AVScreenCapture_SetCaptureContentChangedCallback error");
995         return AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT;
996     }
997     MEDIA_LOGD("OH_AVScreenCapture_SetCaptureContentChangedCallback E");
998     return AV_SCREEN_CAPTURE_ERR_OK;
999 }
1000 
OH_AVScreenCapture_SetErrorCallback(struct OH_AVScreenCapture * capture,OH_AVScreenCapture_OnError callback,void * userData)1001 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_SetErrorCallback(struct OH_AVScreenCapture *capture,
1002     OH_AVScreenCapture_OnError callback, void *userData)
1003 {
1004     MEDIA_LOGD("OH_AVScreenCapture_SetErrorCallback S");
1005     CHECK_AND_RETURN_RET_LOG(capture != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input capture is nullptr!");
1006     CHECK_AND_RETURN_RET_LOG(callback != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input errorCallback is nullptr!");
1007     struct ScreenCaptureObject *screenCaptureObj = reinterpret_cast<ScreenCaptureObject *>(capture);
1008     CHECK_AND_RETURN_RET_LOG(screenCaptureObj->screenCapture_ != nullptr,
1009         AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "screenCapture_ is null");
1010 
1011     OH_AVSCREEN_CAPTURE_ErrCode errCode = AVScreenCaptureSetCallback(capture, screenCaptureObj);
1012     CHECK_AND_RETURN_RET_LOG(errCode == AV_SCREEN_CAPTURE_ERR_OK, errCode, "SetErrorCallback is null");
1013 
1014     if (screenCaptureObj->callback_ == nullptr || !screenCaptureObj->callback_->SetErrorCallback(callback, userData)) {
1015         MEDIA_LOGE("OH_AVScreenCapture_SetErrorCallback error");
1016         return AV_SCREEN_CAPTURE_ERR_NO_MEMORY;
1017     }
1018     MEDIA_LOGD("OH_AVScreenCapture_SetErrorCallback E");
1019     return AV_SCREEN_CAPTURE_ERR_OK;
1020 }
1021 
OH_AVScreenCapture_SetDataCallback(struct OH_AVScreenCapture * capture,OH_AVScreenCapture_OnBufferAvailable callback,void * userData)1022 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_SetDataCallback(struct OH_AVScreenCapture *capture,
1023     OH_AVScreenCapture_OnBufferAvailable callback, void *userData)
1024 {
1025     MEDIA_LOGD("OH_AVScreenCapture_SetDataCallback E");
1026     CHECK_AND_RETURN_RET_LOG(capture != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input capture is nullptr!");
1027     CHECK_AND_RETURN_RET_LOG(callback != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input dataCallback is nullptr!");
1028     struct ScreenCaptureObject *screenCaptureObj = reinterpret_cast<ScreenCaptureObject *>(capture);
1029     CHECK_AND_RETURN_RET_LOG(screenCaptureObj->screenCapture_ != nullptr,
1030         AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "screenCapture_ is null");
1031 
1032     OH_AVSCREEN_CAPTURE_ErrCode errCode = AVScreenCaptureSetCallback(capture, screenCaptureObj);
1033     CHECK_AND_RETURN_RET_LOG(errCode == AV_SCREEN_CAPTURE_ERR_OK, errCode, "SetDataCallback is null");
1034 
1035     if (screenCaptureObj->callback_ == nullptr ||
1036         !screenCaptureObj->callback_->SetDataCallback(callback, userData)) {
1037         MEDIA_LOGE("OH_AVScreenCapture_SetDataCallback error");
1038         return AV_SCREEN_CAPTURE_ERR_NO_MEMORY;
1039     }
1040     MEDIA_LOGD("OH_AVScreenCapture_SetDataCallback E");
1041     return AV_SCREEN_CAPTURE_ERR_OK;
1042 }
1043 
OH_AVScreenCapture_SetCanvasRotation(struct OH_AVScreenCapture * capture,bool canvasRotation)1044 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_SetCanvasRotation(struct OH_AVScreenCapture *capture,
1045     bool canvasRotation)
1046 {
1047     CHECK_AND_RETURN_RET_LOG(capture != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input capture is nullptr!");
1048 
1049     struct ScreenCaptureObject *screenCaptureObj = reinterpret_cast<ScreenCaptureObject *>(capture);
1050     CHECK_AND_RETURN_RET_LOG(screenCaptureObj->screenCapture_ != nullptr,
1051                              AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "screenCapture_ is null");
1052 
1053     int32_t ret = screenCaptureObj->screenCapture_->SetCanvasRotation(canvasRotation);
1054     if (ret == MSERR_UNSUPPORT) {
1055         MEDIA_LOGE("SetCanvasRotation failed, ret: %{public}d", ret);
1056         return AV_SCREEN_CAPTURE_ERR_UNSUPPORT;
1057     }
1058     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT,
1059                              "SetCanvasRotation failed!");
1060 
1061     return AV_SCREEN_CAPTURE_ERR_OK;
1062 }
1063 
OH_AVScreenCapture_ShowCursor(struct OH_AVScreenCapture * capture,bool showCursor)1064 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_ShowCursor(struct OH_AVScreenCapture *capture,
1065     bool showCursor)
1066 {
1067     CHECK_AND_RETURN_RET_LOG(capture != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input capture is nullptr!");
1068 
1069     struct ScreenCaptureObject *screenCaptureObj = reinterpret_cast<ScreenCaptureObject *>(capture);
1070     CHECK_AND_RETURN_RET_LOG(screenCaptureObj->screenCapture_ != nullptr,
1071                              AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "screenCapture_ is null");
1072     int32_t ret = screenCaptureObj->screenCapture_->ShowCursor(showCursor);
1073     if (ret == MSERR_UNSUPPORT) {
1074         MEDIA_LOGE("ShowCursor failed, ret: %{public}d", ret);
1075         return AV_SCREEN_CAPTURE_ERR_UNSUPPORT;
1076     }
1077     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT,
1078                              "ShowCursor failed!");
1079 
1080     return AV_SCREEN_CAPTURE_ERR_OK;
1081 }
1082 
OH_AVScreenCapture_ContentFilter_AddWindowContent(struct OH_AVScreenCapture_ContentFilter * filter,int32_t * windowIDs,int32_t windowCount)1083 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_ContentFilter_AddWindowContent(
1084     struct OH_AVScreenCapture_ContentFilter *filter, int32_t *windowIDs, int32_t windowCount)
1085 {
1086     CHECK_AND_RETURN_RET_LOG(filter != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input filter is nullptr!");
1087     struct ScreenCaptureContentFilterObject *contentFilterObj =
1088             reinterpret_cast<ScreenCaptureContentFilterObject *>(filter);
1089     CHECK_AND_RETURN_RET_LOG(windowIDs != nullptr && windowCount > 0 && windowCount < MAX_WINDOWS_LEN,
1090                              AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input window invalid!");
1091     std::vector<uint64_t> vec;
1092     for (int32_t i = 0; i < windowCount; i++) {
1093         if (static_cast<int32_t>(*(windowIDs + i)) >= 0) {
1094             vec.push_back(static_cast<uint64_t>(*(windowIDs + i)));
1095         }
1096     }
1097     contentFilterObj->screenCaptureContentFilter.windowIDsVec = vec;
1098     return AV_SCREEN_CAPTURE_ERR_OK;
1099 }
1100 
OH_AVScreenCapture_ResizeCanvas(struct OH_AVScreenCapture * capture,int32_t width,int32_t height)1101 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_ResizeCanvas(struct OH_AVScreenCapture *capture,
1102     int32_t width, int32_t height)
1103 {
1104     CHECK_AND_RETURN_RET_LOG(capture != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input capture is nullptr!");
1105 
1106     struct ScreenCaptureObject *screenCaptureObj = reinterpret_cast<ScreenCaptureObject *>(capture);
1107     CHECK_AND_RETURN_RET_LOG(screenCaptureObj->screenCapture_ != nullptr,
1108                              AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "screenCapture_ is null");
1109 
1110     CHECK_AND_RETURN_RET_LOG(width > 0 && height > 0, AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT,
1111                              "input width or height invalid!");
1112 
1113     int32_t ret = screenCaptureObj->screenCapture_->ResizeCanvas(width, height);
1114     if (ret == MSERR_UNSUPPORT) {
1115         MEDIA_LOGE("ResizeCanvas failed, ret: %{public}d", ret);
1116         return AV_SCREEN_CAPTURE_ERR_UNSUPPORT;
1117     }
1118     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT,
1119                              "ResizeCanvas failed!");
1120 
1121     return AV_SCREEN_CAPTURE_ERR_OK;
1122 }
1123 
OH_AVScreenCapture_SkipPrivacyMode(struct OH_AVScreenCapture * capture,int32_t * windowIDs,int32_t windowCount)1124 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_SkipPrivacyMode(struct OH_AVScreenCapture *capture,
1125     int32_t *windowIDs, int32_t windowCount)
1126 {
1127     CHECK_AND_RETURN_RET_LOG(capture != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input capture is nullptr!");
1128     struct ScreenCaptureObject *screenCaptureObj = reinterpret_cast<ScreenCaptureObject *>(capture);
1129     CHECK_AND_RETURN_RET_LOG(screenCaptureObj->screenCapture_ != nullptr,
1130                              AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "screenCapture_ is null");
1131     CHECK_AND_RETURN_RET_LOG(windowCount >= 0 && windowCount < MAX_WINDOWS_LEN,
1132                              AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input window invalid!");
1133     CHECK_AND_RETURN_RET_LOG(!(windowIDs == nullptr && windowCount > 0),
1134                              AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input window invalid, nullptr but size not 0!");
1135     std::vector<uint64_t> vec;
1136     for (int32_t i = 0; i < windowCount; i++) {
1137         if (static_cast<int32_t>(*(windowIDs + i)) >= 0) {
1138             vec.push_back(static_cast<uint64_t>(*(windowIDs + i)));
1139         }
1140     }
1141     CHECK_AND_RETURN_RET_LOG(vec.size() >= 0, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input window content invalid!");
1142     int32_t ret = screenCaptureObj->screenCapture_->SkipPrivacyMode(vec);
1143     if (ret == MSERR_UNSUPPORT) {
1144         MEDIA_LOGE("SkipPrivacyMode failed, ret: %{public}d", ret);
1145         return AV_SCREEN_CAPTURE_ERR_UNSUPPORT;
1146     }
1147     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT,
1148                              "SkipPrivacyMode failed!");
1149     return AV_SCREEN_CAPTURE_ERR_OK;
1150 }
1151 
OH_AVScreenCapture_SetMaxVideoFrameRate(struct OH_AVScreenCapture * capture,int32_t frameRate)1152 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_SetMaxVideoFrameRate(struct OH_AVScreenCapture *capture,
1153     int32_t frameRate)
1154 {
1155     CHECK_AND_RETURN_RET_LOG(capture != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input capture is nullptr!");
1156 
1157     struct ScreenCaptureObject *screenCaptureObj = reinterpret_cast<ScreenCaptureObject *>(capture);
1158     CHECK_AND_RETURN_RET_LOG(screenCaptureObj->screenCapture_ != nullptr,
1159                              AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "screenCapture_ is null");
1160 
1161     CHECK_AND_RETURN_RET_LOG(frameRate > 0, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input frameRate invalid!");
1162 
1163     int32_t ret = screenCaptureObj->screenCapture_->SetMaxVideoFrameRate(frameRate);
1164     if (ret == MSERR_UNSUPPORT) {
1165         MEDIA_LOGE("SetMaxVideoFrameRate failed, ret: %{public}d", ret);
1166         return AV_SCREEN_CAPTURE_ERR_UNSUPPORT;
1167     }
1168     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT,
1169                              "SetMaxVideoFrameRate failed!");
1170 
1171     return AV_SCREEN_CAPTURE_ERR_OK;
1172 }
1173 
OH_AVScreenCapture_SetDisplayCallback(struct OH_AVScreenCapture * capture,OH_AVScreenCapture_OnDisplaySelected callback,void * userData)1174 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_SetDisplayCallback(struct OH_AVScreenCapture *capture,
1175     OH_AVScreenCapture_OnDisplaySelected callback, void *userData)
1176 {
1177     MEDIA_LOGD("OH_AVScreenCapture_SetDisplayCallback S");
1178     CHECK_AND_RETURN_RET_LOG(capture != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input capture is nullptr!");
1179     CHECK_AND_RETURN_RET_LOG(callback != nullptr,
1180         AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input displayCallback is nullptr!");
1181     struct ScreenCaptureObject *screenCaptureObj = reinterpret_cast<ScreenCaptureObject *>(capture);
1182     CHECK_AND_RETURN_RET_LOG(screenCaptureObj->screenCapture_ != nullptr,
1183         AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "screenCapture_ is null");
1184 
1185     CHECK_AND_RETURN_RET_LOG(!screenCaptureObj->isStart,
1186         AV_SCREEN_CAPTURE_ERR_INVALID_STATE, "This interface should be called before Start is called!");
1187     OH_AVSCREEN_CAPTURE_ErrCode errCode = AVScreenCaptureSetCallback(capture, screenCaptureObj);
1188     CHECK_AND_RETURN_RET_LOG(errCode == AV_SCREEN_CAPTURE_ERR_OK, errCode, "SetDisplayCallback is null");
1189 
1190     if (screenCaptureObj->callback_ == nullptr ||
1191         !screenCaptureObj->callback_->SetDisplayCallback(callback, userData)) {
1192         MEDIA_LOGE("OH_AVScreenCapture_SetDisplayCallback error");
1193         return AV_SCREEN_CAPTURE_ERR_NO_MEMORY;
1194     }
1195     MEDIA_LOGD("OH_AVScreenCapture_SetDisplayCallback E");
1196     return AV_SCREEN_CAPTURE_ERR_OK;
1197 }
1198 
1199 struct ScreenCaptureStrategyObject : public OH_AVScreenCapture_CaptureStrategy {
1200     ScreenCaptureStrategyObject() = default;
1201     ~ScreenCaptureStrategyObject() = default;
1202 
1203     ScreenCaptureStrategy strategy;
1204 };
1205 
OH_AVScreenCapture_CreateCaptureStrategy(void)1206 OH_AVScreenCapture_CaptureStrategy* OH_AVScreenCapture_CreateCaptureStrategy(void)
1207 {
1208     struct ScreenCaptureStrategyObject *object = new(std::nothrow) ScreenCaptureStrategyObject();
1209     CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to new ScreenCaptureStrategyObject");
1210     return object;
1211 }
1212 
OH_AVScreenCapture_ReleaseCaptureStrategy(OH_AVScreenCapture_CaptureStrategy * strategy)1213 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_ReleaseCaptureStrategy(OH_AVScreenCapture_CaptureStrategy* strategy)
1214 {
1215     CHECK_AND_RETURN_RET_LOG(strategy != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input strategy is nullptr!");
1216     struct ScreenCaptureStrategyObject *strategyObj = reinterpret_cast<ScreenCaptureStrategyObject *>(strategy);
1217     delete strategyObj;
1218     return AV_SCREEN_CAPTURE_ERR_OK;
1219 }
1220 
OH_AVScreenCapture_SetCaptureStrategy(struct OH_AVScreenCapture * capture,OH_AVScreenCapture_CaptureStrategy * strategy)1221 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_SetCaptureStrategy(struct OH_AVScreenCapture *capture,
1222     OH_AVScreenCapture_CaptureStrategy *strategy)
1223 {
1224     CHECK_AND_RETURN_RET_LOG(capture != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input capture is nullptr");
1225     CHECK_AND_RETURN_RET_LOG(strategy != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input strategy is nullptr");
1226 
1227     struct ScreenCaptureObject *screenCaptureObj = reinterpret_cast<ScreenCaptureObject *>(capture);
1228     CHECK_AND_RETURN_RET_LOG(screenCaptureObj->screenCapture_ != nullptr,
1229         AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "screenCapture is nullptr");
1230 
1231     struct ScreenCaptureStrategyObject *strategyObj = reinterpret_cast<ScreenCaptureStrategyObject *>(strategy);
1232     CHECK_AND_RETURN_RET_LOG(strategyObj != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "strategyObj is nullptr");
1233 
1234     int32_t ret = screenCaptureObj->screenCapture_->SetScreenCaptureStrategy(strategyObj->strategy);
1235     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_SCREEN_CAPTURE_ERR_INVALID_STATE, "SetScreenCaptureStrategy failed");
1236     return AV_SCREEN_CAPTURE_ERR_OK;
1237 }
1238 
OH_AVScreenCapture_StrategyForKeepCaptureDuringCall(OH_AVScreenCapture_CaptureStrategy * strategy,bool value)1239 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_StrategyForKeepCaptureDuringCall(
1240     OH_AVScreenCapture_CaptureStrategy *strategy, bool value)
1241 {
1242     CHECK_AND_RETURN_RET_LOG(strategy != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input strategy is nullptr");
1243     struct ScreenCaptureStrategyObject *strategyObj = reinterpret_cast<ScreenCaptureStrategyObject *>(strategy);
1244     CHECK_AND_RETURN_RET_LOG(strategyObj != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "strategyObj is nullptr");
1245     strategyObj->strategy.keepCaptureDuringCall = value;
1246     return AV_SCREEN_CAPTURE_ERR_OK;
1247 }
1248 
OH_AVScreenCapture_SetCaptureArea(struct OH_AVScreenCapture * capture,uint64_t displayId,OH_Rect * area)1249 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_SetCaptureArea(struct OH_AVScreenCapture *capture,
1250     uint64_t displayId, OH_Rect* area)
1251 {
1252     MEDIA_LOGD("OH_AVScreenCapture_SetCaptureArea S");
1253     CHECK_AND_RETURN_RET_LOG(capture != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input capture is nullptr!");
1254     struct ScreenCaptureObject *screenCaptureObj = reinterpret_cast<ScreenCaptureObject *>(capture);
1255     CHECK_AND_RETURN_RET_LOG(screenCaptureObj->screenCapture_ != nullptr,
1256         AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "screenCapture_ is null");
1257 
1258     CHECK_AND_RETURN_RET_LOG(displayId >= 0 && displayId < VIRTUAL_DISPLAY_ID_START,
1259         AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input displayId invalid");
1260     CHECK_AND_RETURN_RET_LOG(area != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input area is nullptr");
1261     CHECK_AND_RETURN_RET_LOG(area->x > 0 && area->y > 0 && area->width > 0 && area->height > 0,
1262         AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input area invalid");
1263     OHOS::Rect region;
1264     region.x = area->x;
1265     region.y = area->y;
1266     region.w = area->width;
1267     region.h = area->height;
1268 
1269     int32_t ret = screenCaptureObj->screenCapture_->SetCaptureArea(displayId, region);
1270     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_SCREEN_CAPTURE_ERR_INVALID_VAL,
1271         "SetCaptureArea failed!");
1272     MEDIA_LOGD("OH_AVScreenCapture_SetCaptureArea E");
1273     return AV_SCREEN_CAPTURE_ERR_OK;
1274 }
1275 
OH_AVScreenCapture_StrategyForPrivacyMaskMode(OH_AVScreenCapture_CaptureStrategy * strategy,int32_t value)1276 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_StrategyForPrivacyMaskMode(
1277     OH_AVScreenCapture_CaptureStrategy *strategy, int32_t value)
1278 {
1279     MEDIA_LOGD("OH_AVScreenCapture_StrategyForPrivacyMaskMode S");
1280     CHECK_AND_RETURN_RET_LOG(strategy != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input strategy is nullptr!");
1281     struct ScreenCaptureStrategyObject *strategyObj = reinterpret_cast<ScreenCaptureStrategyObject *>(strategy);
1282     CHECK_AND_RETURN_RET_LOG(strategyObj != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "strategyObj is nullptr");
1283     CHECK_AND_RETURN_RET_LOG(value == 0 || value == 1, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input value is invalid");
1284     strategyObj->strategy.strategyForPrivacyMaskMode = value;
1285     MEDIA_LOGD("OH_AVScreenCapture_StrategyForPrivacyMaskMode E");
1286     return AV_SCREEN_CAPTURE_ERR_OK;
1287 }
1288 
OH_AVScreenCapture_StrategyForCanvasFollowRotation(OH_AVScreenCapture_CaptureStrategy * strategy,bool value)1289 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_StrategyForCanvasFollowRotation(
1290     OH_AVScreenCapture_CaptureStrategy *strategy, bool value)
1291 {
1292     MEDIA_LOGD("OH_AVScreenCapture_StrategyForCanvasFollowRotation S");
1293     CHECK_AND_RETURN_RET_LOG(strategy != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input strategy is nullptr!");
1294     struct ScreenCaptureStrategyObject *strategyObj = reinterpret_cast<ScreenCaptureStrategyObject *>(strategy);
1295     CHECK_AND_RETURN_RET_LOG(strategyObj != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "strategyObj is nullptr");
1296     strategyObj->strategy.canvasFollowRotation = value;
1297     MEDIA_LOGD("OH_AVScreenCapture_StrategyForCanvasFollowRotation E");
1298     return AV_SCREEN_CAPTURE_ERR_OK;
1299 }
1300 
OH_AVScreenCapture_SetSelectionCallback(struct OH_AVScreenCapture * capture,OH_AVScreenCapture_OnUserSelected callback,void * userData)1301 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_SetSelectionCallback(struct OH_AVScreenCapture *capture,
1302     OH_AVScreenCapture_OnUserSelected callback, void *userData)
1303 {
1304     MEDIA_LOGD("OH_AVScreenCapture_SetSelectionCallback S");
1305     CHECK_AND_RETURN_RET_LOG(capture != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input capture is nullptr");
1306     CHECK_AND_RETURN_RET_LOG(callback != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input callback is nullptr");
1307     struct ScreenCaptureObject *screenCaptureObj = reinterpret_cast<ScreenCaptureObject *>(capture);
1308     CHECK_AND_RETURN_RET_LOG(screenCaptureObj->screenCapture_ != nullptr,
1309         AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "screenCapture_ is null");
1310     CHECK_AND_RETURN_RET_LOG(!screenCaptureObj->isStart,
1311         AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "This interface should be called before Start is called!");
1312 
1313     OH_AVSCREEN_CAPTURE_ErrCode errCode = AVScreenCaptureSetCallback(capture, screenCaptureObj);
1314     CHECK_AND_RETURN_RET_LOG(errCode == AV_SCREEN_CAPTURE_ERR_OK, AV_SCREEN_CAPTURE_ERR_INVALID_VAL,
1315         "SetSelectionCallback is null");
1316     if (screenCaptureObj->callback_ == nullptr ||
1317         !screenCaptureObj->callback_->SetUserSelectedCallback(callback, userData)) {
1318         MEDIA_LOGE("OH_AVScreenCapture_SetSelectionCallback error");
1319         return AV_SCREEN_CAPTURE_ERR_INVALID_VAL;
1320     }
1321     MEDIA_LOGD("OH_AVScreenCapture_SetSelectionCallback E");
1322     return AV_SCREEN_CAPTURE_ERR_OK;
1323 }
1324 
OH_AVScreenCapture_GetCaptureTypeSelected(OH_AVScreenCapture_UserSelectionInfo * selection,int32_t * type)1325 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_GetCaptureTypeSelected(OH_AVScreenCapture_UserSelectionInfo *selection,
1326     int32_t* type)
1327 {
1328     MEDIA_LOGD("OH_AVScreenCapture_GetCaptureTypeSelected S");
1329     CHECK_AND_RETURN_RET_LOG(selection != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input selection is nullptr");
1330     struct ScreenCaptureUserSelectionObject *selectionObj =
1331         reinterpret_cast<ScreenCaptureUserSelectionObject *>(selection);
1332     CHECK_AND_RETURN_RET_LOG(selectionObj != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL,
1333         "selectionObj is null");
1334     *type = selectionObj->userSelectionInfo_.selectType;
1335     MEDIA_LOGD("OH_AVScreenCapture_GetCaptureTypeSelected type: %{public}d", *type);
1336     return AV_SCREEN_CAPTURE_ERR_OK;
1337 }
1338 
OH_AVScreenCapture_GetDisplayIdSelected(OH_AVScreenCapture_UserSelectionInfo * selection,uint64_t * displayId)1339 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_GetDisplayIdSelected(OH_AVScreenCapture_UserSelectionInfo *selection,
1340     uint64_t* displayId)
1341 {
1342     MEDIA_LOGD("OH_AVScreenCapture_GetDisplayIdSelected S");
1343     CHECK_AND_RETURN_RET_LOG(selection != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input selection is nullptr");
1344     struct ScreenCaptureUserSelectionObject *selectionObj =
1345         reinterpret_cast<ScreenCaptureUserSelectionObject *>(selection);
1346     CHECK_AND_RETURN_RET_LOG(selectionObj != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL,
1347         "selectionObj is null");
1348     *displayId = selectionObj->userSelectionInfo_.displayId;
1349     MEDIA_LOGD("OH_AVScreenCapture_GetDisplayIdSelected displayId: %{public}" PRIu64, *displayId);
1350     return AV_SCREEN_CAPTURE_ERR_OK;
1351 }
1352 
OH_AVScreenCapture_StrategyForBFramesEncoding(OH_AVScreenCapture_CaptureStrategy * strategy,bool value)1353 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_StrategyForBFramesEncoding(
1354     OH_AVScreenCapture_CaptureStrategy *strategy, bool value)
1355 {
1356     CHECK_AND_RETURN_RET_LOG(strategy != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input strategy is nullptr");
1357     struct ScreenCaptureStrategyObject *strategyObj = reinterpret_cast<ScreenCaptureStrategyObject *>(strategy);
1358     CHECK_AND_RETURN_RET_LOG(strategyObj != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "strategyObj is nullptr");
1359     strategyObj->strategy.enableBFrame = value;
1360     return AV_SCREEN_CAPTURE_ERR_OK;
1361 }
1362 
OH_AVScreenCapture_StrategyForPickerPopUp(OH_AVScreenCapture_CaptureStrategy * strategy,bool value)1363 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_StrategyForPickerPopUp(
1364     OH_AVScreenCapture_CaptureStrategy *strategy, bool value)
1365 {
1366     MEDIA_LOGD("OH_AVScreenCapture_StrategyForPickerPopUp S");
1367     CHECK_AND_RETURN_RET_LOG(strategy != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input strategy is nullptr!");
1368     auto *strategyObj = reinterpret_cast<ScreenCaptureStrategyObject *>(strategy);
1369     CHECK_AND_RETURN_RET_LOG(strategyObj != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "strategyObj is nullptr");
1370     strategyObj->strategy.pickerPopUp = value ? AVScreenCapturePickerPopUp::SCREEN_CAPTURE_PICKER_POPUP_ENABLE
1371         : AVScreenCapturePickerPopUp::SCREEN_CAPTURE_PICKER_POPUP_DISABLE;
1372     MEDIA_LOGD("OH_AVScreenCapture_StrategyForPickerPopUp E");
1373     return AV_SCREEN_CAPTURE_ERR_OK;
1374 }
1375 
OH_AVScreenCapture_StrategyForFillMode(OH_AVScreenCapture_CaptureStrategy * strategy,OH_AVScreenCapture_FillMode mode)1376 OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_StrategyForFillMode(
1377     OH_AVScreenCapture_CaptureStrategy *strategy, OH_AVScreenCapture_FillMode mode)
1378 {
1379     CHECK_AND_RETURN_RET_LOG(strategy != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input strategy is nullptr");
1380     CHECK_AND_RETURN_RET_LOG(mode == OH_AVScreenCapture_FillMode::OH_SCREENCAPTURE_FILLMODE_ASPECT_SCALE_FIT ||
1381                                  mode == OH_AVScreenCapture_FillMode::OH_SCREENCAPTURE_FILLMODE_SCALE_TO_FILL,
1382         AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input mode is invalid");
1383     struct ScreenCaptureStrategyObject *strategyObj = reinterpret_cast<ScreenCaptureStrategyObject *>(strategy);
1384     CHECK_AND_RETURN_RET_LOG(strategyObj != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "strategyObj is nullptr");
1385     strategyObj->strategy.fillMode = static_cast<AVScreenCaptureFillMode>(mode);
1386     return AV_SCREEN_CAPTURE_ERR_OK;
1387 }