media/track/track-in-band-style.html is flaky
[WebKit-https.git] / Source / WebCore / platform / graphics / avfoundation / InbandTextTrackPrivateAVF.cpp
1 /*
2  * Copyright (C) 2012-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 #include "config.h"
27 #include "InbandTextTrackPrivateAVF.h"
28
29 #if ENABLE(VIDEO) && (USE(AVFOUNDATION) || PLATFORM(IOS_FAMILY))
30
31 #include "ISOVTTCue.h"
32 #include "InbandTextTrackPrivateClient.h"
33 #include "Logging.h"
34 #include "MediaPlayer.h"
35 #include <JavaScriptCore/ArrayBuffer.h>
36 #include <JavaScriptCore/DataView.h>
37 #include <JavaScriptCore/Int8Array.h>
38 #include <pal/avfoundation/MediaTimeAVFoundation.h>
39 #include <pal/cf/CoreMediaSoftLink.h>
40 #include <wtf/MediaTime.h>
41 #include <wtf/StringPrintStream.h>
42 #include <wtf/text/CString.h>
43 #include <wtf/text/StringBuilder.h>
44 #include <wtf/text/WTFString.h>
45 #include <wtf/unicode/CharacterNames.h>
46
47 namespace WebCore {
48
49 AVFInbandTrackParent::~AVFInbandTrackParent() = default;
50
51 InbandTextTrackPrivateAVF::InbandTextTrackPrivateAVF(AVFInbandTrackParent* owner, CueFormat format)
52     : InbandTextTrackPrivate(format)
53     , m_owner(owner)
54     , m_pendingCueStatus(None)
55     , m_index(0)
56     , m_hasBeenReported(false)
57     , m_seeking(false)
58     , m_haveReportedVTTHeader(false)
59 {
60 }
61
62 InbandTextTrackPrivateAVF::~InbandTextTrackPrivateAVF()
63 {
64     disconnect();
65 }
66
67 static bool makeRGBA32FromARGBCFArray(CFArrayRef colorArray, RGBA32& color)
68 {
69     if (CFArrayGetCount(colorArray) < 4)
70         return false;
71
72     float componentArray[4];
73     for (int i = 0; i < 4; i++) {
74         CFNumberRef value = static_cast<CFNumberRef>(CFArrayGetValueAtIndex(colorArray, i));
75         if (CFGetTypeID(value) != CFNumberGetTypeID())
76             return false;
77
78         float component;
79         CFNumberGetValue(value, kCFNumberFloatType, &component);
80         componentArray[i] = component;
81     }
82
83     color = makeRGBA32FromFloats(componentArray[1], componentArray[2], componentArray[3], componentArray[0]);
84     return true;
85 }
86
87 void InbandTextTrackPrivateAVF::processCueAttributes(CFAttributedStringRef attributedString, GenericCueData& cueData)
88 {
89     using namespace PAL;
90     // Some of the attributes we translate into per-cue WebVTT settings are repeated on each part of an attributed string so only
91     // process the first instance of each.
92     enum AttributeFlags {
93         Line = 1 << 0,
94         Position = 1 << 1,
95         Size = 1 << 2,
96         Vertical = 1 << 3,
97         Align = 1 << 4,
98         FontName = 1 << 5
99     };
100     unsigned processed = 0;
101
102     StringBuilder content;
103     String attributedStringValue = CFAttributedStringGetString(attributedString);
104     CFIndex length = attributedStringValue.length();
105     if (!length)
106         return;
107
108     CFRange effectiveRange = CFRangeMake(0, 0);
109     while ((effectiveRange.location + effectiveRange.length) < length) {
110
111         CFDictionaryRef attributes = CFAttributedStringGetAttributes(attributedString, effectiveRange.location + effectiveRange.length, &effectiveRange);
112         if (!attributes)
113             continue;
114
115         StringBuilder tagStart;
116         CFStringRef valueString;
117         String tagEnd;
118         CFIndex attributeCount = CFDictionaryGetCount(attributes);
119         Vector<const void*> keys(attributeCount);
120         Vector<const void*> values(attributeCount);
121         CFDictionaryGetKeysAndValues(attributes, keys.data(), values.data());
122
123         for (CFIndex i = 0; i < attributeCount; ++i) {
124             CFStringRef key = static_cast<CFStringRef>(keys[i]);
125             CFTypeRef value = values[i];
126             if (CFGetTypeID(key) != CFStringGetTypeID() || !CFStringGetLength(key))
127                 continue;
128
129             if (CFStringCompare(key, kCMTextMarkupAttribute_Alignment, 0) == kCFCompareEqualTo) {
130                 valueString = static_cast<CFStringRef>(value);
131                 if (CFGetTypeID(valueString) != CFStringGetTypeID() || !CFStringGetLength(valueString))
132                     continue;
133                 if (processed & Align)
134                     continue;
135                 processed |= Align;
136
137                 if (CFStringCompare(valueString, kCMTextMarkupAlignmentType_Start, 0) == kCFCompareEqualTo)
138                     cueData.setAlign(GenericCueData::Start);
139                 else if (CFStringCompare(valueString, kCMTextMarkupAlignmentType_Middle, 0) == kCFCompareEqualTo)
140                     cueData.setAlign(GenericCueData::Middle);
141                 else if (CFStringCompare(valueString, kCMTextMarkupAlignmentType_End, 0) == kCFCompareEqualTo)
142                     cueData.setAlign(GenericCueData::End);
143                 else
144                     ASSERT_NOT_REACHED();
145
146                 continue;
147             }
148
149             if (CFStringCompare(key, kCMTextMarkupAttribute_BoldStyle, 0) == kCFCompareEqualTo) {
150                 if (static_cast<CFBooleanRef>(value) != kCFBooleanTrue)
151                     continue;
152
153                 tagStart.appendLiteral("<b>");
154                 tagEnd = "</b>" + tagEnd;
155                 continue;
156             }
157
158             if (CFStringCompare(key, kCMTextMarkupAttribute_ItalicStyle, 0) == kCFCompareEqualTo) {
159                 if (static_cast<CFBooleanRef>(value) != kCFBooleanTrue)
160                     continue;
161
162                 tagStart.appendLiteral("<i>");
163                 tagEnd = "</i>" + tagEnd;
164                 continue;
165             }
166
167             if (CFStringCompare(key, kCMTextMarkupAttribute_UnderlineStyle, 0) == kCFCompareEqualTo) {
168                 if (static_cast<CFBooleanRef>(value) != kCFBooleanTrue)
169                     continue;
170
171                 tagStart.appendLiteral("<u>");
172                 tagEnd = "</u>" + tagEnd;
173                 continue;
174             }
175
176             if (CFStringCompare(key, kCMTextMarkupAttribute_OrthogonalLinePositionPercentageRelativeToWritingDirection, 0) == kCFCompareEqualTo) {
177                 if (CFGetTypeID(value) != CFNumberGetTypeID())
178                     continue;
179                 if (processed & Line)
180                     continue;
181                 processed |= Line;
182
183                 CFNumberRef valueNumber = static_cast<CFNumberRef>(value);
184                 double line;
185                 CFNumberGetValue(valueNumber, kCFNumberFloat64Type, &line);
186                 cueData.setLine(line);
187                 continue;
188             }
189
190             if (CFStringCompare(key, kCMTextMarkupAttribute_TextPositionPercentageRelativeToWritingDirection, 0) == kCFCompareEqualTo) {
191                 if (CFGetTypeID(value) != CFNumberGetTypeID())
192                     continue;
193                 if (processed & Position)
194                     continue;
195                 processed |= Position;
196
197                 CFNumberRef valueNumber = static_cast<CFNumberRef>(value);
198                 double position;
199                 CFNumberGetValue(valueNumber, kCFNumberFloat64Type, &position);
200                 cueData.setPosition(position);
201                 continue;
202             }
203
204             if (CFStringCompare(key, kCMTextMarkupAttribute_WritingDirectionSizePercentage, 0) == kCFCompareEqualTo) {
205                 if (CFGetTypeID(value) != CFNumberGetTypeID())
206                     continue;
207                 if (processed & Size)
208                     continue;
209                 processed |= Size;
210
211                 CFNumberRef valueNumber = static_cast<CFNumberRef>(value);
212                 double size;
213                 CFNumberGetValue(valueNumber, kCFNumberFloat64Type, &size);
214                 cueData.setSize(size);
215                 continue;
216             }
217
218             if (CFStringCompare(key, kCMTextMarkupAttribute_VerticalLayout, 0) == kCFCompareEqualTo) {
219                 valueString = static_cast<CFStringRef>(value);
220                 if (CFGetTypeID(valueString) != CFStringGetTypeID() || !CFStringGetLength(valueString))
221                     continue;
222                 
223                 if (CFStringCompare(valueString, kCMTextVerticalLayout_LeftToRight, 0) == kCFCompareEqualTo)
224                     tagStart.append(leftToRightMark);
225                 else if (CFStringCompare(valueString, kCMTextVerticalLayout_RightToLeft, 0) == kCFCompareEqualTo)
226                     tagStart.append(rightToLeftMark);
227                 continue;
228             }
229
230             if (CFStringCompare(key, kCMTextMarkupAttribute_BaseFontSizePercentageRelativeToVideoHeight, 0) == kCFCompareEqualTo) {
231                 if (CFGetTypeID(value) != CFNumberGetTypeID())
232                     continue;
233                 
234                 CFNumberRef valueNumber = static_cast<CFNumberRef>(value);
235                 double baseFontSize;
236                 CFNumberGetValue(valueNumber, kCFNumberFloat64Type, &baseFontSize);
237                 cueData.setBaseFontSize(baseFontSize);
238                 continue;
239             }
240
241             if (CFStringCompare(key, kCMTextMarkupAttribute_RelativeFontSize, 0) == kCFCompareEqualTo) {
242                 if (CFGetTypeID(value) != CFNumberGetTypeID())
243                     continue;
244                 
245                 CFNumberRef valueNumber = static_cast<CFNumberRef>(value);
246                 double relativeFontSize;
247                 CFNumberGetValue(valueNumber, kCFNumberFloat64Type, &relativeFontSize);
248                 cueData.setRelativeFontSize(relativeFontSize);
249                 continue;
250             }
251
252             if (CFStringCompare(key, kCMTextMarkupAttribute_FontFamilyName, 0) == kCFCompareEqualTo) {
253                 valueString = static_cast<CFStringRef>(value);
254                 if (CFGetTypeID(valueString) != CFStringGetTypeID() || !CFStringGetLength(valueString))
255                     continue;
256                 if (processed & FontName)
257                     continue;
258                 processed |= FontName;
259                 
260                 cueData.setFontName(valueString);
261                 continue;
262             }
263
264             if (CFStringCompare(key, kCMTextMarkupAttribute_ForegroundColorARGB, 0) == kCFCompareEqualTo) {
265                 CFArrayRef arrayValue = static_cast<CFArrayRef>(value);
266                 if (CFGetTypeID(arrayValue) != CFArrayGetTypeID())
267                     continue;
268                 
269                 RGBA32 color;
270                 if (!makeRGBA32FromARGBCFArray(arrayValue, color))
271                     continue;
272                 cueData.setForegroundColor(color);
273                 continue;
274             }
275             
276             if (CFStringCompare(key, kCMTextMarkupAttribute_BackgroundColorARGB, 0) == kCFCompareEqualTo) {
277                 CFArrayRef arrayValue = static_cast<CFArrayRef>(value);
278                 if (CFGetTypeID(arrayValue) != CFArrayGetTypeID())
279                     continue;
280                 
281                 RGBA32 color;
282                 if (!makeRGBA32FromARGBCFArray(arrayValue, color))
283                     continue;
284                 cueData.setBackgroundColor(color);
285                 continue;
286             }
287
288             if (CFStringCompare(key, kCMTextMarkupAttribute_CharacterBackgroundColorARGB, 0) == kCFCompareEqualTo) {
289                 CFArrayRef arrayValue = static_cast<CFArrayRef>(value);
290                 if (CFGetTypeID(arrayValue) != CFArrayGetTypeID())
291                     continue;
292                 
293                 RGBA32 color;
294                 if (!makeRGBA32FromARGBCFArray(arrayValue, color))
295                     continue;
296                 cueData.setHighlightColor(color);
297                 continue;
298             }
299         }
300
301         content.append(tagStart);
302         content.append(attributedStringValue.substring(effectiveRange.location, effectiveRange.length));
303         content.append(tagEnd);
304     }
305
306     if (content.length())
307         cueData.setContent(content.toString());
308 }
309
310 void InbandTextTrackPrivateAVF::processCue(CFArrayRef attributedStrings, CFArrayRef nativeSamples, const MediaTime& time)
311 {
312     if (!client())
313         return;
314
315     processAttributedStrings(attributedStrings, time);
316     processNativeSamples(nativeSamples, time);
317 }
318
319 void InbandTextTrackPrivateAVF::processAttributedStrings(CFArrayRef attributedStrings, const MediaTime& time)
320 {
321     CFIndex count = attributedStrings ? CFArrayGetCount(attributedStrings) : 0;
322
323     if (count)
324         INFO_LOG(LOGIDENTIFIER, "added ", count, count > 1 ? " cues" : " cue", " at time ", time);
325
326     Vector<Ref<GenericCueData>> arrivingCues;
327     if (count) {
328         for (CFIndex i = 0; i < count; i++) {
329             CFAttributedStringRef attributedString = static_cast<CFAttributedStringRef>(CFArrayGetValueAtIndex(attributedStrings, i));
330
331             if (!attributedString || !CFAttributedStringGetLength(attributedString))
332                 continue;
333
334             auto cueData = GenericCueData::create();
335             processCueAttributes(attributedString, cueData.get());
336             if (!cueData->content().length())
337                 continue;
338
339             cueData->setStartTime(time);
340             cueData->setEndTime(MediaTime::positiveInfiniteTime());
341
342             // AVFoundation cue "position" is to the center of the text so adjust relative to the edge because we will use it to
343             // set CSS "left".
344             if (cueData->position() >= 0 && cueData->size() > 0)
345                 cueData->setPosition(cueData->position() - cueData->size() / 2);
346
347             cueData->setStatus(GenericCueData::Partial);
348
349             arrivingCues.append(WTFMove(cueData));
350         }
351     }
352
353     if (m_pendingCueStatus != None) {
354         // Cues do not have an explicit duration, they are displayed until the next "cue" (which might be empty) is emitted.
355         m_currentCueEndTime = time;
356
357         if (m_currentCueEndTime >= m_currentCueStartTime) {
358             for (auto& cueData : m_cues) {
359                 // See if one of the newly-arrived cues is an extension of this cue.
360                 Vector<Ref<GenericCueData>> nonExtensionCues;
361                 for (auto& arrivingCue : arrivingCues) {
362                     if (!arrivingCue->doesExtendCueData(*cueData))
363                         nonExtensionCues.append(WTFMove(arrivingCue));
364                     else
365                         INFO_LOG(LOGIDENTIFIER, "found an extension cue ", *cueData);
366                 }
367
368                 bool currentCueIsExtended = (arrivingCues.size() != nonExtensionCues.size());
369
370                 arrivingCues = WTFMove(nonExtensionCues);
371                 
372                 if (currentCueIsExtended)
373                     continue;
374
375                 if (m_pendingCueStatus == Valid) {
376                     cueData->setEndTime(m_currentCueEndTime);
377                     cueData->setStatus(GenericCueData::Complete);
378
379                     INFO_LOG(LOGIDENTIFIER, "updating cue ", *cueData);
380
381                     client()->updateGenericCue(*cueData);
382                 } else {
383                     // We have to assume that the implicit duration is invalid for cues delivered during a seek because the AVF decode pipeline may not
384                     // see every cue, so DO NOT update cue duration while seeking.
385                     INFO_LOG(LOGIDENTIFIER, "ignoring cue delivered during seek ", *cueData);
386                 }
387             }
388         } else
389             ERROR_LOG(LOGIDENTIFIER, "negative length cue(s): ", MediaTimeRange { m_currentCueStartTime, m_currentCueEndTime });
390
391         removeCompletedCues();
392     }
393
394     if (arrivingCues.isEmpty())
395         return;
396
397     m_currentCueStartTime = time;
398
399     for (auto& cueData : arrivingCues) {
400         m_cues.append(cueData.ptr());
401         INFO_LOG(LOGIDENTIFIER, "adding cue ", cueData.get());
402         client()->addGenericCue(cueData);
403     }
404
405     m_pendingCueStatus = seeking() ? DeliveredDuringSeek : Valid;
406 }
407
408 void InbandTextTrackPrivateAVF::beginSeeking()
409 {
410     // Forget any partially accumulated cue data as the seek could be to a time outside of the cue's
411     // range, which will mean that the next cue delivered will result in the current cue getting the
412     // incorrect duration.
413     resetCueValues();
414     m_seeking = true;
415 }
416
417 void InbandTextTrackPrivateAVF::disconnect()
418 {
419     m_owner = 0;
420     m_index = 0;
421 }
422
423 void InbandTextTrackPrivateAVF::removeCompletedCues()
424 {
425     if (client()) {
426         long currentCue = m_cues.size() - 1;
427         for (; currentCue >= 0; --currentCue) {
428             auto& cue = m_cues[currentCue];
429             if (cue->status() != GenericCueData::Complete)
430                 continue;
431
432             INFO_LOG(LOGIDENTIFIER, "removing cue ", *cue);
433
434             m_cues.remove(currentCue);
435         }
436     }
437
438     if (m_cues.isEmpty())
439         m_pendingCueStatus = None;
440
441     m_currentCueStartTime = MediaTime::zeroTime();
442     m_currentCueEndTime = MediaTime::zeroTime();
443 }
444
445 void InbandTextTrackPrivateAVF::resetCueValues()
446 {
447     if (m_currentCueEndTime && m_cues.size())
448         INFO_LOG(LOGIDENTIFIER, "flushing data for cues: start = ", m_currentCueStartTime);
449
450     if (auto* client = this->client()) {
451         for (auto& cue : m_cues)
452             client->removeGenericCue(*cue);
453     }
454
455     m_cues.shrink(0);
456     m_pendingCueStatus = None;
457     m_currentCueStartTime = MediaTime::zeroTime();
458     m_currentCueEndTime = MediaTime::zeroTime();
459 }
460
461 void InbandTextTrackPrivateAVF::setMode(InbandTextTrackPrivate::Mode newMode)
462 {
463     if (!m_owner)
464         return;
465
466     InbandTextTrackPrivate::Mode oldMode = mode();
467     InbandTextTrackPrivate::setMode(newMode);
468
469     if (oldMode == newMode)
470         return;
471
472     m_owner->trackModeChanged();
473 }
474
475 void InbandTextTrackPrivateAVF::processNativeSamples(CFArrayRef nativeSamples, const MediaTime& presentationTime)
476 {
477     using namespace PAL;
478
479     if (!nativeSamples)
480         return;
481
482     CFIndex count = CFArrayGetCount(nativeSamples);
483     if (!count)
484         return;
485
486     INFO_LOG(LOGIDENTIFIER, count, " sample buffers at time ", presentationTime);
487
488     for (CFIndex i = 0; i < count; i++) {
489         RefPtr<ArrayBuffer> buffer;
490         MediaTime duration;
491         CMFormatDescriptionRef formatDescription;
492         if (!readNativeSampleBuffer(nativeSamples, i, buffer, duration, formatDescription))
493             continue;
494
495         auto view = JSC::DataView::create(WTFMove(buffer), 0, buffer->byteLength());
496         auto peekResult = ISOBox::peekBox(view, 0);
497         if (!peekResult)
498             continue;
499
500         auto type = peekResult.value().first;
501         auto boxLength = peekResult.value().second;
502         if (boxLength > view->byteLength()) {
503             ERROR_LOG(LOGIDENTIFIER, "chunk  type = '", type.toString(), "', size = ", (size_t)boxLength, " larger than buffer length!");
504             continue;
505         }
506
507         INFO_LOG(LOGIDENTIFIER, "chunk  type = '", type.toString(), "', size = ", (size_t)boxLength);
508
509         do {
510             if (m_haveReportedVTTHeader || !formatDescription)
511                 break;
512
513             CFDictionaryRef extensions = CMFormatDescriptionGetExtensions(formatDescription);
514             if (!extensions)
515                 break;
516
517             CFDictionaryRef sampleDescriptionExtensions = static_cast<CFDictionaryRef>(CFDictionaryGetValue(extensions, kCMFormatDescriptionExtension_SampleDescriptionExtensionAtoms));
518             if (!sampleDescriptionExtensions)
519                 break;
520             
521             CFDataRef webvttHeaderData = static_cast<CFDataRef>(CFDictionaryGetValue(sampleDescriptionExtensions, CFSTR("vttC")));
522             if (!webvttHeaderData)
523                 break;
524
525             unsigned length = CFDataGetLength(webvttHeaderData);
526             if (!length)
527                 break;
528
529             // A WebVTT header is terminated by "One or more WebVTT line terminators" so append two line feeds to make sure the parser
530             // reccognized this string as a full header.
531             StringBuilder header;
532             header.append(reinterpret_cast<const unsigned char*>(CFDataGetBytePtr(webvttHeaderData)), length);
533             header.append("\n\n");
534
535             INFO_LOG(LOGIDENTIFIER, "VTT header ", &header);
536             client()->parseWebVTTFileHeader(header.toString());
537             m_haveReportedVTTHeader = true;
538         } while (0);
539
540         if (type == ISOWebVTTCue::boxTypeName()) {
541             ISOWebVTTCue cueData = ISOWebVTTCue(presentationTime, duration);
542             cueData.read(view);
543             INFO_LOG(LOGIDENTIFIER, "VTT cue data ", cueData);
544             client()->parseWebVTTCueData(cueData);
545         }
546
547         m_sampleInputBuffer.remove(0, (size_t)boxLength);
548     }
549 }
550
551 bool InbandTextTrackPrivateAVF::readNativeSampleBuffer(CFArrayRef nativeSamples, CFIndex index, RefPtr<ArrayBuffer>& buffer, MediaTime& duration, CMFormatDescriptionRef& formatDescription)
552 {
553     using namespace PAL;
554 #if OS(WINDOWS) && HAVE(AVCFPLAYERITEM_CALLBACK_VERSION_2)
555     return false;
556 #else
557     CMSampleBufferRef sampleBuffer = reinterpret_cast<CMSampleBufferRef>(const_cast<void*>(CFArrayGetValueAtIndex(nativeSamples, index)));
558     if (!sampleBuffer)
559         return false;
560
561     CMSampleTimingInfo timingInfo;
562     OSStatus status = CMSampleBufferGetSampleTimingInfo(sampleBuffer, index, &timingInfo);
563     if (status) {
564         ERROR_LOG(LOGIDENTIFIER, "CMSampleBufferGetSampleTimingInfo returned error ", status, "' for sample ", index);
565         return false;
566     }
567
568     duration = PAL::toMediaTime(timingInfo.duration);
569
570     CMBlockBufferRef blockBuffer = CMSampleBufferGetDataBuffer(sampleBuffer);
571     size_t bufferLength = CMBlockBufferGetDataLength(blockBuffer);
572     if (bufferLength < ISOBox::minimumBoxSize()) {
573         ERROR_LOG(LOGIDENTIFIER, "CMSampleBuffer size length unexpectedly small ", bufferLength);
574         return false;
575     }
576
577     m_sampleInputBuffer.grow(m_sampleInputBuffer.size() + bufferLength);
578     CMBlockBufferCopyDataBytes(blockBuffer, 0, bufferLength, m_sampleInputBuffer.data() + m_sampleInputBuffer.size() - bufferLength);
579
580     buffer = ArrayBuffer::create(m_sampleInputBuffer.data(), m_sampleInputBuffer.size());
581
582     formatDescription = CMSampleBufferGetFormatDescription(sampleBuffer);
583
584     return true;
585 #endif
586 }
587
588 } // namespace WebCore
589
590 #endif // ENABLE(VIDEO) && (USE(AVFOUNDATION) || PLATFORM(IOS_FAMILY))