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

Commit 9baa6c1

Browse files
committed
[Android] Support third part media player on Crosswalk
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
1 parent 87feec5 commit 9baa6c1

File tree

6 files changed

+300
-4
lines changed

6 files changed

+300
-4
lines changed

runtime/android/core_internal/src/org/xwalk/core/internal/XWalkContent.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public XWalkContent(Context context, String animatable, XWalkViewInternal xwView
132132
mGeolocationPermissions = new XWalkGeolocationPermissions(sharedPreferences);
133133

134134
MediaPlayerBridge.setResourceLoadingFilter(
135-
new XWalkMediaPlayerResourceLoadingFilter());
135+
new XWalkMediaPlayerResourceLoadingFilter(mContentsClientBridge));
136136

137137
setNativeContent(nativeInit(), animatable);
138138

@@ -379,6 +379,11 @@ public void setXWalkWebChromeClient(XWalkWebChromeClient client) {
379379
mContentsClientBridge.setXWalkWebChromeClient(client);
380380
}
381381

382+
public void setXWalkMediaPlayer(XWalkMediaPlayerInternal mediaPlayer) {
383+
if (mNativeContent == 0) return;
384+
mContentsClientBridge.setXWalkMediaPlayer(mediaPlayer);
385+
}
386+
382387
public XWalkWebChromeClient getXWalkWebChromeClient() {
383388
if (mNativeContent == 0) return null;
384389
return mContentsClientBridge.getXWalkWebChromeClient();

runtime/android/core_internal/src/org/xwalk/core/internal/XWalkContentsClientBridge.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class XWalkContentsClientBridge extends XWalkContentsClient
7070
private XWalkNavigationHandler mNavigationHandler;
7171
private XWalkNotificationService mNotificationService;
7272
private Handler mUiThreadHandler;
73+
private XWalkMediaPlayerInternal mXWalkMediaPlayerInternal;
7374

7475
/** State recording variables */
7576
// For fullscreen state.
@@ -163,6 +164,13 @@ public void setResourceClient(XWalkResourceClientInternal client) {
163164
mXWalkResourceClient = new XWalkResourceClientInternal(mXWalkView);
164165
}
165166

167+
public void setXWalkMediaPlayer(XWalkMediaPlayerInternal mediaPlayer) {
168+
mXWalkMediaPlayerInternal = mediaPlayer;
169+
}
170+
171+
public XWalkMediaPlayerInternal getExternalMediaPlayer() {
172+
return mXWalkMediaPlayerInternal;
173+
}
166174

167175
public void setXWalkWebChromeClient(XWalkWebChromeClient client) {
168176
// If it's null, use Crosswalk implementation.
Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
// Copyright (c) 2016 Intel Corporation. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
package org.xwalk.core.internal;
6+
7+
import android.content.Context;
8+
import android.media.MediaPlayer.OnBufferingUpdateListener;
9+
import android.media.MediaPlayer.OnCompletionListener;
10+
import android.media.MediaPlayer.OnErrorListener;
11+
import android.media.MediaPlayer.OnPreparedListener;
12+
import android.media.MediaPlayer.OnSeekCompleteListener;
13+
import android.media.MediaPlayer.OnVideoSizeChangedListener;
14+
import android.media.MediaPlayer.TrackInfo;
15+
import android.net.Uri;
16+
import android.util.Log;
17+
import android.view.Surface;
18+
19+
import java.io.FileDescriptor;
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
23+
import org.chromium.media.ExternalMediaPlayer;
24+
25+
@XWalkAPI(createExternally = true)
26+
public class XWalkMediaPlayerInternal extends ExternalMediaPlayer {
27+
private final static String TAG = "XWalkMediaPlayerInternal";
28+
29+
private void unsupported() {
30+
Log.e(TAG, "ERROR: The function must be implemented");
31+
throw new UnsupportedOperationException();
32+
}
33+
34+
/**
35+
* Sets the Surface to be used as the sink for the video portion of the media.
36+
* @param surface the Surface to be used for the video portion of the media.
37+
* @since 7.0
38+
*/
39+
@XWalkAPI
40+
public void setSurface(Surface surface) {
41+
unsupported();
42+
}
43+
44+
/**
45+
* Sets the data source as a content Uri.
46+
* @param context the Context to use when resolving the Uri.
47+
* @param uri the Content URI of the data you want to play.
48+
* @param headers the headers to be sent together with the request for the data.
49+
* @since 7.0
50+
*/
51+
@XWalkAPI
52+
public void setDataSource(Context context, Uri uri, Map<String, String> headers) {
53+
unsupported();
54+
}
55+
56+
/**
57+
* Sets the data source (FileDescriptor) to use.
58+
* @param offset the offset into the file where the data to be played starts, in bytes.
59+
* @param length the length in bytes of the data to be played.
60+
* @since 7.0
61+
*/
62+
@XWalkAPI
63+
public void setDataSource(FileDescriptor fd, long offset, long length) {
64+
unsupported();
65+
}
66+
67+
/**
68+
* Sets the data source as a content Uri.
69+
* @param context the Context to use when resolving the Uri.
70+
* @param uri the Content URI of the data you want to play.
71+
* @since 7.0
72+
*/
73+
@XWalkAPI
74+
public void setDataSource(Context context, Uri uri) {
75+
unsupported();
76+
}
77+
78+
/**
79+
* Prepares the player for playback, asynchronously.
80+
* @since 7.0
81+
*/
82+
@XWalkAPI
83+
public void prepareAsync() {
84+
unsupported();
85+
}
86+
87+
/**
88+
* Checks whether the MediaPlayer is playing.
89+
* @since 7.0
90+
*/
91+
@XWalkAPI
92+
public boolean isPlaying() {
93+
unsupported();
94+
return false;
95+
}
96+
97+
/**
98+
* Returns the width of the video.
99+
* @since 7.0
100+
*/
101+
@XWalkAPI
102+
public int getVideoWidth() {
103+
unsupported();
104+
return 0;
105+
}
106+
107+
/**
108+
* Returns the height of the video.
109+
* @since 7.0
110+
*/
111+
@XWalkAPI
112+
public int getVideoHeight() {
113+
unsupported();
114+
return 0;
115+
}
116+
117+
/**
118+
* Gets the current playback position.
119+
* @since 7.0
120+
*/
121+
@XWalkAPI
122+
public int getCurrentPosition() {
123+
unsupported();
124+
return 0;
125+
}
126+
127+
/**
128+
* Gets the duration of the file.
129+
* @since 7.0
130+
*/
131+
@XWalkAPI
132+
public int getDuration() {
133+
unsupported();
134+
return 0;
135+
}
136+
137+
/**
138+
* Releases resources associated with this MediaPlayer object.
139+
* @since 7.0
140+
*/
141+
@XWalkAPI
142+
public void release() {
143+
unsupported();
144+
}
145+
146+
/**
147+
* Sets the volume on this player.
148+
* @since 7.0
149+
*/
150+
@XWalkAPI
151+
public void setVolume(float volume1, float volume2) {
152+
unsupported();
153+
}
154+
155+
/**
156+
* Starts or resumes playback. If playback had previously been paused,
157+
* playback will continue from where it was paused. If playback had been stopped,
158+
* or never started before, playback will start at the beginning.
159+
* @since 7.0
160+
*/
161+
@XWalkAPI
162+
public void start() {
163+
unsupported();
164+
}
165+
166+
/**
167+
* Pauses playback. Call start() to resume.
168+
* @since 7.0
169+
*/
170+
@XWalkAPI
171+
public void pause() {
172+
unsupported();
173+
}
174+
175+
/**
176+
* Seeks to specified time position.
177+
* @since 7.0
178+
*/
179+
@XWalkAPI
180+
public void seekTo(int msec) {
181+
unsupported();
182+
}
183+
184+
/**
185+
* Returns an array of track information.
186+
* @since 7.0
187+
*/
188+
@XWalkAPI
189+
public TrackInfo[] getTrackInfo() {
190+
unsupported();
191+
return null;
192+
}
193+
194+
/**
195+
* Register a callback to be invoked when the status of a network stream's buffer has changed.
196+
* @param listener the callback that will be run.
197+
* @since 7.0
198+
*/
199+
@XWalkAPI
200+
public void setOnBufferingUpdateListener(OnBufferingUpdateListener listener) {
201+
unsupported();
202+
}
203+
204+
/**
205+
* Register a callback to be invoked when the end of a media source has been reached during playback.
206+
* @param listener the callback that will be run.
207+
* @since 7.0
208+
*/
209+
@XWalkAPI
210+
public void setOnCompletionListener(OnCompletionListener listener) {
211+
unsupported();
212+
}
213+
214+
/**
215+
* Register a callback to be invoked when an error has happened during an asynchronous operation.
216+
* @param listener the callback that will be run.
217+
* @since 7.0
218+
*/
219+
@XWalkAPI
220+
public void setOnErrorListener(OnErrorListener listener) {
221+
unsupported();
222+
}
223+
224+
/**
225+
* Register a callback to be invoked when the media source is ready for playback.
226+
* @param listener the callback that will be run.
227+
* @since 7.0
228+
*/
229+
@XWalkAPI
230+
public void setOnPreparedListener(OnPreparedListener listener) {
231+
unsupported();
232+
}
233+
234+
/**
235+
* Register a callback to be invoked when a seek operation has been completed.
236+
* @param listener the callback that will be run.
237+
* @since 7.0
238+
*/
239+
@XWalkAPI
240+
public void setOnSeekCompleteListener(OnSeekCompleteListener listener) {
241+
unsupported();
242+
}
243+
244+
/**
245+
* Register a callback to be invoked when the video size is known or updated.
246+
* @param listener the callback that will be run.
247+
* @since 7.0
248+
*/
249+
@XWalkAPI
250+
public void setOnVideoSizeChangedListener(OnVideoSizeChangedListener listener) {
251+
unsupported();
252+
}
253+
}

runtime/android/core_internal/src/org/xwalk/core/internal/XWalkMediaPlayerResourceLoadingFilter.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
import android.content.Context;
88
import android.content.res.AssetFileDescriptor;
9-
import android.media.MediaPlayer;
109
import android.net.Uri;
1110

11+
import org.chromium.media.ExternalMediaPlayer;
1212
import org.chromium.media.MediaPlayerBridge;
1313

1414
import java.io.File;
@@ -22,8 +22,14 @@
2222

2323
class XWalkMediaPlayerResourceLoadingFilter extends
2424
MediaPlayerBridge.ResourceLoadingFilter {
25+
private XWalkContentsClientBridge mContentsClientBridge;
26+
27+
XWalkMediaPlayerResourceLoadingFilter(XWalkContentsClientBridge clientBridge) {
28+
mContentsClientBridge = clientBridge;
29+
}
30+
2531
@Override
26-
public boolean shouldOverrideResourceLoading(MediaPlayer mediaPlayer,
32+
public boolean shouldOverrideResourceLoading(ExternalMediaPlayer mediaPlayer,
2733
Context context, Uri uri) {
2834
String scheme = uri.getScheme();
2935
if (scheme == null) return false;
@@ -39,10 +45,20 @@ public boolean shouldOverrideResourceLoading(MediaPlayer mediaPlayer,
3945
context.getAssets().openFd(AndroidProtocolHandler.getAssetPath(uri));
4046
mediaPlayer.setDataSource(
4147
afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
42-
4348
return true;
4449
} catch (Exception e) {
4550
return false;
4651
}
4752
}
53+
54+
@Override
55+
public ExternalMediaPlayer getExternalMediaPlayer() {
56+
ExternalMediaPlayer exMediaPlayer = mContentsClientBridge.getExternalMediaPlayer();
57+
58+
if (exMediaPlayer == null) {
59+
exMediaPlayer = new ExternalMediaPlayer();
60+
}
61+
62+
return exMediaPlayer;
63+
}
4864
}

runtime/android/core_internal/src/org/xwalk/core/internal/XWalkViewInternal.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,19 @@ public void setResourceClient(XWalkResourceClientInternal client) {
954954
mContent.setResourceClient(client);
955955
}
956956

957+
/**
958+
* Support third party MediaPlayer in Crosswalk with implementing the api
959+
* from XWalkMediaPlayerInternal.
960+
* @param mediaPlayer the XWalkMediaPlayerInternal implemented by customers.
961+
* @since 7.0
962+
*/
963+
@XWalkAPI(reservable = true)
964+
public void setXWalkMediaPlayer(XWalkMediaPlayerInternal mediaPlayer) {
965+
if (mContent == null) return;
966+
checkThreadSafety();
967+
mContent.setXWalkMediaPlayer(mediaPlayer);
968+
}
969+
957970
/**
958971
* Set Background color of the view
959972
*/

tools/reflection_generator/reflection_generator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
'XWalkWebResourceRequestHandlerInternal',
5151
'XWalkWebResourceRequestInternal',
5252
'XWalkWebResourceResponseInternal',
53+
'XWalkMediaPlayerInternal',
5354
]
5455

5556
REFLECTION_HERLPER = [

0 commit comments

Comments
 (0)