Skip to content

Commit

Permalink
Camera is paused occasionally when torch is enabled
Browse files Browse the repository at this point in the history
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
  • Loading branch information
youennf committed Mar 28, 2024
1 parent fbe967b commit 7d9473d
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 @@ -145,6 +145,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 @@ -821,6 +821,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 @@ -917,6 +922,11 @@ static inline IntDegrees sensorOrientation(AVCaptureVideoOrientation videoOrient

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

beginConfigurationForConstraintsIfNeeded();

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

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

beginConfigurationForConstraintsIfNeeded();

if (!lockForConfiguration())
Expand Down Expand Up @@ -1163,6 +1178,25 @@ static inline IntDegrees sensorOrientation(AVCaptureVideoOrientation videoOrient
resolvePendingPhotoRequest(makeVector(data), "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 @@ -1172,6 +1206,8 @@ static inline IntDegrees sensorOrientation(AVCaptureVideoOrientation videoOrient

m_isRunning = state;

reconfigureIfNeeded();

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

0 comments on commit 7d9473d

Please sign in to comment.