8495315c8ff11ee6cf0b656bfc35da3dc8bfb2a6
[WebKit-https.git] / Source / WebCore / platform / graphics / MediaPlayer.cpp
1 /*
2  * Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013 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)
29 #include "MediaPlayer.h"
30
31 #include "ContentType.h"
32 #include "Document.h"
33 #include "Frame.h"
34 #include "FrameView.h"
35 #include "IntRect.h"
36 #include "Logging.h"
37 #include "MIMETypeRegistry.h"
38 #include "MediaPlayerPrivate.h"
39 #include "Settings.h"
40 #include "TimeRanges.h"
41 #include <wtf/text/CString.h>
42
43 #if ENABLE(VIDEO_TRACK)
44 #include "InbandTextTrackPrivate.h"
45 #endif
46
47 #if ENABLE(MEDIA_SOURCE)
48 #include "HTMLMediaSource.h"
49 #endif
50
51 #if USE(GSTREAMER)
52 #include "MediaPlayerPrivateGStreamer.h"
53 #define PlatformMediaEngineClassName MediaPlayerPrivateGStreamer
54 #endif
55
56 #if PLATFORM(MAC)
57 #if PLATFORM(IOS)
58 #include "MediaPlayerPrivateIOS.h"
59 #define PlatformMediaEngineClassName MediaPlayerPrivateIOS
60 #else
61 #include "MediaPlayerPrivateQTKit.h"
62 #endif
63 #if USE(AVFOUNDATION)
64 #include "MediaPlayerPrivateAVFoundationObjC.h"
65 #if ENABLE(MEDIA_SOURCE)
66 #include "MediaPlayerPrivateMediaSourceAVFObjC.h"
67 #endif
68 #endif
69 #elif OS(WINCE)
70 #include "MediaPlayerPrivateWinCE.h"
71 #define PlatformMediaEngineClassName MediaPlayerPrivate
72 #elif PLATFORM(WIN) && !USE(GSTREAMER)
73 #if USE(AVFOUNDATION)
74 #include "MediaPlayerPrivateAVFoundationCF.h"
75 #endif // USE(AVFOUNDATION)
76 #endif
77
78 namespace WebCore {
79
80 const PlatformMedia NoPlatformMedia = { PlatformMedia::None, {0} };
81
82 // a null player to make MediaPlayer logic simpler
83
84 class NullMediaPlayerPrivate : public MediaPlayerPrivateInterface {
85 public:
86     NullMediaPlayerPrivate(MediaPlayer*) { }
87
88     virtual void load(const String&) { }
89 #if ENABLE(MEDIA_SOURCE)
90     virtual void load(const String&, PassRefPtr<HTMLMediaSource>) { }
91 #endif
92     virtual void cancelLoad() { }
93
94     virtual void prepareToPlay() { }
95     virtual void play() { }
96     virtual void pause() { }    
97
98     virtual PlatformMedia platformMedia() const { return NoPlatformMedia; }
99 #if USE(ACCELERATED_COMPOSITING)
100     virtual PlatformLayer* platformLayer() const { return 0; }
101 #endif
102
103     virtual IntSize naturalSize() const { return IntSize(0, 0); }
104
105     virtual bool hasVideo() const { return false; }
106     virtual bool hasAudio() const { return false; }
107
108     virtual void setVisible(bool) { }
109
110     virtual double durationDouble() const { return 0; }
111
112     virtual double currentTimeDouble() const { return 0; }
113     virtual void seekDouble(double) { }
114     virtual bool seeking() const { return false; }
115
116     virtual void setRateDouble(double) { }
117     virtual void setPreservesPitch(bool) { }
118     virtual bool paused() const { return false; }
119
120     virtual void setVolumeDouble(double) { }
121
122     virtual bool supportsMuting() const { return false; }
123     virtual void setMuted(bool) { }
124
125     virtual bool hasClosedCaptions() const { return false; }
126     virtual void setClosedCaptionsVisible(bool) { };
127
128     virtual MediaPlayer::NetworkState networkState() const { return MediaPlayer::Empty; }
129     virtual MediaPlayer::ReadyState readyState() const { return MediaPlayer::HaveNothing; }
130
131     virtual double maxTimeSeekableDouble() const { return 0; }
132     virtual double minTimeSeekable() const { return 0; }
133     virtual PassRefPtr<TimeRanges> buffered() const { return TimeRanges::create(); }
134
135     virtual unsigned totalBytes() const { return 0; }
136     virtual bool didLoadingProgress() const { return false; }
137
138     virtual void setSize(const IntSize&) { }
139
140     virtual void paint(GraphicsContext*, const IntRect&) { }
141
142     virtual bool canLoadPoster() const { return false; }
143     virtual void setPoster(const String&) { }
144
145 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
146     virtual void deliverNotification(MediaPlayerProxyNotificationType) { }
147     virtual void setMediaPlayerProxy(WebMediaPlayerProxy*) { }
148     virtual void setControls(bool) { }
149 #endif
150
151     virtual bool hasSingleSecurityOrigin() const { return true; }
152
153 #if ENABLE(ENCRYPTED_MEDIA)
154     virtual MediaPlayer::MediaKeyException generateKeyRequest(const String&, const unsigned char*, unsigned) OVERRIDE { return MediaPlayer::InvalidPlayerState; }
155     virtual MediaPlayer::MediaKeyException addKey(const String&, const unsigned char*, unsigned, const unsigned char*, unsigned, const String&) OVERRIDE { return MediaPlayer::InvalidPlayerState; }
156     virtual MediaPlayer::MediaKeyException cancelKeyRequest(const String&, const String&) OVERRIDE { return MediaPlayer::InvalidPlayerState; }
157 #endif
158 };
159
160 static PassOwnPtr<MediaPlayerPrivateInterface> createNullMediaPlayer(MediaPlayer* player) 
161
162     return adoptPtr(new NullMediaPlayerPrivate(player)); 
163 }
164
165
166 // engine support
167
168 struct MediaPlayerFactory {
169     WTF_MAKE_NONCOPYABLE(MediaPlayerFactory); WTF_MAKE_FAST_ALLOCATED;
170 public:
171     MediaPlayerFactory(CreateMediaEnginePlayer constructor, MediaEngineSupportedTypes getSupportedTypes, MediaEngineSupportsType supportsTypeAndCodecs,
172         MediaEngineGetSitesInMediaCache getSitesInMediaCache, MediaEngineClearMediaCache clearMediaCache, MediaEngineClearMediaCacheForSite clearMediaCacheForSite)
173         : constructor(constructor)
174         , getSupportedTypes(getSupportedTypes)
175         , supportsTypeAndCodecs(supportsTypeAndCodecs)
176         , getSitesInMediaCache(getSitesInMediaCache)
177         , clearMediaCache(clearMediaCache)
178         , clearMediaCacheForSite(clearMediaCacheForSite)
179     {
180     }
181
182     CreateMediaEnginePlayer constructor;
183     MediaEngineSupportedTypes getSupportedTypes;
184     MediaEngineSupportsType supportsTypeAndCodecs;
185     MediaEngineGetSitesInMediaCache getSitesInMediaCache;
186     MediaEngineClearMediaCache clearMediaCache;
187     MediaEngineClearMediaCacheForSite clearMediaCacheForSite;
188 };
189
190 static void addMediaEngine(CreateMediaEnginePlayer, MediaEngineSupportedTypes, MediaEngineSupportsType, MediaEngineGetSitesInMediaCache, MediaEngineClearMediaCache, MediaEngineClearMediaCacheForSite);
191
192 static MediaPlayerFactory* bestMediaEngineForSupportParameters(const MediaEngineSupportParameters&, MediaPlayerFactory* current = 0);
193 static MediaPlayerFactory* nextMediaEngine(MediaPlayerFactory* current);
194
195 enum RequeryEngineOptions { DoNotResetEngines, ResetEngines };
196 static Vector<MediaPlayerFactory*>& installedMediaEngines(RequeryEngineOptions requeryFlags = DoNotResetEngines )
197 {
198     DEFINE_STATIC_LOCAL(Vector<MediaPlayerFactory*>, installedEngines, ());
199     static bool enginesQueried = false;
200
201     if (requeryFlags == ResetEngines) {
202         installedEngines.clear();
203         enginesQueried = false;
204         return installedEngines;
205     }
206
207     if (!enginesQueried) {
208         enginesQueried = true;
209
210 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
211     if (Settings::isVideoPluginProxyEnabled())
212         MediaPlayerPrivateIOS::registerMediaEngine(addMediaEngine);
213 #endif
214
215 #if USE(AVFOUNDATION)
216         if (Settings::isAVFoundationEnabled()) {
217 #if PLATFORM(MAC)
218             MediaPlayerPrivateAVFoundationObjC::registerMediaEngine(addMediaEngine);
219 #if ENABLE(MEDIA_SOURCE)
220             MediaPlayerPrivateMediaSourceAVFObjC::registerMediaEngine(addMediaEngine);
221 #endif
222 #elif PLATFORM(WIN)
223             MediaPlayerPrivateAVFoundationCF::registerMediaEngine(addMediaEngine);
224 #endif
225         }
226 #endif
227
228 #if !PLATFORM(IOS)
229 #if PLATFORM(MAC)
230         if (Settings::isQTKitEnabled())
231             MediaPlayerPrivateQTKit::registerMediaEngine(addMediaEngine);
232 #endif
233 #endif
234
235 #if defined(PlatformMediaEngineClassName)
236         PlatformMediaEngineClassName::registerMediaEngine(addMediaEngine);
237 #endif
238     }
239
240     return installedEngines;
241 }
242
243 static void addMediaEngine(CreateMediaEnginePlayer constructor, MediaEngineSupportedTypes getSupportedTypes, MediaEngineSupportsType supportsType,
244     MediaEngineGetSitesInMediaCache getSitesInMediaCache, MediaEngineClearMediaCache clearMediaCache, MediaEngineClearMediaCacheForSite clearMediaCacheForSite)
245 {
246     ASSERT(constructor);
247     ASSERT(getSupportedTypes);
248     ASSERT(supportsType);
249
250     installedMediaEngines().append(new MediaPlayerFactory(constructor, getSupportedTypes, supportsType, getSitesInMediaCache, clearMediaCache, clearMediaCacheForSite));
251 }
252
253 static const AtomicString& applicationOctetStream()
254 {
255     DEFINE_STATIC_LOCAL(const AtomicString, applicationOctetStream, ("application/octet-stream", AtomicString::ConstructFromLiteral));
256     return applicationOctetStream;
257 }
258
259 static const AtomicString& textPlain()
260 {
261     DEFINE_STATIC_LOCAL(const AtomicString, textPlain, ("text/plain", AtomicString::ConstructFromLiteral));
262     return textPlain;
263 }
264
265 static const AtomicString& codecs()
266 {
267     DEFINE_STATIC_LOCAL(const AtomicString, codecs, ("codecs", AtomicString::ConstructFromLiteral));
268     return codecs;
269 }
270
271 static MediaPlayerFactory* bestMediaEngineForSupportParameters(const MediaEngineSupportParameters& parameters, MediaPlayerFactory* current)
272 {
273     if (parameters.type.isEmpty())
274         return 0;
275
276     Vector<MediaPlayerFactory*>& engines = installedMediaEngines();
277     if (engines.isEmpty())
278         return 0;
279
280     // 4.8.10.3 MIME types - In the absence of a specification to the contrary, the MIME type "application/octet-stream" 
281     // when used with parameters, e.g. "application/octet-stream;codecs=theora", is a type that the user agent knows 
282     // it cannot render.
283     if (parameters.type == applicationOctetStream()) {
284         if (!parameters.codecs.isEmpty())
285             return 0;
286     }
287
288     MediaPlayerFactory* engine = 0;
289     MediaPlayer::SupportsType supported = MediaPlayer::IsNotSupported;
290     unsigned count = engines.size();
291     for (unsigned ndx = 0; ndx < count; ndx++) {
292         if (current) {
293             if (current == engines[ndx])
294                 current = 0;
295             continue;
296         }
297         MediaPlayer::SupportsType engineSupport = engines[ndx]->supportsTypeAndCodecs(parameters);
298         if (engineSupport > supported) {
299             supported = engineSupport;
300             engine = engines[ndx];
301         }
302     }
303
304     return engine;
305 }
306
307 static MediaPlayerFactory* nextMediaEngine(MediaPlayerFactory* current)
308 {
309     Vector<MediaPlayerFactory*>& engines = installedMediaEngines();
310     if (engines.isEmpty())
311         return 0;
312
313     if (!current) 
314         return engines.first();
315
316     size_t currentIndex = engines.find(current);
317     if (currentIndex == WTF::notFound || currentIndex + 1 >= engines.size()) 
318         return 0;
319
320     return engines[currentIndex + 1];
321 }
322
323 // media player
324
325 MediaPlayer::MediaPlayer(MediaPlayerClient* client)
326     : m_mediaPlayerClient(client)
327     , m_reloadTimer(this, &MediaPlayer::reloadTimerFired)
328     , m_private(createNullMediaPlayer(this))
329     , m_currentMediaEngine(0)
330     , m_frameView(0)
331     , m_preload(Auto)
332     , m_visible(false)
333     , m_rate(1.0f)
334     , m_volume(1.0f)
335     , m_muted(false)
336     , m_preservesPitch(true)
337     , m_privateBrowsing(false)
338     , m_shouldPrepareToRender(false)
339     , m_contentMIMETypeWasInferredFromExtension(false)
340 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
341     , m_playerProxy(0)
342 #endif
343 {
344 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
345     Vector<MediaPlayerFactory*>& engines = installedMediaEngines();
346     if (!engines.isEmpty()) {
347         m_currentMediaEngine = engines[0];
348         m_private = engines[0]->constructor(this);
349         if (m_mediaPlayerClient)
350             m_mediaPlayerClient->mediaPlayerEngineUpdated(this);
351     }
352 #endif
353 }
354
355 MediaPlayer::~MediaPlayer()
356 {
357     m_mediaPlayerClient = 0;
358 }
359
360 bool MediaPlayer::load(const URL& url, const ContentType& contentType, const String& keySystem)
361 {
362     m_contentMIMEType = contentType.type().lower();
363     m_contentTypeCodecs = contentType.parameter(codecs());
364     m_url = url;
365     m_keySystem = keySystem.lower();
366     m_contentMIMETypeWasInferredFromExtension = false;
367
368 #if ENABLE(MEDIA_SOURCE)
369     m_mediaSource = 0;
370 #endif
371
372     // If the MIME type is missing or is not meaningful, try to figure it out from the URL.
373     if (m_contentMIMEType.isEmpty() || m_contentMIMEType == applicationOctetStream() || m_contentMIMEType == textPlain()) {
374         if (m_url.protocolIsData())
375             m_contentMIMEType = mimeTypeFromDataURL(m_url.string());
376         else {
377             String lastPathComponent = url.lastPathComponent();
378             size_t pos = lastPathComponent.reverseFind('.');
379             if (pos != notFound) {
380                 String extension = lastPathComponent.substring(pos + 1);
381                 String mediaType = MIMETypeRegistry::getMediaMIMETypeForExtension(extension);
382                 if (!mediaType.isEmpty()) {
383                     m_contentMIMEType = mediaType;
384                     m_contentMIMETypeWasInferredFromExtension = true;
385                 }
386             }
387         }
388     }
389
390     loadWithNextMediaEngine(0);
391     return m_currentMediaEngine;
392 }
393
394 #if ENABLE(MEDIA_SOURCE)
395 bool MediaPlayer::load(const URL& url, const ContentType& contentType, PassRefPtr<HTMLMediaSource> mediaSource)
396 {
397     m_mediaSource = mediaSource;
398     m_contentMIMEType = contentType.type().lower();
399     m_contentTypeCodecs = contentType.parameter(codecs());
400     m_url = url;
401     m_keySystem = "";
402     m_contentMIMETypeWasInferredFromExtension = false;
403     loadWithNextMediaEngine(0);
404     return m_currentMediaEngine;
405 }
406 #endif
407
408 MediaPlayerFactory* MediaPlayer::nextBestMediaEngine(MediaPlayerFactory* current) const
409 {
410     MediaEngineSupportParameters parameters;
411     parameters.type = m_contentMIMEType;
412     parameters.codecs = m_contentTypeCodecs;
413     parameters.url = m_url;
414 #if ENABLE(ENCRYPTED_MEDIA)
415     parameters.keySystem = m_keySystem;
416 #endif
417 #if ENABLE(MEDIA_SOURCE)
418     parameters.isMediaSource = !!m_mediaSource;
419 #endif
420
421     return bestMediaEngineForSupportParameters(parameters, current);
422 }
423
424 void MediaPlayer::loadWithNextMediaEngine(MediaPlayerFactory* current)
425 {
426     MediaPlayerFactory* engine = 0;
427
428     if (!m_contentMIMEType.isEmpty())
429         engine = nextBestMediaEngine(current);
430
431     // If no MIME type is specified or the type was inferred from the file extension, just use the next engine.
432     if (!engine && (m_contentMIMEType.isEmpty() || m_contentMIMETypeWasInferredFromExtension))
433         engine = nextMediaEngine(current);
434
435     // Don't delete and recreate the player unless it comes from a different engine.
436     if (!engine) {
437         LOG(Media, "MediaPlayer::loadWithNextMediaEngine - no media engine found for type \"%s\"", m_contentMIMEType.utf8().data());
438         m_currentMediaEngine = engine;
439         m_private = nullptr;
440     } else if (m_currentMediaEngine != engine) {
441         m_currentMediaEngine = engine;
442         m_private = engine->constructor(this);
443         if (m_mediaPlayerClient)
444             m_mediaPlayerClient->mediaPlayerEngineUpdated(this);
445 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
446         m_private->setMediaPlayerProxy(m_playerProxy);
447 #endif
448         m_private->setPrivateBrowsingMode(m_privateBrowsing);
449         m_private->setPreload(m_preload);
450         m_private->setPreservesPitch(preservesPitch());
451         if (m_shouldPrepareToRender)
452             m_private->prepareForRendering();
453     }
454
455     if (m_private) {
456 #if ENABLE(MEDIA_SOURCE)
457         if (m_mediaSource)
458             m_private->load(m_url.string(), m_mediaSource);
459         else
460 #endif
461         m_private->load(m_url.string());
462     } else {
463         m_private = createNullMediaPlayer(this);
464         if (m_mediaPlayerClient) {
465             m_mediaPlayerClient->mediaPlayerEngineUpdated(this);
466             m_mediaPlayerClient->mediaPlayerResourceNotSupported(this);
467         }
468     }
469 }
470
471 bool MediaPlayer::hasAvailableVideoFrame() const
472 {
473     return m_private->hasAvailableVideoFrame();
474 }
475
476 void MediaPlayer::prepareForRendering()
477 {
478     m_shouldPrepareToRender = true;
479     m_private->prepareForRendering();
480 }
481
482 bool MediaPlayer::canLoadPoster() const
483 {
484     return m_private->canLoadPoster();
485 }
486
487 void MediaPlayer::setPoster(const String& url)
488 {
489     m_private->setPoster(url);
490 }    
491
492 void MediaPlayer::cancelLoad()
493 {
494     m_private->cancelLoad();
495 }    
496
497 void MediaPlayer::prepareToPlay()
498 {
499     m_private->prepareToPlay();
500 }
501
502 void MediaPlayer::play()
503 {
504     m_private->play();
505 }
506
507 void MediaPlayer::pause()
508 {
509     m_private->pause();
510 }
511
512 #if ENABLE(ENCRYPTED_MEDIA)
513 MediaPlayer::MediaKeyException MediaPlayer::generateKeyRequest(const String& keySystem, const unsigned char* initData, unsigned initDataLength)
514 {
515     return m_private->generateKeyRequest(keySystem.lower(), initData, initDataLength);
516 }
517
518 MediaPlayer::MediaKeyException MediaPlayer::addKey(const String& keySystem, const unsigned char* key, unsigned keyLength, const unsigned char* initData, unsigned initDataLength, const String& sessionId)
519 {
520     return m_private->addKey(keySystem.lower(), key, keyLength, initData, initDataLength, sessionId);
521 }
522
523 MediaPlayer::MediaKeyException MediaPlayer::cancelKeyRequest(const String& keySystem, const String& sessionId)
524 {
525     return m_private->cancelKeyRequest(keySystem.lower(), sessionId);
526 }
527 #endif
528
529 double MediaPlayer::duration() const
530 {
531     return m_private->durationDouble();
532 }
533
534 double MediaPlayer::startTime() const
535 {
536     return m_private->startTimeDouble();
537 }
538
539 double MediaPlayer::initialTime() const
540 {
541     return m_private->initialTime();
542 }
543
544 double MediaPlayer::currentTime() const
545 {
546     return m_private->currentTimeDouble();
547 }
548
549 void MediaPlayer::seekWithTolerance(double time, double negativeTolerance, double positiveTolerance)
550 {
551     m_private->seekWithTolerance(time, negativeTolerance, positiveTolerance);
552 }
553
554 void MediaPlayer::seek(double time)
555 {
556     m_private->seekDouble(time);
557 }
558
559 bool MediaPlayer::paused() const
560 {
561     return m_private->paused();
562 }
563
564 bool MediaPlayer::seeking() const
565 {
566     return m_private->seeking();
567 }
568
569 bool MediaPlayer::supportsFullscreen() const
570 {
571     return m_private->supportsFullscreen();
572 }
573
574 bool MediaPlayer::supportsSave() const
575 {
576     return m_private->supportsSave();
577 }
578
579 bool MediaPlayer::supportsScanning() const
580 {
581     return m_private->supportsScanning();
582 }
583
584 bool MediaPlayer::requiresImmediateCompositing() const
585 {
586     return m_private->requiresImmediateCompositing();
587 }
588
589 IntSize MediaPlayer::naturalSize()
590 {
591     return m_private->naturalSize();
592 }
593
594 bool MediaPlayer::hasVideo() const
595 {
596     return m_private->hasVideo();
597 }
598
599 bool MediaPlayer::hasAudio() const
600 {
601     return m_private->hasAudio();
602 }
603
604 bool MediaPlayer::inMediaDocument()
605 {
606     if (!m_frameView)
607         return false;
608     Document* document = m_frameView->frame().document();
609     return document && document->isMediaDocument();
610 }
611
612 PlatformMedia MediaPlayer::platformMedia() const
613 {
614     return m_private->platformMedia();
615 }
616
617 #if USE(ACCELERATED_COMPOSITING)
618 PlatformLayer* MediaPlayer::platformLayer() const
619 {
620     return m_private->platformLayer();
621 }
622 #endif
623
624 MediaPlayer::NetworkState MediaPlayer::networkState()
625 {
626     return m_private->networkState();
627 }
628
629 MediaPlayer::ReadyState MediaPlayer::readyState()
630 {
631     return m_private->readyState();
632 }
633
634 double MediaPlayer::volume() const
635 {
636     return m_volume;
637 }
638
639 void MediaPlayer::setVolume(double volume)
640 {
641     m_volume = volume;
642
643     if (m_private->supportsMuting() || !m_muted)
644         m_private->setVolumeDouble(volume);
645 }
646
647 bool MediaPlayer::muted() const
648 {
649     return m_muted;
650 }
651
652 void MediaPlayer::setMuted(bool muted)
653 {
654     m_muted = muted;
655
656     if (m_private->supportsMuting())
657         m_private->setMuted(muted);
658     else
659         m_private->setVolume(muted ? 0 : m_volume);
660 }
661
662 bool MediaPlayer::hasClosedCaptions() const
663 {
664     return m_private->hasClosedCaptions();
665 }
666
667 void MediaPlayer::setClosedCaptionsVisible(bool closedCaptionsVisible)
668 {
669     m_private->setClosedCaptionsVisible(closedCaptionsVisible);
670 }
671
672 double MediaPlayer::rate() const
673 {
674     return m_rate;
675 }
676
677 void MediaPlayer::setRate(double rate)
678 {
679     m_rate = rate;
680     m_private->setRateDouble(rate);
681 }
682
683 bool MediaPlayer::preservesPitch() const
684 {
685     return m_preservesPitch;
686 }
687
688 void MediaPlayer::setPreservesPitch(bool preservesPitch)
689 {
690     m_preservesPitch = preservesPitch;
691     m_private->setPreservesPitch(preservesPitch);
692 }
693
694 PassRefPtr<TimeRanges> MediaPlayer::buffered()
695 {
696     return m_private->buffered();
697 }
698
699 PassRefPtr<TimeRanges> MediaPlayer::seekable()
700 {
701     return m_private->seekable();
702 }
703
704 double MediaPlayer::maxTimeSeekable()
705 {
706     return m_private->maxTimeSeekableDouble();
707 }
708
709 double MediaPlayer::minTimeSeekable()
710 {
711     return m_private->minTimeSeekable();
712 }
713
714 bool MediaPlayer::didLoadingProgress()
715 {
716     return m_private->didLoadingProgress();
717 }
718
719 void MediaPlayer::setSize(const IntSize& size)
720
721     m_size = size;
722     m_private->setSize(size);
723 }
724
725 bool MediaPlayer::visible() const
726 {
727     return m_visible;
728 }
729
730 void MediaPlayer::setVisible(bool b)
731 {
732     m_visible = b;
733     m_private->setVisible(b);
734 }
735
736 MediaPlayer::Preload MediaPlayer::preload() const
737 {
738     return m_preload;
739 }
740
741 void MediaPlayer::setPreload(MediaPlayer::Preload preload)
742 {
743     m_preload = preload;
744     m_private->setPreload(preload);
745 }
746
747 void MediaPlayer::paint(GraphicsContext* p, const IntRect& r)
748 {
749     m_private->paint(p, r);
750 }
751
752 void MediaPlayer::paintCurrentFrameInContext(GraphicsContext* p, const IntRect& r)
753 {
754     m_private->paintCurrentFrameInContext(p, r);
755 }
756
757 bool MediaPlayer::copyVideoTextureToPlatformTexture(GraphicsContext3D* context, Platform3DObject texture, GC3Dint level, GC3Denum type, GC3Denum internalFormat, bool premultiplyAlpha, bool flipY)
758 {
759     return m_private->copyVideoTextureToPlatformTexture(context, texture, level, type, internalFormat, premultiplyAlpha, flipY);
760 }
761
762 PassNativeImagePtr MediaPlayer::nativeImageForCurrentTime()
763 {
764     return m_private->nativeImageForCurrentTime();
765 }
766
767 MediaPlayer::SupportsType MediaPlayer::supportsType(const MediaEngineSupportParameters& parameters, const MediaPlayerSupportsTypeClient* client)
768 {
769     // 4.8.10.3 MIME types - The canPlayType(type) method must return the empty string if type is a type that the 
770     // user agent knows it cannot render or is the type "application/octet-stream"
771     if (parameters.type == applicationOctetStream())
772         return IsNotSupported;
773
774     MediaPlayerFactory* engine = bestMediaEngineForSupportParameters(parameters);
775     if (!engine)
776         return IsNotSupported;
777
778 #if PLATFORM(MAC)
779     // YouTube will ask if the HTMLMediaElement canPlayType video/webm, then
780     // video/x-flv, then finally video/mp4, and will then load a URL of the first type
781     // in that list which returns "probably". When Perian is installed,
782     // MediaPlayerPrivateQTKit claims to support both video/webm and video/x-flv, but
783     // due to a bug in Perian, loading media in these formats will sometimes fail on
784     // slow connections. <https://bugs.webkit.org/show_bug.cgi?id=86409>
785     if (client && client->mediaPlayerNeedsSiteSpecificHacks()) {
786         String host = client->mediaPlayerDocumentHost();
787         if ((host.endsWith(".youtube.com", false) || equalIgnoringCase("youtube.com", host))
788             && (parameters.type.startsWith("video/webm", false) || parameters.type.startsWith("video/x-flv", false)))
789             return IsNotSupported;
790     }
791 #else
792     UNUSED_PARAM(client);
793 #endif
794
795     return engine->supportsTypeAndCodecs(parameters);
796 }
797
798 void MediaPlayer::getSupportedTypes(HashSet<String>& types)
799 {
800     Vector<MediaPlayerFactory*>& engines = installedMediaEngines();
801     if (engines.isEmpty())
802         return;
803
804     unsigned count = engines.size();
805     for (unsigned ndx = 0; ndx < count; ndx++)
806         engines[ndx]->getSupportedTypes(types);
807
808
809 bool MediaPlayer::isAvailable()
810 {
811     return !installedMediaEngines().isEmpty();
812
813
814 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
815 void MediaPlayer::deliverNotification(MediaPlayerProxyNotificationType notification)
816 {
817     m_private->deliverNotification(notification);
818 }
819
820 void MediaPlayer::setMediaPlayerProxy(WebMediaPlayerProxy* proxy)
821 {
822     m_playerProxy = proxy;
823     m_private->setMediaPlayerProxy(proxy);
824 }
825
826 void MediaPlayer::setControls(bool controls)
827 {
828     m_private->setControls(controls);
829 }
830 #endif
831
832 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO) || USE(NATIVE_FULLSCREEN_VIDEO)
833 void MediaPlayer::enterFullscreen()
834 {
835     m_private->enterFullscreen();
836 }
837
838 void MediaPlayer::exitFullscreen()
839 {
840     m_private->exitFullscreen();
841 }
842 #endif
843
844 #if ENABLE(IOS_AIRPLAY)
845 bool MediaPlayer::isCurrentPlaybackTargetWireless() const
846 {
847     return m_private->isCurrentPlaybackTargetWireless();
848 }
849
850 void MediaPlayer::showPlaybackTargetPicker()
851 {
852     m_private->showPlaybackTargetPicker();
853 }
854
855 bool MediaPlayer::hasWirelessPlaybackTargets() const
856 {
857     return m_private->hasWirelessPlaybackTargets();
858 }
859
860 bool MediaPlayer::wirelessVideoPlaybackDisabled() const
861 {
862     return m_private->wirelessVideoPlaybackDisabled();
863 }
864
865 void MediaPlayer::setWirelessVideoPlaybackDisabled(bool disabled)
866 {
867     m_private->setWirelessVideoPlaybackDisabled(disabled);
868 }
869
870 void MediaPlayer::setHasPlaybackTargetAvailabilityListeners(bool hasListeners)
871 {
872     m_private->setHasPlaybackTargetAvailabilityListeners(hasListeners);
873 }
874
875 void MediaPlayer::currentPlaybackTargetIsWirelessChanged()
876 {
877     if (m_mediaPlayerClient)
878         m_mediaPlayerClient->mediaPlayerCurrentPlaybackTargetIsWirelessChanged(this);
879 }
880
881 void MediaPlayer::playbackTargetAvailabilityChanged()
882 {
883     if (m_mediaPlayerClient)
884         m_mediaPlayerClient->mediaPlayerPlaybackTargetAvailabilityChanged(this);
885 }
886 #endif
887
888 #if USE(NATIVE_FULLSCREEN_VIDEO)
889 bool MediaPlayer::canEnterFullscreen() const
890 {
891     return m_private->canEnterFullscreen();
892 }
893 #endif
894
895 #if USE(ACCELERATED_COMPOSITING)
896 void MediaPlayer::acceleratedRenderingStateChanged()
897 {
898     m_private->acceleratedRenderingStateChanged();
899 }
900
901 bool MediaPlayer::supportsAcceleratedRendering() const
902 {
903     return m_private->supportsAcceleratedRendering();
904 }
905 #endif // USE(ACCELERATED_COMPOSITING)
906
907 bool MediaPlayer::shouldMaintainAspectRatio() const
908 {
909     return m_private->shouldMaintainAspectRatio();
910 }
911
912 void MediaPlayer::setShouldMaintainAspectRatio(bool maintainAspectRatio)
913 {
914     m_private->setShouldMaintainAspectRatio(maintainAspectRatio);
915 }
916
917 bool MediaPlayer::hasSingleSecurityOrigin() const
918 {
919     return m_private->hasSingleSecurityOrigin();
920 }
921
922 bool MediaPlayer::didPassCORSAccessCheck() const
923 {
924     return m_private->didPassCORSAccessCheck();
925 }
926
927 MediaPlayer::MovieLoadType MediaPlayer::movieLoadType() const
928 {
929     return m_private->movieLoadType();
930 }
931
932 double MediaPlayer::mediaTimeForTimeValue(double timeValue) const
933 {
934     return m_private->mediaTimeForTimeValueDouble(timeValue);
935 }
936
937 double MediaPlayer::maximumDurationToCacheMediaTime() const
938 {
939     return m_private->maximumDurationToCacheMediaTime();
940 }
941
942 unsigned MediaPlayer::decodedFrameCount() const
943 {
944     return m_private->decodedFrameCount();
945 }
946
947 unsigned MediaPlayer::droppedFrameCount() const
948 {
949     return m_private->droppedFrameCount();
950 }
951
952 unsigned MediaPlayer::audioDecodedByteCount() const
953 {
954     return m_private->audioDecodedByteCount();
955 }
956
957 unsigned MediaPlayer::videoDecodedByteCount() const
958 {
959     return m_private->videoDecodedByteCount();
960 }
961
962 void MediaPlayer::reloadTimerFired(Timer<MediaPlayer>*)
963 {
964     m_private->cancelLoad();
965     loadWithNextMediaEngine(m_currentMediaEngine);
966 }
967
968 void MediaPlayer::getSitesInMediaCache(Vector<String>& sites)
969 {
970     Vector<MediaPlayerFactory*>& engines = installedMediaEngines();
971     unsigned size = engines.size();
972     for (unsigned i = 0; i < size; i++) {
973         if (!engines[i]->getSitesInMediaCache)
974             continue;
975         Vector<String> engineSites;
976         engines[i]->getSitesInMediaCache(engineSites);
977         sites.appendVector(engineSites);
978     }
979 }
980
981 void MediaPlayer::clearMediaCache()
982 {
983     Vector<MediaPlayerFactory*>& engines = installedMediaEngines();
984     unsigned size = engines.size();
985     for (unsigned i = 0; i < size; i++) {
986         if (engines[i]->clearMediaCache)
987             engines[i]->clearMediaCache();
988     }
989 }
990
991 void MediaPlayer::clearMediaCacheForSite(const String& site)
992 {
993     Vector<MediaPlayerFactory*>& engines = installedMediaEngines();
994     unsigned size = engines.size();
995     for (unsigned i = 0; i < size; i++) {
996         if (engines[i]->clearMediaCacheForSite)
997             engines[i]->clearMediaCacheForSite(site);
998     }
999 }
1000
1001 void MediaPlayer::setPrivateBrowsingMode(bool privateBrowsingMode)
1002 {
1003     m_privateBrowsing = privateBrowsingMode;
1004     m_private->setPrivateBrowsingMode(m_privateBrowsing);
1005 }
1006
1007 #if PLATFORM(IOS)
1008 void MediaPlayer::attributeChanged(const String& name, const String& value) 
1009 {
1010     m_private->attributeChanged(name, value);
1011 }
1012
1013 bool MediaPlayer::readyForPlayback() const
1014 {
1015     return m_private->readyForPlayback();
1016 }
1017 #endif
1018
1019 // Client callbacks.
1020 void MediaPlayer::networkStateChanged()
1021 {
1022     // If more than one media engine is installed and this one failed before finding metadata,
1023     // let the next engine try.
1024     if (m_private->networkState() >= FormatError
1025         && m_private->readyState() < HaveMetadata
1026         && installedMediaEngines().size() > 1) {
1027         if (m_contentMIMEType.isEmpty() || nextBestMediaEngine(m_currentMediaEngine)) {
1028             m_reloadTimer.startOneShot(0);
1029             return;
1030         }
1031     }
1032     if (m_mediaPlayerClient)
1033         m_mediaPlayerClient->mediaPlayerNetworkStateChanged(this);
1034 }
1035
1036 void MediaPlayer::readyStateChanged()
1037 {
1038     if (m_mediaPlayerClient)
1039         m_mediaPlayerClient->mediaPlayerReadyStateChanged(this);
1040 }
1041
1042 void MediaPlayer::volumeChanged(double newVolume)
1043 {
1044 #if PLATFORM(IOS)
1045     UNUSED_PARAM(newVolume);
1046     m_volume = m_private->volume();
1047 #else
1048     m_volume = newVolume;
1049 #endif
1050     if (m_mediaPlayerClient)
1051         m_mediaPlayerClient->mediaPlayerVolumeChanged(this);
1052 }
1053
1054 void MediaPlayer::muteChanged(bool newMuted)
1055 {
1056     m_muted = newMuted;
1057     if (m_mediaPlayerClient)
1058         m_mediaPlayerClient->mediaPlayerMuteChanged(this);
1059 }
1060
1061 void MediaPlayer::timeChanged()
1062 {
1063     if (m_mediaPlayerClient)
1064         m_mediaPlayerClient->mediaPlayerTimeChanged(this);
1065 }
1066
1067 void MediaPlayer::sizeChanged()
1068 {
1069     if (m_mediaPlayerClient)
1070         m_mediaPlayerClient->mediaPlayerSizeChanged(this);
1071 }
1072
1073 void MediaPlayer::repaint()
1074 {
1075     if (m_mediaPlayerClient)
1076         m_mediaPlayerClient->mediaPlayerRepaint(this);
1077 }
1078
1079 void MediaPlayer::durationChanged()
1080 {
1081     if (m_mediaPlayerClient)
1082         m_mediaPlayerClient->mediaPlayerDurationChanged(this);
1083 }
1084
1085 void MediaPlayer::rateChanged()
1086 {
1087     if (m_mediaPlayerClient)
1088         m_mediaPlayerClient->mediaPlayerRateChanged(this);
1089 }
1090
1091 void MediaPlayer::playbackStateChanged()
1092 {
1093     if (m_mediaPlayerClient)
1094         m_mediaPlayerClient->mediaPlayerPlaybackStateChanged(this);
1095 }
1096
1097 void MediaPlayer::firstVideoFrameAvailable()
1098 {
1099     if (m_mediaPlayerClient)
1100         m_mediaPlayerClient->mediaPlayerFirstVideoFrameAvailable(this);
1101 }
1102
1103 void MediaPlayer::characteristicChanged()
1104 {
1105     if (m_mediaPlayerClient)
1106         m_mediaPlayerClient->mediaPlayerCharacteristicChanged(this);
1107 }
1108
1109 #if ENABLE(WEB_AUDIO)
1110 AudioSourceProvider* MediaPlayer::audioSourceProvider()
1111 {
1112     return m_private->audioSourceProvider();
1113 }
1114 #endif // WEB_AUDIO
1115
1116 #if ENABLE(ENCRYPTED_MEDIA)
1117 void MediaPlayer::keyAdded(const String& keySystem, const String& sessionId)
1118 {
1119     if (m_mediaPlayerClient)
1120         m_mediaPlayerClient->mediaPlayerKeyAdded(this, keySystem, sessionId);
1121 }
1122
1123 void MediaPlayer::keyError(const String& keySystem, const String& sessionId, MediaPlayerClient::MediaKeyErrorCode errorCode, unsigned short systemCode)
1124 {
1125     if (m_mediaPlayerClient)
1126         m_mediaPlayerClient->mediaPlayerKeyError(this, keySystem, sessionId, errorCode, systemCode);
1127 }
1128
1129 void MediaPlayer::keyMessage(const String& keySystem, const String& sessionId, const unsigned char* message, unsigned messageLength, const URL& defaultURL)
1130 {
1131     if (m_mediaPlayerClient)
1132         m_mediaPlayerClient->mediaPlayerKeyMessage(this, keySystem, sessionId, message, messageLength, defaultURL);
1133 }
1134
1135 bool MediaPlayer::keyNeeded(const String& keySystem, const String& sessionId, const unsigned char* initData, unsigned initDataLength)
1136 {
1137     if (m_mediaPlayerClient)
1138         return m_mediaPlayerClient->mediaPlayerKeyNeeded(this, keySystem, sessionId, initData, initDataLength);
1139     return false;
1140 }
1141 #endif
1142
1143 #if ENABLE(ENCRYPTED_MEDIA_V2)
1144 bool MediaPlayer::keyNeeded(Uint8Array* initData)
1145 {
1146     if (m_mediaPlayerClient)
1147         return m_mediaPlayerClient->mediaPlayerKeyNeeded(this, initData);
1148     return false;
1149 }
1150 #endif
1151
1152 String MediaPlayer::referrer() const
1153 {
1154     if (!m_mediaPlayerClient)
1155         return String();
1156
1157     return m_mediaPlayerClient->mediaPlayerReferrer();
1158 }
1159
1160 String MediaPlayer::userAgent() const
1161 {
1162     if (!m_mediaPlayerClient)
1163         return String();
1164     
1165     return m_mediaPlayerClient->mediaPlayerUserAgent();
1166 }
1167
1168 String MediaPlayer::engineDescription() const
1169 {
1170     if (!m_private)
1171         return String();
1172
1173     return m_private->engineDescription();
1174 }
1175
1176 #if PLATFORM(WIN) && USE(AVFOUNDATION)
1177 GraphicsDeviceAdapter* MediaPlayer::graphicsDeviceAdapter() const
1178 {
1179     if (!m_mediaPlayerClient)
1180         return 0;
1181     
1182     return m_mediaPlayerClient->mediaPlayerGraphicsDeviceAdapter(this);
1183 }
1184 #endif
1185
1186 CachedResourceLoader* MediaPlayer::cachedResourceLoader()
1187 {
1188     if (!m_mediaPlayerClient)
1189         return 0;
1190
1191     return m_mediaPlayerClient->mediaPlayerCachedResourceLoader();
1192 }
1193
1194 #if ENABLE(VIDEO_TRACK)
1195 void MediaPlayer::addAudioTrack(PassRefPtr<AudioTrackPrivate> track)
1196 {
1197     if (!m_mediaPlayerClient)
1198         return;
1199
1200     m_mediaPlayerClient->mediaPlayerDidAddAudioTrack(track);
1201 }
1202
1203 void MediaPlayer::removeAudioTrack(PassRefPtr<AudioTrackPrivate> track)
1204 {
1205     if (!m_mediaPlayerClient)
1206         return;
1207
1208     m_mediaPlayerClient->mediaPlayerDidRemoveAudioTrack(track);
1209 }
1210
1211 void MediaPlayer::addTextTrack(PassRefPtr<InbandTextTrackPrivate> track)
1212 {
1213     if (!m_mediaPlayerClient)
1214         return;
1215
1216     m_mediaPlayerClient->mediaPlayerDidAddTextTrack(track);
1217 }
1218
1219 void MediaPlayer::removeTextTrack(PassRefPtr<InbandTextTrackPrivate> track)
1220 {
1221     if (!m_mediaPlayerClient)
1222         return;
1223
1224     m_mediaPlayerClient->mediaPlayerDidRemoveTextTrack(track);
1225 }
1226
1227 void MediaPlayer::addVideoTrack(PassRefPtr<VideoTrackPrivate> track)
1228 {
1229     if (!m_mediaPlayerClient)
1230         return;
1231
1232     m_mediaPlayerClient->mediaPlayerDidAddVideoTrack(track);
1233 }
1234
1235 void MediaPlayer::removeVideoTrack(PassRefPtr<VideoTrackPrivate> track)
1236 {
1237     if (!m_mediaPlayerClient)
1238         return;
1239
1240     m_mediaPlayerClient->mediaPlayerDidRemoveVideoTrack(track);
1241 }
1242
1243 bool MediaPlayer::requiresTextTrackRepresentation() const
1244 {
1245     return m_private->requiresTextTrackRepresentation();
1246 }
1247
1248 void MediaPlayer::setTextTrackRepresentation(TextTrackRepresentation* representation)
1249 {
1250     m_private->setTextTrackRepresentation(representation);
1251 }
1252 #endif // ENABLE(VIDEO_TRACK)
1253
1254 #if USE(PLATFORM_TEXT_TRACK_MENU)
1255 bool MediaPlayer::implementsTextTrackControls() const
1256 {
1257     return m_private->implementsTextTrackControls();
1258 }
1259
1260 PassRefPtr<PlatformTextTrackMenuInterface> MediaPlayer::textTrackMenu()
1261 {
1262     return m_private->textTrackMenu();
1263 }
1264 #endif // USE(PLATFORM_TEXT_TRACK_MENU)
1265
1266 void MediaPlayer::resetMediaEngines()
1267 {
1268     installedMediaEngines(ResetEngines);
1269 }
1270
1271 #if USE(GSTREAMER)
1272 void MediaPlayer::simulateAudioInterruption()
1273 {
1274     if (!m_private)
1275         return;
1276
1277     m_private->simulateAudioInterruption();
1278 }
1279 #endif
1280
1281 String MediaPlayer::languageOfPrimaryAudioTrack() const
1282 {
1283     if (!m_private)
1284         return emptyString();
1285     
1286     return m_private->languageOfPrimaryAudioTrack();
1287 }
1288
1289 size_t MediaPlayer::extraMemoryCost() const
1290 {
1291     if (!m_private)
1292         return 0;
1293
1294     return m_private->extraMemoryCost();
1295 }
1296
1297 unsigned long long MediaPlayer::fileSize() const
1298 {
1299     if (!m_private)
1300         return 0;
1301     
1302     return m_private->fileSize();
1303 }
1304
1305 #if ENABLE(MEDIA_SOURCE)
1306 unsigned long MediaPlayer::totalVideoFrames()
1307 {
1308     if (!m_private)
1309         return 0;
1310
1311     return m_private->totalVideoFrames();
1312 }
1313
1314 unsigned long MediaPlayer::droppedVideoFrames()
1315 {
1316     if (!m_private)
1317         return 0;
1318
1319     return m_private->droppedVideoFrames();
1320 }
1321
1322 unsigned long MediaPlayer::corruptedVideoFrames()
1323 {
1324     if (!m_private)
1325         return 0;
1326
1327     return m_private->corruptedVideoFrames();
1328 }
1329
1330 double MediaPlayer::totalFrameDelay()
1331 {
1332     if (!m_private)
1333         return 0;
1334
1335     return m_private->totalFrameDelay();
1336 }
1337 #endif
1338
1339 bool MediaPlayer::shouldWaitForResponseToAuthenticationChallenge(const AuthenticationChallenge& challenge)
1340 {
1341     if (!m_mediaPlayerClient)
1342         return false;
1343
1344     return m_mediaPlayerClient->mediaPlayerShouldWaitForResponseToAuthenticationChallenge(challenge);
1345 }
1346
1347 void MediaPlayerFactorySupport::callRegisterMediaEngine(MediaEngineRegister registerMediaEngine)
1348 {
1349     registerMediaEngine(addMediaEngine);
1350 }
1351
1352 }
1353
1354 #endif