[iOS] Unset active media capture source when stopped capturing
[WebKit-https.git] / Source / WebCore / platform / mediastream / mac / AVVideoCaptureSource.mm
1 /*
2  * Copyright (C) 2013-2017 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #import "config.h"
27 #import "AVVideoCaptureSource.h"
28
29 #if ENABLE(MEDIA_STREAM) && USE(AVFOUNDATION)
30
31 #import "AVCaptureDeviceManager.h"
32 #import "GraphicsContextCG.h"
33 #import "ImageBuffer.h"
34 #import "IntRect.h"
35 #import "Logging.h"
36 #import "MediaConstraints.h"
37 #import "MediaSampleAVFObjC.h"
38 #import "NotImplemented.h"
39 #import "PixelBufferConformerCV.h"
40 #import "PlatformLayer.h"
41 #import "RealtimeMediaSourceCenter.h"
42 #import "RealtimeMediaSourceSettings.h"
43 #import "WebActionDisablingCALayerDelegate.h"
44 #import <AVFoundation/AVCaptureDevice.h>
45 #import <AVFoundation/AVCaptureInput.h>
46 #import <AVFoundation/AVCaptureOutput.h>
47 #import <AVFoundation/AVCaptureSession.h>
48 #import <AVFoundation/AVCaptureVideoPreviewLayer.h>
49 #import <objc/runtime.h>
50
51 #if PLATFORM(IOS)
52 #include "WebCoreThread.h"
53 #include "WebCoreThreadRun.h"
54 #endif
55
56 #import "CoreMediaSoftLink.h"
57 #import "CoreVideoSoftLink.h"
58
59 typedef AVCaptureConnection AVCaptureConnectionType;
60 typedef AVCaptureDevice AVCaptureDeviceTypedef;
61 typedef AVCaptureDeviceFormat AVCaptureDeviceFormatType;
62 typedef AVCaptureDeviceInput AVCaptureDeviceInputType;
63 typedef AVCaptureOutput AVCaptureOutputType;
64 typedef AVCaptureVideoDataOutput AVCaptureVideoDataOutputType;
65 typedef AVFrameRateRange AVFrameRateRangeType;
66 typedef AVCaptureVideoPreviewLayer AVCaptureVideoPreviewLayerType;
67
68 SOFT_LINK_FRAMEWORK_OPTIONAL(AVFoundation)
69
70 SOFT_LINK_CLASS(AVFoundation, AVCaptureConnection)
71 SOFT_LINK_CLASS(AVFoundation, AVCaptureDevice)
72 SOFT_LINK_CLASS(AVFoundation, AVCaptureDeviceFormat)
73 SOFT_LINK_CLASS(AVFoundation, AVCaptureDeviceInput)
74 SOFT_LINK_CLASS(AVFoundation, AVCaptureOutput)
75 SOFT_LINK_CLASS(AVFoundation, AVCaptureVideoDataOutput)
76 SOFT_LINK_CLASS(AVFoundation, AVCaptureVideoPreviewLayer)
77 SOFT_LINK_CLASS(AVFoundation, AVFrameRateRange)
78
79 #define AVCaptureConnection getAVCaptureConnectionClass()
80 #define AVCaptureDevice getAVCaptureDeviceClass()
81 #define AVCaptureDeviceFormat getAVCaptureDeviceFormatClass()
82 #define AVCaptureDeviceInput getAVCaptureDeviceInputClass()
83 #define AVCaptureOutput getAVCaptureOutputClass()
84 #define AVCaptureVideoDataOutput getAVCaptureVideoDataOutputClass()
85 #define AVCaptureVideoPreviewLayer getAVCaptureVideoPreviewLayerClass()
86 #define AVFrameRateRange getAVFrameRateRangeClass()
87
88 SOFT_LINK_POINTER(AVFoundation, AVMediaTypeVideo, NSString *)
89 SOFT_LINK_POINTER_OPTIONAL(AVFoundation, AVCaptureSessionPreset1280x720, NSString *)
90 SOFT_LINK_POINTER_OPTIONAL(AVFoundation, AVCaptureSessionPreset960x540, NSString *)
91 SOFT_LINK_POINTER_OPTIONAL(AVFoundation, AVCaptureSessionPreset640x480, NSString *)
92 SOFT_LINK_POINTER_OPTIONAL(AVFoundation, AVCaptureSessionPreset352x288, NSString *)
93 SOFT_LINK_POINTER_OPTIONAL(AVFoundation, AVCaptureSessionPreset320x240, NSString *)
94 SOFT_LINK_POINTER_OPTIONAL(AVFoundation, AVCaptureSessionPresetLow, NSString *)
95
96 #define AVCaptureSessionPreset1280x720 getAVCaptureSessionPreset1280x720()
97 #define AVCaptureSessionPreset960x540 getAVCaptureSessionPreset960x540()
98 #define AVCaptureSessionPreset640x480 getAVCaptureSessionPreset640x480()
99 #define AVCaptureSessionPreset352x288 getAVCaptureSessionPreset352x288()
100 #define AVCaptureSessionPreset320x240 getAVCaptureSessionPreset320x240()
101 #define AVCaptureSessionPresetLow getAVCaptureSessionPresetLow()
102
103 using namespace WebCore;
104
105 namespace WebCore {
106
107 #if PLATFORM(MAC)
108 const OSType videoCaptureFormat = kCVPixelFormatType_420YpCbCr8Planar;
109 #else
110 const OSType videoCaptureFormat = kCVPixelFormatType_420YpCbCr8BiPlanarFullRange;
111 #endif
112
113 class AVVideoCaptureSourceFactory : public RealtimeMediaSource::VideoCaptureFactory
114 #if PLATFORM(IOS)
115     , public RealtimeMediaSource::SingleSourceFactory<AVVideoCaptureSource>
116 #endif
117 {
118 public:
119     CaptureSourceOrError createVideoCaptureSource(const String& deviceID, const MediaConstraints* constraints) final {
120         AVCaptureDeviceTypedef *device = [getAVCaptureDeviceClass() deviceWithUniqueID:deviceID];
121         if (!device)
122             return { };
123         return AVVideoCaptureSource::create(device, emptyString(), constraints);
124     }
125 };
126
127 CaptureSourceOrError AVVideoCaptureSource::create(AVCaptureDeviceTypedef* device, const AtomicString& id, const MediaConstraints* constraints)
128 {
129     auto source = adoptRef(*new AVVideoCaptureSource(device, id));
130     if (constraints) {
131         auto result = source->applyConstraints(*constraints);
132         if (result)
133             return WTFMove(result.value().first);
134     }
135
136     return CaptureSourceOrError(WTFMove(source));
137 }
138
139 static AVVideoCaptureSourceFactory& avVideoCaptureSourceFactory()
140 {
141     static NeverDestroyed<AVVideoCaptureSourceFactory> factory;
142     return factory.get();
143 }
144
145 RealtimeMediaSource::VideoCaptureFactory& AVVideoCaptureSource::factory()
146 {
147     return avVideoCaptureSourceFactory();
148 }
149
150 AVVideoCaptureSource::AVVideoCaptureSource(AVCaptureDeviceTypedef* device, const AtomicString& id)
151     : AVMediaCaptureSource(device, id, Type::Video)
152 {
153 }
154
155 AVVideoCaptureSource::~AVVideoCaptureSource()
156 {
157 #if PLATFORM(IOS)
158     avVideoCaptureSourceFactory().unsetActiveSource(*this);
159 #endif
160 }
161
162 static void updateSizeMinMax(int& min, int& max, int value)
163 {
164     min = std::min<int>(min, value);
165     max = std::max<int>(max, value);
166 }
167
168 static void updateAspectRatioMinMax(double& min, double& max, double value)
169 {
170     min = std::min<double>(min, value);
171     max = std::max<double>(max, value);
172 }
173
174 void AVVideoCaptureSource::initializeCapabilities(RealtimeMediaSourceCapabilities& capabilities)
175 {
176     AVCaptureDeviceTypedef *videoDevice = device();
177
178     if ([videoDevice position] == AVCaptureDevicePositionFront)
179         capabilities.addFacingMode(RealtimeMediaSourceSettings::User);
180     if ([videoDevice position] == AVCaptureDevicePositionBack)
181         capabilities.addFacingMode(RealtimeMediaSourceSettings::Environment);
182
183     Float64 lowestFrameRateRange = std::numeric_limits<double>::infinity();
184     Float64 highestFrameRateRange = 0;
185     int minimumWidth = std::numeric_limits<int>::infinity();
186     int maximumWidth = 0;
187     int minimumHeight = std::numeric_limits<int>::infinity();
188     int maximumHeight = 0;
189     double minimumAspectRatio = std::numeric_limits<double>::infinity();
190     double maximumAspectRatio = 0;
191
192     for (AVCaptureDeviceFormatType *format in [videoDevice formats]) {
193
194         for (AVFrameRateRangeType *range in [format videoSupportedFrameRateRanges]) {
195             lowestFrameRateRange = std::min<Float64>(lowestFrameRateRange, range.minFrameRate);
196             highestFrameRateRange = std::max<Float64>(highestFrameRateRange, range.maxFrameRate);
197         }
198
199         if (AVCaptureSessionPreset1280x720 && [videoDevice supportsAVCaptureSessionPreset:AVCaptureSessionPreset1280x720]) {
200             updateSizeMinMax(minimumWidth, maximumWidth, 1280);
201             updateSizeMinMax(minimumHeight, maximumHeight, 720);
202             updateAspectRatioMinMax(minimumAspectRatio, maximumAspectRatio, 1280.0 / 720);
203         }
204         if (AVCaptureSessionPreset960x540 && [videoDevice supportsAVCaptureSessionPreset:AVCaptureSessionPreset960x540]) {
205             updateSizeMinMax(minimumWidth, maximumWidth, 960);
206             updateSizeMinMax(minimumHeight, maximumHeight, 540);
207             updateAspectRatioMinMax(minimumAspectRatio, maximumAspectRatio, 960 / 540);
208         }
209         if (AVCaptureSessionPreset640x480 && [videoDevice supportsAVCaptureSessionPreset:AVCaptureSessionPreset640x480]) {
210             updateSizeMinMax(minimumWidth, maximumWidth, 640);
211             updateSizeMinMax(minimumHeight, maximumHeight, 480);
212             updateAspectRatioMinMax(minimumAspectRatio, maximumAspectRatio, 640 / 480);
213         }
214         if (AVCaptureSessionPreset352x288 && [videoDevice supportsAVCaptureSessionPreset:AVCaptureSessionPreset352x288]) {
215             updateSizeMinMax(minimumWidth, maximumWidth, 352);
216             updateSizeMinMax(minimumHeight, maximumHeight, 288);
217             updateAspectRatioMinMax(minimumAspectRatio, maximumAspectRatio, 352 / 288);
218         }
219         if (AVCaptureSessionPreset320x240 && [videoDevice supportsAVCaptureSessionPreset:AVCaptureSessionPreset320x240]) {
220             updateSizeMinMax(minimumWidth, maximumWidth, 320);
221             updateSizeMinMax(minimumHeight, maximumHeight, 240);
222             updateAspectRatioMinMax(minimumAspectRatio, maximumAspectRatio, 320 / 240);
223         }
224     }
225
226     capabilities.setFrameRate(CapabilityValueOrRange(lowestFrameRateRange, highestFrameRateRange));
227     capabilities.setWidth(CapabilityValueOrRange(minimumWidth, maximumWidth));
228     capabilities.setHeight(CapabilityValueOrRange(minimumHeight, maximumHeight));
229     capabilities.setAspectRatio(CapabilityValueOrRange(minimumAspectRatio, maximumAspectRatio));
230 }
231
232 void AVVideoCaptureSource::initializeSupportedConstraints(RealtimeMediaSourceSupportedConstraints& supportedConstraints)
233 {
234     supportedConstraints.setSupportsFacingMode([device() position] != AVCaptureDevicePositionUnspecified);
235     supportedConstraints.setSupportsWidth(true);
236     supportedConstraints.setSupportsHeight(true);
237     supportedConstraints.setSupportsAspectRatio(true);
238     supportedConstraints.setSupportsFrameRate(true);
239 }
240
241 void AVVideoCaptureSource::updateSettings(RealtimeMediaSourceSettings& settings)
242 {
243     settings.setDeviceId(id());
244
245     if ([device() position] == AVCaptureDevicePositionFront)
246         settings.setFacingMode(RealtimeMediaSourceSettings::User);
247     else if ([device() position] == AVCaptureDevicePositionBack)
248         settings.setFacingMode(RealtimeMediaSourceSettings::Environment);
249     else
250         settings.setFacingMode(RealtimeMediaSourceSettings::Unknown);
251     
252     settings.setFrameRate(m_frameRate);
253     settings.setWidth(m_width);
254     settings.setHeight(m_height);
255     settings.setAspectRatio(static_cast<float>(m_width) / m_height);
256 }
257
258 bool AVVideoCaptureSource::applySize(const IntSize& size)
259 {
260     NSString *preset = bestSessionPresetForVideoDimensions(size.width(), size.height());
261     if (!preset || ![session() canSetSessionPreset:preset]) {
262         LOG(Media, "AVVideoCaptureSource::applySize(%p), unable find or set preset for width: %i, height: %i", this, size.width(), size.height());
263         return false;
264     }
265
266     return setPreset(preset);
267 }
268
269 static IntSize sizeForPreset(NSString* preset)
270 {
271     if (!preset)
272         return { };
273
274     if (AVCaptureSessionPreset1280x720 && [preset isEqualToString:AVCaptureSessionPreset1280x720])
275         return { 1280, 720 };
276
277     if (AVCaptureSessionPreset960x540 && [preset isEqualToString:AVCaptureSessionPreset960x540])
278         return { 960, 540 };
279
280     if (AVCaptureSessionPreset640x480 && [preset isEqualToString:AVCaptureSessionPreset640x480])
281         return { 640, 480 };
282
283     if (AVCaptureSessionPreset352x288 && [preset isEqualToString:AVCaptureSessionPreset352x288])
284         return { 352, 288 };
285
286     if (AVCaptureSessionPreset320x240 && [preset isEqualToString:AVCaptureSessionPreset320x240])
287         return { 320, 240 };
288     
289     return { };
290     
291 }
292
293 bool AVVideoCaptureSource::setPreset(NSString *preset)
294 {
295     if (!session()) {
296         m_pendingPreset = preset;
297         return true;
298     }
299
300     auto size = sizeForPreset(preset);
301     if (size.width() == m_width && size.height() == m_height)
302         return true;
303
304     @try {
305         session().sessionPreset = preset;
306 #if PLATFORM(MAC)
307         auto settingsDictionary = @{ (NSString*)kCVPixelBufferPixelFormatTypeKey: @(videoCaptureFormat), (NSString*)kCVPixelBufferWidthKey: @(size.width()), (NSString*)kCVPixelBufferHeightKey: @(size.height()), };
308         [m_videoOutput setVideoSettings:settingsDictionary];
309 #endif
310     } @catch(NSException *exception) {
311         LOG(Media, "AVVideoCaptureSource::applySize(%p), exception thrown configuring device: <%s> %s", this, [[exception name] UTF8String], [[exception reason] UTF8String]);
312         return false;
313     }
314
315     return true;
316 }
317
318 bool AVVideoCaptureSource::applyFrameRate(double rate)
319 {
320     AVFrameRateRangeType *bestFrameRateRange = nil;
321     for (AVFrameRateRangeType *frameRateRange in [[device() activeFormat] videoSupportedFrameRateRanges]) {
322         if (rate >= [frameRateRange minFrameRate] && rate <= [frameRateRange maxFrameRate]) {
323             if (!bestFrameRateRange || CMTIME_COMPARE_INLINE([frameRateRange minFrameDuration], >, [bestFrameRateRange minFrameDuration]))
324                 bestFrameRateRange = frameRateRange;
325         }
326     }
327
328     if (!bestFrameRateRange) {
329         LOG(Media, "AVVideoCaptureSource::applyFrameRate(%p), frame rate %f not supported by video device", this, rate);
330         return false;
331     }
332
333     NSError *error = nil;
334     @try {
335         if ([device() lockForConfiguration:&error]) {
336             [device() setActiveVideoMinFrameDuration:[bestFrameRateRange minFrameDuration]];
337             [device() unlockForConfiguration];
338         }
339     } @catch(NSException *exception) {
340         LOG(Media, "AVVideoCaptureSource::applyFrameRate(%p), exception thrown configuring device: <%s> %s", this, [[exception name] UTF8String], [[exception reason] UTF8String]);
341         return false;
342     }
343
344     if (error) {
345         LOG(Media, "AVVideoCaptureSource::applyFrameRate(%p), failed to lock video device for configuration: %s", this, [[error localizedDescription] UTF8String]);
346         return false;
347     }
348
349     LOG(Media, "AVVideoCaptureSource::applyFrameRate(%p) - set frame rate range to %f", this, rate);
350     return true;
351 }
352
353 void AVVideoCaptureSource::applySizeAndFrameRate(std::optional<int> width, std::optional<int> height, std::optional<double> frameRate)
354 {
355     setPreset(bestSessionPresetForVideoDimensions(WTFMove(width), WTFMove(height)));
356
357     if (frameRate)
358         applyFrameRate(frameRate.value());
359 }
360
361 static inline int sensorOrientation(AVCaptureVideoOrientation videoOrientation)
362 {
363 #if PLATFORM(IOS)
364     switch (videoOrientation) {
365     case AVCaptureVideoOrientationPortrait:
366         return 180;
367     case AVCaptureVideoOrientationPortraitUpsideDown:
368         return 0;
369     case AVCaptureVideoOrientationLandscapeRight:
370         return 90;
371     case AVCaptureVideoOrientationLandscapeLeft:
372         return -90;
373     }
374 #else
375     switch (videoOrientation) {
376     case AVCaptureVideoOrientationPortrait:
377         return 0;
378     case AVCaptureVideoOrientationPortraitUpsideDown:
379         return 180;
380     case AVCaptureVideoOrientationLandscapeRight:
381         return 90;
382     case AVCaptureVideoOrientationLandscapeLeft:
383         return -90;
384     }
385 #endif
386 }
387
388 static inline int sensorOrientationFromVideoOutput(AVCaptureVideoDataOutputType* videoOutput)
389 {
390     AVCaptureConnectionType* connection = [videoOutput connectionWithMediaType: getAVMediaTypeVideo()];
391     return connection ? sensorOrientation([connection videoOrientation]) : 0;
392 }
393
394 void AVVideoCaptureSource::setupCaptureSession()
395 {
396 #if PLATFORM(IOS)
397     avVideoCaptureSourceFactory().setActiveSource(*this);
398 #endif
399
400     NSError *error = nil;
401     RetainPtr<AVCaptureDeviceInputType> videoIn = adoptNS([allocAVCaptureDeviceInputInstance() initWithDevice:device() error:&error]);
402     if (error) {
403         LOG(Media, "AVVideoCaptureSource::setupCaptureSession(%p), failed to allocate AVCaptureDeviceInput: %s", this, [[error localizedDescription] UTF8String]);
404         return;
405     }
406
407     if (![session() canAddInput:videoIn.get()]) {
408         LOG(Media, "AVVideoCaptureSource::setupCaptureSession(%p), unable to add video input device", this);
409         return;
410     }
411     [session() addInput:videoIn.get()];
412
413     m_videoOutput = adoptNS([allocAVCaptureVideoDataOutputInstance() init]);
414     auto settingsDictionary = adoptNS([[NSMutableDictionary alloc] initWithObjectsAndKeys: [NSNumber numberWithInt:videoCaptureFormat], kCVPixelBufferPixelFormatTypeKey, nil]);
415     if (m_pendingPreset) {
416 #if PLATFORM(MAC)
417         auto size = sizeForPreset(m_pendingPreset.get());
418         [settingsDictionary.get() setObject:[NSNumber numberWithInt:size.width()] forKey:(NSString*)kCVPixelBufferWidthKey];
419         [settingsDictionary.get() setObject:[NSNumber numberWithInt:size.height()] forKey:(NSString*)kCVPixelBufferHeightKey];
420 #endif
421     }
422
423     [m_videoOutput setVideoSettings:settingsDictionary.get()];
424     [m_videoOutput setAlwaysDiscardsLateVideoFrames:YES];
425     setVideoSampleBufferDelegate(m_videoOutput.get());
426
427     if (![session() canAddOutput:m_videoOutput.get()]) {
428         LOG(Media, "AVVideoCaptureSource::setupCaptureSession(%p), unable to add video sample buffer output delegate", this);
429         return;
430     }
431     [session() addOutput:m_videoOutput.get()];
432
433 #if PLATFORM(IOS)
434     setPreset(m_pendingPreset.get());
435 #endif
436
437     m_sensorOrientation = sensorOrientationFromVideoOutput(m_videoOutput.get());
438     computeSampleRotation();
439 }
440
441 void AVVideoCaptureSource::shutdownCaptureSession()
442 {
443     m_buffer = nullptr;
444     m_lastImage = nullptr;
445     m_videoFrameTimeStamps.clear();
446     m_frameRate = 0;
447     m_width = 0;
448     m_height = 0;
449 }
450
451 bool AVVideoCaptureSource::updateFramerate(CMSampleBufferRef sampleBuffer)
452 {
453     CMTime sampleTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
454     if (!CMTIME_IS_NUMERIC(sampleTime))
455         return false;
456
457     Float64 frameTime = CMTimeGetSeconds(sampleTime);
458     Float64 oneSecondAgo = frameTime - 1;
459     
460     m_videoFrameTimeStamps.append(frameTime);
461     
462     while (m_videoFrameTimeStamps[0] < oneSecondAgo)
463         m_videoFrameTimeStamps.remove(0);
464
465     Float64 frameRate = m_frameRate;
466     m_frameRate = (m_frameRate + m_videoFrameTimeStamps.size()) / 2;
467
468     return frameRate != m_frameRate;
469 }
470
471 void AVVideoCaptureSource::monitorOrientation(OrientationNotifier& notifier)
472 {
473 #if PLATFORM(IOS)
474     notifier.addObserver(*this);
475     orientationChanged(notifier.orientation());
476 #else
477     UNUSED_PARAM(notifier);
478 #endif
479 }
480
481 void AVVideoCaptureSource::orientationChanged(int orientation)
482 {
483     ASSERT(orientation == 0 || orientation == 90 || orientation == -90 || orientation == 180);
484     m_deviceOrientation = orientation;
485     computeSampleRotation();
486 }
487
488 void AVVideoCaptureSource::computeSampleRotation()
489 {
490     switch (m_sensorOrientation - m_deviceOrientation) {
491     case 0:
492         m_sampleRotation = MediaSample::VideoRotation::None;
493         break;
494     case 180:
495     case -180:
496         m_sampleRotation = MediaSample::VideoRotation::UpsideDown;
497         break;
498     case 90:
499         m_sampleRotation = MediaSample::VideoRotation::Left;
500         break;
501     case -90:
502     case -270:
503         m_sampleRotation = MediaSample::VideoRotation::Right;
504         break;
505     default:
506         ASSERT_NOT_REACHED();
507         m_sampleRotation = MediaSample::VideoRotation::None;
508     }
509 }
510
511 void AVVideoCaptureSource::processNewFrame(RetainPtr<CMSampleBufferRef> sampleBuffer, RetainPtr<AVCaptureConnectionType> connection)
512 {
513     // Ignore frames delivered when the session is not running, we want to hang onto the last image
514     // delivered before it stopped.
515     if (m_lastImage && (!isProducingData() || muted()))
516         return;
517
518     CMFormatDescriptionRef formatDescription = CMSampleBufferGetFormatDescription(sampleBuffer.get());
519     if (!formatDescription)
520         return;
521
522     updateFramerate(sampleBuffer.get());
523     m_buffer = sampleBuffer;
524     m_lastImage = nullptr;
525
526     CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions(formatDescription);
527     if (m_sampleRotation == MediaSample::VideoRotation::Left || m_sampleRotation == MediaSample::VideoRotation::Right)
528         std::swap(dimensions.width, dimensions.height);
529
530     if (dimensions.width != m_width || dimensions.height != m_height) {
531         m_width = dimensions.width;
532         m_height = dimensions.height;
533
534         settingsDidChange();
535     }
536
537     videoSampleAvailable(MediaSampleAVFObjC::create(m_buffer.get(), m_sampleRotation, [connection isVideoMirrored]));
538 }
539
540 void AVVideoCaptureSource::captureOutputDidOutputSampleBufferFromConnection(AVCaptureOutputType*, CMSampleBufferRef sampleBuffer, AVCaptureConnectionType* captureConnection)
541 {
542     RetainPtr<CMSampleBufferRef> buffer = sampleBuffer;
543     RetainPtr<AVCaptureConnectionType> connection = captureConnection;
544
545     scheduleDeferredTask([this, buffer, connection] {
546         this->processNewFrame(buffer, connection);
547     });
548 }
549
550 NSString* AVVideoCaptureSource::bestSessionPresetForVideoDimensions(std::optional<int> width, std::optional<int> height) const
551 {
552     if (!width && !height)
553         return nil;
554
555     AVCaptureDeviceTypedef *videoDevice = device();
556     if ((!width || width.value() == 1280) && (!height || height.value() == 720) && AVCaptureSessionPreset1280x720)
557         return [videoDevice supportsAVCaptureSessionPreset:AVCaptureSessionPreset1280x720] ? AVCaptureSessionPreset1280x720 : nil;
558
559     if ((!width || width.value() == 960) && (!height || height.value() == 540) && AVCaptureSessionPreset960x540)
560         return [videoDevice supportsAVCaptureSessionPreset:AVCaptureSessionPreset960x540] ? AVCaptureSessionPreset960x540 : nil;
561
562     if ((!width || width.value() == 640) && (!height || height.value() == 480 ) && AVCaptureSessionPreset640x480)
563         return [videoDevice supportsAVCaptureSessionPreset:AVCaptureSessionPreset640x480] ? AVCaptureSessionPreset640x480 : nil;
564
565     if ((!width || width.value() == 352) && (!height || height.value() == 288 ) && AVCaptureSessionPreset352x288)
566         return [videoDevice supportsAVCaptureSessionPreset:AVCaptureSessionPreset352x288] ? AVCaptureSessionPreset352x288 : nil;
567
568     if ((!width || width.value() == 320) && (!height || height.value() == 240 ) && AVCaptureSessionPreset320x240)
569         return [videoDevice supportsAVCaptureSessionPreset:AVCaptureSessionPreset320x240] ? AVCaptureSessionPreset320x240 : nil;
570
571     return nil;
572 }
573
574 bool AVVideoCaptureSource::supportsSizeAndFrameRate(std::optional<int> width, std::optional<int> height, std::optional<double> frameRate)
575 {
576     if (!height && !width && !frameRate)
577         return true;
578
579     if ((height || width) && !bestSessionPresetForVideoDimensions(WTFMove(width), WTFMove(height)))
580         return false;
581
582     if (!frameRate)
583         return true;
584
585     int rate = static_cast<int>(frameRate.value());
586     for (AVFrameRateRangeType *range in [[device() activeFormat] videoSupportedFrameRateRanges]) {
587         if (rate >= static_cast<int>(range.minFrameRate) && rate <= static_cast<int>(range.maxFrameRate))
588             return true;
589     }
590
591     return false;
592 }
593
594 } // namespace WebCore
595
596 #endif // ENABLE(MEDIA_STREAM)