Home | History | Annotate | Download | only in web
      1 /*
      2  * Copyright (C) 2009 Google 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 are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #ifndef WebMediaPlayerClientImpl_h
     32 #define WebMediaPlayerClientImpl_h
     33 
     34 #include "WebAudioSourceProviderClient.h"
     35 #include "WebMediaPlayerClient.h"
     36 #include "core/platform/audio/AudioSourceProvider.h"
     37 #include "core/platform/graphics/InbandTextTrackPrivate.h"
     38 #include "core/platform/graphics/MediaPlayer.h"
     39 #if OS(ANDROID)
     40 #include "third_party/skia/include/core/SkBitmap.h"
     41 #include "third_party/skia/include/core/SkRefCnt.h"
     42 #include "third_party/skia/include/gpu/GrTexture.h"
     43 #endif
     44 #include "weborigin/KURL.h"
     45 #include "wtf/OwnPtr.h"
     46 #include "wtf/PassOwnPtr.h"
     47 #include "wtf/Threading.h"
     48 
     49 namespace WebCore {
     50 class AudioSourceProviderClient;
     51 class HTMLMediaSource;
     52 }
     53 
     54 namespace WebKit {
     55 
     56 class WebHelperPluginImpl;
     57 class WebAudioSourceProvider;
     58 class WebMediaPlayer;
     59 
     60 // This class serves as a bridge between WebCore::MediaPlayer and
     61 // WebKit::WebMediaPlayer.
     62 class WebMediaPlayerClientImpl : public WebCore::MediaPlayer, public WebMediaPlayerClient {
     63 
     64 public:
     65     static PassOwnPtr<WebCore::MediaPlayer> create(WebCore::MediaPlayerClient*);
     66 
     67     // Returns the encapsulated WebKit::WebMediaPlayer.
     68     WebMediaPlayer* mediaPlayer() const;
     69 
     70     // WebMediaPlayerClient methods:
     71     virtual ~WebMediaPlayerClientImpl();
     72     virtual void networkStateChanged();
     73     virtual void readyStateChanged();
     74     virtual void timeChanged();
     75     virtual void repaint();
     76     virtual void durationChanged();
     77     virtual void sizeChanged();
     78     virtual void setOpaque(bool);
     79     virtual double volume() const;
     80     virtual void playbackStateChanged();
     81     virtual WebMediaPlayer::Preload preload() const;
     82     virtual void keyAdded(const WebString& keySystem, const WebString& sessionId);
     83     virtual void keyError(const WebString& keySystem, const WebString& sessionId, MediaKeyErrorCode, unsigned short systemCode);
     84     virtual void keyMessage(const WebString& keySystem, const WebString& sessionId, const unsigned char* message, unsigned messageLength, const WebURL& defaultURL);
     85     virtual void keyNeeded(const WebString& keySystem, const WebString& sessionId, const unsigned char* initData, unsigned initDataLength);
     86     virtual WebPlugin* createHelperPlugin(const WebString& pluginType, WebFrame*);
     87     virtual void closeHelperPlugin();
     88     virtual void closeHelperPluginSoon(WebFrame*);
     89     virtual bool needsWebLayerForVideo() const;
     90     virtual void setWebLayer(WebLayer*);
     91     virtual void addTextTrack(WebInbandTextTrack*);
     92     virtual void removeTextTrack(WebInbandTextTrack*);
     93 
     94     // MediaPlayer methods:
     95     virtual void load(const WTF::String& url) OVERRIDE;
     96     virtual void load(const WTF::String& url, PassRefPtr<WebCore::HTMLMediaSource>) OVERRIDE;
     97 
     98     virtual WebKit::WebLayer* platformLayer() const OVERRIDE;
     99     virtual void play() OVERRIDE;
    100     virtual void pause() OVERRIDE;
    101     virtual void prepareToPlay() OVERRIDE;
    102     virtual bool supportsFullscreen() const OVERRIDE;
    103     virtual bool supportsSave() const OVERRIDE;
    104     virtual WebCore::IntSize naturalSize() const OVERRIDE;
    105     virtual bool hasVideo() const OVERRIDE;
    106     virtual bool hasAudio() const OVERRIDE;
    107     virtual double duration() const OVERRIDE;
    108     virtual double currentTime() const OVERRIDE;
    109     virtual void seek(double time) OVERRIDE;
    110     virtual bool seeking() const OVERRIDE;
    111     virtual double rate() const OVERRIDE;
    112     virtual void setRate(double) OVERRIDE;
    113     virtual bool paused() const OVERRIDE;
    114     virtual void setVolume(double) OVERRIDE;
    115     virtual void setMuted(bool) OVERRIDE;
    116     virtual WebCore::MediaPlayer::NetworkState networkState() const OVERRIDE;
    117     virtual WebCore::MediaPlayer::ReadyState readyState() const OVERRIDE;
    118     virtual double maxTimeSeekable() const OVERRIDE;
    119     virtual WTF::PassRefPtr<WebCore::TimeRanges> buffered() const OVERRIDE;
    120     virtual bool didLoadingProgress() const OVERRIDE;
    121     virtual void paint(WebCore::GraphicsContext*, const WebCore::IntRect&) OVERRIDE;
    122     virtual void paintCurrentFrameInContext(WebCore::GraphicsContext*, const WebCore::IntRect&) OVERRIDE;
    123     virtual bool copyVideoTextureToPlatformTexture(WebCore::GraphicsContext3D*, Platform3DObject texture, GC3Dint level, GC3Denum type, GC3Denum internalFormat, bool premultiplyAlpha, bool flipY) OVERRIDE;
    124     virtual void setPreload(WebCore::MediaPlayer::Preload) OVERRIDE;
    125     virtual bool hasSingleSecurityOrigin() const OVERRIDE;
    126     virtual bool didPassCORSAccessCheck() const OVERRIDE;
    127     virtual double mediaTimeForTimeValue(double timeValue) const OVERRIDE;
    128     virtual unsigned decodedFrameCount() const OVERRIDE;
    129     virtual unsigned droppedFrameCount() const OVERRIDE;
    130     virtual unsigned audioDecodedByteCount() const OVERRIDE;
    131     virtual unsigned videoDecodedByteCount() const OVERRIDE;
    132 #if USE(NATIVE_FULLSCREEN_VIDEO)
    133     virtual void enterFullscreen() OVERRIDE;
    134     virtual void exitFullscreen() OVERRIDE;
    135     virtual bool canEnterFullscreen() const OVERRIDE;
    136 #endif
    137 
    138 #if ENABLE(WEB_AUDIO)
    139     virtual WebCore::AudioSourceProvider* audioSourceProvider() OVERRIDE;
    140 #endif
    141 
    142     virtual bool supportsAcceleratedRendering() const OVERRIDE;
    143 
    144     virtual WebCore::MediaPlayer::MediaKeyException generateKeyRequest(const String& keySystem, const unsigned char* initData, unsigned initDataLength) OVERRIDE;
    145     virtual WebCore::MediaPlayer::MediaKeyException addKey(const String& keySystem, const unsigned char* key, unsigned keyLength, const unsigned char* initData, unsigned initDataLength, const String& sessionId) OVERRIDE;
    146     virtual WebCore::MediaPlayer::MediaKeyException cancelKeyRequest(const String& keySystem, const String& sessionId) OVERRIDE;
    147 
    148 private:
    149     explicit WebMediaPlayerClientImpl(WebCore::MediaPlayerClient*);
    150 
    151     void startDelayedLoad();
    152     void loadRequested();
    153     void loadInternal();
    154 
    155     bool acceleratedRenderingInUse();
    156 
    157 #if OS(ANDROID)
    158     // FIXME: This path "only works" on Android. It is a workaround for the problem that Skia could not handle Android's GL_TEXTURE_EXTERNAL_OES
    159     // texture internally. It should be removed and replaced by the normal paint path.
    160     // https://code.google.com/p/skia/issues/detail?id=1189
    161     void paintOnAndroid(WebCore::GraphicsContext* context, WebCore::GraphicsContext3D* context3D, const WebCore::IntRect& rect, uint8_t alpha);
    162     SkAutoTUnref<GrTexture> m_texture;
    163     SkBitmap m_bitmap;
    164 #endif
    165 
    166     WebCore::MediaPlayerClient* m_client;
    167     OwnPtr<WebMediaPlayer> m_webMediaPlayer;
    168     WebCore::KURL m_url;
    169     bool m_isMediaStream;
    170     bool m_delayingLoad;
    171     WebCore::MediaPlayer::Preload m_preload;
    172     RefPtr<WebHelperPluginImpl> m_helperPlugin;
    173     WebLayer* m_videoLayer;
    174     bool m_opaque;
    175     bool m_needsWebLayerForVideo;
    176     double m_volume;
    177     bool m_muted;
    178     double m_rate;
    179 
    180 #if ENABLE(WEB_AUDIO)
    181     // AudioClientImpl wraps an AudioSourceProviderClient.
    182     // When the audio format is known, Chromium calls setFormat() which then dispatches into WebCore.
    183 
    184     class AudioClientImpl : public WebKit::WebAudioSourceProviderClient {
    185     public:
    186         AudioClientImpl(WebCore::AudioSourceProviderClient* client)
    187             : m_client(client)
    188         {
    189         }
    190 
    191         virtual ~AudioClientImpl() { }
    192 
    193         // WebAudioSourceProviderClient
    194         virtual void setFormat(size_t numberOfChannels, float sampleRate);
    195 
    196     private:
    197         WebCore::AudioSourceProviderClient* m_client;
    198     };
    199 
    200     // AudioSourceProviderImpl wraps a WebAudioSourceProvider.
    201     // provideInput() calls into Chromium to get a rendered audio stream.
    202 
    203     class AudioSourceProviderImpl : public WebCore::AudioSourceProvider {
    204     public:
    205         AudioSourceProviderImpl()
    206             : m_webAudioSourceProvider(0)
    207         {
    208         }
    209 
    210         virtual ~AudioSourceProviderImpl() { }
    211 
    212         // Wraps the given WebAudioSourceProvider.
    213         void wrap(WebAudioSourceProvider*);
    214 
    215         // WebCore::AudioSourceProvider
    216         virtual void setClient(WebCore::AudioSourceProviderClient*);
    217         virtual void provideInput(WebCore::AudioBus*, size_t framesToProcess);
    218 
    219     private:
    220         WebAudioSourceProvider* m_webAudioSourceProvider;
    221         OwnPtr<AudioClientImpl> m_client;
    222         Mutex provideInputLock;
    223     };
    224 
    225     AudioSourceProviderImpl m_audioSourceProvider;
    226 #endif
    227 
    228     RefPtr<WebCore::HTMLMediaSource> m_mediaSource;
    229 };
    230 
    231 } // namespace WebKit
    232 
    233 #endif
    234