summaryrefslogtreecommitdiffstats
path: root/src/plugins/qt7/mediaplayer/qt7playersession.mm
blob: 1c1277a01606ae580ffb5a8ea9e701b5422fdfc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#import <QTKit/QTDataReference.h>
#import <QTKit/QTMovie.h>

#include "qt7backend.h"

#include "qt7playersession.h"
#include "qt7playercontrol.h"
#include "qt7videooutput.h"

#include <QtNetwork/qnetworkcookie.h>
#include <qmediaplaylistnavigator.h>

#include <CoreFoundation/CoreFoundation.h>
#include <Foundation/Foundation.h>

#include <QtCore/qdatetime.h>
#include <QtCore/qurl.h>

#include <QtCore/qdebug.h>

QT_USE_NAMESPACE

//#define QT_DEBUG_QT7

@interface QTMovieObserver : NSObject
{
@private
    QT7PlayerSession *m_session;
    QTMovie *m_movie;
}

- (QTMovieObserver *) initWithPlayerSession:(QT7PlayerSession*)session;
- (void) setMovie:(QTMovie *)movie;
- (void) processEOS:(NSNotification *)notification;
- (void) processLoadStateChange:(NSNotification *)notification;
- (void) processVolumeChange:(NSNotification *)notification;
- (void) processNaturalSizeChange :(NSNotification *)notification;
- (void) processPositionChange :(NSNotification *)notification;
@end

@implementation QTMovieObserver

- (QTMovieObserver *) initWithPlayerSession:(QT7PlayerSession*)session
{
    if (!(self = [super init]))
        return nil;

    self->m_session = session;
    return self;
}

- (void) setMovie:(QTMovie *)movie
{
    if (m_movie == movie)
        return;

    if (m_movie) {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        [m_movie release];
    }

    m_movie = movie;

    if (movie) {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(processEOS:)
                                                     name:QTMovieDidEndNotification
                                                   object:m_movie];

        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(processLoadStateChange:)
                                                     name:QTMovieLoadStateDidChangeNotification
                                                   object:m_movie];

        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(processVolumeChange:)
                                                     name:QTMovieVolumeDidChangeNotification
                                                   object:m_movie];

        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(processPositionChange:)
                                                     name:QTMovieTimeDidChangeNotification
                                                   object:m_movie];

        if (QSysInfo::MacintoshVersion >= QSysInfo::MV_10_6) {
            [[NSNotificationCenter defaultCenter] addObserver:self
                                                     selector:@selector(processNaturalSizeChange:)
                                                         name:@"QTMovieNaturalSizeDidChangeNotification"
                                                       object:m_movie];

        }
        else {
            [[NSNotificationCenter defaultCenter] addObserver:self
                                                     selector:@selector(processNaturalSizeChange:)
                                                         name:QTMovieEditedNotification
                                                       object:m_movie];
        }

        [movie retain];
    }
}

- (void) processEOS:(NSNotification *)notification
{
    Q_UNUSED(notification);
    QMetaObject::invokeMethod(m_session, "processEOS", Qt::AutoConnection);
}

- (void) processLoadStateChange:(NSNotification *)notification
{
    Q_UNUSED(notification);
    QMetaObject::invokeMethod(m_session, "processLoadStateChange", Qt::AutoConnection);
}

- (void) processVolumeChange:(NSNotification *)notification
{
    Q_UNUSED(notification);
    QMetaObject::invokeMethod(m_session, "processVolumeChange", Qt::AutoConnection);
}

- (void) processNaturalSizeChange :(NSNotification *)notification
{
    Q_UNUSED(notification);
    QMetaObject::invokeMethod(m_session, "processNaturalSizeChange", Qt::AutoConnection);
}

- (void) processPositionChange :(NSNotification *)notification
{
    Q_UNUSED(notification);
    QMetaObject::invokeMethod(m_session, "processPositionChange", Qt::AutoConnection);
}

@end

