summaryrefslogtreecommitdiffstats
path: root/WebKit/android/WebCoreSupport/MediaPlayerPrivateAndroid.cpp
blob: bfb530597963f6b0428d6c4b0d1bf529ffa3f171 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
 * Copyright 2009, The Android Open Source Project
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "MediaPlayerPrivateAndroid.h"

#if ENABLE(VIDEO)

#include "GraphicsContext.h"
#include "SkiaUtils.h"
#include "TimeRanges.h"
#include "WebCoreJni.h"
#include "WebViewCore.h"

#include <GraphicsJNI.h>
#include <JNIHelp.h>
#include <JNIUtility.h>
#include <SkBitmap.h>

using namespace android;

namespace WebCore {

static const char* g_ProxyJavaClass = "android/webkit/HTML5VideoViewProxy";

struct MediaPlayerPrivate::JavaGlue
{
    jobject   m_javaProxy;
    jmethodID m_getInstance;
    jmethodID m_play;
    jmethodID m_teardown;
    jmethodID m_loadPoster;
    jmethodID m_seek;
    jmethodID m_pause;
};

MediaPlayerPrivate::~MediaPlayerPrivate()
{
    if (m_glue->m_javaProxy) {
        JNIEnv* env = JSC::Bindings::getJNIEnv();
        if (env) {
            env->CallVoidMethod(m_glue->m_javaProxy, m_glue->m_teardown);
            env->DeleteGlobalRef(m_glue->m_javaProxy);
        }
    }

    delete m_glue;
}

void MediaPlayerPrivate::registerMediaEngine(MediaEngineRegistrar registrar)
{
    registrar(create, getSupportedTypes, supportsType);
}

void MediaPlayerPrivate::load(const String& url)
{
    // Just save the URl.
    m_url = url;
}

void MediaPlayerPrivate::cancelLoad()
{
}

void MediaPlayerPrivate::play()
{
    JNIEnv* env = JSC::Bindings::getJNIEnv();
    if (!env || !m_glue->m_javaProxy || !m_url.length())
        return;

    m_paused = false;
    jstring jUrl = env->NewString((unsigned short *)m_url.characters(), m_url.length());
    env->CallVoidMethod(m_glue->m_javaProxy, m_glue->m_play, jUrl);
    env->DeleteLocalRef(jUrl);
    checkException(env);
}

void MediaPlayerPrivate::pause()
{
    JNIEnv* env = JSC::Bindings::getJNIEnv();
    if (!env || !m_glue->m_javaProxy || !m_url.length())
        return;

    m_paused = true;
    env->CallVoidMethod(m_glue->m_javaProxy, m_glue->m_pause);
    checkException(env);
}

IntSize MediaPlayerPrivate::naturalSize() const
{
    return m_naturalSize;
}

bool MediaPlayerPrivate::hasAudio() const
{
    // TODO
    return false;
}

bool MediaPlayerPrivate::hasVideo() const
{
    return m_hasVideo;
}

void MediaPlayerPrivate::setVisible(bool visible)
{
    m_isVisible = visible;
    if (m_isVisible)
        createJavaPlayerIfNeeded();
}

float MediaPlayerPrivate::duration() const
{
    return m_duration;
}

float MediaPlayerPrivate::currentTime() const
{
    return m_currentTime;
}

void MediaPlayerPrivate::seek(float time)
{
    JNIEnv* env = JSC::Bindings::getJNIEnv();
    if (!env || !m_glue->m_javaProxy || !m_url.length())
        return;

    m_currentTime = time;
    env->CallVoidMethod(m_glue->m_javaProxy, m_glue->m_seek, static_cast<jint>(time * 1000.0f));
    checkException(env);
}

bool MediaPlayerPrivate::seeking() const
{
    return false;
}

void MediaPlayerPrivate::setEndTime(float)
{
}

void MediaPlayerPrivate::setRate(float)
{
}

bool MediaPlayerPrivate::paused() const
{
    return m_paused;
}

void MediaPlayerPrivate::setVolume(float)
{
}

MediaPlayer::NetworkState MediaPlayerPrivate::networkState() const
{
    return m_networkState;
}

MediaPlayer::ReadyState MediaPlayerPrivate::readyState() const
{
    return m_readyState;
}

float MediaPlayerPrivate::maxTimeSeekable() const
{
    return 0;
}

PassRefPtr<TimeRanges> MediaPlayerPrivate::buffered() const
{
    return TimeRanges::create();
}

int MediaPlayerPrivate::dataRate() const
{
    return 0;
}

unsigned MediaPlayerPrivate::totalBytes() const
{
    return 0;
}

unsigned MediaPlayerPrivate::bytesLoaded() const
{
    return 0;
}

void MediaPlayerPrivate::setSize(const IntSize&)
{
}

void MediaPlayerPrivate::setPoster(const String& url)
{
    m_posterUrl = url;
    JNIEnv* env = JSC::Bindings::getJNIEnv();
    if (!env || !m_glue->m_javaProxy || !m_posterUrl.length())
        return;
    // Send the poster
    jstring jUrl = env->NewString((unsigned short *)m_posterUrl.characters(), m_posterUrl.length());
    env->CallVoidMethod(m_glue->m_javaProxy, m_glue->m_loadPoster, jUrl);
    env->DeleteLocalRef(jUrl);
}

void MediaPlayerPrivate::prepareToPlay() {
    // We are about to start playing. Since our Java VideoView cannot
    // buffer any data, we just simply transition to the HaveEnoughData
    // state in here. This will allow the MediaPlayer to transition to
    // the "play" state, at which point our VideoView will start downloading
    // the content and start the playback.
    m_networkState = MediaPlayer::Loaded;
    m_player->networkStateChanged();
    m_readyState = MediaPlayer::HaveEnoughData;
    m_player->readyStateChanged();
}

void MediaPlayerPrivate::paint(GraphicsContext* ctxt, const IntRect& r)
{
    if (ctxt->paintingDisabled())
        return;

    if (!m_isVisible)
        return;

    if (!m_poster || (!m_poster->getPixels() && !m_poster->pixelRef()))
        return;

    SkCanvas*   canvas = ctxt->platformContext()->mCanvas;
    // We paint with the following rules in mind:
    // - only downscale the poster, never upscale
    // - maintain the natural aspect ratio of the poster
    // - the poster should be centered in the target rect
    float originalRatio = static_cast<float>(m_poster->width()) / static_cast<float>(m_poster->height());
    int posterWidth = r.width() > m_poster->width() ? m_poster->width() : r.width();
    int posterHeight = posterWidth / originalRatio;
    int posterX = ((r.width() - posterWidth) / 2) + r.x();
    int posterY = ((r.height() - posterHeight) / 2) + r.y();
    IntRect targetRect(posterX, posterY, posterWidth, posterHeight);
    canvas->drawBitmapRect(*m_poster, 0, targetRect, 0);
}

MediaPlayerPrivateInterface* MediaPlayerPrivate::create(MediaPlayer* player)
{
    return new MediaPlayerPrivate(player);
}

void MediaPlayerPrivate::getSupportedTypes(HashSet<String>&)
{
}

MediaPlayer::SupportsType MediaPlayerPrivate::supportsType(const String& type, const String& codecs)
{
    return MediaPlayer::IsNotSupported;
}

MediaPlayerPrivate::MediaPlayerPrivate(MediaPlayer* player)
    : m_player(player),
    m_glue(0),
    m_duration(6000),
    m_currentTime(0),
    m_paused(true),
    m_hasVideo(false),
    m_readyState(MediaPlayer::HaveNothing),
    m_networkState(MediaPlayer::Empty),
    m_poster(0),
    m_naturalSize(100, 100),
    m_naturalSizeUnknown(true),
    m_isVisible(false)
{
    JNIEnv* env = JSC::Bindings::getJNIEnv();
    if (!env)
        return;

    jclass clazz = env->FindClass(g_ProxyJavaClass);
    if (!clazz)
        return;

    m_glue = new JavaGlue;
    m_glue->m_getInstance = env->GetStaticMethodID(clazz, "getInstance", "(Landroid/webkit/WebViewCore;I)Landroid/webkit/HTML5VideoViewProxy;");
    m_glue->m_play = env->GetMethodID(clazz, "play", "(Ljava/lang/String;)V");
    m_glue->m_teardown = env->GetMethodID(clazz, "teardown", "()V");
    m_glue->m_loadPoster = env->GetMethodID(clazz, "loadPoster", "(Ljava/lang/String;)V");
    m_glue->m_seek = env->GetMethodID(clazz, "seek", "(I)V");
    m_glue->m_pause = env->GetMethodID(clazz, "pause", "()V");
    m_glue->m_javaProxy = NULL;
    env->DeleteLocalRef(clazz);
    // An exception is raised if any of the above fails.
    checkException(env);
}

void MediaPlayerPrivate::createJavaPlayerIfNeeded()
{
    // Check if we have been already created.
    if (m_glue->m_javaProxy)
        return;

    FrameView* frameView = m_player->frameView();
    if (!frameView)
        return;

    JNIEnv* env = JSC::Bindings::getJNIEnv();
    if (!env)
        return;

    jclass clazz = env->FindClass(g_ProxyJavaClass);
    if (!clazz)
        return;

    WebViewCore* webViewCore =  WebViewCore::getWebViewCore(frameView);
    ASSERT(webViewCore);

    // Get the HTML5VideoViewProxy instance
    jobject obj = env->CallStaticObjectMethod(clazz, m_glue->m_getInstance, webViewCore->getJavaObject().get(), this);
    m_glue->m_javaProxy = env->NewGlobalRef(obj);
    // Send the poster
    jstring jUrl = 0;
    if (m_posterUrl.length())
        jUrl = env->NewString((unsigned short *)m_posterUrl.characters(), m_posterUrl.length());
    // Sending a NULL jUrl allows the Java side to try to load the default poster.
    env->CallVoidMethod(m_glue->m_javaProxy, m_glue->m_loadPoster, jUrl);
    if (jUrl)
        env->DeleteLocalRef(jUrl);
    // Clean up.
    env->DeleteLocalRef(obj);
    env->DeleteLocalRef(clazz);
    checkException(env);
}

void MediaPlayerPrivate::onPrepared(int duration, int width, int height) {
    m_duration = duration / 1000.0f;
    m_naturalSize = IntSize(width, height);
    m_naturalSizeUnknown = false;
    m_hasVideo = true;
    m_player->durationChanged();
    m_player->sizeChanged();
}

void MediaPlayerPrivate::onEnded() {
    m_currentTime = duration();
    m_player->timeChanged();
    m_paused = true;
    m_currentTime = 0;
    m_hasVideo = false;
    m_networkState = MediaPlayer::Idle;
    m_readyState = MediaPlayer::HaveNothing;
}

void MediaPlayerPrivate::onPosterFetched(SkBitmap* poster) {
    m_poster = poster;
    if (m_naturalSizeUnknown) {
        // We had to fake the size at startup, or else our paint
        // method would not be called. If we haven't yet received
        // the onPrepared event, update the intrinsic size to the size
        // of the poster. That will be overriden when onPrepare comes.
        // In case of an error, we should report the poster size, rather
        // than our initial fake value.
        m_naturalSize = IntSize(poster->width(), poster->height());
        m_player->sizeChanged();
    }
}

void MediaPlayerPrivate::onTimeupdate(int position) {
    m_currentTime = position / 1000.0f;
    m_player->timeChanged();
}

}

