[MediaStream] RealtimeMediaSource should be able to vend hashed IDs
[WebKit-https.git] / Source / WebCore / platform / mediastream / mac / MockRealtimeVideoSourceMac.mm
1 /*
2  * Copyright (C) 2015-2018 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  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer
12  *    in the documentation and/or other materials provided with the
13  *    distribution.
14  * 3. Neither the name of Google Inc. nor the names of its contributors
15  *    may be used to endorse or promote products derived from this
16  *    software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #import "config.h"
32 #import "MockRealtimeVideoSourceMac.h"
33
34 #if ENABLE(MEDIA_STREAM)
35 #import "GraphicsContextCG.h"
36 #import "ImageBuffer.h"
37 #import "MediaConstraints.h"
38 #import "MediaSampleAVFObjC.h"
39 #import "NotImplemented.h"
40 #import "PixelBufferResizer.h"
41 #import "PlatformLayer.h"
42 #import "RealtimeMediaSourceSettings.h"
43 #import "RealtimeVideoUtilities.h"
44 #import <QuartzCore/CALayer.h>
45 #import <QuartzCore/CATransaction.h>
46 #import <objc/runtime.h>
47
48 #import <pal/cf/CoreMediaSoftLink.h>
49 #import "CoreVideoSoftLink.h"
50
51 namespace WebCore {
52 using namespace PAL;
53
54 static const int videoSampleRate = 90000;
55
56 CaptureSourceOrError MockRealtimeVideoSource::create(String&& deviceID, String&& name, String&& hashSalt, const MediaConstraints* constraints)
57 {
58 #ifndef NDEBUG
59     auto device = MockRealtimeMediaSourceCenter::mockDeviceWithPersistentID(deviceID);
60     ASSERT(device);
61     if (!device)
62         return { };
63 #endif
64
65     auto source = adoptRef(*new MockRealtimeVideoSourceMac(WTFMove(deviceID), WTFMove(name), WTFMove(hashSalt)));
66     // FIXME: We should report error messages
67     if (constraints && source->applyConstraints(*constraints))
68         return { };
69
70     return CaptureSourceOrError(WTFMove(source));
71 }
72
73 MockRealtimeVideoSourceMac::MockRealtimeVideoSourceMac(String&& deviceID, String&& name, String&& hashSalt)
74     : MockRealtimeVideoSource(WTFMove(deviceID), WTFMove(name), WTFMove(hashSalt))
75 {
76 }
77
78 RetainPtr<CMSampleBufferRef> MockRealtimeVideoSourceMac::CMSampleBufferFromPixelBuffer(CVPixelBufferRef pixelBuffer)
79 {
80     if (!pixelBuffer)
81         return nullptr;
82     
83     CMTime sampleTime = CMTimeMake(((elapsedTime() + 100_ms) * videoSampleRate).seconds(), videoSampleRate);
84     CMSampleTimingInfo timingInfo = { kCMTimeInvalid, sampleTime, sampleTime };
85
86     CMVideoFormatDescriptionRef formatDescription = nullptr;
87     OSStatus status = CMVideoFormatDescriptionCreateForImageBuffer(kCFAllocatorDefault, (CVImageBufferRef)pixelBuffer, &formatDescription);
88     if (status != noErr) {
89         LOG_ERROR("Failed to initialize CMVideoFormatDescription with error code: %d", status);
90         return nullptr;
91     }
92
93     CMSampleBufferRef sampleBuffer;
94     status = CMSampleBufferCreateReadyWithImageBuffer(kCFAllocatorDefault, (CVImageBufferRef)pixelBuffer, formatDescription, &timingInfo, &sampleBuffer);
95     CFRelease(formatDescription);
96     if (status != noErr) {
97         LOG_ERROR("Failed to initialize CMSampleBuffer with error code: %d", status);
98         return nullptr;
99     }
100
101     return adoptCF(sampleBuffer);
102 }
103
104 RetainPtr<CVPixelBufferRef> MockRealtimeVideoSourceMac::pixelBufferFromCGImage(CGImageRef image) const
105 {
106     static CGColorSpaceRef sRGBColorSpace = sRGBColorSpaceRef();
107
108     CGSize imageSize = CGSizeMake(CGImageGetWidth(image), CGImageGetHeight(image));
109     if (!m_bufferPool) {
110         CVPixelBufferPoolRef bufferPool;
111         CFDictionaryRef sourcePixelBufferOptions = (__bridge CFDictionaryRef) @{
112             (__bridge NSString *)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_32ARGB),
113             (__bridge NSString *)kCVPixelBufferWidthKey : @(imageSize.width),
114             (__bridge NSString *)kCVPixelBufferHeightKey : @(imageSize.height),
115 #if PLATFORM(IOS)
116             (__bridge NSString *)kCVPixelFormatOpenGLESCompatibility : @(YES),
117 #else
118             (__bridge NSString *)kCVPixelBufferOpenGLCompatibilityKey : @(YES),
119 #endif
120             (__bridge NSString *)kCVPixelBufferIOSurfacePropertiesKey : @{/*empty dictionary*/}
121         };
122
123         CFDictionaryRef pixelBufferPoolOptions = (__bridge CFDictionaryRef) @{
124            (__bridge NSString *)kCVPixelBufferPoolMinimumBufferCountKey : @(4)
125         };
126
127         CVReturn status = CVPixelBufferPoolCreate(kCFAllocatorDefault, pixelBufferPoolOptions, sourcePixelBufferOptions, &bufferPool);
128         if (status != kCVReturnSuccess)
129             return nullptr;
130
131         m_bufferPool = adoptCF(bufferPool);
132     }
133
134     CVPixelBufferRef pixelBuffer;
135     CVReturn status = CVPixelBufferPoolCreatePixelBuffer(nullptr, m_bufferPool.get(), &pixelBuffer);
136     if (status != kCVReturnSuccess)
137         return nullptr;
138
139     CVPixelBufferLockBaseAddress(pixelBuffer, 0);
140     void* data = CVPixelBufferGetBaseAddress(pixelBuffer);
141     auto context = adoptCF(CGBitmapContextCreate(data, imageSize.width, imageSize.height, 8, CVPixelBufferGetBytesPerRow(pixelBuffer), sRGBColorSpace, (CGBitmapInfo) kCGImageAlphaNoneSkipFirst));
142     CGContextDrawImage(context.get(), CGRectMake(0, 0, CGImageGetWidth(image), CGImageGetHeight(image)), image);
143     CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
144
145     return adoptCF(pixelBuffer);
146 }
147
148 void MockRealtimeVideoSourceMac::updateSampleBuffer()
149 {
150     auto imageBuffer = this->imageBuffer();
151     if (!imageBuffer)
152         return;
153
154     auto pixelBuffer = pixelBufferFromCGImage(imageBuffer->copyImage()->nativeImage().get());
155     if (m_pixelBufferResizer)
156         pixelBuffer = m_pixelBufferResizer->resize(pixelBuffer.get());
157     else {
158         if (!m_pixelBufferConformer) {
159             m_pixelBufferConformer = std::make_unique<PixelBufferConformerCV>((__bridge CFDictionaryRef)@{
160                 (__bridge NSString *)kCVPixelBufferPixelFormatTypeKey: @(preferedPixelBufferFormat())
161             });
162         }
163
164         pixelBuffer = m_pixelBufferConformer->convert(pixelBuffer.get());
165     }
166     auto sampleBuffer = CMSampleBufferFromPixelBuffer(pixelBuffer.get());
167
168     // We use m_deviceOrientation to emulate sensor orientation
169     dispatchMediaSampleToObservers(MediaSampleAVFObjC::create(sampleBuffer.get(), m_deviceOrientation));
170 }
171
172 void MockRealtimeVideoSourceMac::settingsDidChange(OptionSet<RealtimeMediaSourceSettings::Flag> settings)
173 {
174     if (settings.containsAny({ RealtimeMediaSourceSettings::Flag::Width, RealtimeMediaSourceSettings::Flag::Height }))
175         m_bufferPool = nullptr;
176     MockRealtimeVideoSource::settingsDidChange(settings);
177 }
178
179 void MockRealtimeVideoSourceMac::orientationChanged(int orientation)
180 {
181     // FIXME: Do something with m_deviceOrientation. See bug 169822.
182     switch (orientation) {
183     case 0:
184         m_deviceOrientation = MediaSample::VideoRotation::None;
185         break;
186     case 90:
187         m_deviceOrientation = MediaSample::VideoRotation::Right;
188         break;
189     case -90:
190         m_deviceOrientation = MediaSample::VideoRotation::Left;
191         break;
192     case 180:
193         m_deviceOrientation = MediaSample::VideoRotation::UpsideDown;
194         break;
195     default:
196         return;
197     }
198 }
199
200 void MockRealtimeVideoSourceMac::monitorOrientation(OrientationNotifier& notifier)
201 {
202     notifier.addObserver(*this);
203     orientationChanged(notifier.orientation());
204 }
205
206 void MockRealtimeVideoSourceMac::setSizeAndFrameRateWithPreset(IntSize requestedSize, double, RefPtr<VideoPreset> preset)
207 {
208     if (!preset)
209         return;
210
211     if (preset->size != requestedSize) {
212         if (m_pixelBufferResizer && !m_pixelBufferResizer->canResizeTo(requestedSize))
213             m_pixelBufferResizer = nullptr;
214
215         if (!m_pixelBufferResizer)
216             m_pixelBufferResizer = std::make_unique<PixelBufferResizer>(requestedSize, preferedPixelBufferFormat());
217     } else
218         m_pixelBufferResizer = nullptr;
219 }
220
221 } // namespace WebCore
222
223 #endif // ENABLE(MEDIA_STREAM)