/
WebCoreDecompressionSession.mm
447 lines (377 loc) · 19.1 KB
/
WebCoreDecompressionSession.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
* Copyright (C) 2017 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#import "config.h"
#import "WebCoreDecompressionSession.h"
#if USE(VIDEOTOOLBOX)
#import "Logging.h"
#import "MediaTimeAVFoundation.h"
#import "PixelBufferConformerCV.h"
#import <CoreMedia/CMBufferQueue.h>
#import <CoreMedia/CMFormatDescription.h>
#import <wtf/MainThread.h>
#import <wtf/MediaTime.h>
#import <wtf/StringPrintStream.h>
#import <wtf/Vector.h>
#import "CoreMediaSoftLink.h"
#import "CoreVideoSoftLink.h"
#import "VideoToolboxSoftLink.h"
namespace WebCore {
WebCoreDecompressionSession::WebCoreDecompressionSession()
: m_decompressionQueue(adoptOSObject(dispatch_queue_create("WebCoreDecompressionSession Decompression Queue", DISPATCH_QUEUE_SERIAL)))
, m_enqueingQueue(adoptOSObject(dispatch_queue_create("WebCoreDecompressionSession Enqueueing Queue", DISPATCH_QUEUE_SERIAL)))
, m_hasAvailableImageSemaphore(adoptOSObject(dispatch_semaphore_create(0)))
{
}
void WebCoreDecompressionSession::invalidate()
{
m_invalidated = true;
m_notificationCallback = nullptr;
m_hasAvailableFrameCallback = nullptr;
setTimebase(nullptr);
if (m_timerSource)
dispatch_source_cancel(m_timerSource.get());
}
void WebCoreDecompressionSession::setTimebase(CMTimebaseRef timebase)
{
if (m_timebase == timebase)
return;
if (m_timebase)
CMTimebaseRemoveTimerDispatchSource(m_timebase.get(), m_timerSource.get());
m_timebase = timebase;
if (m_timebase) {
if (!m_timerSource) {
m_timerSource = adoptOSObject(dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue()));
dispatch_source_set_event_handler(m_timerSource.get(), [this] {
automaticDequeue();
});
#if (PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200) || PLATFORM(IOS)
dispatch_activate(m_timerSource.get());
#endif
}
CMTimebaseAddTimerDispatchSource(m_timebase.get(), m_timerSource.get());
}
}
void WebCoreDecompressionSession::maybeBecomeReadyForMoreMediaDataCallback(void* refcon, CMBufferQueueTriggerToken)
{
WebCoreDecompressionSession* session = static_cast<WebCoreDecompressionSession*>(refcon);
session->maybeBecomeReadyForMoreMediaData();
}
void WebCoreDecompressionSession::maybeBecomeReadyForMoreMediaData()
{
LOG(Media, "WebCoreDecompressionSession::maybeBecomeReadyForMoreMediaData(%p) - isReadyForMoreMediaData(%d), hasCallback(%d)", this, isReadyForMoreMediaData(), !!m_notificationCallback);
if (!isReadyForMoreMediaData() || !m_notificationCallback)
return;
if (isMainThread()) {
m_notificationCallback();
return;
}
RefPtr<WebCoreDecompressionSession> protectedThis { this };
dispatch_async(dispatch_get_main_queue(), [protectedThis] {
if (protectedThis->m_notificationCallback)
protectedThis->m_notificationCallback();
});
}
void WebCoreDecompressionSession::enqueueSample(CMSampleBufferRef sampleBuffer, bool displaying)
{
CMItemCount itemCount = 0;
if (noErr != CMSampleBufferGetSampleTimingInfoArray(sampleBuffer, 0, nullptr, &itemCount))
return;
Vector<CMSampleTimingInfo> timingInfoArray;
timingInfoArray.grow(itemCount);
if (noErr != CMSampleBufferGetSampleTimingInfoArray(sampleBuffer, itemCount, timingInfoArray.data(), nullptr))
return;
if (!m_decompressionQueue)
m_decompressionQueue = adoptOSObject(dispatch_queue_create("SourceBufferPrivateAVFObjC Decompression Queue", DISPATCH_QUEUE_SERIAL));
// CMBufferCallbacks contains 64-bit pointers that aren't 8-byte aligned. To suppress the linker
// warning about this, we prepend 4 bytes of padding when building for macOS.
#if PLATFORM(MAC)
const size_t padSize = 4;
#else
const size_t padSize = 0;
#endif
if (!m_producerQueue) {
CMBufferQueueRef outQueue { nullptr };
#pragma pack(push, 4)
struct { uint8_t pad[padSize]; CMBufferCallbacks callbacks; } callbacks { { }, {
0,
nullptr,
&getDecodeTime,
&getPresentationTime,
&getDuration,
nullptr,
&compareBuffers,
nullptr,
nullptr,
} };
#pragma pack(pop)
CMBufferQueueCreate(kCFAllocatorDefault, kMaximumCapacity, &callbacks.callbacks, &outQueue);
m_producerQueue = adoptCF(outQueue);
CMBufferQueueInstallTriggerWithIntegerThreshold(m_producerQueue.get(), maybeBecomeReadyForMoreMediaDataCallback, this, kCMBufferQueueTrigger_WhenBufferCountBecomesLessThan, kLowWaterMark, &m_didBecomeReadyTrigger);
}
if (!m_consumerQueue) {
CMBufferQueueRef outQueue { nullptr };
#pragma pack(push, 4)
struct { uint8_t pad[padSize]; CMBufferCallbacks callbacks; } callbacks { { }, {
0,
nullptr,
&getDecodeTime,
&getPresentationTime,
&getDuration,
nullptr,
&compareBuffers,
nullptr,
nullptr,
} };
#pragma pack(pop)
CMBufferQueueCreate(kCFAllocatorDefault, kMaximumCapacity, &callbacks.callbacks, &outQueue);
m_consumerQueue = adoptCF(outQueue);
}
++m_framesBeingDecoded;
LOG(Media, "WebCoreDecompressionSession::enqueueSample(%p) - framesBeingDecoded(%d)", this, m_framesBeingDecoded);
dispatch_async(m_decompressionQueue.get(), [protectedThis = makeRefPtr(*this), strongBuffer = retainPtr(sampleBuffer), displaying] {
protectedThis->decodeSample(strongBuffer.get(), displaying);
});
}
void WebCoreDecompressionSession::decodeSample(CMSampleBufferRef sample, bool displaying)
{
if (isInvalidated())
return;
CMVideoFormatDescriptionRef videoFormatDescription = CMSampleBufferGetFormatDescription(sample);
if (m_decompressionSession && !VTDecompressionSessionCanAcceptFormatDescription(m_decompressionSession.get(), videoFormatDescription)) {
VTDecompressionSessionWaitForAsynchronousFrames(m_decompressionSession.get());
m_decompressionSession = nullptr;
}
if (!m_decompressionSession) {
CMVideoFormatDescriptionRef videoFormatDescription = CMSampleBufferGetFormatDescription(sample);
NSDictionary* videoDecoderSpecification = @{ (NSString *)kVTVideoDecoderSpecification_EnableHardwareAcceleratedVideoDecoder: @YES };
#if PLATFORM(IOS)
NSDictionary* attributes = @{(NSString *)kCVPixelBufferIOSurfaceOpenGLESFBOCompatibilityKey: @YES};
#else
NSDictionary* attributes = @{(NSString *)kCVPixelBufferIOSurfaceOpenGLFBOCompatibilityKey: @YES};
#endif
VTDecompressionSessionRef decompressionSessionOut = nullptr;
VTDecompressionOutputCallbackRecord callback {
&decompressionOutputCallback,
this,
};
if (noErr == VTDecompressionSessionCreate(kCFAllocatorDefault, videoFormatDescription, (CFDictionaryRef)videoDecoderSpecification, (CFDictionaryRef)attributes, &callback, &decompressionSessionOut))
m_decompressionSession = adoptCF(decompressionSessionOut);
}
VTDecodeInfoFlags flags { kVTDecodeFrame_EnableTemporalProcessing };
if (!displaying)
flags |= kVTDecodeFrame_DoNotOutputFrame;
VTDecompressionSessionDecodeFrame(m_decompressionSession.get(), sample, flags, reinterpret_cast<void*>(displaying), nullptr);
}
void WebCoreDecompressionSession::decompressionOutputCallback(void* decompressionOutputRefCon, void* sourceFrameRefCon, OSStatus status, VTDecodeInfoFlags infoFlags, CVImageBufferRef imageBuffer, CMTime presentationTimeStamp, CMTime presentationDuration)
{
WebCoreDecompressionSession* session = static_cast<WebCoreDecompressionSession*>(decompressionOutputRefCon);
bool displaying = sourceFrameRefCon;
session->handleDecompressionOutput(displaying, status, infoFlags, imageBuffer, presentationTimeStamp, presentationDuration);
}
void WebCoreDecompressionSession::handleDecompressionOutput(bool displaying, OSStatus status, VTDecodeInfoFlags infoFlags, CVImageBufferRef rawImageBuffer, CMTime presentationTimeStamp, CMTime presentationDuration)
{
UNUSED_PARAM(status);
UNUSED_PARAM(infoFlags);
CMVideoFormatDescriptionRef rawImageBufferDescription = nullptr;
if (noErr != CMVideoFormatDescriptionCreateForImageBuffer(kCFAllocatorDefault, rawImageBuffer, &rawImageBufferDescription))
return;
RetainPtr<CMVideoFormatDescriptionRef> imageBufferDescription = adoptCF(rawImageBufferDescription);
CMSampleTimingInfo imageBufferTiming {
presentationDuration,
presentationTimeStamp,
presentationTimeStamp,
};
CMSampleBufferRef rawImageSampleBuffer = nullptr;
if (noErr != CMSampleBufferCreateReadyWithImageBuffer(kCFAllocatorDefault, rawImageBuffer, imageBufferDescription.get(), &imageBufferTiming, &rawImageSampleBuffer))
return;
RefPtr<WebCoreDecompressionSession> protectedThis { this };
RetainPtr<CMSampleBufferRef> imageSampleBuffer = adoptCF(rawImageSampleBuffer);
dispatch_async(m_enqueingQueue.get(), [protectedThis, imageSampleBuffer, displaying] {
protectedThis->enqueueDecodedSample(imageSampleBuffer.get(), displaying);
});
}
RetainPtr<CVPixelBufferRef> WebCoreDecompressionSession::getFirstVideoFrame()
{
if (!m_producerQueue || CMBufferQueueIsEmpty(m_producerQueue.get()))
return nullptr;
RetainPtr<CMSampleBufferRef> currentSample = adoptCF((CMSampleBufferRef)CMBufferQueueDequeueAndRetain(m_producerQueue.get()));
RetainPtr<CVPixelBufferRef> imageBuffer = (CVPixelBufferRef)CMSampleBufferGetImageBuffer(currentSample.get());
ASSERT(CFGetTypeID(imageBuffer.get()) == CVPixelBufferGetTypeID());
return imageBuffer;
}
void WebCoreDecompressionSession::automaticDequeue()
{
if (!m_timebase)
return;
auto time = toMediaTime(CMTimebaseGetTime(m_timebase.get()));
LOG(Media, "WebCoreDecompressionSession::automaticDequeue(%p) - purging all samples before time(%s)", this, toString(time).utf8().data());
while (CMSampleBufferRef firstSample = (CMSampleBufferRef)CMBufferQueueGetHead(m_producerQueue.get())) {
MediaTime presentationTimestamp = toMediaTime(CMSampleBufferGetPresentationTimeStamp(firstSample));
MediaTime duration = toMediaTime(CMSampleBufferGetDuration(firstSample));
MediaTime presentationEndTimestamp = presentationTimestamp + duration;
if (time > presentationEndTimestamp) {
CFRelease(CMBufferQueueDequeueAndRetain(m_producerQueue.get()));
continue;
}
#if !LOG_DISABLED
auto begin = toMediaTime(CMBufferQueueGetFirstPresentationTimeStamp(m_producerQueue.get()));
auto end = toMediaTime(CMBufferQueueGetEndPresentationTimeStamp(m_producerQueue.get()));
LOG(Media, "WebCoreDecompressionSession::automaticDequeue(%p) - queue(%s -> %s)", this, toString(begin).utf8().data(), toString(end).utf8().data());
#endif
CMTimebaseSetTimerDispatchSourceNextFireTime(m_timebase.get(), m_timerSource.get(), toCMTime(presentationEndTimestamp), 0);
return;
}
LOG(Media, "WebCoreDecompressionSession::automaticDequeue(%p) - queue empty", this, toString(time).utf8().data());
CMTimebaseSetTimerDispatchSourceNextFireTime(m_timebase.get(), m_timerSource.get(), kCMTimePositiveInfinity, 0);
}
void WebCoreDecompressionSession::enqueueDecodedSample(CMSampleBufferRef sample, bool displaying)
{
if (isInvalidated())
return;
--m_framesBeingDecoded;
if (!displaying) {
maybeBecomeReadyForMoreMediaData();
return;
}
CMBufferQueueEnqueue(m_producerQueue.get(), sample);
#if !LOG_DISABLED
auto begin = toMediaTime(CMBufferQueueGetFirstPresentationTimeStamp(m_producerQueue.get()));
auto end = toMediaTime(CMBufferQueueGetEndPresentationTimeStamp(m_producerQueue.get()));
auto presentationTime = toMediaTime(CMSampleBufferGetPresentationTimeStamp(sample));
LOG(Media, "WebCoreDecompressionSession::enqueueDecodedSample(%p) - presentationTime(%s), framesBeingDecoded(%d), producerQueue(%s -> %s)", this, toString(presentationTime).utf8().data(), m_framesBeingDecoded, toString(begin).utf8().data(), toString(end).utf8().data());
#endif
if (m_timebase)
CMTimebaseSetTimerDispatchSourceToFireImmediately(m_timebase.get(), m_timerSource.get());
if (m_hasAvailableFrameCallback) {
std::function<void()> callback { m_hasAvailableFrameCallback };
m_hasAvailableFrameCallback = nullptr;
RefPtr<WebCoreDecompressionSession> protectedThis { this };
dispatch_async(dispatch_get_main_queue(), [protectedThis, callback] {
callback();
});
}
}
bool WebCoreDecompressionSession::isReadyForMoreMediaData() const
{
CMItemCount producerCount = m_producerQueue ? CMBufferQueueGetBufferCount(m_producerQueue.get()) : 0;
return m_framesBeingDecoded + producerCount <= kHighWaterMark;
}
void WebCoreDecompressionSession::requestMediaDataWhenReady(std::function<void()> notificationCallback)
{
LOG(Media, "WebCoreDecompressionSession::requestMediaDataWhenReady(%p), hasNotificationCallback(%d)", this, !!notificationCallback);
m_notificationCallback = notificationCallback;
if (notificationCallback && isReadyForMoreMediaData()) {
RefPtr<WebCoreDecompressionSession> protectedThis { this };
dispatch_async(dispatch_get_main_queue(), [protectedThis] {
if (protectedThis->m_notificationCallback)
protectedThis->m_notificationCallback();
});
}
}
void WebCoreDecompressionSession::stopRequestingMediaData()
{
LOG(Media, "WebCoreDecompressionSession::stopRequestingMediaData(%p)", this);
m_notificationCallback = nullptr;
}
void WebCoreDecompressionSession::notifyWhenHasAvailableVideoFrame(std::function<void()> callback)
{
if (callback && m_producerQueue && !CMBufferQueueIsEmpty(m_producerQueue.get())) {
dispatch_async(dispatch_get_main_queue(), [callback] {
callback();
});
return;
}
m_hasAvailableFrameCallback = callback;
}
RetainPtr<CVPixelBufferRef> WebCoreDecompressionSession::imageForTime(const MediaTime& time, ImageForTimeFlags flags)
{
if (CMBufferQueueIsEmpty(m_producerQueue.get())) {
LOG(Media, "WebCoreDecompressionSession::imageForTime(%p) - time(%s), queue empty", this, toString(time).utf8().data());
return nullptr;
}
bool allowEarlier = flags == WebCoreDecompressionSession::AllowEarlier;
bool allowLater = flags == WebCoreDecompressionSession::AllowLater;
MediaTime startTime = toMediaTime(CMBufferQueueGetFirstPresentationTimeStamp(m_producerQueue.get()));
MediaTime endTime = toMediaTime(CMBufferQueueGetEndPresentationTimeStamp(m_producerQueue.get()));
if (!allowLater && time < startTime) {
LOG(Media, "WebCoreDecompressionSession::imageForTime(%p) - time(%s) too early for queue(%s -> %s)", this, toString(time).utf8().data(), toString(startTime).utf8().data(), toString(endTime).utf8().data());
return nullptr;
}
while (CMSampleBufferRef firstSample = (CMSampleBufferRef)CMBufferQueueGetHead(m_producerQueue.get())) {
MediaTime presentationTimestamp = toMediaTime(CMSampleBufferGetPresentationTimeStamp(firstSample));
MediaTime duration = toMediaTime(CMSampleBufferGetDuration(firstSample));
MediaTime presentationEndTimestamp = presentationTimestamp + duration;
if (!allowLater && presentationTimestamp > time)
return nullptr;
if (!allowEarlier && presentationEndTimestamp < time) {
CFRelease(CMBufferQueueDequeueAndRetain(m_producerQueue.get()));
continue;
}
RetainPtr<CMSampleBufferRef> currentSample = adoptCF((CMSampleBufferRef)CMBufferQueueDequeueAndRetain(m_producerQueue.get()));
RetainPtr<CVPixelBufferRef> imageBuffer = (CVPixelBufferRef)CMSampleBufferGetImageBuffer(currentSample.get());
ASSERT(CFGetTypeID(imageBuffer.get()) == CVPixelBufferGetTypeID());
if (m_timebase)
CMTimebaseSetTimerDispatchSourceToFireImmediately(m_timebase.get(), m_timerSource.get());
LOG(Media, "WebCoreDecompressionSession::imageForTime(%p) - found sample for time(%s) in queue(%s -> %s)", this, toString(time).utf8().data(), toString(startTime).utf8().data(), toString(endTime).utf8().data());
return imageBuffer;
}
if (m_timebase)
CMTimebaseSetTimerDispatchSourceToFireImmediately(m_timebase.get(), m_timerSource.get());
LOG(Media, "WebCoreDecompressionSession::imageForTime(%p) - no matching sample for time(%s) in queue(%s -> %s)", this, toString(time).utf8().data(), toString(startTime).utf8().data(), toString(endTime).utf8().data());
return nullptr;
}
void WebCoreDecompressionSession::flush()
{
dispatch_sync(m_decompressionQueue.get(), [protectedThis = RefPtr<WebCoreDecompressionSession>(this)] {
CMBufferQueueReset(protectedThis->m_producerQueue.get());
dispatch_sync(protectedThis->m_enqueingQueue.get(), [protectedThis] {
CMBufferQueueReset(protectedThis->m_consumerQueue.get());
});
});
}
CMTime WebCoreDecompressionSession::getDecodeTime(CMBufferRef buf, void*)
{
ASSERT(CFGetTypeID(buf) == CMSampleBufferGetTypeID());
CMSampleBufferRef sample = (CMSampleBufferRef)(buf);
return CMSampleBufferGetDecodeTimeStamp(sample);
}
CMTime WebCoreDecompressionSession::getPresentationTime(CMBufferRef buf, void*)
{
ASSERT(CFGetTypeID(buf) == CMSampleBufferGetTypeID());
CMSampleBufferRef sample = (CMSampleBufferRef)(buf);
return CMSampleBufferGetPresentationTimeStamp(sample);
}
CMTime WebCoreDecompressionSession::getDuration(CMBufferRef buf, void*)
{
ASSERT(CFGetTypeID(buf) == CMSampleBufferGetTypeID());
CMSampleBufferRef sample = (CMSampleBufferRef)(buf);
return CMSampleBufferGetDuration(sample);
}
CFComparisonResult WebCoreDecompressionSession::compareBuffers(CMBufferRef buf1, CMBufferRef buf2, void* refcon)
{
return (CFComparisonResult)CMTimeCompare(getPresentationTime(buf1, refcon), getPresentationTime(buf2, refcon));
}
}
#endif