Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
API Extension: Support for optionally specifying a map of extra reque…
Browse files Browse the repository at this point in the history
…st headers when specifying the uri of media data to be played.

related-to-bug: 2393577

Original change by Andrei Popescu <andreip@google.com>
  • Loading branch information
theandi666 committed Jan 28, 2010
1 parent 75d76bc commit 2564300
Show file tree
Hide file tree
Showing 24 changed files with 275 additions and 66 deletions.
2 changes: 1 addition & 1 deletion camera/libcameraservice/CameraService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ void CameraService::decUsers() {
static sp<MediaPlayer> newMediaPlayer(const char *file)
{
sp<MediaPlayer> mp = new MediaPlayer();
if (mp->setDataSource(file) == NO_ERROR) {
if (mp->setDataSource(file, NULL /* headers */) == NO_ERROR) {
mp->setAudioStreamType(AudioSystem::ENFORCED_AUDIBLE);
mp->prepare();
} else {
Expand Down
12 changes: 11 additions & 1 deletion core/java/android/webkit/HTML5VideoViewProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class HTML5VideoViewProxy extends Handler
private static final int ENDED = 201;
private static final int POSTER_FETCHED = 202;

private static final String COOKIE = "Cookie";

// Timer thread -> UI thread
private static final int TIMEUPDATE = 300;

Expand Down Expand Up @@ -165,7 +167,15 @@ public static void play(String url, int time, HTML5VideoViewProxy proxy,
mVideoView = new VideoView(proxy.getContext());
mVideoView.setWillNotDraw(false);
mVideoView.setMediaController(new MediaController(proxy.getContext()));
mVideoView.setVideoURI(Uri.parse(url));

String cookieValue = CookieManager.getInstance().getCookie(url);
Map<String, String> headers = null;
if (cookieValue != null) {
headers = new HashMap<String, String>();
headers.put(COOKIE, cookieValue);
}

mVideoView.setVideoURI(Uri.parse(url), headers);
mVideoView.setOnCompletionListener(proxy);
mVideoView.setOnPreparedListener(proxy);
mVideoView.setOnErrorListener(proxy);
Expand Down
60 changes: 35 additions & 25 deletions core/java/android/widget/VideoView.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import android.widget.MediaController.*;

import java.io.IOException;
import java.util.Map;

/**
* Displays a video file. The VideoView class
Expand All @@ -50,6 +51,7 @@ public class VideoView extends SurfaceView implements MediaPlayerControl {
private String TAG = "VideoView";
// settable by the client
private Uri mUri;
private Map<String, String> mHeaders;
private int mDuration;

// all possible internal states
Expand Down Expand Up @@ -90,12 +92,12 @@ public VideoView(Context context) {
super(context);
initVideoView();
}

public VideoView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
initVideoView();
}

public VideoView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initVideoView();
Expand All @@ -122,7 +124,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//Log.i("@@@@@@@@@@", "setting size: " + width + 'x' + height);
setMeasuredDimension(width, height);
}

public int resolveAdjustedSize(int desiredSize, int measureSpec) {
int result = desiredSize;
int specMode = MeasureSpec.getMode(measureSpec);
Expand All @@ -137,21 +139,21 @@ public int resolveAdjustedSize(int desiredSize, int measureSpec) {
break;

case MeasureSpec.AT_MOST:
/* Parent says we can be as big as we want, up to specSize.
* Don't be larger than specSize, and don't be larger than
/* Parent says we can be as big as we want, up to specSize.
* Don't be larger than specSize, and don't be larger than
* the max size imposed on ourselves.
*/
result = Math.min(desiredSize, specSize);
break;

case MeasureSpec.EXACTLY:
// No choice. Do what we are told.
result = specSize;
break;
}
return result;
}

private void initVideoView() {
mVideoWidth = 0;
mVideoHeight = 0;
Expand All @@ -169,13 +171,21 @@ public void setVideoPath(String path) {
}

public void setVideoURI(Uri uri) {
setVideoURI(uri, null);
}

/**
* @hide
*/
public void setVideoURI(Uri uri, Map<String, String> headers) {
mUri = uri;
mHeaders = headers;
mSeekWhenPrepared = 0;
openVideo();
requestLayout();
invalidate();
}

public void stopPlayback() {
if (mMediaPlayer != null) {
mMediaPlayer.stop();
Expand All @@ -191,7 +201,7 @@ private void openVideo() {
// not ready for playback just yet, will try again later
return;
}
// Tell the music playback service to pause
// Tell the music playback service to pause
// TODO: these constants need to be published somewhere in the framework.
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "pause");
Expand All @@ -209,7 +219,7 @@ private void openVideo() {
mMediaPlayer.setOnErrorListener(mErrorListener);
mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);
mCurrentBufferPercentage = 0;
mMediaPlayer.setDataSource(mContext, mUri);
mMediaPlayer.setDataSource(mContext, mUri, mHeaders);
mMediaPlayer.setDisplay(mSurfaceHolder);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setScreenOnWhilePlaying(true);
Expand All @@ -232,7 +242,7 @@ private void openVideo() {
return;
}
}

public void setMediaController(MediaController controller) {
if (mMediaController != null) {
mMediaController.hide();
Expand All @@ -250,7 +260,7 @@ private void attachMediaController() {
mMediaController.setEnabled(isInPlaybackState());
}
}

MediaPlayer.OnVideoSizeChangedListener mSizeChangedListener =
new MediaPlayer.OnVideoSizeChangedListener() {
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
Expand All @@ -261,7 +271,7 @@ public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
}
}
};

MediaPlayer.OnPreparedListener mPreparedListener = new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mCurrentState = STATE_PREPARED;
Expand Down Expand Up @@ -490,15 +500,15 @@ public boolean onTouchEvent(MotionEvent ev) {
}
return false;
}

@Override
public boolean onTrackballEvent(MotionEvent ev) {
if (isInPlaybackState() && mMediaController != null) {
toggleMediaControlsVisiblity();
}
return false;
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
Expand All @@ -519,7 +529,7 @@ public boolean onKeyDown(int keyCode, KeyEvent event)
mMediaController.hide();
}
return true;
} else if (keyCode == KeyEvent.KEYCODE_MEDIA_STOP
} else if (keyCode == KeyEvent.KEYCODE_MEDIA_STOP
&& mMediaPlayer.isPlaying()) {
pause();
mMediaController.show();
Expand All @@ -532,21 +542,21 @@ public boolean onKeyDown(int keyCode, KeyEvent event)
}

private void toggleMediaControlsVisiblity() {
if (mMediaController.isShowing()) {
if (mMediaController.isShowing()) {
mMediaController.hide();
} else {
mMediaController.show();
}
}

public void start() {
if (isInPlaybackState()) {
mMediaPlayer.start();
mCurrentState = STATE_PLAYING;
}
mTargetState = STATE_PLAYING;
}

public void pause() {
if (isInPlaybackState()) {
if (mMediaPlayer.isPlaying()) {
Expand All @@ -556,7 +566,7 @@ public void pause() {
}
mTargetState = STATE_PAUSED;
}

// cache duration as mDuration for faster access
public int getDuration() {
if (isInPlaybackState()) {
Expand All @@ -569,27 +579,27 @@ public int getDuration() {
mDuration = -1;
return mDuration;
}

public int getCurrentPosition() {
if (isInPlaybackState()) {
return mMediaPlayer.getCurrentPosition();
}
return 0;
}

public void seekTo(int msec) {
if (isInPlaybackState()) {
mMediaPlayer.seekTo(msec);
mSeekWhenPrepared = 0;
} else {
mSeekWhenPrepared = msec;
}
}
}

public boolean isPlaying() {
return isInPlaybackState() && mMediaPlayer.isPlaying();
}

public int getBufferPercentage() {
if (mMediaPlayer != null) {
return mCurrentBufferPercentage;
Expand Down
4 changes: 3 additions & 1 deletion include/media/IMediaPlayerService.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
#define ANDROID_IMEDIAPLAYERSERVICE_H

#include <utils/Errors.h> // for status_t
#include <utils/KeyedVector.h>
#include <utils/RefBase.h>
#include <utils/String8.h>
#include <binder/IInterface.h>
#include <binder/Parcel.h>

Expand All @@ -38,7 +40,7 @@ class IMediaPlayerService: public IInterface

virtual sp<IMediaRecorder> createMediaRecorder(pid_t pid) = 0;
virtual sp<IMediaMetadataRetriever> createMetadataRetriever(pid_t pid) = 0;
virtual sp<IMediaPlayer> create(pid_t pid, const sp<IMediaPlayerClient>& client, const char* url) = 0;
virtual sp<IMediaPlayer> create(pid_t pid, const sp<IMediaPlayerClient>& client, const char* url, const KeyedVector<String8, String8> *headers = NULL) = 0;
virtual sp<IMediaPlayer> create(pid_t pid, const sp<IMediaPlayerClient>& client, int fd, int64_t offset, int64_t length) = 0;
virtual sp<IMemory> decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat) = 0;
virtual sp<IMemory> decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat) = 0;
Expand Down
10 changes: 8 additions & 2 deletions include/media/MediaPlayerInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@

#include <sys/types.h>
#include <ui/ISurface.h>
#include <utils/RefBase.h>
#include <utils/Errors.h>
#include <utils/KeyedVector.h>
#include <utils/String8.h>
#include <utils/RefBase.h>

#include <media/mediaplayer.h>
#include <media/AudioSystem.h>
Expand Down Expand Up @@ -96,7 +98,11 @@ class MediaPlayerBase : public RefBase
virtual ~MediaPlayerBase() {}
virtual status_t initCheck() = 0;
virtual bool hardwareOutput() = 0;
virtual status_t setDataSource(const char *url) = 0;

virtual status_t setDataSource(
const char *url,
const KeyedVector<String8, String8> *headers = NULL) = 0;

virtual status_t setDataSource(int fd, int64_t offset, int64_t length) = 0;
virtual status_t setVideoSurface(const sp<ISurface>& surface) = 0;
virtual status_t prepare() = 0;
Expand Down
5 changes: 4 additions & 1 deletion include/media/PVPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ class PVPlayer : public MediaPlayerInterface
virtual ~PVPlayer();

virtual status_t initCheck();
virtual status_t setDataSource(const char *url);

virtual status_t setDataSource(
const char *url, const KeyedVector<String8, String8> *headers);

virtual status_t setDataSource(int fd, int64_t offset, int64_t length);
virtual status_t setVideoSurface(const sp<ISurface>& surface);
virtual status_t prepare();
Expand Down
9 changes: 8 additions & 1 deletion include/media/mediaplayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
#include <media/IMediaPlayer.h>
#include <media/IMediaDeathNotifier.h>

#include <utils/KeyedVector.h>
#include <utils/String8.h>

namespace android {

enum media_event_type {
Expand Down Expand Up @@ -130,7 +133,11 @@ class MediaPlayer : public BnMediaPlayerClient,
~MediaPlayer();
void died();
void disconnect();
status_t setDataSource(const char *url);

status_t setDataSource(
const char *url,
const KeyedVector<String8, String8> *headers);

status_t setDataSource(int fd, int64_t offset, int64_t length);
status_t setVideoSurface(const sp<Surface>& surface);
status_t setListener(const sp<MediaPlayerListener>& listener);
Expand Down
28 changes: 27 additions & 1 deletion media/java/android/media/MediaPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import java.io.FileDescriptor;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.lang.ref.WeakReference;

Expand Down Expand Up @@ -665,6 +666,20 @@ public static MediaPlayer create(Context context, int resid) {
*/
public void setDataSource(Context context, Uri uri)
throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
setDataSource(context, uri, null);
}

/**
* Sets the data source as a content Uri.
*
* @param context the Context to use when resolving the Uri
* @param uri the Content URI of the data you want to play
* @param headers the headers to be sent together with the request for the data
* @throws IllegalStateException if it is called in an invalid state
* @hide pending API council
*/
public void setDataSource(Context context, Uri uri, Map<String, String> headers)
throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {

String scheme = uri.getScheme();
if(scheme == null || scheme.equals("file")) {
Expand Down Expand Up @@ -696,7 +711,7 @@ public void setDataSource(Context context, Uri uri)
}
}
Log.d(TAG, "Couldn't open file on client side, trying server side");
setDataSource(uri.toString());
setDataSource(uri.toString(), headers);
return;
}

Expand All @@ -708,6 +723,17 @@ public void setDataSource(Context context, Uri uri)
*/
public native void setDataSource(String path) throws IOException, IllegalArgumentException, IllegalStateException;

/**
* Sets the data source (file-path or http/rtsp URL) to use.
*
* @param path the path of the file, or the http/rtsp URL of the stream you want to play
* @param headers the headers associated with the http request for the stream you want to play
* @throws IllegalStateException if it is called in an invalid state
* @hide pending API council
*/
public native void setDataSource(String path, Map<String, String> headers)
throws IOException, IllegalArgumentException, IllegalStateException;

/**
* Sets the data source (FileDescriptor) to use. It is the caller's responsibility
* to close the file descriptor. It is safe to do so as soon as this call returns.
Expand Down
Loading

0 comments on commit 2564300

Please sign in to comment.