OSDN Git Service

Merge WebKit at r71558: Initial merge by git.
[android-x86/external-webkit.git] / WebCore / platform / graphics / MediaPlayer.cpp
1 /*
2  * Copyright (C) 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
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 "MIMETypeRegistry.h"
37 #include "MediaPlayerPrivate.h"
38 #include "TimeRanges.h"
39
40 #if PLATFORM(QT)
41 #include <QtGlobal>
42 #endif
43
44 #if USE(GSTREAMER)
45 #include "MediaPlayerPrivateGStreamer.h"
46 #endif
47
48 #if PLATFORM(MAC)
49 #include "MediaPlayerPrivateQTKit.h"
50 #elif OS(WINCE) && !PLATFORM(QT)
51 #include "MediaPlayerPrivateWinCE.h"
52 #elif PLATFORM(WIN)
53 #include "MediaPlayerPrivateQuickTimeVisualContext.h"
54 #include "MediaPlayerPrivateQuicktimeWin.h"
55 #elif PLATFORM(QT)
56 #if USE(QT_MULTIMEDIA)
57 #include "MediaPlayerPrivateQt.h"
58 #else
59 #include "MediaPlayerPrivatePhonon.h"
60 #endif
61 #elif PLATFORM(CHROMIUM)
62 #include "MediaPlayerPrivateChromium.h"
63 #elif PLATFORM(ANDROID)
64 #include "MediaPlayerPrivateAndroid.h"
65 #endif
66
67 namespace WebCore {
68
69 const PlatformMedia NoPlatformMedia = { PlatformMedia::None, {0} };
70
71 // a null player to make MediaPlayer logic simpler
72
73 class NullMediaPlayerPrivate : public MediaPlayerPrivateInterface {
74 public:
75     NullMediaPlayerPrivate(MediaPlayer*) { }
76
77     virtual void load(const String&) { }
78     virtual void cancelLoad() { }
79     
80     virtual void prepareToPlay() { }
81     virtual void play() { }
82     virtual void pause() { }    
83
84     virtual PlatformMedia platformMedia() const { return NoPlatformMedia; }
85 #if USE(ACCELERATED_COMPOSITING)
86     virtual PlatformLayer* platformLayer() const { return 0; }
87 #endif
88
89     virtual IntSize naturalSize() const { return IntSize(0, 0); }
90
91     virtual bool hasVideo() const { return false; }
92     virtual bool hasAudio() const { return false; }
93
94     virtual void setVisible(bool) { }
95
96     virtual float duration() const { return 0; }
97
98     virtual float currentTime() const { return 0; }
99     virtual void seek(float) { }
100     virtual bool seeking() const { return false; }
101
102     virtual void setRate(float) { }
103     virtual void setPreservesPitch(bool) { }
104     virtual bool paused() const { return false; }
105
106     virtual void setVolume(float) { }
107
108     virtual bool supportsMuting() const { return false; }
109     virtual void setMuted(bool) { }
110
111     virtual bool hasClosedCaptions() const { return false; }
112     virtual void setClosedCaptionsVisible(bool) { };
113
114     virtual MediaPlayer::NetworkState networkState() const { return MediaPlayer::Empty; }
115     virtual MediaPlayer::ReadyState readyState() const { return MediaPlayer::HaveNothing; }
116
117     virtual float maxTimeSeekable() const { return 0; }
118     virtual PassRefPtr<TimeRanges> buffered() const { return TimeRanges::create(); }
119
120     virtual unsigned totalBytes() const { return 0; }
121     virtual unsigned bytesLoaded() const { return 0; }
122
123     virtual void setSize(const IntSize&) { }
124
125     virtual void paint(GraphicsContext*, const IntRect&) { }
126
127     virtual bool canLoadPoster() const { return false; }
128     virtual void setPoster(const String&) { }
129
130 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
131     virtual void deliverNotification(MediaPlayerProxyNotificationType) { }
132     virtual void setMediaPlayerProxy(WebMediaPlayerProxy*) { }
133     virtual void setControls(bool) { }
134 #endif
135
136     virtual bool hasSingleSecurityOrigin() const { return true; }
137 };
138
139 static MediaPlayerPrivateInterface* createNullMediaPlayer(MediaPlayer* player) 
140
141     return new NullMediaPlayerPrivate(player); 
142 }
143
144
145 // engine support
146
147 struct MediaPlayerFactory : Noncopyable {
148     MediaPlayerFactory(CreateMediaEnginePlayer constructor, MediaEngineSupportedTypes getSupportedTypes, MediaEngineSupportsType supportsTypeAndCodecs) 
149         : constructor(constructor)
150         , getSupportedTypes(getSupportedTypes)
151         , supportsTypeAndCodecs(supportsTypeAndCodecs)  
152     { 
153     }
154
155     CreateMediaEnginePlayer constructor;
156     MediaEngineSupportedTypes getSupportedTypes;
157     MediaEngineSupportsType supportsTypeAndCodecs;
158 };
159
160 static void addMediaEngine(CreateMediaEnginePlayer, MediaEngineSupportedTypes, MediaEngineSupportsType);
161 static MediaPlayerFactory* chooseBestEngineForTypeAndCodecs(const String& type, const String& codecs);
162
163 static Vector<MediaPlayerFactory*>& installedMediaEngines() 
164 {
165     DEFINE_STATIC_LOCAL(Vector<MediaPlayerFactory*>, installedEngines, ());
166     static bool enginesQueried = false;
167
168     if (!enginesQueried) {
169         enginesQueried = true;
170 #if USE(GSTREAMER)
171         MediaPlayerPrivateGStreamer::registerMediaEngine(addMediaEngine);
172 #endif
173
174 #if PLATFORM(WIN)
175         MediaPlayerPrivateQuickTimeVisualContext::registerMediaEngine(addMediaEngine);
176 #elif PLATFORM(QT)
177 #if USE(QT_MULTIMEDIA)
178         MediaPlayerPrivateQt::registerMediaEngine(addMediaEngine);
179 #else
180         MediaPlayerPrivatePhonon::registerMediaEngine(addMediaEngine);
181 #endif
182 #elif !PLATFORM(GTK) && !PLATFORM(EFL)
183         // FIXME: currently all the MediaEngines are named
184         // MediaPlayerPrivate. This code will need an update when bug
185         // 36663 is adressed.
186         MediaPlayerPrivate::registerMediaEngine(addMediaEngine);
187 #endif
188
189         // register additional engines here
190     }
191     
192     return installedEngines;
193 }
194
195 static void addMediaEngine(CreateMediaEnginePlayer constructor, MediaEngineSupportedTypes getSupportedTypes, MediaEngineSupportsType supportsType)
196 {
197     ASSERT(constructor);
198     ASSERT(getSupportedTypes);
199     ASSERT(supportsType);
200     installedMediaEngines().append(new MediaPlayerFactory(constructor, getSupportedTypes, supportsType));
201 }
202
203 static const AtomicString& applicationOctetStream()
204 {
205     DEFINE_STATIC_LOCAL(const AtomicString, applicationOctetStream, ("application/octet-stream"));
206     return applicationOctetStream;
207 }
208
209 static const AtomicString& textPlain()
210 {
211     DEFINE_STATIC_LOCAL(const AtomicString, textPlain, ("text/plain"));
212     return textPlain;
213 }
214
215 static const AtomicString& codecs()
216 {
217     DEFINE_STATIC_LOCAL(const AtomicString, codecs, ("codecs"));
218     return codecs;
219 }
220
221 static MediaPlayerFactory* chooseBestEngineForTypeAndCodecs(const String& type, const String& codecs)
222 {
223     Vector<MediaPlayerFactory*>& engines = installedMediaEngines();
224
225     if (engines.isEmpty())
226         return 0;
227
228     // 4.8.10.3 MIME types - In the absence of a specification to the contrary, the MIME type "application/octet-stream" 
229     // when used with parameters, e.g. "application/octet-stream;codecs=theora", is a type that the user agent knows 
230     // it cannot render.
231     if (type == applicationOctetStream()) {
232         if (!codecs.isEmpty())
233             return 0;
234     }
235     
236     MediaPlayerFactory* engine = 0;
237     MediaPlayer::SupportsType supported = MediaPlayer::IsNotSupported;
238
239     unsigned count = engines.size();
240     for (unsigned ndx = 0; ndx < count; ndx++) {
241         MediaPlayer::SupportsType engineSupport = engines[ndx]->supportsTypeAndCodecs(type, codecs);
242         if (engineSupport > supported) {
243             supported = engineSupport;
244             engine = engines[ndx];
245         }
246     }
247
248     return engine;
249 }
250
251 // media player
252
253 MediaPlayer::MediaPlayer(MediaPlayerClient* client)
254     : m_mediaPlayerClient(client)
255     , m_private(createNullMediaPlayer(this))
256     , m_currentMediaEngine(0)
257     , m_frameView(0)
258     , m_preload(Auto)
259     , m_visible(false)
260     , m_rate(1.0f)
261     , m_volume(1.0f)
262     , m_muted(false)
263     , m_preservesPitch(true)
264 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
265     , m_playerProxy(0)
266 #endif
267 #if PLATFORM(ANDROID)
268     , m_mediaElementType(Video)
269 #endif
270 {
271 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
272     Vector<MediaPlayerFactory*>& engines = installedMediaEngines();
273     if (!engines.isEmpty()) {
274         m_currentMediaEngine = engines[0];
275         m_private.clear();
276         m_private.set(engines[0]->constructor(this));
277     }
278 #endif
279 }
280
281 MediaPlayer::~MediaPlayer()
282 {
283 }
284
285 void MediaPlayer::load(const String& url, const ContentType& contentType)
286 {
287     String type = contentType.type().lower();
288     String typeCodecs = contentType.parameter(codecs());
289
290     // If the MIME type is unhelpful, see if the type registry has a match for the file extension.
291     if (type.isEmpty() || type == applicationOctetStream() || type == textPlain()) {
292         size_t pos = url.reverseFind('.');
293         if (pos != notFound) {
294             String extension = url.substring(pos + 1);
295             String mediaType = MIMETypeRegistry::getMediaMIMETypeForExtension(extension);
296             if (!mediaType.isEmpty())
297                 type = mediaType;
298         }
299     }
300
301     MediaPlayerFactory* engine = 0;
302     if (!type.isEmpty())
303         engine = chooseBestEngineForTypeAndCodecs(type, typeCodecs);
304
305     // If we didn't find an engine and no MIME type is specified, just use the first engine.
306     if (!engine && type.isEmpty() && !installedMediaEngines().isEmpty())
307         engine = installedMediaEngines()[0];
308     
309     // Don't delete and recreate the player unless it comes from a different engine
310     if (!engine) {
311         m_currentMediaEngine = engine;
312         m_private.clear();
313     } else if (m_currentMediaEngine != engine) {
314         m_currentMediaEngine = engine;
315         m_private.clear();
316         m_private.set(engine->constructor(this));
317 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
318         m_private->setMediaPlayerProxy(m_playerProxy);
319 #endif
320         m_private->setPreload(m_preload);
321         m_private->setPreservesPitch(preservesPitch());
322     }
323
324     if (m_private)
325         m_private->load(url);
326     else
327         m_private.set(createNullMediaPlayer(this));
328 }    
329
330 bool MediaPlayer::hasAvailableVideoFrame() const
331 {
332     return m_private->hasAvailableVideoFrame();
333 }
334     
335 void MediaPlayer::prepareForRendering()
336 {
337     return m_private->prepareForRendering();
338 }
339     
340 bool MediaPlayer::canLoadPoster() const
341 {
342     return m_private->canLoadPoster();
343 }
344     
345 void MediaPlayer::setPoster(const String& url)
346 {
347     m_private->setPoster(url);
348 }    
349
350 void MediaPlayer::cancelLoad()
351 {
352     m_private->cancelLoad();
353 }    
354
355 void MediaPlayer::prepareToPlay()
356 {
357     m_private->prepareToPlay();
358 }
359     
360 void MediaPlayer::play()
361 {
362     m_private->play();
363 }
364
365 void MediaPlayer::pause()
366 {
367     m_private->pause();
368 }
369
370 float MediaPlayer::duration() const
371 {
372     return m_private->duration();
373 }
374
375 float MediaPlayer::startTime() const
376 {
377     return m_private->startTime();
378 }
379
380 float MediaPlayer::currentTime() const
381 {
382     return m_private->currentTime();
383 }
384
385 void MediaPlayer::seek(float time)
386 {
387     m_private->seek(time);
388 }
389
390 bool MediaPlayer::paused() const
391 {
392     return m_private->paused();
393 }
394
395 bool MediaPlayer::seeking() const
396 {
397     return m_private->seeking();
398 }
399
400 bool MediaPlayer::supportsFullscreen() const
401 {
402     return m_private->supportsFullscreen();
403 }
404
405 bool MediaPlayer::supportsSave() const
406 {
407     return m_private->supportsSave();
408 }
409
410 IntSize MediaPlayer::naturalSize()
411 {
412     return m_private->naturalSize();
413 }
414
415 bool MediaPlayer::hasVideo() const
416 {
417     return m_private->hasVideo();
418 }
419
420 bool MediaPlayer::hasAudio() const
421 {
422     return m_private->hasAudio();
423 }
424
425 bool MediaPlayer::inMediaDocument()
426 {
427     Frame* frame = m_frameView ? m_frameView->frame() : 0;
428     Document* document = frame ? frame->document() : 0;
429     
430     return document && document->isMediaDocument();
431 }
432
433 PlatformMedia MediaPlayer::platformMedia() const
434 {
435     return m_private->platformMedia();
436 }
437
438 #if USE(ACCELERATED_COMPOSITING)
439 PlatformLayer* MediaPlayer::platformLayer() const
440 {
441     return m_private->platformLayer();
442 }
443 #endif
444
445 MediaPlayer::NetworkState MediaPlayer::networkState()
446 {
447     return m_private->networkState();
448 }
449
450 MediaPlayer::ReadyState MediaPlayer::readyState()
451 {
452     return m_private->readyState();
453 }
454
455 float MediaPlayer::volume() const
456 {
457     return m_volume;
458 }
459
460 void MediaPlayer::setVolume(float volume)
461 {
462     m_volume = volume;
463
464     if (m_private->supportsMuting() || !m_muted)
465         m_private->setVolume(volume);
466 }
467
468 bool MediaPlayer::muted() const
469 {
470     return m_muted;
471 }
472
473 void MediaPlayer::setMuted(bool muted)
474 {
475     m_muted = muted;
476
477     if (m_private->supportsMuting())
478         m_private->setMuted(muted);
479     else
480         m_private->setVolume(muted ? 0 : m_volume);
481 }
482
483 bool MediaPlayer::hasClosedCaptions() const
484 {
485     return m_private->hasClosedCaptions();
486 }
487
488 void MediaPlayer::setClosedCaptionsVisible(bool closedCaptionsVisible)
489 {
490     m_private->setClosedCaptionsVisible(closedCaptionsVisible);
491 }
492
493 float MediaPlayer::rate() const
494 {
495     return m_rate;
496 }
497
498 void MediaPlayer::setRate(float rate)
499 {
500     m_rate = rate;
501     m_private->setRate(rate);   
502 }
503
504 bool MediaPlayer::preservesPitch() const
505 {
506     return m_preservesPitch;
507 }
508
509 void MediaPlayer::setPreservesPitch(bool preservesPitch)
510 {
511     m_preservesPitch = preservesPitch;
512     m_private->setPreservesPitch(preservesPitch);
513 }
514
515 PassRefPtr<TimeRanges> MediaPlayer::buffered()
516 {
517     return m_private->buffered();
518 }
519
520 float MediaPlayer::maxTimeSeekable()
521 {
522     return m_private->maxTimeSeekable();
523 }
524
525 unsigned MediaPlayer::bytesLoaded()
526 {
527     return m_private->bytesLoaded();
528 }
529
530 void MediaPlayer::setSize(const IntSize& size)
531
532     m_size = size;
533     m_private->setSize(size);
534 }
535
536 bool MediaPlayer::visible() const
537 {
538     return m_visible;
539 }
540
541 void MediaPlayer::setVisible(bool b)
542 {
543     m_visible = b;
544     m_private->setVisible(b);
545 }
546
547 MediaPlayer::Preload MediaPlayer::preload() const
548 {
549     return m_preload;
550 }
551
552 void MediaPlayer::setPreload(MediaPlayer::Preload preload)
553 {
554     m_preload = preload;
555     m_private->setPreload(preload);
556 }
557
558 void MediaPlayer::paint(GraphicsContext* p, const IntRect& r)
559 {
560     m_private->paint(p, r);
561 }
562
563 void MediaPlayer::paintCurrentFrameInContext(GraphicsContext* p, const IntRect& r)
564 {
565     m_private->paintCurrentFrameInContext(p, r);
566 }
567
568 MediaPlayer::SupportsType MediaPlayer::supportsType(ContentType contentType)
569 {
570     String type = contentType.type().lower();
571     String typeCodecs = contentType.parameter(codecs());
572
573     // 4.8.10.3 MIME types - The canPlayType(type) method must return the empty string if type is a type that the 
574     // user agent knows it cannot render or is the type "application/octet-stream"
575     if (type == applicationOctetStream())
576         return IsNotSupported;
577
578     MediaPlayerFactory* engine = chooseBestEngineForTypeAndCodecs(type, typeCodecs);
579     if (!engine)
580         return IsNotSupported;
581
582     return engine->supportsTypeAndCodecs(type, typeCodecs);
583 }
584
585 void MediaPlayer::getSupportedTypes(HashSet<String>& types)
586 {
587     Vector<MediaPlayerFactory*>& engines = installedMediaEngines();
588     if (engines.isEmpty())
589         return;
590
591     unsigned count = engines.size();
592     for (unsigned ndx = 0; ndx < count; ndx++)
593         engines[ndx]->getSupportedTypes(types);
594
595
596 bool MediaPlayer::isAvailable()
597 {
598     return !installedMediaEngines().isEmpty();
599
600
601 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
602 void MediaPlayer::deliverNotification(MediaPlayerProxyNotificationType notification)
603 {
604     m_private->deliverNotification(notification);
605 }
606
607 void MediaPlayer::setMediaPlayerProxy(WebMediaPlayerProxy* proxy)
608 {
609     m_playerProxy = proxy;
610     m_private->setMediaPlayerProxy(proxy);
611 }
612
613 void MediaPlayer::setControls(bool controls)
614 {
615     m_private->setControls(controls);
616 }    
617
618 void MediaPlayer::enterFullscreen()
619 {
620     m_private->enterFullscreen();
621 }    
622
623 void MediaPlayer::exitFullscreen()
624 {
625     m_private->exitFullscreen();
626 }    
627 #endif
628
629 #if USE(ACCELERATED_COMPOSITING)
630 void MediaPlayer::acceleratedRenderingStateChanged()
631 {
632     m_private->acceleratedRenderingStateChanged();
633 }
634
635 bool MediaPlayer::supportsAcceleratedRendering() const
636 {
637     return m_private->supportsAcceleratedRendering();
638 }
639 #endif // USE(ACCELERATED_COMPOSITING)
640
641 bool MediaPlayer::hasSingleSecurityOrigin() const
642 {
643     return m_private->hasSingleSecurityOrigin();
644 }
645
646 MediaPlayer::MovieLoadType MediaPlayer::movieLoadType() const
647 {
648     return m_private->movieLoadType();
649 }
650
651 float MediaPlayer::mediaTimeForTimeValue(float timeValue) const
652 {
653     return m_private->mediaTimeForTimeValue(timeValue);
654 }
655
656 // Client callbacks.
657 void MediaPlayer::networkStateChanged()
658 {
659     if (m_mediaPlayerClient)
660         m_mediaPlayerClient->mediaPlayerNetworkStateChanged(this);
661 }
662
663 void MediaPlayer::readyStateChanged()
664 {
665     if (m_mediaPlayerClient)
666         m_mediaPlayerClient->mediaPlayerReadyStateChanged(this);
667 }
668
669 void MediaPlayer::volumeChanged(float newVolume)
670 {
671     m_volume = newVolume;
672     if (m_mediaPlayerClient)
673         m_mediaPlayerClient->mediaPlayerVolumeChanged(this);
674 }
675
676 void MediaPlayer::muteChanged(bool newMuted)
677 {
678     m_muted = newMuted;
679     if (m_mediaPlayerClient)
680         m_mediaPlayerClient->mediaPlayerMuteChanged(this);
681 }
682
683 void MediaPlayer::timeChanged()
684 {
685     if (m_mediaPlayerClient)
686         m_mediaPlayerClient->mediaPlayerTimeChanged(this);
687 }
688
689 void MediaPlayer::sizeChanged()
690 {
691     if (m_mediaPlayerClient)
692         m_mediaPlayerClient->mediaPlayerSizeChanged(this);
693 }
694
695 void MediaPlayer::repaint()
696 {
697     if (m_mediaPlayerClient)
698         m_mediaPlayerClient->mediaPlayerRepaint(this);
699 }
700
701 void MediaPlayer::durationChanged()
702 {
703     if (m_mediaPlayerClient)
704         m_mediaPlayerClient->mediaPlayerDurationChanged(this);
705 }
706
707 void MediaPlayer::rateChanged()
708 {
709     if (m_mediaPlayerClient)
710         m_mediaPlayerClient->mediaPlayerRateChanged(this);
711 }
712
713 void MediaPlayer::playbackStateChanged()
714 {
715     if (m_mediaPlayerClient)
716         m_mediaPlayerClient->mediaPlayerPlaybackStateChanged(this);
717 }
718
719 }
720
721 #endif