50225c3487d72ed137fb5abc3355ad099744221e
[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             }
274             
275             if (CFStringCompare(key, kCMTextMarkupAttribute_BackgroundColorARGB, 0) == kCFCompareEqualTo) {
276                 CFArrayRef arrayValue = static_cast<CFArrayRef>(value);
277                 if (CFGetTypeID(arrayValue) != CFArrayGetTypeID())
278                     continue;
279                 
280                 RGBA32 color;
281                 if (!makeRGBA32FromARGBCFArray(arrayValue, color))
282                     continue;
283                 cueData.setBackgroundColor(color);
284             }
285
286             if (CFStringCompare(key, kCMTextMarkupAttribute_CharacterBackgroundColorARGB, 0) == kCFCompareEqualTo) {
287                 CFArrayRef arrayValue = static_cast<CFArrayRef>(value);
288                 if (CFGetTypeID(arrayValue) != CFArrayGetTypeID())
289                     continue;
290                 
291                 RGBA32 color;
292                 if (!makeRGBA32FromARGBCFArray(arrayValue, color))
293                     continue;
294                 cueData.setHighlightColor(color);
295             }
296         }
297
298         content.append(tagStart);
299         content.append(attributedStringValue.substring(effectiveRange.location, effectiveRange.length));
300         content.append(tagEnd);
301     }
302
303     if (content.length())
304         cueData.setContent(content.toString());
305 }
306
307 void InbandTextTrackPrivateAVF::processCue(CFArrayRef attributedStrings, CFArrayRef nativeSamples, const MediaTime& time)
308 {
309     if (!client())
310         return;
311
312     processAttributedStrings(attributedStrings, time);
313     processNativeSamples(nativeSamples, time);
314 }
315
316 void InbandTextTrackPrivateAVF::processAttributedStrings(CFArrayRef attributedStrings, const MediaTime& time)
317 {
318     CFIndex count = attributedStrings ? CFArrayGetCount(attributedStrings) : 0;
319
320     if (count)
321         INFO_LOG(LOGIDENTIFIER, "added ", count, count > 1 ? " cues" : " cue", " at time ", time);
322
323     Vector<Ref<GenericCueData>> arrivingCues;
324     if (count) {
325         for (CFIndex i = 0; i < count; i++) {
326             CFAttributedStringRef attributedString = static_cast<CFAttributedStringRef>(CFArrayGetValueAtIndex(attributedStrings, i));
327
328             if (!attributedString || !CFAttributedStringGetLength(attributedString))
329                 continue;
330
331             auto cueData = GenericCueData::create();
332             processCueAttributes(attributedString, cueData.get());
333             if (!cueData->content().length())
334                 continue;
335
336             cueData->setStartTime(time);
337             cueData->setEndTime(MediaTime::positiveInfiniteTime());
338
339             // AVFoundation cue "position" is to the center of the text so adjust relative to the edge because we will use it to
340             // set CSS "left".
341             if (cueData->position() >= 0 && cueData->size() > 0)
342                 cueData->setPosition(cueData->position() - cueData->size() / 2);
343
344             cueData->setStatus(GenericCueData::Partial);
345
346             arrivingCues.append(WTFMove(cueData));
347         }
348     }
349
350     if (m_pendingCueStatus != None) {
351         // Cues do not have an explicit duration, they are displayed until the next "cue" (which might be empty) is emitted.
352         m_currentCueEndTime = time;
353
354         if (m_currentCueEndTime >= m_currentCueStartTime) {
355             for (auto& cueData : m_cues) {
356                 // See if one of the newly-arrived cues is an extension of this cue.
357                 Vector<Ref<GenericCueData>> nonExtensionCues;
358                 for (auto& arrivingCue : arrivingCues) {
359                     if (!arrivingCue->doesExtendCueData(*cueData))
360                         nonExtensionCues.append(WTFMove(arrivingCue));
361                     else
362                         INFO_LOG(LOGIDENTIFIER, "found an extension cue ", *cueData);
363                 }
364
365                 bool currentCueIsExtended = (arrivingCues.size() != nonExtensionCues.size());
366
367                 arrivingCues = WTFMove(nonExtensionCues);
368                 
369                 if (currentCueIsExtended)
370                     continue;
371
372                 if (m_pendingCueStatus == Valid) {
373                     cueData->setEndTime(m_currentCueEndTime);
374                     cueData->setStatus(GenericCueData::Complete);
375
376                     INFO_LOG(LOGIDENTIFIER, "updating cue ", *cueData);
377
378                     client()->updateGenericCue(*cueData);
379                 } else {
380                     // We have to assume that the implicit duration is invalid for cues delivered during a seek because the AVF decode pipeline may not
381                     // see every cue, so DO NOT update cue duration while seeking.
382                     INFO_LOG(LOGIDENTIFIER, "ignoring cue delivered during seek ", *cueData);
383                 }
384             }
385         } else
386             ERROR_LOG(LOGIDENTIFIER, "negative length cue(s): ", MediaTimeRange { m_currentCueStartTime, m_currentCueEndTime });
387
388         removeCompletedCues();
389     }
390
391     if (arrivingCues.isEmpty())
392         return;
393
394     m_currentCueStartTime = time;
395
396     for (auto& cueData : arrivingCues) {
397         m_cues.append(cueData.ptr());
398         INFO_LOG(LOGIDENTIFIER, "adding cue ", cueData.get());
399         client()->addGenericCue(cueData);
400     }
401
402     m_pendingCueStatus = seeking() ? DeliveredDuringSeek : Valid;
403 }
404
405 void InbandTextTrackPrivateAVF::beginSeeking()
406 {
407     // Forget any partially accumulated cue data as the seek could be to a time outside of the cue's
408     // range, which will mean that the next cue delivered will result in the current cue getting the
409     // incorrect duration.
410     resetCueValues();
411     m_seeking = true;
412 }
413
414 void InbandTextTrackPrivateAVF::disconnect()
415 {
416     m_owner = 0;
417     m_index = 0;
418 }
419
420 void InbandTextTrackPrivateAVF::removeCompletedCues()
421 {
422     if (client()) {
423         long currentCue = m_cues.size() - 1;
424         for (; currentCue >= 0; --currentCue) {
425             auto& cue = m_cues[currentCue];
426             if (cue->status() != GenericCueData::Complete)
427                 continue;
428
429             INFO_LOG(LOGIDENTIFIER, "removing cue ", *cue);
430
431             m_cues.remove(currentCue);
432         }
433     }
434
435     if (m_cues.isEmpty())
436         m_pendingCueStatus = None;
437
438     m_currentCueStartTime = MediaTime::zeroTime();
439     m_currentCueEndTime = MediaTime::zeroTime();
440 }
441
442 void InbandTextTrackPrivateAVF::resetCueValues()
443 {
444     if (m_currentCueEndTime && m_cues.size())
445         INFO_LOG(LOGIDENTIFIER, "flushing data for cues: start = ", m_currentCueStartTime);
446
447     if (auto* client = this->client()) {
448         for (auto& cue : m_cues)
449             client->removeGenericCue(*cue);
450     }
451
452     m_cues.shrink(0);
453     m_pendingCueStatus = None;
454     m_currentCueStartTime = MediaTime::zeroTime();
455     m_currentCueEndTime = MediaTime::zeroTime();
456 }
457
458 void InbandTextTrackPrivateAVF::setMode(InbandTextTrackPrivate::Mode newMode)
459 {
460     if (!m_owner)
461         return;
462
463     InbandTextTrackPrivate::Mode oldMode = mode();
464     InbandTextTrackPrivate::setMode(newMode);
465
466     if (oldMode == newMode)
467         return;
468
469     m_owner->trackModeChanged();
470 }
471
472 void InbandTextTrackPrivateAVF::processNativeSamples(CFArrayRef nativeSamples, const MediaTime& presentationTime)
473 {
474     using namespace PAL;
475
476     if (!nativeSamples)
477         return;
478
479     CFIndex count = CFArrayGetCount(nativeSamples);
480     if (!count)
481         return;
482
483     INFO_LOG(LOGIDENTIFIER, count, " sample buffers at time ", presentationTime);
484
485     for (CFIndex i = 0; i < count; i++) {
486         RefPtr<ArrayBuffer> buffer;
487         MediaTime duration;
488         CMFormatDescriptionRef formatDescription;
489         if (!readNativeSampleBuffer(nativeSamples, i, buffer, duration, formatDescription))
490             continue;
491
492         auto view = JSC::DataView::create(WTFMove(buffer), 0, buffer->byteLength());
493         auto peekResult = ISOBox::peekBox(view, 0);
494         if (!peekResult)
495             continue;
496
497         auto type = peekResult.value().first;
498         auto boxLength = peekResult.value().second;
499         if (boxLength > view->byteLength()) {
500             ERROR_LOG(LOGIDENTIFIER, "chunk  type = '", type.toString(), "', size = ", (size_t)boxLength, " larger than buffer length!");
501             continue;
502         }
503
504         INFO_LOG(LOGIDENTIFIER, "chunk  type = '", type.toString(), "', size = ", (size_t)boxLength);
505
506         do {
507             if (m_haveReportedVTTHeader || !formatDescription)
508                 break;
509
510             CFDictionaryRef extensions = CMFormatDescriptionGetExtensions(formatDescription);
511             if (!extensions)
512                 break;
513
514             CFDictionaryRef sampleDescriptionExtensions = static_cast<CFDictionaryRef>(CFDictionaryGetValue(extensions, kCMFormatDescriptionExtension_SampleDescriptionExtensionAtoms));
515             if (!sampleDescriptionExtensions)
516                 break;
517             
518             CFDataRef webvttHeaderData = static_cast<CFDataRef>(CFDictionaryGetValue(sampleDescriptionExtensions, CFSTR("vttC")));
519             if (!webvttHeaderData)
520                 break;
521
522             unsigned length = CFDataGetLength(webvttHeaderData);
523             if (!length)
524                 break;
525
526             // A WebVTT header is terminated by "One or more WebVTT line terminators" so append two line feeds to make sure the parser
527             // reccognized this string as a full header.
528             StringBuilder header;
529             header.append(reinterpret_cast<const unsigned char*>(CFDataGetBytePtr(webvttHeaderData)), length);
530             header.append("\n\n");
531
532             INFO_LOG(LOGIDENTIFIER, "VTT header ", &header);
533             client()->parseWebVTTFileHeader(header.toString());
534             m_haveReportedVTTHeader = true;
535         } while (0);
536
537         if (type == ISOWebVTTCue::boxTypeName()) {
538             ISOWebVTTCue cueData = ISOWebVTTCue(presentationTime, duration);
539             cueData.read(view);
540             INFO_LOG(LOGIDENTIFIER, "VTT cue data ", cueData);
541             client()->parseWebVTTCueData(cueData);
542         }
543
544         m_sampleInputBuffer.remove(0, (size_t)boxLength);
545     }
546 }
547
548 bool InbandTextTrackPrivateAVF::readNativeSampleBuffer(CFArrayRef nativeSamples, CFIndex index, RefPtr<ArrayBuffer>& buffer, MediaTime& duration, CMFormatDescriptionRef& formatDescription)
549 {
550     using namespace PAL;
551 #if OS(WINDOWS) && HAVE(AVCFPLAYERITEM_CALLBACK_VERSION_2)
552     return false;
553 #else
554     CMSampleBufferRef sampleBuffer = reinterpret_cast<CMSampleBufferRef>(const_cast<void*>(CFArrayGetValueAtIndex(nativeSamples, index)));
555     if (!sampleBuffer)
556         return false;
557
558     CMSampleTimingInfo timingInfo;
559     OSStatus status = CMSampleBufferGetSampleTimingInfo(sampleBuffer, index, &timingInfo);
560     if (status) {
561         ERROR_LOG(LOGIDENTIFIER, "CMSampleBufferGetSampleTimingInfo returned error ", status, "' for sample ", index);
562         return false;
563     }
564
565     duration = PAL::toMediaTime(timingInfo.duration);
566
567     CMBlockBufferRef blockBuffer = CMSampleBufferGetDataBuffer(sampleBuffer);
568     size_t bufferLength = CMBlockBufferGetDataLength(blockBuffer);
569     if (bufferLength < ISOBox::minimumBoxSize()) {
570         ERROR_LOG(LOGIDENTIFIER, "CMSampleBuffer size length unexpectedly small ", bufferLength);
571         return false;
572     }
573
574     m_sampleInputBuffer.grow(m_sampleInputBuffer.size() + bufferLength);
575     CMBlockBufferCopyDataBytes(blockBuffer, 0, bufferLength, m_sampleInputBuffer.data() + m_sampleInputBuffer.size() - bufferLength);
576
577     buffer = ArrayBuffer::create(m_sampleInputBuffer.data(), m_sampleInputBuffer.size());
578
579     formatDescription = CMSampleBufferGetFormatDescription(sampleBuffer);
580
581     return true;
582 #endif
583 }
584
585 } // namespace WebCore
586
587 #endif // ENABLE(VIDEO) && (USE(AVFOUNDATION) || PLATFORM(IOS_FAMILY))