Skip to content

Commit

Permalink
Cherry-pick 7d9473d. rdar://124434403
Browse files Browse the repository at this point in the history
    Camera is paused occasionally when torch is enabled
    https://bugs.webkit.org/show_bug.cgi?id=270831
    rdar://124434403

    Reviewed by Andy Estes.

    We do not want to update AV capture devices before the session is running, as it may trigger interruption.
    We now apply changes to device as soon as the session is running, but not before.
    We apply this to torch, white balance, resolution and frame rate.

    Whenever we want to update any of these, if the session is not running, we exit early and we remember to reconfigure.
    When the session resumes, we check whether we need to reconfigure and do so.

    * Source/WebCore/platform/mediastream/mac/AVVideoCaptureSource.h:
    * Source/WebCore/platform/mediastream/mac/AVVideoCaptureSource.mm:
    (WebCore::AVVideoCaptureSource::setSessionSizeFrameRateAndZoom):
    (WebCore::AVVideoCaptureSource::updateWhiteBalanceMode):
    (WebCore::AVVideoCaptureSource::updateTorch):
    (WebCore::AVVideoCaptureSource::reconfigure):
    (WebCore::AVVideoCaptureSource::captureSessionIsRunningDidChange):

    Canonical link: https://commits.webkit.org/276773@main

Canonical link: https://commits.webkit.org/272448.837@safari-7618-branch
  • Loading branch information
youennf authored and Dan Robson committed Mar 29, 2024
1 parent 2714ca9 commit 6bb527f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ class AVVideoCaptureSource : public RealtimeVideoCaptureSource, private Orientat
void updateWhiteBalanceMode();
void updateTorch();

void reconfigureIfNeeded();

void rejectPendingPhotoRequest(const String&);
void resolvePendingPhotoRequest(Vector<uint8_t>&&, const String&);
RetainPtr<AVCapturePhotoSettings> photoConfiguration(const PhotoSettings&);
Expand Down Expand Up @@ -180,6 +182,9 @@ class AVVideoCaptureSource : public RealtimeVideoCaptureSource, private Orientat
bool m_interrupted { false };
bool m_isRunning { false };
bool m_hasBegunConfigurationForConstraints { false };
bool m_needsResolutionReconfiguration { false };
bool m_needsTorchReconfiguration { false };
bool m_needsWhiteBalanceReconfiguration { false };

static constexpr Seconds verifyCaptureInterval = 30_s;
static const uint64_t framesToDropWhenStarting = 4;
Expand Down
36 changes: 36 additions & 0 deletions Source/WebCore/platform/mediastream/mac/AVVideoCaptureSource.mm
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,11 @@ static bool isSameFrameRateRange(AVFrameRateRange* a, AVFrameRateRange* b)

ASSERT(m_currentPreset->format());

if (!m_isRunning) {
m_needsResolutionReconfiguration = true;
return;
}

if (!lockForConfiguration())
return;

Expand Down Expand Up @@ -908,6 +913,11 @@ static inline IntDegrees sensorOrientation(AVCaptureVideoOrientation videoOrient

void AVVideoCaptureSource::updateWhiteBalanceMode()
{
if (!m_isRunning) {
m_needsWhiteBalanceReconfiguration = true;
return;
}

beginConfigurationForConstraintsIfNeeded();

if (!lockForConfiguration())
Expand All @@ -925,6 +935,11 @@ static inline IntDegrees sensorOrientation(AVCaptureVideoOrientation videoOrient

void AVVideoCaptureSource::updateTorch()
{
if (!m_isRunning) {
m_needsTorchReconfiguration = true;
return;
}

beginConfigurationForConstraintsIfNeeded();

if (!lockForConfiguration())
Expand Down Expand Up @@ -1154,6 +1169,25 @@ static inline IntDegrees sensorOrientation(AVCaptureVideoOrientation videoOrient
resolvePendingPhotoRequest({ static_cast<const uint8_t*>(data.bytes), data.length }, "image/jpeg"_s);
}

void AVVideoCaptureSource::reconfigureIfNeeded()
{
if (!m_isRunning || (!m_needsResolutionReconfiguration && !m_needsTorchReconfiguration && !m_needsWhiteBalanceReconfiguration))
return;

beginConfiguration();

if (std::exchange(m_needsResolutionReconfiguration, false))
setSessionSizeFrameRateAndZoom();

if (std::exchange(m_needsTorchReconfiguration, false))
updateTorch();

if (std::exchange(m_needsWhiteBalanceReconfiguration, false))
updateWhiteBalanceMode();

commitConfiguration();
}

void AVVideoCaptureSource::captureSessionIsRunningDidChange(bool state)
{
scheduleDeferredTask([this, logIdentifier = LOGIDENTIFIER, state] {
Expand All @@ -1163,6 +1197,8 @@ static inline IntDegrees sensorOrientation(AVCaptureVideoOrientation videoOrient

m_isRunning = state;

reconfigureIfNeeded();

updateVerifyCapturingTimer();
notifyMutedChange(!m_isRunning);
});
Expand Down

0 comments on commit 6bb527f

Please sign in to comment.