static inline NSString *qString2CFStringRef(const QString &string)
{
    return [NSString stringWithCharacters:reinterpret_cast<const UniChar *>(string.unicode()) length:string.length()];
}

QT7PlayerSession::QT7PlayerSession(QObject *parent)
   : QObject(parent)
   , m_QTMovie(0)
   , m_state(QMediaPlayer::StoppedState)
   , m_mediaStatus(QMediaPlayer::NoMedia)
   , m_mediaStream(0)
   , m_videoOutput(0)
   , m_muted(false)
   , m_tryingAsync(false)
   , m_volume(100)
   , m_rate(1.0)
   , m_duration(0)
   , m_videoAvailable(false)
   , m_audioAvailable(false)
{
    m_movieObserver = [[QTMovieObserver alloc] initWithPlayerSession:this];
}

QT7PlayerSession::~QT7PlayerSession()
{
    if (m_videoOutput)
        m_videoOutput->setMovie(0);

    [(QTMovieObserver*)m_movieObserver setMovie:nil];
    [(QTMovieObserver*)m_movieObserver release];
    [(QTMovie*)m_QTMovie release];
}

void *QT7PlayerSession::movie() const
{
    return m_QTMovie;
}

void QT7PlayerSession::setVideoOutput(QT7VideoOutput *output)
{
    if (m_videoOutput == output)
        return;

    if (m_videoOutput)
        m_videoOutput->setMovie(0);

    m_videoOutput = output;

    if (m_videoOutput && m_state != QMediaPlayer::StoppedState)
        m_videoOutput->setMovie(m_QTMovie);
}

qint64 QT7PlayerSession::position() const
{
    if (!m_QTMovie)
        return 0;

    QTTime qtTime = [(QTMovie*)m_QTMovie currentTime];

    return static_cast<quint64>(float(qtTime.timeValue) / float(qtTime.timeScale) * 1000.0f);
}

qint64 QT7PlayerSession::duration() const
{
    if (!m_QTMovie)
        return 0;

    QTTime qtTime = [(QTMovie*)m_QTMovie duration];

    return static_cast<quint64>(float(qtTime.timeValue) / float(qtTime.timeScale) * 1000.0f);
}

QMediaPlayer::State QT7PlayerSession::state() const
{
    return m_state;
}

QMediaPlayer::MediaStatus QT7PlayerSession::mediaStatus() const
{
    return m_mediaStatus;
}

int QT7PlayerSession::bufferStatus() const
{
    return 100;
}

int QT7PlayerSession::volume() const
{
    return m_volume;
}

bool QT7PlayerSession::isMuted() const
{
    return m_muted;
}

bool QT7PlayerSession::isSeekable() const
{
    return true;
}

#ifndef QUICKTIME_C_API_AVAILABLE
@interface QTMovie(QtExtensions)
- (NSArray*)loadedRanges;
- (QTTime)maxTimeLoaded;
@end
#endif

QMediaTimeRange QT7PlayerSession::availablePlaybackRanges() const
{
    QTMovie *movie = (QTMovie*)m_QTMovie;
#ifndef QUICKTIME_C_API_AVAILABLE
    AutoReleasePool pool;
    if ([movie respondsToSelector:@selector(loadedRanges)]) {
        QMediaTimeRange rc;
        NSArray *r = [movie loadedRanges];
        for (NSValue *tr in r) {
            QTTimeRange timeRange = [tr QTTimeRangeValue];
            qint64 startTime = qint64(float(timeRange.time.timeValue) / timeRange.time.timeScale * 1000.0);
            rc.addInterval(startTime, startTime + qint64(float(timeRange.duration.timeValue) / timeRange.duration.timeScale * 1000.0));
        }
        return rc;
    }
    else if ([movie respondsToSelector:@selector(maxTimeLoaded)]) {
        QTTime loadedTime = [movie maxTimeLoaded];
        return QMediaTimeRange(0, qint64(float(loadedTime.timeValue) / loadedTime.timeScale * 1000.0));
    }
#else
    TimeValue loadedTime;
    TimeScale scale;
    Movie m = [movie quickTimeMovie];
    if (GetMaxLoadedTimeInMovie(m, &loadedTime) == noErr) {
        scale = GetMovieTimeScale(m);
        return QMediaTimeRange(0, qint64(float(loadedTime) / scale * 1000.0f));
    }
#endif
    return QMediaTimeRange(0, duration());
}

