1/*
2 * Copyright (C) 2007, 2008, 2009, 2010 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 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.
24 */
25#include "config.h"
26
27#include "QTMovieTask.h"
28
29// Put Movies.h first so build failures here point clearly to QuickTime
30#include <Movies.h>
31
32#include <wtf/HashSet.h>
33#include <wtf/StdLibExtras.h>
34#include <wtf/Vector.h>
35
36QTMovieTask::QTMovieTask()
37    : m_setTaskTimerDelay(0)
38    , m_stopTaskTimer(0)
39{
40}
41
42QTMovieTask::~QTMovieTask()
43{
44}
45
46QTMovieTask* QTMovieTask::sharedTask()
47{
48    static QTMovieTask* s_sharedTask = new QTMovieTask;
49    return s_sharedTask;
50}
51
52void QTMovieTask::updateTaskTimer(double maxInterval, double minInterval)
53{
54    ASSERT(m_setTaskTimerDelay);
55    if (!m_setTaskTimerDelay)
56        return;
57
58    ASSERT(m_stopTaskTimer);
59    if (!m_taskList.size() && m_stopTaskTimer) {
60        m_stopTaskTimer();
61        return;
62    }
63
64    long intervalInMS;
65    OSStatus status = QTGetTimeUntilNextTask(&intervalInMS, 1000);
66    double interval = intervalInMS / 1000.0;
67    if (interval < minInterval)
68        interval = minInterval;
69    if (interval > maxInterval)
70        interval = maxInterval;
71    m_setTaskTimerDelay(interval);
72}
73
74void QTMovieTask::fireTaskClients()
75{
76    Vector<QTMovieTaskClient*> clients;
77    copyToVector(m_taskList, clients);
78    for (Vector<QTMovieTaskClient*>::iterator i = clients.begin(); i != clients.end(); ++i)
79        (*i)->task();
80}
81
82void QTMovieTask::addTaskClient(QTMovieTaskClient* client)
83{
84    ASSERT(client);
85    if (!client)
86        return;
87
88    m_taskList.add(client);
89}
90
91void QTMovieTask::removeTaskClient(QTMovieTaskClient* client)
92{
93    ASSERT(client);
94    if (!client)
95        return;
96
97    m_taskList.remove(client);
98}
99
100void QTMovieTask::setTaskTimerFuncs(SetTaskTimerDelayFunc setTaskTimerDelay, StopTaskTimerFunc stopTaskTimer)
101{
102    m_setTaskTimerDelay = setTaskTimerDelay;
103    m_stopTaskTimer = stopTaskTimer;
104}
105
106