[Mac] user caption styles not applied to correct element
[WebKit-https.git] / Source / WebCore / platform / graphics / avfoundation / MediaPlayerPrivateAVFoundation.cpp
1 /*
2  * Copyright (C) 2011, 2012 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
26 #include "config.h"
27
28 #if ENABLE(VIDEO) && USE(AVFOUNDATION)
29
30 #include "MediaPlayerPrivateAVFoundation.h"
31
32 #include "DocumentLoader.h"
33 #include "Frame.h"
34 #include "FrameView.h"
35 #include "GraphicsContext.h"
36 #include "InbandTextTrackPrivateAVF.h"
37 #include "InbandTextTrackPrivateClient.h"
38 #include "KURL.h"
39 #include "Logging.h"
40 #include "PlatformLayer.h"
41 #include "SoftLinking.h"
42 #include "TimeRanges.h"
43 #include <CoreMedia/CoreMedia.h>
44 #include <wtf/MainThread.h>
45 #include <wtf/UnusedParam.h>
46
47 using namespace std;
48
49 namespace WebCore {
50
51 MediaPlayerPrivateAVFoundation::MediaPlayerPrivateAVFoundation(MediaPlayer* player)
52     : m_player(player)
53     , m_queuedNotifications()
54     , m_queueMutex()
55     , m_networkState(MediaPlayer::Empty)
56     , m_readyState(MediaPlayer::HaveNothing)
57     , m_preload(MediaPlayer::Auto)
58     , m_cachedMaxTimeLoaded(0)
59     , m_cachedMaxTimeSeekable(0)
60     , m_cachedDuration(MediaPlayer::invalidTime())
61     , m_reportedDuration(MediaPlayer::invalidTime())
62     , m_maxTimeLoadedAtLastDidLoadingProgress(MediaPlayer::invalidTime())
63     , m_seekTo(MediaPlayer::invalidTime())
64     , m_requestedRate(1)
65     , m_delayCallbacks(0)
66     , m_mainThreadCallPending(false)
67     , m_assetIsPlayable(false)
68     , m_visible(false)
69     , m_loadingMetadata(false)
70     , m_isAllowedToRender(false)
71     , m_cachedHasAudio(false)
72     , m_cachedHasVideo(false)
73     , m_cachedHasCaptions(false)
74     , m_ignoreLoadStateChanges(false)
75     , m_haveReportedFirstVideoFrame(false)
76     , m_playWhenFramesAvailable(false)
77 #if HAVE(AVFOUNDATION_TEXT_TRACK_SUPPORT)
78     , m_inbandTrackConfigurationPending(false)
79 #endif
80 {
81     LOG(Media, "MediaPlayerPrivateAVFoundation::MediaPlayerPrivateAVFoundation(%p)", this);
82 }
83
84 MediaPlayerPrivateAVFoundation::~MediaPlayerPrivateAVFoundation()
85 {
86     LOG(Media, "MediaPlayerPrivateAVFoundation::~MediaPlayerPrivateAVFoundation(%p)", this);
87     setIgnoreLoadStateChanges(true);
88     cancelCallOnMainThread(mainThreadCallback, this);
89 }
90
91 MediaPlayerPrivateAVFoundation::MediaRenderingMode MediaPlayerPrivateAVFoundation::currentRenderingMode() const
92 {
93 #if USE(ACCELERATED_COMPOSITING)
94     if (platformLayer())
95         return MediaRenderingToLayer;
96 #endif
97
98     if (hasContextRenderer())
99         return MediaRenderingToContext;
100
101     return MediaRenderingNone;
102 }
103
104 MediaPlayerPrivateAVFoundation::MediaRenderingMode MediaPlayerPrivateAVFoundation::preferredRenderingMode() const
105 {
106     if (!m_player->visible() || !m_player->frameView() || assetStatus() == MediaPlayerAVAssetStatusUnknown)
107         return MediaRenderingNone;
108
109 #if USE(ACCELERATED_COMPOSITING)
110     if (supportsAcceleratedRendering() && m_player->mediaPlayerClient()->mediaPlayerRenderingCanBeAccelerated(m_player))
111         return MediaRenderingToLayer;
112 #endif
113
114     return MediaRenderingToContext;
115 }
116
117 void MediaPlayerPrivateAVFoundation::setUpVideoRendering()
118 {
119     if (!isReadyForVideoSetup())
120         return;
121
122     MediaRenderingMode currentMode = currentRenderingMode();
123     MediaRenderingMode preferredMode = preferredRenderingMode();
124
125     if (preferredMode == MediaRenderingNone)
126         preferredMode = MediaRenderingToContext;
127
128     if (currentMode == preferredMode && currentMode != MediaRenderingNone)
129         return;
130
131     LOG(Media, "MediaPlayerPrivateAVFoundation::setUpVideoRendering(%p) - current mode = %d, preferred mode = %d", 
132         this, static_cast<int>(currentMode), static_cast<int>(preferredMode));
133
134     if (currentMode != MediaRenderingNone)  
135         tearDownVideoRendering();
136
137     switch (preferredMode) {
138     case MediaRenderingNone:
139     case MediaRenderingToContext:
140         createContextVideoRenderer();
141         break;
142         
143 #if USE(ACCELERATED_COMPOSITING)
144     case MediaRenderingToLayer:
145         createVideoLayer();
146         break;
147 #endif
148     }
149
150 #if USE(ACCELERATED_COMPOSITING)
151     // If using a movie layer, inform the client so the compositing tree is updated.
152     if (currentMode == MediaRenderingToLayer || preferredMode == MediaRenderingToLayer) {
153         LOG(Media, "MediaPlayerPrivateAVFoundation::setUpVideoRendering(%p) - calling mediaPlayerRenderingModeChanged()", this);
154         m_player->mediaPlayerClient()->mediaPlayerRenderingModeChanged(m_player);
155     }
156 #endif
157 }
158
159 void MediaPlayerPrivateAVFoundation::tearDownVideoRendering()
160 {
161     LOG(Media, "MediaPlayerPrivateAVFoundation::tearDownVideoRendering(%p)", this);
162
163     destroyContextVideoRenderer();
164
165 #if USE(ACCELERATED_COMPOSITING)
166     if (platformLayer())
167         destroyVideoLayer();
168 #endif
169 }
170
171 bool MediaPlayerPrivateAVFoundation::hasSetUpVideoRendering() const
172 {
173     return hasLayerRenderer() || hasContextRenderer();
174 }
175
176 void MediaPlayerPrivateAVFoundation::load(const String& url)
177 {
178     LOG(Media, "MediaPlayerPrivateAVFoundation::load(%p)", this);
179
180     if (m_networkState != MediaPlayer::Loading) {
181         m_networkState = MediaPlayer::Loading;
182         m_player->networkStateChanged();
183     }
184     if (m_readyState != MediaPlayer::HaveNothing) {
185         m_readyState = MediaPlayer::HaveNothing;
186         m_player->readyStateChanged();
187     }
188
189     m_assetURL = url;
190
191     // Don't do any more work if the url is empty.
192     if (!url.length())
193         return;
194
195     setPreload(m_preload);
196 }
197
198 void MediaPlayerPrivateAVFoundation::playabilityKnown()
199 {
200     LOG(Media, "MediaPlayerPrivateAVFoundation::playabilityKnown(%p)", this);
201
202     if (m_assetIsPlayable)
203         return;
204
205     // Nothing more to do if we already have all of the item's metadata.
206     if (assetStatus() > MediaPlayerAVAssetStatusLoading) {
207         LOG(Media, "MediaPlayerPrivateAVFoundation::playabilityKnown(%p) - all metadata loaded", this);
208         return;
209     }
210
211     // At this point we are supposed to load metadata. It is OK to ask the asset to load the same 
212     // information multiple times, because if it has already been loaded the completion handler 
213     // will just be called synchronously.
214     m_loadingMetadata = true;
215     beginLoadingMetadata();
216 }
217
218 void MediaPlayerPrivateAVFoundation::prepareToPlay()
219 {
220     LOG(Media, "MediaPlayerPrivateAVFoundation::prepareToPlay(%p)", this);
221
222     setPreload(MediaPlayer::Auto);
223 }
224
225 void MediaPlayerPrivateAVFoundation::play()
226 {
227     LOG(Media, "MediaPlayerPrivateAVFoundation::play(%p)", this);
228
229     // If the file has video, don't request playback until the first frame of video is ready to display
230     // or the audio may start playing before we can render video.
231     if (!m_cachedHasVideo || hasAvailableVideoFrame())
232         platformPlay();
233     else
234         m_playWhenFramesAvailable = true;
235 }
236
237 void MediaPlayerPrivateAVFoundation::pause()
238 {
239     LOG(Media, "MediaPlayerPrivateAVFoundation::pause(%p)", this);
240     m_playWhenFramesAvailable = false;
241     platformPause();
242 }
243
244 float MediaPlayerPrivateAVFoundation::duration() const
245 {
246     if (m_cachedDuration != MediaPlayer::invalidTime())
247         return m_cachedDuration;
248
249     float duration = platformDuration();
250     if (!duration || duration == MediaPlayer::invalidTime())
251         return 0;
252
253     m_cachedDuration = duration;
254     LOG(Media, "MediaPlayerPrivateAVFoundation::duration(%p) - caching %f", this, m_cachedDuration);
255     return m_cachedDuration;
256 }
257
258 void MediaPlayerPrivateAVFoundation::seek(float time)
259 {
260     if (!metaDataAvailable())
261         return;
262
263     if (time > duration())
264         time = duration();
265
266     if (currentTime() == time)
267         return;
268
269 #if HAVE(AVFOUNDATION_TEXT_TRACK_SUPPORT)
270     // Forget any partially accumulated cue data as the seek could be to a time outside of the cue's
271     // range, which will mean that the next cue delivered will result in the current cue getting the
272     // incorrect duration.
273     if (currentTrack())
274         currentTrack()->resetCueValues();
275 #endif
276     
277     LOG(Media, "MediaPlayerPrivateAVFoundation::seek(%p) - seeking to %f", this, time);
278     m_seekTo = time;
279
280     seekToTime(time);
281 }
282
283 void MediaPlayerPrivateAVFoundation::setRate(float rate)
284 {
285     LOG(Media, "MediaPlayerPrivateAVFoundation::setRate(%p) - seting to %f", this, rate);
286     m_requestedRate = rate;
287
288     updateRate();
289 }
290
291 bool MediaPlayerPrivateAVFoundation::paused() const
292 {
293     if (!metaDataAvailable())
294         return true;
295
296     return rate() == 0;
297 }
298
299 bool MediaPlayerPrivateAVFoundation::seeking() const
300 {
301     if (!metaDataAvailable())
302         return false;
303
304     return m_seekTo != MediaPlayer::invalidTime();
305 }
306
307 IntSize MediaPlayerPrivateAVFoundation::naturalSize() const
308 {
309     if (!metaDataAvailable())
310         return IntSize();
311
312     // In spite of the name of this method, return the natural size transformed by the 
313     // initial movie scale because the spec says intrinsic size is:
314     //
315     //    ... the dimensions of the resource in CSS pixels after taking into account the resource's 
316     //    dimensions, aspect ratio, clean aperture, resolution, and so forth, as defined for the 
317     //    format used by the resource
318
319     return m_cachedNaturalSize;
320 }
321
322 void MediaPlayerPrivateAVFoundation::setNaturalSize(IntSize size)
323 {
324     LOG(Media, "MediaPlayerPrivateAVFoundation:setNaturalSize(%p) - size = %d x %d", this, size.width(), size.height());
325
326     IntSize oldSize = m_cachedNaturalSize;
327     m_cachedNaturalSize = size;
328     if (oldSize != m_cachedNaturalSize)
329         m_player->sizeChanged();
330 }
331
332 void MediaPlayerPrivateAVFoundation::setHasVideo(bool b)
333 {
334     if (m_cachedHasVideo != b) {
335         m_cachedHasVideo = b;
336         m_player->characteristicChanged();
337     }
338 }
339
340 void MediaPlayerPrivateAVFoundation::setHasAudio(bool b)
341 {
342     if (m_cachedHasAudio != b) {
343         m_cachedHasAudio = b;
344         m_player->characteristicChanged();
345     }
346 }
347
348 void MediaPlayerPrivateAVFoundation::setHasClosedCaptions(bool b)
349 {
350     if (m_cachedHasCaptions != b) {
351         m_cachedHasCaptions = b;
352         m_player->characteristicChanged();
353     }
354 }
355
356 PassRefPtr<TimeRanges> MediaPlayerPrivateAVFoundation::buffered() const
357 {
358     if (!m_cachedLoadedTimeRanges)
359         m_cachedLoadedTimeRanges = platformBufferedTimeRanges();
360
361     return m_cachedLoadedTimeRanges->copy();
362 }
363
364 float MediaPlayerPrivateAVFoundation::maxTimeSeekable() const
365 {
366     if (!metaDataAvailable())
367         return 0;
368
369     if (!m_cachedMaxTimeSeekable)
370         m_cachedMaxTimeSeekable = platformMaxTimeSeekable();
371
372     LOG(Media, "MediaPlayerPrivateAVFoundation::maxTimeSeekable(%p) - returning %f", this, m_cachedMaxTimeSeekable);
373     return m_cachedMaxTimeSeekable;   
374 }
375
376 float MediaPlayerPrivateAVFoundation::maxTimeLoaded() const
377 {
378     if (!metaDataAvailable())
379         return 0;
380
381     if (!m_cachedMaxTimeLoaded)
382         m_cachedMaxTimeLoaded = platformMaxTimeLoaded();
383
384     return m_cachedMaxTimeLoaded;   
385 }
386
387 bool MediaPlayerPrivateAVFoundation::didLoadingProgress() const
388 {
389     if (!duration() || !totalBytes())
390         return false;
391     float currentMaxTimeLoaded = maxTimeLoaded();
392     bool didLoadingProgress = currentMaxTimeLoaded != m_maxTimeLoadedAtLastDidLoadingProgress;
393     m_maxTimeLoadedAtLastDidLoadingProgress = currentMaxTimeLoaded;
394
395     return didLoadingProgress;
396 }
397
398 bool MediaPlayerPrivateAVFoundation::isReadyForVideoSetup() const
399 {
400     // AVFoundation will not return true for firstVideoFrameAvailable until
401     // an AVPlayerLayer has been added to the AVPlayerItem, so allow video setup
402     // here if a video track to trigger allocation of a AVPlayerLayer.
403     return (m_isAllowedToRender || m_cachedHasVideo) && m_readyState >= MediaPlayer::HaveMetadata && m_player->visible();
404 }
405
406 void MediaPlayerPrivateAVFoundation::prepareForRendering()
407 {
408     if (m_isAllowedToRender)
409         return;
410     m_isAllowedToRender = true;
411
412     setUpVideoRendering();
413
414     if (currentRenderingMode() == MediaRenderingToLayer || preferredRenderingMode() == MediaRenderingToLayer)
415         m_player->mediaPlayerClient()->mediaPlayerRenderingModeChanged(m_player);
416 }
417
418 bool MediaPlayerPrivateAVFoundation::supportsFullscreen() const
419 {
420 #if ENABLE(FULLSCREEN_API)
421     return true;
422 #else
423     // FIXME: WebVideoFullscreenController assumes a QTKit/QuickTime media engine
424     return false;
425 #endif
426 }
427
428 void MediaPlayerPrivateAVFoundation::updateStates()
429 {
430     if (m_ignoreLoadStateChanges)
431         return;
432
433     MediaPlayer::NetworkState oldNetworkState = m_networkState;
434     MediaPlayer::ReadyState oldReadyState = m_readyState;
435
436     if (m_loadingMetadata)
437         m_networkState = MediaPlayer::Loading;
438     else {
439         // -loadValuesAsynchronouslyForKeys:completionHandler: has invoked its handler; test status of keys and determine state.
440         AssetStatus assetStatus = this->assetStatus();
441         ItemStatus itemStatus = playerItemStatus();
442         
443         m_assetIsPlayable = (assetStatus == MediaPlayerAVAssetStatusPlayable);
444         if (m_readyState < MediaPlayer::HaveMetadata && assetStatus > MediaPlayerAVAssetStatusLoading) {
445             if (m_assetIsPlayable) {
446                 if (assetStatus >= MediaPlayerAVAssetStatusLoaded)
447                     m_readyState = MediaPlayer::HaveMetadata;
448                 if (itemStatus <= MediaPlayerAVPlayerItemStatusUnknown) {
449                     if (assetStatus == MediaPlayerAVAssetStatusFailed || m_preload > MediaPlayer::MetaData || isLiveStream()) {
450                         // The asset is playable but doesn't support inspection prior to playback (eg. streaming files),
451                         // or we are supposed to prepare for playback immediately, so create the player item now.
452                         m_networkState = MediaPlayer::Loading;
453                         prepareToPlay();
454                     } else
455                         m_networkState = MediaPlayer::Idle;
456                 }
457             } else {
458                 // FIX ME: fetch the error associated with the @"playable" key to distinguish between format 
459                 // and network errors.
460                 m_networkState = MediaPlayer::FormatError;
461             }
462         }
463         
464         if (assetStatus >= MediaPlayerAVAssetStatusLoaded && itemStatus > MediaPlayerAVPlayerItemStatusUnknown) {
465             switch (itemStatus) {
466             case MediaPlayerAVPlayerItemStatusDoesNotExist:
467             case MediaPlayerAVPlayerItemStatusUnknown:
468             case MediaPlayerAVPlayerItemStatusFailed:
469                 break;
470
471             case MediaPlayerAVPlayerItemStatusPlaybackLikelyToKeepUp:
472             case MediaPlayerAVPlayerItemStatusPlaybackBufferFull:
473                 // If the status becomes PlaybackBufferFull, loading stops and the status will not
474                 // progress to LikelyToKeepUp. Set the readyState to  HAVE_ENOUGH_DATA, on the
475                 // presumption that if the playback buffer is full, playback will probably not stall.
476                 m_readyState = MediaPlayer::HaveEnoughData;
477                 break;
478
479             case MediaPlayerAVPlayerItemStatusReadyToPlay:
480                 // If the readyState is already HaveEnoughData, don't go lower because of this state change.
481                 if (m_readyState == MediaPlayer::HaveEnoughData)
482                     break;
483
484             case MediaPlayerAVPlayerItemStatusPlaybackBufferEmpty:
485                 if (maxTimeLoaded() > currentTime())
486                     m_readyState = MediaPlayer::HaveFutureData;
487                 else
488                     m_readyState = MediaPlayer::HaveCurrentData;
489                 break;
490             }
491
492             if (itemStatus == MediaPlayerAVPlayerItemStatusPlaybackBufferFull)
493                 m_networkState = MediaPlayer::Idle;
494             else if (itemStatus == MediaPlayerAVPlayerItemStatusFailed)
495                 m_networkState = MediaPlayer::DecodeError;
496             else if (itemStatus != MediaPlayerAVPlayerItemStatusPlaybackBufferFull && itemStatus >= MediaPlayerAVPlayerItemStatusReadyToPlay)
497                 m_networkState = (maxTimeLoaded() == duration()) ? MediaPlayer::Loaded : MediaPlayer::Loading;
498         }
499     }
500
501     if (isReadyForVideoSetup() && currentRenderingMode() != preferredRenderingMode())
502         setUpVideoRendering();
503
504     if (!m_haveReportedFirstVideoFrame && m_cachedHasVideo && hasAvailableVideoFrame()) {
505         if (m_readyState < MediaPlayer::HaveCurrentData)
506             m_readyState = MediaPlayer::HaveCurrentData;
507         m_haveReportedFirstVideoFrame = true;
508         m_player->firstVideoFrameAvailable();
509     }
510
511     if (m_networkState != oldNetworkState)
512         m_player->networkStateChanged();
513
514     if (m_readyState != oldReadyState)
515         m_player->readyStateChanged();
516
517     if (m_playWhenFramesAvailable && hasAvailableVideoFrame()) {
518         m_playWhenFramesAvailable = false;
519         platformPlay();
520     }
521
522 #if !LOG_DISABLED
523     if (m_networkState != oldNetworkState || oldReadyState != m_readyState) {
524         LOG(Media, "MediaPlayerPrivateAVFoundation::updateStates(%p) - entered with networkState = %i, readyState = %i,  exiting with networkState = %i, readyState = %i",
525             this, static_cast<int>(oldNetworkState), static_cast<int>(oldReadyState), static_cast<int>(m_networkState), static_cast<int>(m_readyState));
526     }
527 #endif
528 }
529
530 void MediaPlayerPrivateAVFoundation::setSize(const IntSize&) 
531
532 }
533
534 void MediaPlayerPrivateAVFoundation::setVisible(bool visible)
535 {
536     if (m_visible == visible)
537         return;
538
539     m_visible = visible;
540     if (visible)
541         setUpVideoRendering();
542     
543     platformSetVisible(visible);
544 }
545
546 void MediaPlayerPrivateAVFoundation::acceleratedRenderingStateChanged()
547 {
548     // Set up or change the rendering path if necessary.
549     setUpVideoRendering();
550 }
551
552 void MediaPlayerPrivateAVFoundation::metadataLoaded()
553 {
554     m_loadingMetadata = false;
555     tracksChanged();
556 }
557
558 void MediaPlayerPrivateAVFoundation::rateChanged()
559 {
560     m_player->rateChanged();
561 }
562
563 void MediaPlayerPrivateAVFoundation::loadedTimeRangesChanged()
564 {
565     m_cachedLoadedTimeRanges = 0;
566     m_cachedMaxTimeLoaded = 0;
567     invalidateCachedDuration();
568 }
569
570 void MediaPlayerPrivateAVFoundation::seekableTimeRangesChanged()
571 {
572     m_cachedMaxTimeSeekable = 0;
573 }
574
575 void MediaPlayerPrivateAVFoundation::timeChanged(double time)
576 {
577     LOG(Media, "MediaPlayerPrivateAVFoundation::timeChanged(%p) - time = %f", this, time);
578     UNUSED_PARAM(time);
579 }
580
581 void MediaPlayerPrivateAVFoundation::seekCompleted(bool finished)
582 {
583     LOG(Media, "MediaPlayerPrivateAVFoundation::seekCompleted(%p) - finished = %d", this, finished);
584     UNUSED_PARAM(finished);
585
586 #if HAVE(AVFOUNDATION_TEXT_TRACK_SUPPORT)
587     if (currentTrack())
588         currentTrack()->resetCueValues();
589 #endif
590
591     m_seekTo = MediaPlayer::invalidTime();
592     updateStates();
593     m_player->timeChanged();
594 }
595
596 void MediaPlayerPrivateAVFoundation::didEnd()
597 {
598     // Hang onto the current time and use it as duration from now on since we are definitely at
599     // the end of the movie. Do this because the initial duration is sometimes an estimate.
600     float now = currentTime();
601     if (now > 0)
602         m_cachedDuration = now;
603
604     updateStates();
605     m_player->timeChanged();
606 }
607
608 void MediaPlayerPrivateAVFoundation::invalidateCachedDuration()
609 {
610     LOG(Media, "MediaPlayerPrivateAVFoundation::invalidateCachedDuration(%p)", this);
611     
612     m_cachedDuration = MediaPlayer::invalidTime();
613
614     // For some media files, reported duration is estimated and updated as media is loaded
615     // so report duration changed when the estimate is upated.
616     float duration = this->duration();
617     if (duration != m_reportedDuration) {
618         if (m_reportedDuration != MediaPlayer::invalidTime())
619             m_player->durationChanged();
620         m_reportedDuration = duration;
621     }
622     
623 }
624
625 void MediaPlayerPrivateAVFoundation::repaint()
626 {
627     m_player->repaint();
628 }
629
630 MediaPlayer::MovieLoadType MediaPlayerPrivateAVFoundation::movieLoadType() const
631 {
632     if (!metaDataAvailable() || assetStatus() == MediaPlayerAVAssetStatusUnknown)
633         return MediaPlayer::Unknown;
634
635     if (isLiveStream())
636         return MediaPlayer::LiveStream;
637
638     return MediaPlayer::Download;
639 }
640
641 void MediaPlayerPrivateAVFoundation::setPreload(MediaPlayer::Preload preload)
642 {
643     m_preload = preload;
644     if (!m_assetURL.length())
645         return;
646
647     setDelayCallbacks(true);
648
649     if (m_preload >= MediaPlayer::MetaData && assetStatus() == MediaPlayerAVAssetStatusDoesNotExist) {
650         createAVAssetForURL(m_assetURL);
651         checkPlayability();
652     }
653
654     // Don't force creation of the player and player item unless we already know that the asset is playable. If we aren't
655     // there yet, or if we already know it is not playable, creating them now won't help.
656     if (m_preload == MediaPlayer::Auto && m_assetIsPlayable) {
657         createAVPlayerItem();
658         createAVPlayer();
659     }
660
661     setDelayCallbacks(false);
662 }
663
664 void MediaPlayerPrivateAVFoundation::setDelayCallbacks(bool delay) const
665 {
666     MutexLocker lock(m_queueMutex);
667     if (delay)
668         ++m_delayCallbacks;
669     else {
670         ASSERT(m_delayCallbacks);
671         --m_delayCallbacks;
672     }
673 }
674
675 void MediaPlayerPrivateAVFoundation::mainThreadCallback(void* context)
676 {
677     LOG(Media, "MediaPlayerPrivateAVFoundation::mainThreadCallback(%p)", context);
678     MediaPlayerPrivateAVFoundation* player = static_cast<MediaPlayerPrivateAVFoundation*>(context);
679     player->clearMainThreadPendingFlag();
680     player->dispatchNotification();
681 }
682
683 void MediaPlayerPrivateAVFoundation::clearMainThreadPendingFlag()
684 {
685     MutexLocker lock(m_queueMutex);
686     m_mainThreadCallPending = false;
687 }
688
689 void MediaPlayerPrivateAVFoundation::scheduleMainThreadNotification(Notification::Type type, double time)
690 {
691     scheduleMainThreadNotification(Notification(type, time));
692 }
693
694 void MediaPlayerPrivateAVFoundation::scheduleMainThreadNotification(Notification::Type type, bool finished)
695 {
696     scheduleMainThreadNotification(Notification(type, finished));
697 }
698
699 #if !LOG_DISABLED
700 static const char* notificationName(MediaPlayerPrivateAVFoundation::Notification& notification)
701 {
702 #define DEFINE_TYPE_STRING_CASE(type) case MediaPlayerPrivateAVFoundation::Notification::type: return #type;
703     switch (notification.type()) {
704         FOR_EACH_MEDIAPLAYERPRIVATEAVFOUNDATION_NOTIFICATION_TYPE(DEFINE_TYPE_STRING_CASE)
705         default: return "";
706     }
707 #undef DEFINE_TYPE_STRING_CASE
708 }
709 #endif // !LOG_DISABLED
710     
711
712 void MediaPlayerPrivateAVFoundation::scheduleMainThreadNotification(Notification notification)
713 {
714     LOG(Media, "MediaPlayerPrivateAVFoundation::scheduleMainThreadNotification(%p) - notification %s", this, notificationName(notification));
715     m_queueMutex.lock();
716
717     // It is important to always process the properties in the order that we are notified, 
718     // so always go through the queue because notifications happen on different threads.
719     m_queuedNotifications.append(notification);
720
721     bool delayDispatch = m_delayCallbacks || !isMainThread();
722     if (delayDispatch && !m_mainThreadCallPending) {
723         m_mainThreadCallPending = true;
724         callOnMainThread(mainThreadCallback, this);
725     }
726
727     m_queueMutex.unlock();
728
729     if (delayDispatch) {
730         LOG(Media, "MediaPlayerPrivateAVFoundation::scheduleMainThreadNotification(%p) - early return", this);
731         return;
732     }
733
734     dispatchNotification();
735 }
736
737 void MediaPlayerPrivateAVFoundation::dispatchNotification()
738 {
739     ASSERT(isMainThread());
740
741     Notification notification = Notification();
742     {
743         MutexLocker lock(m_queueMutex);
744         
745         if (m_queuedNotifications.isEmpty())
746             return;
747         
748         if (!m_delayCallbacks) {
749             // Only dispatch one notification callback per invocation because they can cause recursion.
750             notification = m_queuedNotifications.first();
751             m_queuedNotifications.remove(0);
752         }
753         
754         if (!m_queuedNotifications.isEmpty() && !m_mainThreadCallPending)
755             callOnMainThread(mainThreadCallback, this);
756         
757         if (!notification.isValid())
758             return;
759     }
760
761     LOG(Media, "MediaPlayerPrivateAVFoundation::dispatchNotification(%p) - dispatching %s", this, notificationName(notification));
762
763     switch (notification.type()) {
764     case Notification::ItemDidPlayToEndTime:
765         didEnd();
766         break;
767     case Notification::ItemTracksChanged:
768         tracksChanged();
769         updateStates();
770         break;
771     case Notification::ItemStatusChanged:
772         updateStates();
773         break;
774     case Notification::ItemSeekableTimeRangesChanged:
775         seekableTimeRangesChanged();
776         updateStates();
777         break;
778     case Notification::ItemLoadedTimeRangesChanged:
779         loadedTimeRangesChanged();
780         updateStates();
781         break;
782     case Notification::ItemPresentationSizeChanged:
783         sizeChanged();
784         updateStates();
785         break;
786     case Notification::ItemIsPlaybackLikelyToKeepUpChanged:
787         updateStates();
788         break;
789     case Notification::ItemIsPlaybackBufferEmptyChanged:
790         updateStates();
791         break;
792     case Notification::ItemIsPlaybackBufferFullChanged:
793         updateStates();
794         break;
795     case Notification::PlayerRateChanged:
796         updateStates();
797         rateChanged();
798         break;
799     case Notification::PlayerTimeChanged:
800         timeChanged(notification.time());
801         break;
802     case Notification::SeekCompleted:
803         seekCompleted(notification.finished());
804         break;
805     case Notification::AssetMetadataLoaded:
806         metadataLoaded();
807         updateStates();
808         break;
809     case Notification::AssetPlayabilityKnown:
810         updateStates();
811         playabilityKnown();
812         break;
813     case Notification::DurationChanged:
814         invalidateCachedDuration();
815         break;
816     case Notification::ContentsNeedsDisplay:
817         contentsNeedsDisplay();
818         break;
819     case Notification::InbandTracksNeedConfiguration:
820 #if HAVE(AVFOUNDATION_TEXT_TRACK_SUPPORT)
821         m_inbandTrackConfigurationPending = false;
822         configureInbandTracks();
823 #endif
824         break;
825
826     case Notification::None:
827         ASSERT_NOT_REACHED();
828         break;
829     }
830 }
831
832 #if HAVE(AVFOUNDATION_TEXT_TRACK_SUPPORT)
833 void MediaPlayerPrivateAVFoundation::configureInbandTracks()
834 {
835     RefPtr<InbandTextTrackPrivateAVF> trackToEnable;
836
837     // AVFoundation can only emit cues for one track at a time, so enable the first track that is showing, or the first that
838     // is hidden if none are showing. Otherwise disable all tracks.
839     for (unsigned i = 0; i < m_textTracks.size(); ++i) {
840         RefPtr<InbandTextTrackPrivateAVF> track = m_textTracks[i];
841         if (track->mode() == InbandTextTrackPrivate::Showing) {
842             trackToEnable = track;
843             break;
844         }
845         if (track->mode() == InbandTextTrackPrivate::Hidden)
846             trackToEnable = track;
847     }
848
849     setCurrentTrack(trackToEnable.get());
850 }
851
852 void MediaPlayerPrivateAVFoundation::trackModeChanged()
853 {
854     if (m_inbandTrackConfigurationPending)
855         return;
856     m_inbandTrackConfigurationPending = true;
857     scheduleMainThreadNotification(Notification::InbandTracksNeedConfiguration);
858 }
859 #endif
860
861 } // namespace WebCore
862
863 #endif