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#include "third_party/khronos/GLES2/gl2.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 "platform/weborigin/KURL.h"
45#include "wtf/OwnPtr.h"
46#include "wtf/PassOwnPtr.h"
47#include "wtf/ThreadingPrimitives.h"
48
49namespace WebCore {
50class AudioSourceProviderClient;
51class HTMLMediaElement;
52}
53
54namespace blink {
55
56class WebAudioSourceProvider;
57class WebContentDecryptionModule;
58class WebMediaPlayer;
59class WebGraphicsContext3D;
60
61// This class serves as a bridge between WebCore::MediaPlayer and
62// blink::WebMediaPlayer.
63class WebMediaPlayerClientImpl FINAL : public WebCore::MediaPlayer, public WebMediaPlayerClient {
64
65public:
66    static PassOwnPtr<WebCore::MediaPlayer> create(WebCore::MediaPlayerClient*);
67
68    virtual ~WebMediaPlayerClientImpl();
69
70    // WebMediaPlayerClient methods:
71    virtual void networkStateChanged() OVERRIDE;
72    virtual void readyStateChanged() OVERRIDE;
73    virtual void timeChanged() OVERRIDE;
74    virtual void repaint() OVERRIDE;
75    virtual void durationChanged() OVERRIDE;
76    virtual void sizeChanged() OVERRIDE;
77    virtual double volume() const OVERRIDE;
78    virtual void playbackStateChanged() OVERRIDE;
79    virtual WebMediaPlayer::Preload preload() const OVERRIDE;
80
81    // WebEncryptedMediaPlayerClient methods:
82    virtual void keyAdded(const WebString& keySystem, const WebString& sessionId) OVERRIDE;
83    virtual void keyError(const WebString& keySystem, const WebString& sessionId, MediaKeyErrorCode, unsigned short systemCode) OVERRIDE;
84    virtual void keyMessage(const WebString& keySystem, const WebString& sessionId, const unsigned char* message, unsigned messageLength, const WebURL& defaultURL) OVERRIDE;
85    virtual void keyNeeded(const WebString& contentType, const unsigned char* initData, unsigned initDataLength) OVERRIDE;
86
87    virtual void setWebLayer(WebLayer*) OVERRIDE;
88    virtual WebMediaPlayer::TrackId addAudioTrack(const WebString& id, AudioTrackKind, const WebString& label, const WebString& language, bool enabled) OVERRIDE;
89    virtual void removeAudioTrack(WebMediaPlayer::TrackId) OVERRIDE;
90    virtual WebMediaPlayer::TrackId addVideoTrack(const WebString& id, VideoTrackKind, const WebString& label, const WebString& language, bool selected) OVERRIDE;
91    virtual void removeVideoTrack(WebMediaPlayer::TrackId) OVERRIDE;
92    virtual void addTextTrack(WebInbandTextTrack*) OVERRIDE;
93    virtual void removeTextTrack(WebInbandTextTrack*) OVERRIDE;
94    virtual void mediaSourceOpened(WebMediaSource*) OVERRIDE;
95    virtual void requestFullscreen() OVERRIDE;
96    virtual void requestSeek(double) OVERRIDE;
97
98    // MediaPlayer methods:
99    virtual WebMediaPlayer* webMediaPlayer() const OVERRIDE;
100    virtual void load(WebMediaPlayer::LoadType, const WTF::String& url, WebMediaPlayer::CORSMode) OVERRIDE;
101    virtual void play() OVERRIDE;
102    virtual void pause() OVERRIDE;
103    virtual bool supportsSave() const OVERRIDE;
104    virtual double duration() const OVERRIDE;
105    virtual double currentTime() const OVERRIDE;
106    virtual void seek(double time) OVERRIDE;
107    virtual bool seeking() const OVERRIDE;
108    virtual double rate() const OVERRIDE;
109    virtual void setRate(double) OVERRIDE;
110    virtual bool paused() const OVERRIDE;
111    virtual void setPoster(const WebCore::KURL&) OVERRIDE;
112    virtual WebCore::MediaPlayer::NetworkState networkState() const OVERRIDE;
113    virtual double maxTimeSeekable() const OVERRIDE;
114    virtual WTF::PassRefPtr<WebCore::TimeRanges> buffered() const OVERRIDE;
115    virtual bool didLoadingProgress() const OVERRIDE;
116    virtual void paint(WebCore::GraphicsContext*, const WebCore::IntRect&) OVERRIDE;
117    virtual bool copyVideoTextureToPlatformTexture(WebGraphicsContext3D*, Platform3DObject texture, GLint level, GLenum type, GLenum internalFormat, bool premultiplyAlpha, bool flipY) OVERRIDE;
118    virtual void setPreload(WebCore::MediaPlayer::Preload) OVERRIDE;
119    virtual bool hasSingleSecurityOrigin() const OVERRIDE;
120    virtual double mediaTimeForTimeValue(double timeValue) const OVERRIDE;
121
122#if ENABLE(WEB_AUDIO)
123    virtual WebCore::AudioSourceProvider* audioSourceProvider() OVERRIDE;
124#endif
125
126private:
127    explicit WebMediaPlayerClientImpl(WebCore::MediaPlayerClient*);
128
129    WebCore::HTMLMediaElement& mediaElement() const;
130
131    WebCore::MediaPlayerClient* m_client;
132    OwnPtr<WebMediaPlayer> m_webMediaPlayer;
133    WebCore::MediaPlayer::Preload m_preload;
134    double m_rate;
135
136#if OS(ANDROID)
137    // 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
138    // texture internally. It should be removed and replaced by the normal paint path.
139    // https://code.google.com/p/skia/issues/detail?id=1189
140    void paintOnAndroid(WebCore::GraphicsContext*, const WebCore::IntRect&, uint8_t alpha);
141    SkBitmap m_bitmap;
142    bool m_usePaintOnAndroid;
143#endif
144
145#if ENABLE(WEB_AUDIO)
146    // AudioClientImpl wraps an AudioSourceProviderClient.
147    // When the audio format is known, Chromium calls setFormat() which then dispatches into WebCore.
148
149    class AudioClientImpl FINAL : public blink::WebAudioSourceProviderClient {
150    public:
151        AudioClientImpl(WebCore::AudioSourceProviderClient* client)
152            : m_client(client)
153        {
154        }
155
156        virtual ~AudioClientImpl() { }
157
158        // WebAudioSourceProviderClient
159        virtual void setFormat(size_t numberOfChannels, float sampleRate) OVERRIDE;
160
161    private:
162        WebCore::AudioSourceProviderClient* m_client;
163    };
164
165    // AudioSourceProviderImpl wraps a WebAudioSourceProvider.
166    // provideInput() calls into Chromium to get a rendered audio stream.
167
168    class AudioSourceProviderImpl FINAL : public WebCore::AudioSourceProvider {
169    public:
170        AudioSourceProviderImpl()
171            : m_webAudioSourceProvider(0)
172        {
173        }
174
175        virtual ~AudioSourceProviderImpl() { }
176
177        // Wraps the given WebAudioSourceProvider.
178        void wrap(WebAudioSourceProvider*);
179
180        // WebCore::AudioSourceProvider
181        virtual void setClient(WebCore::AudioSourceProviderClient*) OVERRIDE;
182        virtual void provideInput(WebCore::AudioBus*, size_t framesToProcess) OVERRIDE;
183
184    private:
185        WebAudioSourceProvider* m_webAudioSourceProvider;
186        OwnPtr<AudioClientImpl> m_client;
187        Mutex provideInputLock;
188    };
189
190    AudioSourceProviderImpl m_audioSourceProvider;
191#endif
192};
193
194} // namespace blink
195
196#endif
197