namespace android {

static void OnPrepared(JNIEnv* env, jobject obj, int duration, int width, int height, int pointer) {
    if (pointer) {
        WebCore::MediaPlayerPrivate* player = reinterpret_cast<WebCore::MediaPlayerPrivate*>(pointer);
        player->onPrepared(duration, width, height);
    }
}

static void OnEnded(JNIEnv* env, jobject obj, int pointer) {
    if (pointer) {
        WebCore::MediaPlayerPrivate* player = reinterpret_cast<WebCore::MediaPlayerPrivate*>(pointer);
        player->onEnded();
    }
}

static void OnPosterFetched(JNIEnv* env, jobject obj, jobject poster, int pointer) {
    if (!pointer || !poster)
        return;

    WebCore::MediaPlayerPrivate* player = reinterpret_cast<WebCore::MediaPlayerPrivate*>(pointer);
    SkBitmap* posterNative = GraphicsJNI::getNativeBitmap(env, poster);
    if (!posterNative)
        return;
    player->onPosterFetched(posterNative);
}

static void OnTimeupdate(JNIEnv* env, jobject obj, int position, int pointer) {
    if (pointer) {
        WebCore::MediaPlayerPrivate* player = reinterpret_cast<WebCore::MediaPlayerPrivate*>(pointer);
        player->onTimeupdate(position);
    }
}

/*
 * JNI registration
 */
static JNINativeMethod g_MediaPlayerMethods[] = {
    { "nativeOnPrepared", "(IIII)V",
        (void*) OnPrepared },
    { "nativeOnEnded", "(I)V",
        (void*) OnEnded },
    { "nativeOnPosterFetched", "(Landroid/graphics/Bitmap;I)V",
        (void*) OnPosterFetched },
    { "nativeOnTimeupdate", "(II)V",
        (void*) OnTimeupdate },
};

int register_mediaplayer(JNIEnv* env)
{
    return jniRegisterNativeMethods(env, g_ProxyJavaClass,
            g_MediaPlayerMethods, NELEM(g_MediaPlayerMethods));
}

}
#endif // VIDEO