diff --git a/frameworks/native/capi/screencapture/native_avscreen_capture.cpp b/frameworks/native/capi/screencapture/native_avscreen_capture.cpp index cb5cdfe762ecb576020fd06bf6996704abef49d9..eaf888bd0f8761731bf002888cf5bc1392f7bc7d 100644 --- a/frameworks/native/capi/screencapture/native_avscreen_capture.cpp +++ b/frameworks/native/capi/screencapture/native_avscreen_capture.cpp @@ -144,6 +144,7 @@ private: CHECK_AND_RETURN_RET_LOG(screenCaptureObj->screenCapture_ != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "screenCapture is null"); + MEDIA_LOGD("OnProcessAudioBuffer() is called, bufferType %{public}d", bufferType); AudioCaptureSourceType audioSourceType; if (bufferType == OH_AVScreenCaptureBufferType::OH_SCREEN_CAPTURE_BUFFERTYPE_AUDIO_INNER) { audioSourceType = AudioCaptureSourceType::ALL_PLAYBACK; @@ -342,11 +343,11 @@ public: private: std::shared_mutex mutex_; - struct OH_AVScreenCapture *capture_; + struct OH_AVScreenCapture *capture_ = nullptr; struct OH_AVScreenCaptureCallback callback_; - std::shared_ptr stateChangeCallback_; - std::shared_ptr errorCallback_; - std::shared_ptr dataCallback_; + std::shared_ptr stateChangeCallback_ = nullptr; + std::shared_ptr errorCallback_ = nullptr; + std::shared_ptr dataCallback_ = nullptr; }; struct ScreenCaptureContentFilterObject : public OH_AVScreenCapture_ContentFilter { @@ -730,11 +731,17 @@ OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_SetMicrophoneEnabled(struct OH_AV static OH_AVSCREEN_CAPTURE_ErrCode AVScreenCaptureSetCallback(struct OH_AVScreenCapture *capture, struct ScreenCaptureObject *screenCaptureObj) { + MEDIA_LOGD("AVScreenCaptureSetCallback S"); CHECK_AND_RETURN_RET_LOG(capture != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input capture is nullptr!"); CHECK_AND_RETURN_RET_LOG(screenCaptureObj != nullptr && screenCaptureObj->screenCapture_ != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "screenCapture_ is nullptr!"); if (screenCaptureObj->callback_ == nullptr) { - OH_AVScreenCaptureCallback dummyCallback = { nullptr, nullptr, nullptr }; + MEDIA_LOGD("AVScreenCaptureSetCallback new NativeScreenCaptureCallback"); + OH_AVScreenCaptureCallback dummyCallback = { + .onError = nullptr, + .onAudioBufferAvailable = nullptr, + .onVideoBufferAvailable = nullptr + }; screenCaptureObj->callback_ = std::make_shared(capture, dummyCallback); CHECK_AND_RETURN_RET_LOG(screenCaptureObj->callback_ != nullptr, AV_SCREEN_CAPTURE_ERR_NO_MEMORY, "callback_ is nullptr!"); @@ -743,12 +750,14 @@ static OH_AVSCREEN_CAPTURE_ErrCode AVScreenCaptureSetCallback(struct OH_AVScreen CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT, "SetScreenCaptureCallback failed!"); } + MEDIA_LOGD("AVScreenCaptureSetCallback E"); return AV_SCREEN_CAPTURE_ERR_OK; } OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_SetStateCallback(struct OH_AVScreenCapture *capture, OH_AVScreenCapture_OnStateChange callback, void *userData) { + MEDIA_LOGD("OH_AVScreenCapture_SetStateCallback S"); CHECK_AND_RETURN_RET_LOG(capture != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input capture is nullptr!"); CHECK_AND_RETURN_RET_LOG(callback != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input stateCallback is nullptr!"); struct ScreenCaptureObject *screenCaptureObj = reinterpret_cast(capture); @@ -760,14 +769,17 @@ OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_SetStateCallback(struct OH_AVScre if (screenCaptureObj->callback_ == nullptr || !screenCaptureObj->callback_->SetStateChangeCallback(callback, userData)) { + MEDIA_LOGE("OH_AVScreenCapture_SetStateCallback error"); return AV_SCREEN_CAPTURE_ERR_NO_MEMORY; } + MEDIA_LOGD("OH_AVScreenCapture_SetStateCallback E"); return AV_SCREEN_CAPTURE_ERR_OK; } OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_SetErrorCallback(struct OH_AVScreenCapture *capture, OH_AVScreenCapture_OnError callback, void *userData) { + MEDIA_LOGD("OH_AVScreenCapture_SetErrorCallback S"); CHECK_AND_RETURN_RET_LOG(capture != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input capture is nullptr!"); CHECK_AND_RETURN_RET_LOG(callback != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input errorCallback is nullptr!"); struct ScreenCaptureObject *screenCaptureObj = reinterpret_cast(capture); @@ -778,14 +790,17 @@ OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_SetErrorCallback(struct OH_AVScre CHECK_AND_RETURN_RET_LOG(errCode == AV_SCREEN_CAPTURE_ERR_OK, errCode, "SetErrorCallback is null"); if (screenCaptureObj->callback_ == nullptr || !screenCaptureObj->callback_->SetErrorCallback(callback, userData)) { + MEDIA_LOGE("OH_AVScreenCapture_SetErrorCallback error"); return AV_SCREEN_CAPTURE_ERR_NO_MEMORY; } + MEDIA_LOGD("OH_AVScreenCapture_SetErrorCallback E"); return AV_SCREEN_CAPTURE_ERR_OK; } OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_SetDataCallback(struct OH_AVScreenCapture *capture, OH_AVScreenCapture_OnBufferAvailable callback, void *userData) { + MEDIA_LOGD("OH_AVScreenCapture_SetDataCallback E"); CHECK_AND_RETURN_RET_LOG(capture != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input capture is nullptr!"); CHECK_AND_RETURN_RET_LOG(callback != nullptr, AV_SCREEN_CAPTURE_ERR_INVALID_VAL, "input dataCallback is nullptr!"); struct ScreenCaptureObject *screenCaptureObj = reinterpret_cast(capture); @@ -797,8 +812,10 @@ OH_AVSCREEN_CAPTURE_ErrCode OH_AVScreenCapture_SetDataCallback(struct OH_AVScree if (screenCaptureObj->callback_ == nullptr || !screenCaptureObj->callback_->SetDataCallback(callback, userData)) { + MEDIA_LOGE("OH_AVScreenCapture_SetErrorCallback error"); return AV_SCREEN_CAPTURE_ERR_NO_MEMORY; } + MEDIA_LOGD("OH_AVScreenCapture_SetDataCallback E"); return AV_SCREEN_CAPTURE_ERR_OK; } diff --git a/interfaces/inner_api/native/screen_capture.h b/interfaces/inner_api/native/screen_capture.h index 480e6cb039630584e15fdaeb36995d4e6a6c3b7a..dcebbcf414fa3f5ed7621093039b9f87add27021 100644 --- a/interfaces/inner_api/native/screen_capture.h +++ b/interfaces/inner_api/native/screen_capture.h @@ -114,6 +114,8 @@ enum AVScreenCaptureStateCode { }; enum AVScreenCaptureBufferType { + /* Buffer of video data from screen */ + SCREEN_CAPTURE_BUFFERTYPE_INVALID = -1, /* Buffer of video data from screen */ SCREEN_CAPTURE_BUFFERTYPE_VIDEO = 0, /* Buffer of audio data from inner capture */ diff --git a/services/services/screen_capture/ipc/screen_capture_listener_stub.cpp b/services/services/screen_capture/ipc/screen_capture_listener_stub.cpp index 65d89fb1d01f4d24d94a2611f3d30050de827054..f007f2d19359198a1388803e8dd6b885592d7245 100644 --- a/services/services/screen_capture/ipc/screen_capture_listener_stub.cpp +++ b/services/services/screen_capture/ipc/screen_capture_listener_stub.cpp @@ -36,6 +36,7 @@ ScreenCaptureListenerStub::~ScreenCaptureListenerStub() int ScreenCaptureListenerStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) { + MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances OnRemoteRequest code:%{public}u", FAKE_POINTER(this), code); auto remoteDescriptor = data.ReadInterfaceToken(); if (ScreenCaptureListenerStub::GetDescriptor() != remoteDescriptor) { MEDIA_LOGE("Invalid descriptor"); diff --git a/services/services/screen_capture/server/audio_capturer_wrapper.cpp b/services/services/screen_capture/server/audio_capturer_wrapper.cpp index d391cd966d27297f30ffadb3c986692c596715c4..9afc416732960c732eded10c28829aa8075a5726 100644 --- a/services/services/screen_capture/server/audio_capturer_wrapper.cpp +++ b/services/services/screen_capture/server/audio_capturer_wrapper.cpp @@ -19,26 +19,26 @@ #include "media_errors.h" namespace { -constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "ScreenCaptureServer"}; +constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "SC_AudioCapturerWrapper"}; } namespace OHOS { namespace Media { void AudioCapturerCallbackImpl::OnInterrupt(const InterruptEvent &interruptEvent) { - MEDIA_LOGI("OnInterrupt hintType:%{public}d, eventType:%{public}d, forceType:%{public}d", + MEDIA_LOGI("AudioCapturerCallbackImpl OnInterrupt hintType:%{public}d, eventType:%{public}d, forceType:%{public}d", interruptEvent.hintType, interruptEvent.eventType, interruptEvent.forceType); } void AudioCapturerCallbackImpl::OnStateChange(const CapturerState state) { - MEDIA_LOGI("OnStateChange state:%{public}d", state); + MEDIA_LOGI("AudioCapturerCallbackImpl OnStateChange state:%{public}d", state); switch (state) { case CAPTURER_PREPARED: - MEDIA_LOGD("OnStateChange CAPTURER_PREPARED"); + MEDIA_LOGD("AudioCapturerCallbackImpl OnStateChange CAPTURER_PREPARED"); break; default: - MEDIA_LOGD("OnStateChange NOT A VALID state"); + MEDIA_LOGD("AudioCapturerCallbackImpl OnStateChange NOT A VALID state"); break; } } @@ -60,8 +60,8 @@ int32_t AudioCapturerWrapper::Start(const OHOS::AudioStandard::AppInfo &appInfo) OnStartFailed(ScreenCaptureErrorType::SCREEN_CAPTURE_ERROR_INTERNAL, SCREEN_CAPTURE_ERR_UNKNOWN); return MSERR_UNKNOWN; } + MEDIA_LOGI("0x%{public}06" PRIXPTR "Start success, threadName:%{public}s", FAKE_POINTER(this), threadName_.c_str()); - MEDIA_LOGI("Start success, threadName:%{public}s", threadName_.c_str()); isRunning_.store(true); readAudioLoop_ = std::make_unique(&AudioCapturerWrapper::CaptureAudio, this); audioCapturer_ = audioCapturer; @@ -71,6 +71,7 @@ int32_t AudioCapturerWrapper::Start(const OHOS::AudioStandard::AppInfo &appInfo) int32_t AudioCapturerWrapper::Stop() { std::lock_guard lock(mutex_); + MEDIA_LOGI("0x%{public}06" PRIXPTR " Stop S, threadName:%{public}s", FAKE_POINTER(this), threadName_.c_str()); if (isRunning_.load()) { isRunning_.store(false); if (readAudioLoop_ != nullptr && readAudioLoop_->joinable()) { @@ -86,6 +87,7 @@ int32_t AudioCapturerWrapper::Stop() } std::unique_lock bufferLock(bufferMutex_); + MEDIA_LOGD("0x%{public}06" PRIXPTR " Stop pop, threadName:%{public}s", FAKE_POINTER(this), threadName_.c_str()); while (!availBuffers_.empty()) { if (availBuffers_.front() != nullptr) { free(availBuffers_.front()->buffer); @@ -93,11 +95,13 @@ int32_t AudioCapturerWrapper::Stop() } availBuffers_.pop(); } + MEDIA_LOGI("0x%{public}06" PRIXPTR " Stop E, threadName:%{public}s", FAKE_POINTER(this), threadName_.c_str()); return MSERR_OK; } void AudioCapturerWrapper::SetIsMuted(bool isMuted) { + MEDIA_LOGI("0x%{public}06" PRIXPTR " SetIsMuted isMuted:%{public}d", FAKE_POINTER(this), isMuted); isMuted_.store(isMuted); } @@ -130,7 +134,7 @@ std::shared_ptr AudioCapturerWrapper::CreateAudioCapturer(const O int32_t AudioCapturerWrapper::CaptureAudio() { - MEDIA_LOGI("CaptureAudio start, threadName:%{public}s", threadName_.c_str()); + MEDIA_LOGI("0x%{public}06" PRIXPTR " CaptureAudio S, name:%{public}s", FAKE_POINTER(this), threadName_.c_str()); std::string name = threadName_.substr(0, std::min(threadName_.size(), static_cast(MAX_THREAD_NAME_LENGTH))); pthread_setname_np(pthread_self(), name.c_str()); @@ -166,6 +170,7 @@ int32_t AudioCapturerWrapper::CaptureAudio() CHECK_AND_RETURN_RET_LOG(screenCaptureCb_ != nullptr, MSERR_OK, "no consumer, will drop audio frame"); screenCaptureCb_->OnAudioBufferAvailable(true, audioInfo_.audioSource); } + MEDIA_LOGI("0x%{public}06" PRIXPTR " CaptureAudio E, name:%{public}s", FAKE_POINTER(this), threadName_.c_str()); return MSERR_OK; } @@ -174,6 +179,7 @@ int32_t AudioCapturerWrapper::AcquireAudioBuffer(std::shared_ptr &a using namespace std::chrono_literals; std::unique_lock lock(bufferMutex_); CHECK_AND_RETURN_RET_LOG(isRunning_.load(), MSERR_UNKNOWN, "AcquireAudioBuffer failed, not running"); + MEDIA_LOGD("0x%{public}06" PRIXPTR " Acquire Buffer S, name:%{public}s", FAKE_POINTER(this), threadName_.c_str()); if (!bufferCond_.wait_for( lock, std::chrono::milliseconds(OPERATION_TIMEOUT_IN_MS), [this] { return !availBuffers_.empty(); })) { @@ -181,6 +187,7 @@ int32_t AudioCapturerWrapper::AcquireAudioBuffer(std::shared_ptr &a return MSERR_UNKNOWN; } audioBuffer = availBuffers_.front(); + MEDIA_LOGD("0x%{public}06" PRIXPTR " Acquire Buffer E, name:%{public}s", FAKE_POINTER(this), threadName_.c_str()); return MSERR_OK; } @@ -188,9 +195,11 @@ int32_t AudioCapturerWrapper::ReleaseAudioBuffer() { using namespace std::chrono_literals; std::unique_lock lock(bufferMutex_); + MEDIA_LOGD("0x%{public}06" PRIXPTR " Release Buffer S, name:%{public}s", FAKE_POINTER(this), threadName_.c_str()); CHECK_AND_RETURN_RET_LOG(isRunning_.load(), MSERR_UNKNOWN, "ReleaseAudioBuffer failed, not running"); CHECK_AND_RETURN_RET_LOG(!availBuffers_.empty(), MSERR_UNKNOWN, "ReleaseAudioBuffer failed, no frame to release"); availBuffers_.pop(); + MEDIA_LOGD("0x%{public}06" PRIXPTR " Release Buffer E, name:%{public}s", FAKE_POINTER(this), threadName_.c_str()); return MSERR_OK; } diff --git a/services/services/screen_capture/server/screen_capture_server.cpp b/services/services/screen_capture/server/screen_capture_server.cpp index f7804a5629c42c5303fe7f0fee54ea77d4fd7a72..5a8421acabb7b4395b68adbc8f4cc67a226e2352 100644 --- a/services/services/screen_capture/server/screen_capture_server.cpp +++ b/services/services/screen_capture/server/screen_capture_server.cpp @@ -28,15 +28,28 @@ #include "screen_capture_listener_proxy.h" using OHOS::Rosen::DMError; + +namespace OHOS { +namespace Media { namespace { constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "ScreenCaptureServer"}; -static std::map> serverMap; +static std::map> serverMap; std::atomic activeSessionId(-1); + +// As in the ScreenCaptureServer destructor function mutexGlobal is required to update serverMap +// MAKE SURE THAT when mutexGlobal is been holding MUST NOT trigger ScreenCaptureServer destructor to be called std::mutex mutexGlobal; + +std::shared_ptr GetScreenCaptureServerByIdWithLock(int32_t id) +{ + auto iter = serverMap.find(id); + if (iter == serverMap.end()) { + return nullptr; + } + return (iter->second).lock(); +} } -namespace OHOS { -namespace Media { static const std::string MP4 = "mp4"; static const std::string M4A = "m4a"; @@ -57,6 +70,7 @@ static const int32_t MICROPHONE_OFF = 0; static const int32_t MICROPHONE_STATE_COUNT = 2; static const int32_t MAX_SESSION_ID = 256; +static const int32_t MAX_SESSION_PER_UID = 8; static const auto NOTIFICATION_SUBSCRIBER = NotificationSubscriber(); void NotificationSubscriber::OnConnected() @@ -72,15 +86,27 @@ void NotificationSubscriber::OnDisconnected() void NotificationSubscriber::OnResponse(int32_t notificationId, OHOS::sptr buttonOption) { - MEDIA_LOGI("NotificationSubscriber OnResponse notificationId : %{public}d ", notificationId); - MEDIA_LOGI("NotificationSubscriber OnResponse ButtonName : %{public}s ", (buttonOption->GetButtonName()).c_str()); + MEDIA_LOGI("NotificationSubscriber OnResponse notificationId : %{public}d, ButtonName : %{public}s ", + notificationId, (buttonOption->GetButtonName()).c_str()); + + // To avoid deadlock: first release mutexGlobal, then be destructed + std::shared_ptr server; + { + std::unique_lock lock(mutexGlobal); + server = GetScreenCaptureServerByIdWithLock(notificationId); + } + if (server == nullptr) { + MEDIA_LOGW("OnResponse ScreenCaptureServer not exist, notificationId : %{public}d, ButtonName : %{public}s ", + notificationId, (buttonOption->GetButtonName()).c_str()); + return; + } if (BUTTON_NAME_STOP.compare(buttonOption->GetButtonName()) == 0) { - std::shared_ptr server = serverMap.at(notificationId); server->StopScreenCaptureByEvent(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_STOPPED_BY_USER); + return; } if (BUTTON_NAME_MIC.compare(buttonOption->GetButtonName()) == 0) { - std::shared_ptr server = serverMap.at(notificationId); server->UpdateMicrophoneEnabled(); + return; } } @@ -93,57 +119,87 @@ std::shared_ptr ScreenCaptureServer::Create() { std::shared_ptr serverTemp = std::make_shared(); CHECK_AND_RETURN_RET_LOG(serverTemp != nullptr, nullptr, "Failed to new ScreenCaptureServer"); - + int32_t countForUid = 0; int32_t newSessionId = 0; + + // To avoid deadlock: first release mutexGlobal, then be destructed + std::shared_ptr server; for (int32_t i = 0; i < MAX_SESSION_ID; i++) { - auto it = serverMap.find(newSessionId); - if (it != serverMap.end()) { + // To avoid deadlock: first release mutexGlobal, then be destructed + { + std::unique_lock lock(mutexGlobal); + server = GetScreenCaptureServerByIdWithLock(newSessionId); + } + if (server != nullptr) { newSessionId++; + countForUid += (server->appInfo_.appUid == serverTemp->appInfo_.appUid) ? 1 : 0; + // To avoid deadlock: first release mutexGlobal, then be destructed + // Do this without holding mutexGlobal to avoid dead lock when set nullptr trigger server destruct + server = nullptr; continue; } - serverMap.insert(std::make_pair(newSessionId, serverTemp)); + if (countForUid >= MAX_SESSION_PER_UID) { + MEDIA_LOGE("Create failed, uid(%{public}d) has created too many ScreenCaptureServer instances", + serverTemp->appInfo_.appUid); + return nullptr; + } + { + std::unique_lock lock(mutexGlobal); + serverMap.insert(std::make_pair(newSessionId, serverTemp)); + MEDIA_LOGI("ScreenCaptureServer::Create newSessionId: %{public}d, serverMap size:%{public}zu", + newSessionId, serverMap.size()); + } break; } - MEDIA_LOGI("ScreenCaptureServer::Create newSessionId: %{public}d", newSessionId); - MEDIA_LOGI("ScreenCaptureServer::Create serverMap size : %{public}s", - std::to_string(serverMap.size()).c_str()); + + if (newSessionId == MAX_SESSION_ID) { + MEDIA_LOGE("Create failed for uid(%{public}d), there are too many ScreenCaptureServer instances", + serverTemp->appInfo_.appUid); + return nullptr; + } serverTemp->SetSessionId(newSessionId); - std::shared_ptr server = - std::static_pointer_cast(serverTemp); - return server; + return std::static_pointer_cast(serverTemp); } int32_t ScreenCaptureServer::ReportAVScreenCaptureUserChoice(int32_t sessionId, std::string choice) { - std::lock_guard lock(mutexGlobal); - MEDIA_LOGI("ScreenCaptureServer::ReportAVScreenCaptureUserChoice user sessionId is : %{public}d", sessionId); - MEDIA_LOGI("ScreenCaptureServer::ReportAVScreenCaptureUserChoice user choice is : %{public}s", choice.c_str()); + MEDIA_LOGI("ReportAVScreenCaptureUserChoice sessionId: %{public}d, choice: %{public}s", sessionId, choice.c_str()); + + // To avoid deadlock: first release mutexGlobal, then be destructed std::shared_ptr server; - auto it = serverMap.find(sessionId); - if (it != serverMap.end() && it->second != nullptr) { - server = it->second; - } else { - MEDIA_LOGI("ScreenCaptureServer::ReportAVScreenCaptureUserChoice Failed to get report ScreenCaptureServer"); + { + std::lock_guard lock(mutexGlobal); + server = GetScreenCaptureServerByIdWithLock(sessionId); + } + if (server == nullptr) { + MEDIA_LOGE("ReportAVScreenCaptureUserChoice failed to get instance, sessionId: %{public}d", sessionId); return MSERR_UNKNOWN; } + // To avoid deadlock: first release mutexGlobal, then be destructed + std::shared_ptr currentServer; if (USER_CHOICE_ALLOW.compare(choice) == 0) { + std::lock_guard lock(mutexGlobal); if (activeSessionId.load() >= 0) { - std::shared_ptr currentServer = serverMap.at(activeSessionId.load()); - currentServer->StopScreenCaptureByEvent( - AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_INTERRUPTED_BY_OTHER); + currentServer = GetScreenCaptureServerByIdWithLock(activeSessionId.load()); + if (currentServer != nullptr) { + MEDIA_LOGW("ReportAVScreenCaptureUserChoice uid(%{public}d) is interrupted by uid(%{public}d)", + currentServer->appInfo_.appUid, server->appInfo_.appUid); + currentServer->StopScreenCaptureByEvent( + AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_INTERRUPTED_BY_OTHER); + } } activeSessionId.store(SESSION_ID_INVALID); int32_t ret = server->OnReceiveUserPrivacyAuthority(true); CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "ReportAVScreenCaptureUserChoice user choice is true but start failed"); activeSessionId.store(sessionId); - MEDIA_LOGI("ScreenCaptureServer::ReportAVScreenCaptureUserChoice user choice is true and start success"); + MEDIA_LOGI("ReportAVScreenCaptureUserChoice user choice is true and start success"); return MSERR_OK; } else if (USER_CHOICE_DENY.compare(choice) == 0) { return server->OnReceiveUserPrivacyAuthority(false); } - MEDIA_LOGI("ScreenCaptureServer::ReportAVScreenCaptureUserChoice user choice is not support"); + MEDIA_LOGW("ReportAVScreenCaptureUserChoice user choice is not support"); return MSERR_UNKNOWN; } @@ -156,8 +212,7 @@ ScreenCaptureServer::ScreenCaptureServer() ScreenCaptureServer::~ScreenCaptureServer() { MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this)); - std::lock_guard lock(mutex_); - StopScreenCaptureInner(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_INVLID); + Release(); } void ScreenCaptureServer::SetSessionId(int32_t sessionId) @@ -705,6 +760,7 @@ void ScreenCaptureServer::PostStartScreenCapture(bool isSuccess) } BehaviorEventWriteForScreenCapture("start", "AVScreenCapture", appInfo_.appUid, appInfo_.appPid); captureState_ = AVScreenCaptureState::STARTED; + screenCaptureCb_->OnStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_STARTED); } else { MEDIA_LOGE("PostStartScreenCapture handle failure"); isPrivacyAuthorityEnabled_ = false; @@ -1083,7 +1139,7 @@ int32_t ScreenCaptureServer::StartHomeVideoCapture() std::string virtualScreenName = "screen_capture"; if (isSurfaceMode_) { int32_t ret = CreateVirtualScreen(virtualScreenName, surface_); - CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "create virtual screen with input surface failed"); + CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "create virtual screen with input surface failed"); return MSERR_OK; } @@ -1105,7 +1161,7 @@ int32_t ScreenCaptureServer::StartHomeVideoCapture() CHECK_AND_RETURN_RET_LOG(surfaceCb_ != nullptr, MSERR_UNKNOWN, "MakeSptr surfaceCb_ failed"); consumer_->RegisterConsumerListener(surfaceCb_); int32_t ret = CreateVirtualScreen(virtualScreenName, producerSurface); - CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "CreateVirtualScreen failed"); + CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "create virtual screen without input surface failed"); CANCEL_SCOPE_EXIT_GUARD(0); return MSERR_OK; @@ -1464,16 +1520,18 @@ int32_t ScreenCaptureServer::StopScreenCaptureInner(AVScreenCaptureStateCode sta int32_t ScreenCaptureServer::StopScreenCapture() { MediaTrace trace("ScreenCaptureServer::StopScreenCapture"); - MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances StopScreenCapture", FAKE_POINTER(this)); + MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances StopScreenCapture S", FAKE_POINTER(this)); std::lock_guard lock(mutex_); - return StopScreenCaptureInner(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_INVLID); + int32_t ret = StopScreenCaptureInner(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_INVLID); + MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances StopScreenCapture E", FAKE_POINTER(this)); + return ret; } void ScreenCaptureServer::Release() { MediaTrace trace("ScreenCaptureServer::Release"); - MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances Release", FAKE_POINTER(this)); + MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances Release S", FAKE_POINTER(this)); int32_t sessionId; { std::lock_guard lock(mutex_); @@ -1487,6 +1545,7 @@ void ScreenCaptureServer::Release() std::lock_guard lock(mutex_); StopScreenCaptureInner(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_INVLID); + MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances Release E", FAKE_POINTER(this)); } void ScreenCapBufferConsumerListener::OnBufferAvailable() diff --git a/test/unittest/resources/ohos_test.xml b/test/unittest/resources/ohos_test.xml index 5f0bfd55d48422f2d321718dd6f2dacf0e52e122..4a77f635f21cec80cf95776ee8eb6523b53aa560 100644 --- a/test/unittest/resources/ohos_test.xml +++ b/test/unittest/resources/ohos_test.xml @@ -111,6 +111,30 @@