qreal QT7PlayerSession::playbackRate() const
{
    return m_rate;
}

void QT7PlayerSession::setPlaybackRate(qreal rate)
{
    if (qFuzzyCompare(m_rate, rate))
        return;

    m_rate = rate;

    if (m_QTMovie != 0 && m_state == QMediaPlayer::PlayingState) {
        AutoReleasePool pool;
        float preferredRate = [[(QTMovie*)m_QTMovie attributeForKey:@"QTMoviePreferredRateAttribute"] floatValue];
        [(QTMovie*)m_QTMovie setRate:preferredRate * m_rate];
    }
}

void QT7PlayerSession::setPosition(qint64 pos)
{
    if ( !isSeekable() || pos == position())
        return;

    if (duration() > 0)
        pos = qMin(pos, duration());

    QTTime newQTTime = [(QTMovie*)m_QTMovie currentTime];
    newQTTime.timeValue = (pos / 1000.0f) * newQTTime.timeScale;
    [(QTMovie*)m_QTMovie setCurrentTime:newQTTime];

    //reset the EndOfMedia status position is changed after playback is finished
    if (m_mediaStatus == QMediaPlayer::EndOfMedia)
        processLoadStateChange();
}

void QT7PlayerSession::play()
{
    if (m_state == QMediaPlayer::PlayingState)
        return;

    m_state = QMediaPlayer::PlayingState;

    if (m_videoOutput)
        m_videoOutput->setMovie(m_QTMovie);

    //reset the EndOfMedia status if the same file is played again
    if (m_mediaStatus == QMediaPlayer::EndOfMedia)
        processLoadStateChange();

    AutoReleasePool pool;
    float preferredRate = [[(QTMovie*)m_QTMovie attributeForKey:@"QTMoviePreferredRateAttribute"] floatValue];
    [(QTMovie*)m_QTMovie setRate:preferredRate * m_rate];

    processLoadStateChange();
    emit stateChanged(m_state);
}

void QT7PlayerSession::pause()
{
    if (m_state == QMediaPlayer::PausedState)
        return;

    m_state = QMediaPlayer::PausedState;

    if (m_videoOutput)
        m_videoOutput->setMovie(m_QTMovie);

    //reset the EndOfMedia status if the same file is played again
    if (m_mediaStatus == QMediaPlayer::EndOfMedia)
        processLoadStateChange();

    [(QTMovie*)m_QTMovie setRate:0];

    processLoadStateChange();
    emit stateChanged(m_state);
}

void QT7PlayerSession::stop()
{
    if (m_state == QMediaPlayer::StoppedState)
        return;

    m_state = QMediaPlayer::StoppedState;

    [(QTMovie*)m_QTMovie setRate:0];
    setPosition(0);

    if (m_videoOutput)
        m_videoOutput->setMovie(0);

    processLoadStateChange();
    emit stateChanged(m_state);
    emit positionChanged(position());
}

void QT7PlayerSession::setVolume(int volume)
{
    if (m_volume == volume)
        return;

    m_volume = volume;

    if (m_QTMovie != 0)
        [(QTMovie*)m_QTMovie setVolume:m_volume / 100.0f];

    emit volumeChanged(m_volume);
}

void QT7PlayerSession::setMuted(bool muted)
{
    if (m_muted == muted)
        return;

    m_muted = muted;

    if (m_QTMovie != 0)
        [(QTMovie*)m_QTMovie setMuted:m_muted];

    emit mutedChanged(muted);
}

QMediaContent QT7PlayerSession::media() const
{
    return m_resources;
}

