Skip to content
This repository has been archived by the owner on Apr 3, 2020. It is now read-only.

Commit

Permalink
[Android] Support third part media player on Crosswalk
Browse files Browse the repository at this point in the history
A requirement from an important customer who want to forward web resources with proxy
on Crosswalk, but android system MediaPlayer can't set a proxy with a standard API.
The ExoMediaPlayer is playing videos and music is a popular activity on Android devices,
and it can be configured with proxy.
https://developer.android.com/guide/topics/media/exoplayer.html

BUG=XWALK-6770
  • Loading branch information
fujunwei committed Jun 3, 2016
1 parent 3bdf4cc commit 024bae0
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public XWalkContent(Context context, AttributeSet attrs, XWalkViewInternal xwVie
mGeolocationPermissions = new XWalkGeolocationPermissions(sharedPreferences);

MediaPlayerBridge.setResourceLoadingFilter(
new XWalkMediaPlayerResourceLoadingFilter());
new XWalkMediaPlayerResourceLoadingFilter(mContentsClientBridge));

setNativeContent(nativeInit());

Expand Down Expand Up @@ -371,6 +371,11 @@ public void setXWalkWebChromeClient(XWalkWebChromeClient client) {
mContentsClientBridge.setXWalkWebChromeClient(client);
}

public void setExMediaPlayer(XWalkExMediaPlayerInternal mediaPlayer) {
if (mNativeContent == 0) return;
mContentsClientBridge.setExMediaPlayer(mediaPlayer);
}

public XWalkWebChromeClient getXWalkWebChromeClient() {
if (mNativeContent == 0) return null;
return mContentsClientBridge.getXWalkWebChromeClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class XWalkContentsClientBridge extends XWalkContentsClient
private XWalkNavigationHandler mNavigationHandler;
private XWalkNotificationService mNotificationService;
private Handler mUiThreadHandler;
private XWalkExMediaPlayerInternal mXWalkExMediaPlayerInternal;

/** State recording variables */
// For fullscreen state.
Expand Down Expand Up @@ -162,6 +163,18 @@ public void setResourceClient(XWalkResourceClientInternal client) {
mXWalkResourceClient = new XWalkResourceClientInternal(mXWalkView);
}

public void setExMediaPlayer(XWalkExMediaPlayerInternal mediaPlayer) {
// If it's null, use Crosswalk implementation.
if (mediaPlayer != null) {
mXWalkExMediaPlayerInternal = mediaPlayer;
return;
}
mXWalkExMediaPlayerInternal = new XWalkExMediaPlayerInternal();
}

public XWalkExMediaPlayerInternal getExMediaPlayer() {
return mXWalkExMediaPlayerInternal;
}

public void setXWalkWebChromeClient(XWalkWebChromeClient client) {
// If it's null, use Crosswalk implementation.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Copyright (c) 2013-2014 Intel Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.xwalk.core.internal;

import android.content.Context;
import android.net.Uri;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnSeekCompleteListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.util.Log;
import android.view.Surface;

import java.io.FileDescriptor;
import java.util.HashMap;
import java.util.Map;

import org.chromium.media.ExMediaPlayer;

@XWalkAPI(createExternally = true)
public class XWalkExMediaPlayerInternal extends ExMediaPlayer {
private final static String TAG = "XWalkExMediaPlayerInternal";

private void unsupported() {
Log.e(TAG, "ERROR: The function must be implemented");
throw new UnsupportedOperationException();
}

@XWalkAPI
public void setSurface(Surface surface) {
unsupported();
}

@XWalkAPI
public void setDataSource(Context context, Uri uri, Map<String, String> headers) {
unsupported();
}

@XWalkAPI
public void setDataSource(FileDescriptor fd, long offset, long length) {
unsupported();
}

@XWalkAPI
public void setDataSource(Context context, Uri uri) {
unsupported();
}

@XWalkAPI
public void prepareAsync() {
unsupported();
}

@XWalkAPI
public boolean isPlaying() {
unsupported();
return false;
}

@XWalkAPI
public int getVideoWidth() {
unsupported();
return 0;
}

@XWalkAPI
public int getVideoHeight() {
unsupported();
return 0;
}

@XWalkAPI
public int getCurrentPosition() {
unsupported();
return 0;
}

@XWalkAPI
public int getDuration() {
unsupported();
return 0;
}

@XWalkAPI
public void release() {
unsupported();
}

@XWalkAPI
public void setVolume(float volume1, float volume2) {
unsupported();
}

@XWalkAPI
public void start() {
unsupported();
}

@XWalkAPI
public void pause() {
unsupported();
}

@XWalkAPI
public void seekTo(int msec) {
unsupported();
}

@XWalkAPI
public void setOnBufferingUpdateListener(OnBufferingUpdateListener listener) {
unsupported();
}

@XWalkAPI
public void setOnCompletionListener(OnCompletionListener listener) {
unsupported();
}

@XWalkAPI
public void setOnErrorListener(OnErrorListener listener) {
unsupported();
}

@XWalkAPI
public void setOnPreparedListener(OnPreparedListener listener) {
unsupported();
}

@XWalkAPI
public void setOnSeekCompleteListener(OnSeekCompleteListener listener) {
unsupported();
}

@XWalkAPI
public void setOnVideoSizeChangedListener(OnVideoSizeChangedListener listener) {
unsupported();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
import android.media.MediaPlayer;
import android.net.Uri;

import org.chromium.media.ExMediaPlayer;
import org.chromium.media.MediaPlayerBridge;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* This class inherits from MediaPlayerBridge.ResourceLoadingFilter to
Expand All @@ -22,8 +24,14 @@

class XWalkMediaPlayerResourceLoadingFilter extends
MediaPlayerBridge.ResourceLoadingFilter {
private XWalkContentsClientBridge mContentsClientBridge;

XWalkMediaPlayerResourceLoadingFilter(XWalkContentsClientBridge clientBridge) {
mContentsClientBridge = clientBridge;
}

@Override
public boolean shouldOverrideResourceLoading(MediaPlayer mediaPlayer,
public boolean shouldOverrideResourceLoading(ExMediaPlayer mediaPlayer,
Context context, Uri uri) {
String scheme = uri.getScheme();
if (scheme == null) return false;
Expand All @@ -39,10 +47,20 @@ public boolean shouldOverrideResourceLoading(MediaPlayer mediaPlayer,
context.getAssets().openFd(AndroidProtocolHandler.getAssetPath(uri));
mediaPlayer.setDataSource(
afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());

return true;
} catch (Exception e) {
return false;
}
}

@Override
public ExMediaPlayer getExMediaPlayer() {
ExMediaPlayer exMediaPlayer = mContentsClientBridge.getExMediaPlayer();

if (exMediaPlayer == null) {
exMediaPlayer = new ExMediaPlayer();
}

return exMediaPlayer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,13 @@ public void setResourceClient(XWalkResourceClientInternal client) {
mContent.setResourceClient(client);
}

@XWalkAPI(reservable = true)
public void setExMediaPlayer(XWalkExMediaPlayerInternal mediaPlayer) {
if (mContent == null) return;
checkThreadSafety();
mContent.setExMediaPlayer(mediaPlayer);
}

/**
* Set Background color of the view
*/
Expand Down
1 change: 1 addition & 0 deletions tools/reflection_generator/reflection_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
'XWalkWebResourceRequestHandlerInternal',
'XWalkWebResourceRequestInternal',
'XWalkWebResourceResponseInternal',
'XWalkExMediaPlayerInternal',
]

REFLECTION_HERLPER = [
Expand Down

0 comments on commit 024bae0

Please sign in to comment.