[Win] Update Media Player logic for new Legible Output API
[WebKit-https.git] / Source / WebCore / platform / graphics / avfoundation / cf / InbandTextTrackPrivateAVCF.cpp
index 1eecba5..f7cb476 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Apple Inc. All rights reserved.
+ * Copyright (C) 2013, 2015 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
 #include "InbandTextTrackPrivateAVCF.h"
 
 #include "FloatConversion.h"
+#include "ISOVTTCue.h"
 #include "InbandTextTrackPrivate.h"
 #include "InbandTextTrackPrivateAVF.h"
 #include "Logging.h"
+#include "MediaTimeAVFoundation.h"
 #include "SoftLinking.h"
+#include <JavaScriptCore/ArrayBuffer.h>
 
 #include <AVFoundationCF/AVCFPlayerItemLegibleOutput.h>
 #include <AVFoundationCF/AVFoundationCF.h>
@@ -45,8 +48,8 @@
 
 namespace WebCore {
 
-InbandTextTrackPrivateAVCF::InbandTextTrackPrivateAVCF(AVFInbandTrackParent* player, AVCFMediaSelectionOptionRef selection)
-    : InbandTextTrackPrivateAVF(player)
+InbandTextTrackPrivateAVCF::InbandTextTrackPrivateAVCF(AVFInbandTrackParent* player, AVCFMediaSelectionOptionRef selection, InbandTextTrackPrivate::CueFormat format)
+    : InbandTextTrackPrivateAVF(player, format)
     , m_mediaSelectionOption(selection)
 {
 }
@@ -143,13 +146,16 @@ AtomicString InbandTextTrackPrivateAVCF::label() const
 
     RetainPtr<CFArrayRef> commonMetaData = adoptCF(AVCFMediaSelectionOptionCopyCommonMetadata(mediaSelectionOption()));
     RetainPtr<CFArrayRef> titles = adoptCF(AVCFMetadataItemCopyItemsWithKeyAndKeySpace(commonMetaData.get(), AVCFMetadataCommonKeyTitle, AVCFMetadataKeySpaceCommon));
+    if (!titles)
+        return emptyAtom;
+
     CFIndex titlesCount = CFArrayGetCount(titles.get());
     if (!titlesCount)
         return emptyAtom;
 
     // If possible, return a title in one of the user's preferred languages.
-    RetainPtr<CFArrayRef> preferredLanguages = CFLocaleCopyPreferredLanguages();
-    RetainPtr<CFArrayRef> titlesForPreferredLanguages = AVCFMediaSelectionCopyOptionsFromArrayFilteredAndSortedAccordingToPreferredLanguages(titles.get(), preferredLanguages.get());
+    RetainPtr<CFArrayRef> preferredLanguages = adoptCF(CFLocaleCopyPreferredLanguages());
+    RetainPtr<CFArrayRef> titlesForPreferredLanguages = adoptCF(AVCFMediaSelectionCopyOptionsFromArrayFilteredAndSortedAccordingToPreferredLanguages(titles.get(), preferredLanguages.get()));
     CFIndex preferredTitlesCount = CFArrayGetCount(titlesForPreferredLanguages.get());
     if (preferredTitlesCount) {
         AVCFMetadataItemRef titleMetadata = static_cast<AVCFMetadataItemRef>(CFArrayGetValueAtIndex(titlesForPreferredLanguages.get(), 0));
@@ -170,6 +176,9 @@ AtomicString InbandTextTrackPrivateAVCF::language() const
         return emptyAtom;
 
     RetainPtr<CFLocaleRef> locale = adoptCF(AVCFMediaSelectionOptionCopyLocale(mediaSelectionOption()));
+    if (!locale)
+        return emptyAtom;
+
     return CFLocaleGetIdentifier(locale.get());
 }
 
@@ -178,6 +187,29 @@ bool InbandTextTrackPrivateAVCF::isDefault() const
     return false;
 }
 
+bool InbandTextTrackPrivateAVCF::readNativeSampleBuffer(CFArrayRef nativeSamples, CFIndex index, RefPtr<ArrayBuffer>& buffer, MediaTime& duration, CMFormatDescriptionRef& formatDescription)
+{
+    const AVCFPlayerItemLegibleOutputSample* sampleBuffer = reinterpret_cast<const AVCFPlayerItemLegibleOutputSample*>(CFArrayGetValueAtIndex(nativeSamples, index));
+    if (!sampleBuffer)
+        return false;
+
+    duration = toMediaTime(sampleBuffer->duration);
+    formatDescription = sampleBuffer->formatDescription;
+
+    size_t bufferLength = CFDataGetLength(sampleBuffer->buffer);
+    if (bufferLength < ISOBox::boxHeaderSize()) {
+        LOG(Media, "InbandTextTrackPrivateLegacyAVCF::readNativeSampleBuffer(%p) - ERROR: CFBuffer size length unexpectedly small (%zu)!!", this, bufferLength);
+        return false;
+    }
+
+    m_sampleInputBuffer.resize(m_sampleInputBuffer.size() + bufferLength);
+    CFDataGetBytes(sampleBuffer->buffer, CFRangeMake(0, bufferLength), reinterpret_cast<UInt8*>(m_sampleInputBuffer.data()) + m_sampleInputBuffer.size() - bufferLength);
+
+    buffer = ArrayBuffer::create(m_sampleInputBuffer.data(), m_sampleInputBuffer.size());
+
+    return true;
+}
+
 } // namespace WebCore
 
 #endif