const QIODevice *QT7PlayerSession::mediaStream() const
{
    return m_mediaStream;
}

void QT7PlayerSession::setMedia(const QMediaContent &content, QIODevice *stream)
{
    AutoReleasePool pool;

#ifdef QT_DEBUG_QT7
    qDebug() << Q_FUNC_INFO << content.canonicalUrl();
#endif

    if (m_QTMovie) {
        [(QTMovieObserver*)m_movieObserver setMovie:nil];

        if (m_videoOutput)
            m_videoOutput->setMovie(0);

        [(QTMovie*)m_QTMovie release];
        m_QTMovie = 0;
        m_resourceHandler.clear();
    }

    m_resources = content;
    m_mediaStream = stream;
    QMediaPlayer::MediaStatus oldMediaStatus = m_mediaStatus;

    if (content.isNull()) {
        m_mediaStatus = QMediaPlayer::NoMedia;
        if (m_state != QMediaPlayer::StoppedState)
            emit stateChanged(m_state = QMediaPlayer::StoppedState);

        if (m_mediaStatus != oldMediaStatus)
            emit mediaStatusChanged(m_mediaStatus);
        emit positionChanged(position());
        return;
    }

    m_mediaStatus = QMediaPlayer::LoadingMedia;
    if (m_mediaStatus != oldMediaStatus)
        emit mediaStatusChanged(m_mediaStatus);

    QNetworkRequest request = content.canonicalResource().request();

    QVariant cookies = request.header(QNetworkRequest::CookieHeader);
    if (cookies.isValid()) {
        NSHTTPCookieStorage *store = [NSHTTPCookieStorage sharedHTTPCookieStorage];
        QList<QNetworkCookie> cookieList = cookies.value<QList<QNetworkCookie> >();

        foreach (const QNetworkCookie &requestCookie, cookieList) {
            NSMutableDictionary *p = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                               qString2CFStringRef(requestCookie.name()), NSHTTPCookieName,
                               qString2CFStringRef(requestCookie.value()), NSHTTPCookieValue,
                               qString2CFStringRef(requestCookie.domain()), NSHTTPCookieDomain,
                               qString2CFStringRef(requestCookie.path()), NSHTTPCookiePath,
                               nil
                               ];
            if (requestCookie.isSessionCookie())
                [p setObject:[NSString stringWithUTF8String:"TRUE"] forKey:NSHTTPCookieDiscard];
            else
                [p setObject:[NSDate dateWithTimeIntervalSince1970:requestCookie.expirationDate().toTime_t()] forKey:NSHTTPCookieExpires];

            [store setCookie:[NSHTTPCookie cookieWithProperties:p]];
        }
    }

    // Attempt multiple times to open the movie.
    // First try - attempt open in async mode
    openMovie(true);

    emit positionChanged(position());
}

