Skip to content

Commit

Permalink
[ImageCapture] Commit configuration changes after changing/restoring …
Browse files Browse the repository at this point in the history
…settings for image capture

https://bugs.webkit.org/show_bug.cgi?id=267580
rdar://121045984

Reviewed by Youenn Fablet.

AVCapture sometimes throws an exception if an AVCaptureSession is started while it is being
configured, so ensure that session configuration is ended explicitly after setting up or
restoring session configuration for image capture.

* Source/WebCore/platform/mediastream/RealtimeVideoCaptureSource.cpp:
(WebCore::RealtimeVideoCaptureSource::takePhoto): Call startApplyingConstraints and
endApplyingConstraints around setting and restoring configuration.

* Source/WebCore/platform/mediastream/mac/AVVideoCaptureSource.mm:
(WebCore::AVVideoCaptureSource::rejectPendingPhotoRequest): Drive-by: don't log if the
photo producer doesn't exist.
(WebCore::AVVideoCaptureSource::takePhotoInternal): Reject the promise if there are no
settings.

* Source/WebCore/platform/mock/MockRealtimeVideoSource.cpp:
(WebCore::MockRealtimeVideoSource::setFrameRateAndZoomWithPreset): Assert if
`m_beingConfigured` is  not set.
(WebCore::MockRealtimeVideoSource::startProducingData): Ditto.
(WebCore::MockRealtimeVideoSource::startApplyingConstraints): Set `m_beingConfigured`.
(WebCore::MockRealtimeVideoSource::endApplyingConstraints): Clear `m_beingConfigured`.
* Source/WebCore/platform/mock/MockRealtimeVideoSource.h:

Canonical link: https://commits.webkit.org/273091@main
  • Loading branch information
eric-carlson committed Jan 16, 2024
1 parent 15ebca4 commit 519c837
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2023 Apple Inc. All rights reserved.
* Copyright (C) 2018-2024 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
Expand Down Expand Up @@ -426,10 +426,13 @@ void RealtimeVideoCaptureSource::setSizeFrameRateAndZoom(std::optional<int> widt

m_currentPreset = match->encodingPreset;
auto newSize = match->encodingPreset->size();

startApplyingConstraints();
setFrameRateAndZoomWithPreset(match->requestedFrameRate, match->requestedZoom, WTFMove(match->encodingPreset));
setSize(newSize);
setFrameRate(match->requestedFrameRate);
setZoom(match->requestedZoom);
endApplyingConstraints();
}

auto RealtimeVideoCaptureSource::takePhotoInternal(PhotoSettings&&) -> Ref<TakePhotoNativePromise>
Expand Down Expand Up @@ -489,8 +492,10 @@ auto RealtimeVideoCaptureSource::takePhoto(PhotoSettings&& photoSettings) -> Ref

m_currentPreset = newPresetForPhoto->encodingPreset;
auto newSize = newPresetForPhoto->encodingPreset->size();
startApplyingConstraints();
setFrameRateAndZoomWithPreset(newPresetForPhoto->requestedFrameRate, newPresetForPhoto->requestedZoom, WTFMove(newPresetForPhoto->encodingPreset));
setSize(newSize);
endApplyingConstraints();
}

return takePhotoInternal(WTFMove(photoSettings))->whenSettled(RunLoop::main(), [this, protectedThis = Ref { *this }, configurationToRestore = WTFMove(configurationToRestore)] (auto&& result) mutable {
Expand All @@ -500,16 +505,17 @@ auto RealtimeVideoCaptureSource::takePhoto(PhotoSettings&& photoSettings) -> Ref
if (configurationToRestore) {
m_currentPreset = configurationToRestore->encodingPreset;
auto newSize = configurationToRestore->encodingPreset->size();
startApplyingConstraints();
setFrameRateAndZoomWithPreset(configurationToRestore->requestedFrameRate, configurationToRestore->requestedZoom, WTFMove(configurationToRestore->encodingPreset));
setSize(newSize);
endApplyingConstraints();

if (m_mutedForPhotoCapture) {
m_mutedForPhotoCapture = false;
setMuted(false);
}
}

// FIXME: Resize image if preset size doesn't match requested size.

return TakePhotoNativePromise::createAndSettle(WTFMove(result));
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2013-2023 Apple Inc. All rights reserved.
* Copyright (C) 2013-2024 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
Expand Down Expand Up @@ -558,11 +558,10 @@ static bool isZoomSupported(const Vector<VideoPreset>& presets)
{
Locker lock { m_photoLock };

if (!m_photoProducer) {
ERROR_LOG_IF(loggerPtr(), LOGIDENTIFIER, "no photo producer");
if (!m_photoProducer)
return;
}

ERROR_LOG_IF(loggerPtr(), LOGIDENTIFIER, error);
m_photoProducer->reject(error);
m_photoProducer = nullptr;
}
Expand Down Expand Up @@ -654,7 +653,7 @@ static bool isZoomSupported(const Vector<VideoPreset>& presets)
}

auto avPhotoSettings = photoConfiguration(photoSettings);
if (avPhotoSettings) {
if (!avPhotoSettings) {
ERROR_LOG_IF(loggerPtr(), LOGIDENTIFIER, "photoConfiguration() failed");
return TakePhotoNativePromise::createAndReject("Internal error"_s);
}
Expand Down
12 changes: 12 additions & 0 deletions Source/WebCore/platform/mock/MockRealtimeVideoSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ const RealtimeMediaSourceSettings& MockRealtimeVideoSource::settings()
void MockRealtimeVideoSource::setFrameRateAndZoomWithPreset(double frameRate, double zoom, std::optional<VideoPreset>&& preset)
{
UNUSED_PARAM(zoom);
ASSERT(m_beingConfigured);
m_preset = WTFMove(preset);
if (m_preset)
setIntrinsicSize(m_preset->size());
Expand Down Expand Up @@ -416,6 +417,7 @@ void MockRealtimeVideoSource::startCaptureTimer()

void MockRealtimeVideoSource::startProducingData()
{
ASSERT(!m_beingConfigured);
startCaptureTimer();
m_startTime = MonotonicTime::now();
}
Expand Down Expand Up @@ -727,6 +729,16 @@ void MockRealtimeVideoSource::setIsInterrupted(bool isInterrupted)
}
}

void MockRealtimeVideoSource::startApplyingConstraints()
{
m_beingConfigured = true;
}

void MockRealtimeVideoSource::endApplyingConstraints()
{
m_beingConfigured = false;
}

} // namespace WebCore

#endif // ENABLE(MEDIA_STREAM)
4 changes: 4 additions & 0 deletions Source/WebCore/platform/mock/MockRealtimeVideoSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ class MockRealtimeVideoSource : public RealtimeVideoCaptureSource, private Orien
bool mockWindow() const { return mockDisplayType(CaptureDevice::DeviceType::Window); }
bool mockDisplayType(CaptureDevice::DeviceType) const;

void startApplyingConstraints() final;
void endApplyingConstraints() final;

class DrawingState {
public:
explicit DrawingState(float baseFontSize)
Expand Down Expand Up @@ -167,6 +170,7 @@ class MockRealtimeVideoSource : public RealtimeVideoCaptureSource, private Orien
Lock m_imageBufferLock;
std::optional<PhotoCapabilities> m_photoCapabilities;
std::optional<PhotoSettings> m_photoSettings;
bool m_beingConfigured { false };
};

} // namespace WebCore
Expand Down

0 comments on commit 519c837

Please sign in to comment.