2
* Copyright (C) 2007, 2008, 2009, 2010 Apple, Inc. All rights reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
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.
13
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, 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 COMPUTER, 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.
27
#include "QTMovieVisualContext.h"
29
#include "QTMovieTask.h"
31
#include <CVHostTime.h>
32
#include <ImageCompression.h>
35
#include <wtf/PassOwnPtr.h>
37
struct QTCVTimeStamp {
41
class QTMovieVisualContextPriv {
43
QTMovieVisualContextPriv(QTMovieVisualContext* parent, QTMovieVisualContextClient* client, QTPixelBuffer::Type contextType);
44
~QTMovieVisualContextPriv();
46
bool isImageAvailableForTime(const QTCVTimeStamp*) const;
47
QTPixelBuffer imageForTime(const QTCVTimeStamp*);
50
QTVisualContextRef visualContextRef();
52
void setMovie(PassRefPtr<QTMovie>);
53
QTMovie* movie() const;
55
static void imageAvailableCallback(QTVisualContextRef visualContext, const CVTimeStamp *timeStamp, void *refCon);
58
QTMovieVisualContext* m_parent;
59
QTMovieVisualContextClient* m_client;
60
QTVisualContextRef m_visualContext;
61
RefPtr<QTMovie> m_movie;
65
static CFDictionaryRef createPixelBufferOptionsDictionary(QTPixelBuffer::Type contextType)
67
const void* key = kQTVisualContextPixelBufferAttributesKey;
68
const void* value = QTPixelBuffer::createPixelBufferAttributesDictionary(contextType);
69
CFDictionaryRef pixelBufferOptions = CFDictionaryCreate(kCFAllocatorDefault, &key, &value, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
71
return pixelBufferOptions;
74
static CFDictionaryRef pixelBufferCreationOptions(QTPixelBuffer::Type contextType)
76
if (contextType == QTPixelBuffer::ConfigureForCAImageQueue) {
77
static CFDictionaryRef imageQueueOptions = createPixelBufferOptionsDictionary(contextType);
78
return imageQueueOptions;
81
ASSERT(contextType == QTPixelBuffer::ConfigureForCGImage);
82
static CFDictionaryRef cgImageOptions = createPixelBufferOptionsDictionary(contextType);
83
return cgImageOptions;
86
QTMovieVisualContextPriv::QTMovieVisualContextPriv(QTMovieVisualContext* parent, QTMovieVisualContextClient* client, QTPixelBuffer::Type contextType)
91
typedef OSStatus ( __cdecl *pfnQTPixelBufferContextCreate)(CFAllocatorRef, CFDictionaryRef, QTVisualContextRef*);
92
static pfnQTPixelBufferContextCreate pPixelBufferContextCreate = 0;
93
if (!pPixelBufferContextCreate) {
94
HMODULE qtmlDLL = ::LoadLibraryW(L"QTMLClient.dll");
97
pPixelBufferContextCreate = reinterpret_cast<pfnQTPixelBufferContextCreate>(GetProcAddress(qtmlDLL, "QTPixelBufferContextCreate"));
98
if (!pPixelBufferContextCreate)
102
OSStatus status = pPixelBufferContextCreate(kCFAllocatorDefault, pixelBufferCreationOptions(contextType), &m_visualContext);
103
if (status == noErr && m_visualContext)
104
QTVisualContextSetImageAvailableCallback(m_visualContext, &QTMovieVisualContextPriv::imageAvailableCallback, static_cast<void*>(this));
107
QTMovieVisualContextPriv::~QTMovieVisualContextPriv()
110
QTVisualContextSetImageAvailableCallback(m_visualContext, 0, 0);
113
bool QTMovieVisualContextPriv::isImageAvailableForTime(const QTCVTimeStamp* timeStamp) const
115
if (!m_visualContext)
118
return QTVisualContextIsNewImageAvailable(m_visualContext, reinterpret_cast<const CVTimeStamp*>(timeStamp));
121
QTPixelBuffer QTMovieVisualContextPriv::imageForTime(const QTCVTimeStamp* timeStamp)
123
QTPixelBuffer pixelBuffer;
124
if (m_visualContext) {
125
CVImageBufferRef newImage = 0;
126
OSStatus status = QTVisualContextCopyImageForTime(m_visualContext, kCFAllocatorDefault, reinterpret_cast<const CVTimeStamp*>(timeStamp), &newImage);
128
pixelBuffer.adopt(newImage);
133
void QTMovieVisualContextPriv::task()
136
QTVisualContextTask(m_visualContext);
139
QTVisualContextRef QTMovieVisualContextPriv::visualContextRef()
141
return m_visualContext;
144
void QTMovieVisualContextPriv::setMovie(PassRefPtr<QTMovie> movie)
146
if (movie == m_movie)
150
SetMovieVisualContext(m_movie->getMovieHandle(), 0);
155
OSStatus status = SetMovieVisualContext(movie->getMovieHandle(), m_visualContext);
160
QTMovie* QTMovieVisualContextPriv::movie() const
162
return m_movie.get();
165
void QTMovieVisualContextPriv::imageAvailableCallback(QTVisualContextRef visualContext, const CVTimeStamp *timeStamp, void *refCon)
170
QTMovieVisualContextPriv* vc = static_cast<QTMovieVisualContextPriv*>(refCon);
174
vc->m_client->imageAvailableForTime(reinterpret_cast<const QTCVTimeStamp*>(timeStamp));
177
PassRefPtr<QTMovieVisualContext> QTMovieVisualContext::create(QTMovieVisualContextClient* client, QTPixelBuffer::Type contextType)
179
return adoptRef(new QTMovieVisualContext(client, contextType));
182
QTMovieVisualContext::QTMovieVisualContext(QTMovieVisualContextClient* client, QTPixelBuffer::Type contextType)
183
: m_private(adoptPtr(new QTMovieVisualContextPriv(this, client, contextType)))
187
QTMovieVisualContext::~QTMovieVisualContext()
191
bool QTMovieVisualContext::isImageAvailableForTime(const QTCVTimeStamp* timeStamp) const
193
return m_private->isImageAvailableForTime(timeStamp);
196
QTPixelBuffer QTMovieVisualContext::imageForTime(const QTCVTimeStamp* timeStamp)
198
return m_private->imageForTime(timeStamp);
201
void QTMovieVisualContext::task()
206
QTVisualContextRef QTMovieVisualContext::visualContextRef()
208
return m_private->visualContextRef();
211
void QTMovieVisualContext::setMovie(PassRefPtr<QTMovie> movie)
213
m_private->setMovie(movie);
216
QTMovie* QTMovieVisualContext::movie() const
218
return m_private->movie();
221
double QTMovieVisualContext::currentHostTime()
223
return CVGetCurrentHostTime() / CVGetHostClockFrequency();