void QT7PlayerSession::openMovie(bool tryAsync)
{
    QUrl requestUrl = m_resources.canonicalResource().request().url();
    if (requestUrl.scheme().isEmpty())
        requestUrl.setScheme(QLatin1String("file"));

#ifdef QT_DEBUG_QT7
    qDebug() << Q_FUNC_INFO << requestUrl;
#endif

    NSError *err = 0;
    NSString *urlString = [NSString stringWithUTF8String:requestUrl.toEncoded().constData()];

    NSMutableDictionary *attr = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                [NSNumber numberWithBool:YES], QTMovieOpenAsyncOKAttribute,
                [NSNumber numberWithBool:YES], QTMovieIsActiveAttribute,
                [NSNumber numberWithBool:YES], QTMovieResolveDataRefsAttribute,
                [NSNumber numberWithBool:YES], QTMovieDontInteractWithUserAttribute,
                nil];


    if (requestUrl.scheme() == QLatin1String("qrc")) {
        // Load from Qt resource
        m_resourceHandler.setResourceFile(QLatin1Char(':') + requestUrl.path());
        if (!m_resourceHandler.isValid()) {
            emit error(QMediaPlayer::FormatError, tr("Attempting to play invalid Qt resource"));
            return;
        }

        CFDataRef resourceData =
                CFDataCreateWithBytesNoCopy(0, m_resourceHandler.data(), m_resourceHandler.size(), kCFAllocatorNull);

        QTDataReference *dataReference =
                [QTDataReference dataReferenceWithReferenceToData:(NSData*)resourceData
                                                             name:qString2CFStringRef(requestUrl.path())
                                                         MIMEType:nil];

        [attr setObject:dataReference forKey:QTMovieDataReferenceAttribute];

        CFRelease(resourceData);
    } else {
        [attr setObject:[NSURL URLWithString:urlString] forKey:QTMovieURLAttribute];
    }

    if (tryAsync && QSysInfo::MacintoshVersion >= QSysInfo::MV_10_6) {
        [attr setObject:[NSNumber numberWithBool:YES] forKey:@"QTMovieOpenAsyncRequiredAttribute"];
// XXX: This is disabled for now. causes some problems with video playback for some formats
//        [attr setObject:[NSNumber numberWithBool:YES] forKey:@"QTMovieOpenForPlaybackAttribute"];
        m_tryingAsync = true;
    }
    else
        m_tryingAsync = false;

    m_QTMovie = [QTMovie movieWithAttributes:attr error:&err];
    if (err != nil) {
        // First attempt to test for inability to perform async
//        if ([err code] == QTErrorMovieOpeningCannotBeAsynchronous) { XXX: error code unknown!
        if (m_tryingAsync) {
            m_tryingAsync = false;
            err = nil;
            [attr removeObjectForKey:@"QTMovieOpenAsyncRequiredAttribute"];
            m_QTMovie = [QTMovie movieWithAttributes:attr error:&err];
        }
    }

    if (err != nil) {
        m_QTMovie = 0;
        QString description = QString::fromUtf8([[err localizedDescription] UTF8String]);
        emit error(QMediaPlayer::FormatError, description);

#ifdef QT_DEBUG_QT7
        qDebug() << Q_FUNC_INFO << description;
#endif
    }
    else {
        [(QTMovie*)m_QTMovie retain];

        [(QTMovieObserver*)m_movieObserver setMovie:(QTMovie*)m_QTMovie];

        if (m_state != QMediaPlayer::StoppedState && m_videoOutput)
            m_videoOutput->setMovie(m_QTMovie);

        processLoadStateChange();

        [(QTMovie*)m_QTMovie setMuted:m_muted];
        [(QTMovie*)m_QTMovie setVolume:m_volume / 100.0f];
    }
}

bool QT7PlayerSession::isAudioAvailable() const
{
    if (!m_QTMovie)
        return false;

    AutoReleasePool pool;
    return [[(QTMovie*)m_QTMovie attributeForKey:@"QTMovieHasAudioAttribute"] boolValue] == YES;
}

bool QT7PlayerSession::isVideoAvailable() const
{
    if (!m_QTMovie)
        return false;

    AutoReleasePool pool;
    return [[(QTMovie*)m_QTMovie attributeForKey:@"QTMovieHasVideoAttribute"] boolValue] == YES;
}

void QT7PlayerSession::processEOS()
{
#ifdef QT_DEBUG_QT7
    qDebug() << Q_FUNC_INFO;
#endif
    emit positionChanged(position());
    m_mediaStatus = QMediaPlayer::EndOfMedia;
    if (m_videoOutput)
        m_videoOutput->setMovie(0);
    emit stateChanged(m_state = QMediaPlayer::StoppedState);
    emit mediaStatusChanged(m_mediaStatus);
}

