tagging audio streams and changing audio sink to pulseaudio
[profile/ivi/webkit-efl.git] / Source / WebCore / platform / graphics / MediaPlayer.cpp
1 /*
2  * Copyright (C) 2007, 2008, 2009, 2010, 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)
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 PLATFORM(QT)
44 #include <QtGlobal>
45 #endif
46
47 #if USE(GSTREAMER)
48 #include "MediaPlayerPrivateGStreamer.h"
49 #define PlatformMediaEngineClassName MediaPlayerPrivateGStreamer
50 #endif
51
52 #if PLATFORM(MAC) || (PLATFORM(QT) && USE(QTKIT))
53 #include "MediaPlayerPrivateQTKit.h"
54 #if USE(AVFOUNDATION)
55 #include "MediaPlayerPrivateAVFoundationObjC.h"
56 #endif
57 #define PlatformMediaEngineClassName MediaPlayerPrivateQTKit
58 #elif OS(WINCE) && !PLATFORM(QT)
59 #include "MediaPlayerPrivateWinCE.h"
60 #define PlatformMediaEngineClassName MediaPlayerPrivate
61 #elif PLATFORM(WIN)
62 #include "MediaPlayerPrivateQuickTimeVisualContext.h"
63 #define PlatformMediaEngineClassName MediaPlayerPrivateQuickTimeVisualContext
64 #if USE(AVFOUNDATION)
65 #include "MediaPlayerPrivateAVFoundationCF.h"
66 #endif
67 #elif PLATFORM(QT)
68 #if USE(QT_MULTIMEDIA) && !USE(GSTREAMER)
69 #include "MediaPlayerPrivateQt.h"
70 #define PlatformMediaEngineClassName MediaPlayerPrivateQt
71 #endif
72 #elif PLATFORM(CHROMIUM)
73 #include "MediaPlayerPrivateChromium.h"
74 #define PlatformMediaEngineClassName MediaPlayerPrivate
75 #elif PLATFORM(BLACKBERRY)
76 #include "MediaPlayerPrivateBlackBerry.h"
77 #define PlatformMediaEngineClassName MediaPlayerPrivate
78 #endif
79
80 namespace WebCore {
81
82 const PlatformMedia NoPlatformMedia = { PlatformMedia::None, {0} };
83
84 // a null player to make MediaPlayer logic simpler
85
86 class NullMediaPlayerPrivate : public MediaPlayerPrivateInterface {
87 public:
88     NullMediaPlayerPrivate(MediaPlayer*) { }
89
90     virtual void load(const String&) { }
91     virtual void cancelLoad() { }
92
93     virtual void prepareToPlay() { }
94     virtual void play() { }
95     virtual void pause() { }    
96
97     virtual PlatformMedia platformMedia() const { return NoPlatformMedia; }
98 #if USE(ACCELERATED_COMPOSITING)
99     virtual PlatformLayer* platformLayer() const { return 0; }
100 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING)
101     virtual bool supportsAcceleratedRendering() const { return true; }
102 #endif
103 #endif
104
105     virtual IntSize naturalSize() const { return IntSize(0, 0); }
106
107     virtual bool hasVideo() const { return false; }
108     virtual bool hasAudio() const { return false; }
109
110     virtual void setVisible(bool) { }
111
112     virtual float duration() const { return 0; }
113
114     virtual float currentTime() const { return 0; }
115     virtual void seek(float) { }
116     virtual bool seeking() const { return false; }
117
118     virtual void setRate(float) { }
119     virtual void setPreservesPitch(bool) { }
120     virtual bool paused() const { return false; }
121
122     virtual void setVolume(float) { }
123
124     virtual bool supportsMuting() const { return false; }
125     virtual void setMuted(bool) { }
126
127     virtual bool hasClosedCaptions() const { return false; }
128     virtual void setClosedCaptionsVisible(bool) { };
129
130     virtual MediaPlayer::NetworkState networkState() const { return MediaPlayer::Empty; }
131     virtual MediaPlayer::ReadyState readyState() const { return MediaPlayer::HaveNothing; }
132
133     virtual float maxTimeSeekable() const { return 0; }
134     virtual PassRefPtr<TimeRanges> buffered() const { return TimeRanges::create(); }
135
136     virtual unsigned totalBytes() const { return 0; }
137     virtual bool didLoadingProgress() const { return false; }
138
139     virtual void setSize(const IntSize&) { }
140
141     virtual void paint(GraphicsContext*, const IntRect&) { }
142
143     virtual bool canLoadPoster() const { return false; }
144     virtual void setPoster(const String&) { }
145
146 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
147     virtual void deliverNotification(MediaPlayerProxyNotificationType) { }
148     virtual void setMediaPlayerProxy(WebMediaPlayerProxy*) { }
149     virtual void setControls(bool) { }
150 #endif
151
152     virtual bool hasSingleSecurityOrigin() const { return true; }
153
154 #if ENABLE(MEDIA_SOURCE)
155     virtual MediaPlayer::AddIdStatus sourceAddId(const String& id, const String& type, const Vector<String>& codecs) { return MediaPlayer::NotSupported; }
156     virtual PassRefPtr<TimeRanges> sourceBuffered(const String&) { return TimeRanges::create(); }
157     virtual bool sourceRemoveId(const String&) { return false; }
158     virtual bool sourceAppend(const String&, const unsigned char*, unsigned) { return false; }
159     virtual bool sourceAbort(const String&) { return false; }
160     virtual void sourceEndOfStream(MediaPlayer::EndOfStreamStatus) { }
161 #endif
162
163 #if ENABLE(ENCRYPTED_MEDIA)
164     virtual MediaPlayer::MediaKeyException generateKeyRequest(const String&, const unsigned char*, unsigned) OVERRIDE { return MediaPlayer::InvalidPlayerState; }
165     virtual MediaPlayer::MediaKeyException addKey(const String&, const unsigned char*, unsigned, const unsigned char*, unsigned, const String&) OVERRIDE { return MediaPlayer::InvalidPlayerState; }
166     virtual MediaPlayer::MediaKeyException cancelKeyRequest(const String&, const String&) OVERRIDE { return MediaPlayer::InvalidPlayerState; }
167 #endif
168 };
169
170 static PassOwnPtr<MediaPlayerPrivateInterface> createNullMediaPlayer(MediaPlayer* player) 
171
172     return adoptPtr(new NullMediaPlayerPrivate(player)); 
173 }
174
175
176 // engine support
177
178 struct MediaPlayerFactory {
179     WTF_MAKE_NONCOPYABLE(MediaPlayerFactory); WTF_MAKE_FAST_ALLOCATED;
180 public:
181     MediaPlayerFactory(CreateMediaEnginePlayer constructor, MediaEngineSupportedTypes getSupportedTypes, MediaEngineSupportsType supportsTypeAndCodecs,
182         MediaEngineGetSitesInMediaCache getSitesInMediaCache, MediaEngineClearMediaCache clearMediaCache, MediaEngineClearMediaCacheForSite clearMediaCacheForSite)
183         : constructor(constructor)
184         , getSupportedTypes(getSupportedTypes)
185         , supportsTypeAndCodecs(supportsTypeAndCodecs)
186         , getSitesInMediaCache(getSitesInMediaCache)
187         , clearMediaCache(clearMediaCache)
188         , clearMediaCacheForSite(clearMediaCacheForSite)
189     {
190     }
191
192     CreateMediaEnginePlayer constructor;
193     MediaEngineSupportedTypes getSupportedTypes;
194     MediaEngineSupportsType supportsTypeAndCodecs;
195     MediaEngineGetSitesInMediaCache getSitesInMediaCache;
196     MediaEngineClearMediaCache clearMediaCache;
197     MediaEngineClearMediaCacheForSite clearMediaCacheForSite;
198 };
199
200 static void addMediaEngine(CreateMediaEnginePlayer, MediaEngineSupportedTypes, MediaEngineSupportsType, MediaEngineGetSitesInMediaCache, MediaEngineClearMediaCache, MediaEngineClearMediaCacheForSite);
201
202 static MediaPlayerFactory* bestMediaEngineForTypeAndCodecs(const String& type, const String& codecs, const String& keySystem, const KURL&, MediaPlayerFactory* current = 0);
203 static MediaPlayerFactory* nextMediaEngine(MediaPlayerFactory* current);
204
205 static Vector<MediaPlayerFactory*>& installedMediaEngines() 
206 {
207     DEFINE_STATIC_LOCAL(Vector<MediaPlayerFactory*>, installedEngines, ());
208     static bool enginesQueried = false;
209
210     if (!enginesQueried) {
211         enginesQueried = true;
212
213 #if USE(AVFOUNDATION)
214         if (1 /* @@Settings::isAVFoundationEnabled() @@ */) {
215 #if PLATFORM(MAC)
216             MediaPlayerPrivateAVFoundationObjC::registerMediaEngine(addMediaEngine);
217 #elif PLATFORM(WIN)
218             MediaPlayerPrivateAVFoundationCF::registerMediaEngine(addMediaEngine);
219 #endif
220         }
221 #endif
222
223 #if defined(PlatformMediaEngineClassName)
224         PlatformMediaEngineClassName::registerMediaEngine(addMediaEngine);
225 #endif
226     }
227
228     return installedEngines;
229 }
230
231 static void addMediaEngine(CreateMediaEnginePlayer constructor, MediaEngineSupportedTypes getSupportedTypes, MediaEngineSupportsType supportsType,
232     MediaEngineGetSitesInMediaCache getSitesInMediaCache, MediaEngineClearMediaCache clearMediaCache, MediaEngineClearMediaCacheForSite clearMediaCacheForSite)
233 {
234     ASSERT(constructor);
235     ASSERT(getSupportedTypes);
236     ASSERT(supportsType);
237
238     installedMediaEngines().append(new MediaPlayerFactory(constructor, getSupportedTypes, supportsType, getSitesInMediaCache, clearMediaCache, clearMediaCacheForSite));
239 }
240
241 static const AtomicString& applicationOctetStream()
242 {
243     DEFINE_STATIC_LOCAL(const AtomicString, applicationOctetStream, ("application/octet-stream"));
244     return applicationOctetStream;
245 }
246
247 static const AtomicString& textPlain()
248 {
249     DEFINE_STATIC_LOCAL(const AtomicString, textPlain, ("text/plain"));
250     return textPlain;
251 }
252
253 static const AtomicString& codecs()
254 {
255     DEFINE_STATIC_LOCAL(const AtomicString, codecs, ("codecs"));
256     return codecs;
257 }
258
259 static MediaPlayerFactory* bestMediaEngineForTypeAndCodecs(const String& type, const String& codecs, const String& keySystem, const KURL& url, MediaPlayerFactory* current)
260 {
261     if (type.isEmpty())
262         return 0;
263
264     Vector<MediaPlayerFactory*>& engines = installedMediaEngines();
265     if (engines.isEmpty())
266         return 0;
267
268     // 4.8.10.3 MIME types - In the absence of a specification to the contrary, the MIME type "application/octet-stream" 
269     // when used with parameters, e.g. "application/octet-stream;codecs=theora", is a type that the user agent knows 
270     // it cannot render.
271     if (type == applicationOctetStream()) {
272         if (!codecs.isEmpty())
273             return 0;
274     }
275
276     MediaPlayerFactory* engine = 0;
277     MediaPlayer::SupportsType supported = MediaPlayer::IsNotSupported;
278     unsigned count = engines.size();
279     for (unsigned ndx = 0; ndx < count; ndx++) {
280         if (current) {
281             if (current == engines[ndx])
282                 current = 0;
283             continue;
284         }
285 #if ENABLE(ENCRYPTED_MEDIA)
286         MediaPlayer::SupportsType engineSupport = engines[ndx]->supportsTypeAndCodecs(type, codecs, keySystem, url);
287 #else
288         UNUSED_PARAM(keySystem);
289         ASSERT(keySystem.isEmpty());
290         MediaPlayer::SupportsType engineSupport = engines[ndx]->supportsTypeAndCodecs(type, codecs, url);
291 #endif
292         if (engineSupport > supported) {
293             supported = engineSupport;
294             engine = engines[ndx];
295         }
296     }
297
298     return engine;
299 }
300
301 static MediaPlayerFactory* nextMediaEngine(MediaPlayerFactory* current)
302 {
303     Vector<MediaPlayerFactory*>& engines = installedMediaEngines();
304     if (engines.isEmpty())
305         return 0;
306
307     if (!current) 
308         return engines.first();
309
310     size_t currentIndex = engines.find(current);
311     if (currentIndex == WTF::notFound || currentIndex + 1 >= engines.size()) 
312         return 0;
313
314     return engines[currentIndex + 1];
315 }
316
317 // media player
318
319 MediaPlayer::MediaPlayer(MediaPlayerClient* client)
320     : m_mediaPlayerClient(client)
321     , m_reloadTimer(this, &MediaPlayer::reloadTimerFired)
322     , m_private(createNullMediaPlayer(this))
323     , m_currentMediaEngine(0)
324     , m_frameView(0)
325     , m_preload(Auto)
326     , m_visible(false)
327     , m_rate(1.0f)
328     , m_volume(1.0f)
329     , m_muted(false)
330     , m_preservesPitch(true)
331     , m_privateBrowsing(false)
332     , m_shouldPrepareToRender(false)
333     , m_contentMIMETypeWasInferredFromExtension(false)
334 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
335     , m_playerProxy(0)
336 #endif
337 {
338 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
339     Vector<MediaPlayerFactory*>& engines = installedMediaEngines();
340     if (!engines.isEmpty()) {
341         m_currentMediaEngine = engines[0];
342         m_private = engines[0]->constructor(this);
343         if (m_mediaPlayerClient)
344             m_mediaPlayerClient->mediaPlayerEngineUpdated(this);
345     }
346 #endif
347 }
348
349 MediaPlayer::~MediaPlayer()
350 {
351     m_mediaPlayerClient = 0;
352 }
353
354 bool MediaPlayer::load(const KURL& url, const ContentType& contentType, const String& keySystem)
355 {
356     m_contentMIMEType = contentType.type().lower();
357     m_contentTypeCodecs = contentType.parameter(codecs());
358     m_url = url;
359     m_keySystem = keySystem.lower();
360     m_contentMIMETypeWasInferredFromExtension = false;
361
362     // If the MIME type is missing or is not meaningful, try to figure it out from the URL.
363     if (m_contentMIMEType.isEmpty() || m_contentMIMEType == applicationOctetStream() || m_contentMIMEType == textPlain()) {
364         if (protocolIs(m_url.string(), "data"))
365             m_contentMIMEType = mimeTypeFromDataURL(m_url.string());
366         else {
367             String lastPathComponent = url.lastPathComponent();
368             size_t pos = lastPathComponent.reverseFind('.');
369             if (pos != notFound) {
370                 String extension = lastPathComponent.substring(pos + 1);
371                 String mediaType = MIMETypeRegistry::getMediaMIMETypeForExtension(extension);
372                 if (!mediaType.isEmpty()) {
373                     m_contentMIMEType = mediaType;
374                     m_contentMIMETypeWasInferredFromExtension = true;
375                 }
376             }
377         }
378     }
379
380     loadWithNextMediaEngine(0);
381     return m_currentMediaEngine;
382 }
383
384 void MediaPlayer::loadWithNextMediaEngine(MediaPlayerFactory* current)
385 {
386     MediaPlayerFactory* engine = 0;
387
388     if (!m_contentMIMEType.isEmpty())
389         engine = bestMediaEngineForTypeAndCodecs(m_contentMIMEType, m_contentTypeCodecs, m_keySystem, m_url, current);
390
391     // If no MIME type is specified or the type was inferred from the file extension, just use the next engine.
392     if (!engine && (m_contentMIMEType.isEmpty() || m_contentMIMETypeWasInferredFromExtension))
393         engine = nextMediaEngine(current);
394
395     // Don't delete and recreate the player unless it comes from a different engine.
396     if (!engine) {
397         LOG(Media, "MediaPlayer::loadWithNextMediaEngine - no media engine found for type \"%s\"", m_contentMIMEType.utf8().data());
398         m_currentMediaEngine = engine;
399         m_private = nullptr;
400     } else if (m_currentMediaEngine != engine) {
401         m_currentMediaEngine = engine;
402         m_private = engine->constructor(this);
403         if (m_mediaPlayerClient)
404             m_mediaPlayerClient->mediaPlayerEngineUpdated(this);
405 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
406         m_private->setMediaPlayerProxy(m_playerProxy);
407 #endif
408         m_private->setPrivateBrowsingMode(m_privateBrowsing);
409         m_private->setPreload(m_preload);
410         m_private->setPreservesPitch(preservesPitch());
411         if (m_shouldPrepareToRender)
412             m_private->prepareForRendering();
413     }
414
415     if (m_private)
416         m_private->load(m_url.string());
417     else {
418         m_private = createNullMediaPlayer(this);
419         if (m_mediaPlayerClient) {
420             m_mediaPlayerClient->mediaPlayerEngineUpdated(this);
421             m_mediaPlayerClient->mediaPlayerResourceNotSupported(this);
422         }
423     }
424 }
425
426 bool MediaPlayer::hasAvailableVideoFrame() const
427 {
428     return m_private->hasAvailableVideoFrame();
429 }
430
431 void MediaPlayer::prepareForRendering()
432 {
433     m_shouldPrepareToRender = true;
434     m_private->prepareForRendering();
435 }
436
437 bool MediaPlayer::canLoadPoster() const
438 {
439     return m_private->canLoadPoster();
440 }
441
442 void MediaPlayer::setPoster(const String& url)
443 {
444     m_private->setPoster(url);
445 }    
446
447 void MediaPlayer::cancelLoad()
448 {
449     m_private->cancelLoad();
450 }    
451
452 void MediaPlayer::prepareToPlay()
453 {
454     m_private->prepareToPlay();
455 }
456
457 void MediaPlayer::play()
458 {
459     m_private->play();
460 }
461
462 void MediaPlayer::pause()
463 {
464     m_private->pause();
465 }
466
467 #if ENABLE(MEDIA_SOURCE)
468
469 MediaPlayer::AddIdStatus MediaPlayer::sourceAddId(const String& id, const String& type, const Vector<String>& codecs)
470 {
471     return m_private->sourceAddId(id, type, codecs);
472 }
473
474 PassRefPtr<TimeRanges> MediaPlayer::sourceBuffered(const String& id)
475 {
476     return m_private->sourceBuffered(id);
477 }
478
479 bool MediaPlayer::sourceRemoveId(const String& id)
480 {
481     return m_private->sourceRemoveId(id);
482 }
483
484 bool MediaPlayer::sourceAppend(const String& id, const unsigned char* data, unsigned length)
485 {
486     return m_private->sourceAppend(id, data, length);
487 }
488
489 bool MediaPlayer::sourceAbort(const String& id)
490 {
491     return m_private->sourceAbort(id);
492 }
493
494 void MediaPlayer::sourceEndOfStream(MediaPlayer::EndOfStreamStatus status)
495 {
496     return m_private->sourceEndOfStream(status);
497 }
498 #endif
499
500 #if ENABLE(ENCRYPTED_MEDIA)
501 MediaPlayer::MediaKeyException MediaPlayer::generateKeyRequest(const String& keySystem, const unsigned char* initData, unsigned initDataLength)
502 {
503     return m_private->generateKeyRequest(keySystem.lower(), initData, initDataLength);
504 }
505
506 MediaPlayer::MediaKeyException MediaPlayer::addKey(const String& keySystem, const unsigned char* key, unsigned keyLength, const unsigned char* initData, unsigned initDataLength, const String& sessionId)
507 {
508     return m_private->addKey(keySystem.lower(), key, keyLength, initData, initDataLength, sessionId);
509 }
510
511 MediaPlayer::MediaKeyException MediaPlayer::cancelKeyRequest(const String& keySystem, const String& sessionId)
512 {
513     return m_private->cancelKeyRequest(keySystem.lower(), sessionId);
514 }
515 #endif
516
517 float MediaPlayer::duration() const
518 {
519     return m_private->duration();
520 }
521
522 float MediaPlayer::startTime() const
523 {
524     return m_private->startTime();
525 }
526
527 double MediaPlayer::initialTime() const
528 {
529     return m_private->initialTime();
530 }
531
532 float MediaPlayer::currentTime() const
533 {
534     return m_private->currentTime();
535 }
536
537 void MediaPlayer::seek(float time)
538 {
539     m_private->seek(time);
540 }
541
542 bool MediaPlayer::paused() const
543 {
544     return m_private->paused();
545 }
546
547 bool MediaPlayer::seeking() const
548 {
549     return m_private->seeking();
550 }
551
552 bool MediaPlayer::supportsFullscreen() const
553 {
554     return m_private->supportsFullscreen();
555 }
556
557 bool MediaPlayer::supportsSave() const
558 {
559     return m_private->supportsSave();
560 }
561
562 bool MediaPlayer::supportsScanning() const
563 {
564     return m_private->supportsScanning();
565 }
566
567 IntSize MediaPlayer::naturalSize()
568 {
569     return m_private->naturalSize();
570 }
571
572 bool MediaPlayer::hasVideo() const
573 {
574     return m_private->hasVideo();
575 }
576
577 bool MediaPlayer::hasAudio() const
578 {
579     return m_private->hasAudio();
580 }
581
582 bool MediaPlayer::inMediaDocument()
583 {
584     Frame* frame = m_frameView ? m_frameView->frame() : 0;
585     Document* document = frame ? frame->document() : 0;
586
587     return document && document->isMediaDocument();
588 }
589
590 PlatformMedia MediaPlayer::platformMedia() const
591 {
592     return m_private->platformMedia();
593 }
594
595 #if USE(ACCELERATED_COMPOSITING)
596 PlatformLayer* MediaPlayer::platformLayer() const
597 {
598     return m_private->platformLayer();
599 }
600 #endif
601
602 MediaPlayer::NetworkState MediaPlayer::networkState()
603 {
604     return m_private->networkState();
605 }
606
607 MediaPlayer::ReadyState MediaPlayer::readyState()
608 {
609     return m_private->readyState();
610 }
611
612 float MediaPlayer::volume() const
613 {
614     return m_volume;
615 }
616
617 void MediaPlayer::setVolume(float volume)
618 {
619     m_volume = volume;
620
621     if (m_private->supportsMuting() || !m_muted)
622         m_private->setVolume(volume);
623 }
624
625 bool MediaPlayer::muted() const
626 {
627     return m_muted;
628 }
629
630 void MediaPlayer::setMuted(bool muted)
631 {
632     m_muted = muted;
633
634     if (m_private->supportsMuting())
635         m_private->setMuted(muted);
636     else
637         m_private->setVolume(muted ? 0 : m_volume);
638 }
639
640 bool MediaPlayer::hasClosedCaptions() const
641 {
642     return m_private->hasClosedCaptions();
643 }
644
645 void MediaPlayer::setClosedCaptionsVisible(bool closedCaptionsVisible)
646 {
647     m_private->setClosedCaptionsVisible(closedCaptionsVisible);
648 }
649
650 float MediaPlayer::rate() const
651 {
652     return m_rate;
653 }
654
655 void MediaPlayer::setRate(float rate)
656 {
657     m_rate = rate;
658     m_private->setRate(rate);   
659 }
660
661 bool MediaPlayer::preservesPitch() const
662 {
663     return m_preservesPitch;
664 }
665
666 void MediaPlayer::setPreservesPitch(bool preservesPitch)
667 {
668     m_preservesPitch = preservesPitch;
669     m_private->setPreservesPitch(preservesPitch);
670 }
671
672 PassRefPtr<TimeRanges> MediaPlayer::buffered()
673 {
674     return m_private->buffered();
675 }
676
677 PassRefPtr<TimeRanges> MediaPlayer::seekable()
678 {
679     return m_private->seekable();
680 }
681
682 float MediaPlayer::maxTimeSeekable()
683 {
684     return m_private->maxTimeSeekable();
685 }
686
687 bool MediaPlayer::didLoadingProgress()
688 {
689     return m_private->didLoadingProgress();
690 }
691
692 void MediaPlayer::setSize(const IntSize& size)
693
694     m_size = size;
695     m_private->setSize(size);
696 }
697
698 bool MediaPlayer::visible() const
699 {
700     return m_visible;
701 }
702
703 void MediaPlayer::setVisible(bool b)
704 {
705     m_visible = b;
706     m_private->setVisible(b);
707 }
708
709 MediaPlayer::Preload MediaPlayer::preload() const
710 {
711     return m_preload;
712 }
713
714 void MediaPlayer::setPreload(MediaPlayer::Preload preload)
715 {
716     m_preload = preload;
717     m_private->setPreload(preload);
718 }
719
720 void MediaPlayer::paint(GraphicsContext* p, const IntRect& r)
721 {
722     m_private->paint(p, r);
723 }
724
725 void MediaPlayer::paintCurrentFrameInContext(GraphicsContext* p, const IntRect& r)
726 {
727     m_private->paintCurrentFrameInContext(p, r);
728 }
729
730 MediaPlayer::SupportsType MediaPlayer::supportsType(const ContentType& contentType, const String& keySystem, const KURL& url, const MediaPlayerSupportsTypeClient* client)
731 {
732     String type = contentType.type().lower();
733     // The codecs string is not lower-cased because MP4 values are case sensitive
734     // per http://tools.ietf.org/html/rfc4281#page-7.
735     String typeCodecs = contentType.parameter(codecs());
736     String system = keySystem.lower();
737
738     // 4.8.10.3 MIME types - The canPlayType(type) method must return the empty string if type is a type that the 
739     // user agent knows it cannot render or is the type "application/octet-stream"
740     if (type == applicationOctetStream())
741         return IsNotSupported;
742
743     MediaPlayerFactory* engine = bestMediaEngineForTypeAndCodecs(type, typeCodecs, system, url);
744     if (!engine)
745         return IsNotSupported;
746
747 #if PLATFORM(MAC)
748     // YouTube will ask if the HTMLMediaElement canPlayType video/webm, then
749     // video/x-flv, then finally video/mp4, and will then load a URL of the first type
750     // in that list which returns "probably". When Perian is installed,
751     // MediaPlayerPrivateQTKit claims to support both video/webm and video/x-flv, but
752     // due to a bug in Perian, loading media in these formats will sometimes fail on
753     // slow connections. <https://bugs.webkit.org/show_bug.cgi?id=86409>
754     if (client && client->mediaPlayerNeedsSiteSpecificHacks()) {
755         String host = client->mediaPlayerDocumentHost();
756         if ((host.endsWith(".youtube.com", false) || equalIgnoringCase("youtube.com", host))
757             && (contentType.type().startsWith("video/webm", false) || contentType.type().startsWith("video/x-flv", false)))
758             return IsNotSupported;
759     }
760 #endif
761
762 #if ENABLE(ENCRYPTED_MEDIA)
763     return engine->supportsTypeAndCodecs(type, typeCodecs, system, url);
764 #else
765     ASSERT(system.isEmpty());
766     return engine->supportsTypeAndCodecs(type, typeCodecs, url);
767 #endif
768 }
769
770 void MediaPlayer::getSupportedTypes(HashSet<String>& types)
771 {
772     Vector<MediaPlayerFactory*>& engines = installedMediaEngines();
773     if (engines.isEmpty())
774         return;
775
776     unsigned count = engines.size();
777     for (unsigned ndx = 0; ndx < count; ndx++)
778         engines[ndx]->getSupportedTypes(types);
779
780
781 bool MediaPlayer::isAvailable()
782 {
783     return !installedMediaEngines().isEmpty();
784
785
786 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
787 void MediaPlayer::deliverNotification(MediaPlayerProxyNotificationType notification)
788 {
789     m_private->deliverNotification(notification);
790 }
791
792 void MediaPlayer::setMediaPlayerProxy(WebMediaPlayerProxy* proxy)
793 {
794     m_playerProxy = proxy;
795     m_private->setMediaPlayerProxy(proxy);
796 }
797
798 void MediaPlayer::setControls(bool controls)
799 {
800     m_private->setControls(controls);
801 }
802 #endif
803
804 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO) || USE(NATIVE_FULLSCREEN_VIDEO)
805 void MediaPlayer::enterFullscreen()
806 {
807     m_private->enterFullscreen();
808 }
809
810 void MediaPlayer::exitFullscreen()
811 {
812     m_private->exitFullscreen();
813 }
814 #endif
815
816 #if USE(NATIVE_FULLSCREEN_VIDEO)
817 bool MediaPlayer::canEnterFullscreen() const
818 {
819     return m_private->canEnterFullscreen();
820 }
821 #endif
822
823 #if USE(ACCELERATED_COMPOSITING)
824 void MediaPlayer::acceleratedRenderingStateChanged()
825 {
826     m_private->acceleratedRenderingStateChanged();
827 }
828
829 bool MediaPlayer::supportsAcceleratedRendering() const
830 {
831     return m_private->supportsAcceleratedRendering();
832 }
833 #endif // USE(ACCELERATED_COMPOSITING)
834
835 bool MediaPlayer::hasSingleSecurityOrigin() const
836 {
837     return m_private->hasSingleSecurityOrigin();
838 }
839
840 bool MediaPlayer::didPassCORSAccessCheck() const
841 {
842     return m_private->didPassCORSAccessCheck();
843 }
844
845 MediaPlayer::MovieLoadType MediaPlayer::movieLoadType() const
846 {
847     return m_private->movieLoadType();
848 }
849
850 float MediaPlayer::mediaTimeForTimeValue(float timeValue) const
851 {
852     return m_private->mediaTimeForTimeValue(timeValue);
853 }
854
855 double MediaPlayer::maximumDurationToCacheMediaTime() const
856 {
857     return m_private->maximumDurationToCacheMediaTime();
858 }
859
860 unsigned MediaPlayer::decodedFrameCount() const
861 {
862     return m_private->decodedFrameCount();
863 }
864
865 unsigned MediaPlayer::droppedFrameCount() const
866 {
867     return m_private->droppedFrameCount();
868 }
869
870 unsigned MediaPlayer::audioDecodedByteCount() const
871 {
872     return m_private->audioDecodedByteCount();
873 }
874
875 unsigned MediaPlayer::videoDecodedByteCount() const
876 {
877     return m_private->videoDecodedByteCount();
878 }
879
880 void MediaPlayer::reloadTimerFired(Timer<MediaPlayer>*)
881 {
882     m_private->cancelLoad();
883     loadWithNextMediaEngine(m_currentMediaEngine);
884 }
885
886 void MediaPlayer::getSitesInMediaCache(Vector<String>& sites)
887 {
888     Vector<MediaPlayerFactory*>& engines = installedMediaEngines();
889     unsigned size = engines.size();
890     for (unsigned i = 0; i < size; i++) {
891         if (!engines[i]->getSitesInMediaCache)
892             continue;
893         Vector<String> engineSites;
894         engines[i]->getSitesInMediaCache(engineSites);
895         sites.append(engineSites);
896     }
897 }
898
899 void MediaPlayer::clearMediaCache()
900 {
901     Vector<MediaPlayerFactory*>& engines = installedMediaEngines();
902     unsigned size = engines.size();
903     for (unsigned i = 0; i < size; i++) {
904         if (engines[i]->clearMediaCache)
905             engines[i]->clearMediaCache();
906     }
907 }
908
909 void MediaPlayer::clearMediaCacheForSite(const String& site)
910 {
911     Vector<MediaPlayerFactory*>& engines = installedMediaEngines();
912     unsigned size = engines.size();
913     for (unsigned i = 0; i < size; i++) {
914         if (engines[i]->clearMediaCacheForSite)
915             engines[i]->clearMediaCacheForSite(site);
916     }
917 }
918
919 void MediaPlayer::setPrivateBrowsingMode(bool privateBrowsingMode)
920 {
921     m_privateBrowsing = privateBrowsingMode;
922     m_private->setPrivateBrowsingMode(m_privateBrowsing);
923 }
924
925 #if ENABLE(MEDIA_SOURCE)
926 void MediaPlayer::sourceOpened()
927 {
928     if (m_mediaPlayerClient)
929         m_mediaPlayerClient->mediaPlayerSourceOpened();
930 }
931
932 String MediaPlayer::sourceURL() const
933 {
934     if (m_mediaPlayerClient)
935         return m_mediaPlayerClient->mediaPlayerSourceURL();
936     return String();
937 }
938 #endif
939
940 // Client callbacks.
941 void MediaPlayer::networkStateChanged()
942 {
943     // If more than one media engine is installed and this one failed before finding metadata,
944     // let the next engine try.
945     if (m_private->networkState() >= FormatError
946         && m_private->readyState() < HaveMetadata
947         && installedMediaEngines().size() > 1) {
948         if (m_contentMIMEType.isEmpty() || bestMediaEngineForTypeAndCodecs(m_contentMIMEType, m_contentTypeCodecs, m_keySystem, m_url, m_currentMediaEngine)) {
949             m_reloadTimer.startOneShot(0);
950             return;
951         }
952     }
953     if (m_mediaPlayerClient)
954         m_mediaPlayerClient->mediaPlayerNetworkStateChanged(this);
955 }
956
957 void MediaPlayer::readyStateChanged()
958 {
959     if (m_mediaPlayerClient)
960         m_mediaPlayerClient->mediaPlayerReadyStateChanged(this);
961 }
962
963 void MediaPlayer::volumeChanged(float newVolume)
964 {
965     m_volume = newVolume;
966     if (m_mediaPlayerClient)
967         m_mediaPlayerClient->mediaPlayerVolumeChanged(this);
968 }
969
970 void MediaPlayer::muteChanged(bool newMuted)
971 {
972     m_muted = newMuted;
973     if (m_mediaPlayerClient)
974         m_mediaPlayerClient->mediaPlayerMuteChanged(this);
975 }
976
977 void MediaPlayer::timeChanged()
978 {
979     if (m_mediaPlayerClient)
980         m_mediaPlayerClient->mediaPlayerTimeChanged(this);
981 }
982
983 void MediaPlayer::sizeChanged()
984 {
985     if (m_mediaPlayerClient)
986         m_mediaPlayerClient->mediaPlayerSizeChanged(this);
987 }
988
989 void MediaPlayer::repaint()
990 {
991     if (m_mediaPlayerClient)
992         m_mediaPlayerClient->mediaPlayerRepaint(this);
993 }
994
995 void MediaPlayer::durationChanged()
996 {
997     if (m_mediaPlayerClient)
998         m_mediaPlayerClient->mediaPlayerDurationChanged(this);
999 }
1000
1001 void MediaPlayer::rateChanged()
1002 {
1003     if (m_mediaPlayerClient)
1004         m_mediaPlayerClient->mediaPlayerRateChanged(this);
1005 }
1006
1007 void MediaPlayer::playbackStateChanged()
1008 {
1009     if (m_mediaPlayerClient)
1010         m_mediaPlayerClient->mediaPlayerPlaybackStateChanged(this);
1011 }
1012
1013 void MediaPlayer::firstVideoFrameAvailable()
1014 {
1015     if (m_mediaPlayerClient)
1016         m_mediaPlayerClient->mediaPlayerFirstVideoFrameAvailable(this);
1017 }
1018
1019 void MediaPlayer::characteristicChanged()
1020 {
1021     if (m_mediaPlayerClient)
1022         m_mediaPlayerClient->mediaPlayerCharacteristicChanged(this);
1023 }
1024
1025 #if ENABLE(WEB_AUDIO)
1026 AudioSourceProvider* MediaPlayer::audioSourceProvider()
1027 {
1028     return m_private->audioSourceProvider();
1029 }
1030 #endif // WEB_AUDIO
1031
1032 #if ENABLE(ENCRYPTED_MEDIA)
1033 void MediaPlayer::keyAdded(const String& keySystem, const String& sessionId)
1034 {
1035     if (m_mediaPlayerClient)
1036         m_mediaPlayerClient->mediaPlayerKeyAdded(this, keySystem, sessionId);
1037 }
1038
1039 void MediaPlayer::keyError(const String& keySystem, const String& sessionId, MediaPlayerClient::MediaKeyErrorCode errorCode, unsigned short systemCode)
1040 {
1041     if (m_mediaPlayerClient)
1042         m_mediaPlayerClient->mediaPlayerKeyError(this, keySystem, sessionId, errorCode, systemCode);
1043 }
1044
1045 void MediaPlayer::keyMessage(const String& keySystem, const String& sessionId, const unsigned char* message, unsigned messageLength)
1046 {
1047     if (m_mediaPlayerClient)
1048         m_mediaPlayerClient->mediaPlayerKeyMessage(this, keySystem, sessionId, message, messageLength);
1049 }
1050
1051 void MediaPlayer::keyNeeded(const String& keySystem, const String& sessionId, const unsigned char* initData, unsigned initDataLength)
1052 {
1053     if (m_mediaPlayerClient)
1054         m_mediaPlayerClient->mediaPlayerKeyNeeded(this, keySystem, sessionId, initData, initDataLength);
1055 }
1056 #endif
1057
1058 String MediaPlayer::referrer() const
1059 {
1060     if (!m_mediaPlayerClient)
1061         return String();
1062
1063     return m_mediaPlayerClient->mediaPlayerReferrer();
1064 }
1065
1066 String MediaPlayer::userAgent() const
1067 {
1068     if (!m_mediaPlayerClient)
1069         return String();
1070     
1071     return m_mediaPlayerClient->mediaPlayerUserAgent();
1072 }
1073
1074 String MediaPlayer::engineDescription() const
1075 {
1076     if (!m_private)
1077         return String();
1078
1079     return m_private->engineDescription();
1080 }
1081
1082 #if PLATFORM(WIN) && USE(AVFOUNDATION)
1083 GraphicsDeviceAdapter* MediaPlayer::graphicsDeviceAdapter() const
1084 {
1085     if (!m_mediaPlayerClient)
1086         return 0;
1087     
1088     return m_mediaPlayerClient->mediaPlayerGraphicsDeviceAdapter(this);
1089 }
1090 #endif
1091
1092 }
1093
1094 #endif