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 "platform/audio/AudioSourceProvider.h"
     35 #include "platform/graphics/media/MediaPlayer.h"
     36 #include "public/platform/WebAudioSourceProviderClient.h"
     37 #include "public/platform/WebMediaPlayerClient.h"
     38 #if OS(ANDROID)
     39 #include "third_party/skia/include/core/SkBitmap.h"
     40 #include "third_party/skia/include/core/SkRefCnt.h"
     41 #include "third_party/skia/include/gpu/GrTexture.h"
     42 #endif
     43 #include "platform/weborigin/KURL.h"
     44 #include "wtf/OwnPtr.h"
     45 #include "wtf/PassOwnPtr.h"
     46 #include "wtf/ThreadingPrimitives.h"
     47 
     48 namespace WebCore {
     49 class AudioSourceProviderClient;
     50 class HTMLMediaSource;
     51 }
     52 
     53 namespace blink {
     54 
     55 class WebHelperPluginImpl;
     56 class WebAudioSourceProvider;
     57 class WebMediaPlayer;
     58 
     59 // This class serves as a bridge between WebCore::MediaPlayer and
     60 // blink::WebMediaPlayer.
     61 class WebMediaPlayerClientImpl : public WebCore::MediaPlayer, public WebMediaPlayerClient {
     62 
     63 public:
     64     static PassOwnPtr<WebCore::MediaPlayer> create(WebCore::MediaPlayerClient*);
     65 
     66     // Returns the encapsulated blink::WebMediaPlayer.
     67     WebMediaPlayer* mediaPlayer() const;
     68 
     69     // WebMediaPlayerClient methods:
     70     virtual ~WebMediaPlayerClientImpl();
     71     virtual void networkStateChanged();
     72     virtual void readyStateChanged();
     73     virtual void timeChanged();
     74     virtual void repaint();
     75     virtual void durationChanged();
     76     virtual void sizeChanged();
     77     virtual void setOpaque(bool);
     78     virtual double volume() const;
     79     virtual void playbackStateChanged();
     80     virtual WebMediaPlayer::Preload preload() const;
     81     virtual void keyAdded(const WebString& keySystem, const WebString& sessionId);
     82     virtual void keyError(const WebString& keySystem, const WebString& sessionId, MediaKeyErrorCode, unsigned short systemCode);
     83     virtual void keyMessage(const WebString& keySystem, const WebString& sessionId, const unsigned char* message, unsigned messageLength, const WebURL& defaultURL);
     84     virtual void keyNeeded(const WebString& keySystem, const WebString& sessionId, const unsigned char* initData, unsigned initDataLength);
     85     virtual WebPlugin* createHelperPlugin(const WebString& pluginType, WebFrame*);
     86     virtual void closeHelperPluginSoon(WebFrame*);
     87     virtual bool needsWebLayerForVideo() const;
     88     virtual void setWebLayer(WebLayer*);
     89     virtual void addTextTrack(WebInbandTextTrack*);
     90     virtual void removeTextTrack(WebInbandTextTrack*);
     91     virtual void mediaSourceOpened(WebMediaSource*);
     92     virtual void requestFullscreen();
     93     virtual void requestSeek(double);
     94 
     95     // MediaPlayer methods:
     96     virtual void load(const WTF::String& url) OVERRIDE;
     97     virtual void load(const WTF::String& url, PassRefPtr<WebCore::HTMLMediaSource>) OVERRIDE;
     98 
     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 bool copyVideoTextureToPlatformTexture(WebCore::GraphicsContext3D*, Platform3DObject texture, GC3Dint level, GC3Denum type, GC3Denum internalFormat, bool premultiplyAlpha, bool flipY) OVERRIDE;
    123     virtual void setPreload(WebCore::MediaPlayer::Preload) OVERRIDE;
    124     virtual bool hasSingleSecurityOrigin() const OVERRIDE;
    125     virtual bool didPassCORSAccessCheck() const OVERRIDE;
    126     virtual double mediaTimeForTimeValue(double timeValue) const OVERRIDE;
    127     virtual unsigned decodedFrameCount() const OVERRIDE;
    128     virtual unsigned droppedFrameCount() const OVERRIDE;
    129     virtual unsigned corruptedFrameCount() const OVERRIDE;
    130     virtual unsigned audioDecodedByteCount() const OVERRIDE;
    131     virtual unsigned videoDecodedByteCount() const OVERRIDE;
    132     virtual void showFullscreenOverlay() OVERRIDE;
    133     virtual void hideFullscreenOverlay() OVERRIDE;
    134     virtual bool canShowFullscreenOverlay() const OVERRIDE;
    135 
    136 #if ENABLE(WEB_AUDIO)
    137     virtual WebCore::AudioSourceProvider* audioSourceProvider() OVERRIDE;
    138 #endif
    139 
    140     virtual WebCore::MediaPlayer::MediaKeyException generateKeyRequest(const String& keySystem, const unsigned char* initData, unsigned initDataLength) OVERRIDE;
    141     virtual WebCore::MediaPlayer::MediaKeyException addKey(const String& keySystem, const unsigned char* key, unsigned keyLength, const unsigned char* initData, unsigned initDataLength, const String& sessionId) OVERRIDE;
    142     virtual WebCore::MediaPlayer::MediaKeyException cancelKeyRequest(const String& keySystem, const String& sessionId) OVERRIDE;
    143 
    144 private:
    145     explicit WebMediaPlayerClientImpl(WebCore::MediaPlayerClient*);
    146 
    147     void startDelayedLoad();
    148     void loadRequested();
    149     void loadInternal();
    150 
    151 #if OS(ANDROID)
    152     // 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
    153     // texture internally. It should be removed and replaced by the normal paint path.
    154     // https://code.google.com/p/skia/issues/detail?id=1189
    155     void paintOnAndroid(WebCore::GraphicsContext* context, WebCore::GraphicsContext3D* context3D, const WebCore::IntRect& rect, uint8_t alpha);
    156     SkBitmap m_bitmap;
    157 #endif
    158 
    159     WebCore::MediaPlayerClient* m_client;
    160     OwnPtr<WebMediaPlayer> m_webMediaPlayer;
    161     WebCore::KURL m_url;
    162     bool m_isMediaStream;
    163     bool m_delayingLoad;
    164     WebCore::MediaPlayer::Preload m_preload;
    165     RefPtr<WebHelperPluginImpl> m_helperPlugin;
    166     bool m_needsWebLayerForVideo;
    167     double m_volume;
    168     bool m_muted;
    169     double m_rate;
    170 
    171 #if ENABLE(WEB_AUDIO)
    172     // AudioClientImpl wraps an AudioSourceProviderClient.
    173     // When the audio format is known, Chromium calls setFormat() which then dispatches into WebCore.
    174 
    175     class AudioClientImpl : public blink::WebAudioSourceProviderClient {
    176     public:
    177         AudioClientImpl(WebCore::AudioSourceProviderClient* client)
    178             : m_client(client)
    179         {
    180         }
    181 
    182         virtual ~AudioClientImpl() { }
    183 
    184         // WebAudioSourceProviderClient
    185         virtual void setFormat(size_t numberOfChannels, float sampleRate);
    186 
    187     private:
    188         WebCore::AudioSourceProviderClient* m_client;
    189     };
    190 
    191     // AudioSourceProviderImpl wraps a WebAudioSourceProvider.
    192     // provideInput() calls into Chromium to get a rendered audio stream.
    193 
    194     class AudioSourceProviderImpl : public WebCore::AudioSourceProvider {
    195     public:
    196         AudioSourceProviderImpl()
    197             : m_webAudioSourceProvider(0)
    198         {
    199         }
    200 
    201         virtual ~AudioSourceProviderImpl() { }
    202 
    203         // Wraps the given WebAudioSourceProvider.
    204         void wrap(WebAudioSourceProvider*);
    205 
    206         // WebCore::AudioSourceProvider
    207         virtual void setClient(WebCore::AudioSourceProviderClient*);
    208         virtual void provideInput(WebCore::AudioBus*, size_t framesToProcess);
    209 
    210     private:
    211         WebAudioSourceProvider* m_webAudioSourceProvider;
    212         OwnPtr<AudioClientImpl> m_client;
    213         Mutex provideInputLock;
    214     };
    215 
    216     AudioSourceProviderImpl m_audioSourceProvider;
    217 #endif
    218 
    219     RefPtr<WebCore::HTMLMediaSource> m_mediaSource;
    220 };
    221 
    222 } // namespace blink
    223 
    224 #endif
    225