void QT7PlayerSession::processLoadStateChange()
{
    if (!m_QTMovie)
        return;

    AutoReleasePool pool;

    long state = [[(QTMovie*)m_QTMovie attributeForKey:QTMovieLoadStateAttribute] longValue];

#ifdef QT_DEBUG_QT7
    qDebug() << Q_FUNC_INFO << state;
#endif

#ifndef QUICKTIME_C_API_AVAILABLE
    enum {
      kMovieLoadStateError          = -1L,
      kMovieLoadStateLoading        = 1000,
      kMovieLoadStateLoaded         = 2000,
      kMovieLoadStatePlayable       = 10000,
      kMovieLoadStatePlaythroughOK  = 20000,
      kMovieLoadStateComplete       = 100000
    };
#endif

    if (state == kMovieLoadStateError) {
        if (m_tryingAsync) {
            NSError *error = [(QTMovie*)m_QTMovie attributeForKey:@"QTMovieLoadStateErrorAttribute"];
            if ([error code] == componentNotThreadSafeErr) {
                // Last Async check, try again with no such flag
                openMovie(false);
            }
        }
        else {
            if (m_videoOutput)
                m_videoOutput->setMovie(0);

            emit error(QMediaPlayer::FormatError, tr("Failed to load media"));
            emit mediaStatusChanged(m_mediaStatus = QMediaPlayer::InvalidMedia);
            emit stateChanged(m_state = QMediaPlayer::StoppedState);
        }

        return;
    }

    QMediaPlayer::MediaStatus newStatus = QMediaPlayer::NoMedia;
    bool isPlaying = (m_state != QMediaPlayer::StoppedState);

    if (state >= kMovieLoadStatePlaythroughOK) {
        newStatus = isPlaying ? QMediaPlayer::BufferedMedia : QMediaPlayer::LoadedMedia;
    } else if (state >= kMovieLoadStatePlayable)
        newStatus = isPlaying ? QMediaPlayer::BufferingMedia : QMediaPlayer::LoadingMedia;
    else if (state >= kMovieLoadStateLoading) {
        if (!isPlaying)
            newStatus = QMediaPlayer::LoadingMedia;
        else if (m_mediaStatus >= QMediaPlayer::LoadedMedia)
            newStatus = QMediaPlayer::StalledMedia;
        else
            newStatus = QMediaPlayer::LoadingMedia;
    }

    if (state >= kMovieLoadStatePlayable &&
        m_state == QMediaPlayer::PlayingState &&
        [(QTMovie*)m_QTMovie rate] == 0) {

        float preferredRate = [[(QTMovie*)m_QTMovie attributeForKey:@"QTMoviePreferredRateAttribute"] floatValue];

        [(QTMovie*)m_QTMovie setRate:preferredRate * m_rate];
    }

    if (state >= kMovieLoadStateLoaded) {
        qint64 currentDuration = duration();
        if (m_duration != currentDuration)
            emit durationChanged(m_duration = currentDuration);

        if (m_audioAvailable != isAudioAvailable())
            emit audioAvailableChanged(m_audioAvailable = !m_audioAvailable);

        if (m_videoAvailable != isVideoAvailable())
            emit videoAvailableChanged(m_videoAvailable = !m_videoAvailable);
    }

    if (newStatus != m_mediaStatus)
        emit mediaStatusChanged(m_mediaStatus = newStatus);
}

void QT7PlayerSession::processVolumeChange()
{
    if (!m_QTMovie)
        return;

    int newVolume = qRound(100.0f * [((QTMovie*)m_QTMovie) volume]);

    if (newVolume != m_volume) {
        emit volumeChanged(m_volume = newVolume);
    }
}

void QT7PlayerSession::processNaturalSizeChange()
{
    AutoReleasePool pool;
    NSSize size = [[(QTMovie*)m_QTMovie attributeForKey:@"QTMovieNaturalSizeAttribute"] sizeValue];
#ifdef QT_DEBUG_QT7
    qDebug() << Q_FUNC_INFO << QSize(size.width, size.height);
#endif

    if (m_videoOutput)
        m_videoOutput->updateNaturalSize(QSize(size.width, size.height));
}

void QT7PlayerSession::processPositionChange()
{
    emit positionChanged(position());
}

#include "moc_qt7playersession.cpp"