Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c500a378 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "MediaPlayer2: pass positions to native"

parents a19eae35 2a1407a7
Loading
Loading
Loading
Loading
+43 −22
Original line number Diff line number Diff line
@@ -682,7 +682,9 @@ public final class MediaPlayer2Impl extends MediaPlayer2 {
            case DataSourceDesc.TYPE_CALLBACK:
                handleDataSource(isCurrent,
                                 srcId,
                                 dsd.getMedia2DataSource());
                                 dsd.getMedia2DataSource(),
                                 dsd.getStartPosition(),
                                 dsd.getEndPosition());
                break;

            case DataSourceDesc.TYPE_FD:
@@ -690,7 +692,9 @@ public final class MediaPlayer2Impl extends MediaPlayer2 {
                                 srcId,
                                 dsd.getFileDescriptor(),
                                 dsd.getFileDescriptorOffset(),
                                 dsd.getFileDescriptorLength());
                                 dsd.getFileDescriptorLength(),
                                 dsd.getStartPosition(),
                                 dsd.getEndPosition());
                break;

            case DataSourceDesc.TYPE_URI:
@@ -699,7 +703,9 @@ public final class MediaPlayer2Impl extends MediaPlayer2 {
                                 dsd.getUriContext(),
                                 dsd.getUri(),
                                 dsd.getUriHeaders(),
                                 dsd.getUriCookies());
                                 dsd.getUriCookies(),
                                 dsd.getStartPosition(),
                                 dsd.getEndPosition());
                break;

            default:
@@ -729,7 +735,8 @@ public final class MediaPlayer2Impl extends MediaPlayer2 {
    private void handleDataSource(
            boolean isCurrent, long srcId,
            @NonNull Context context, @NonNull Uri uri,
            @Nullable Map<String, String> headers, @Nullable List<HttpCookie> cookies)
            @Nullable Map<String, String> headers, @Nullable List<HttpCookie> cookies,
            long startPos, long endPos)
            throws IOException {
        // The context and URI usually belong to the calling user. Get a resolver for that user
        // and strip out the userId from the URI if present.
@@ -737,7 +744,7 @@ public final class MediaPlayer2Impl extends MediaPlayer2 {
        final String scheme = uri.getScheme();
        final String authority = ContentProvider.getAuthorityWithoutUserId(uri.getAuthority());
        if (ContentResolver.SCHEME_FILE.equals(scheme)) {
            handleDataSource(isCurrent, srcId, uri.getPath(), null, null);
            handleDataSource(isCurrent, srcId, uri.getPath(), null, null, startPos, endPos);
            return;
        }

@@ -751,7 +758,7 @@ public final class MediaPlayer2Impl extends MediaPlayer2 {
                afd = resolver.openAssetFileDescriptor(uri, "r");
            }
            if (afd != null) {
                handleDataSource(isCurrent, srcId, afd);
                handleDataSource(isCurrent, srcId, afd, startPos, endPos);
                return;
            }
        } catch (NullPointerException | SecurityException | IOException ex) {
@@ -762,29 +769,35 @@ public final class MediaPlayer2Impl extends MediaPlayer2 {
                afd.close();
            }
        }
        handleDataSource(isCurrent, srcId, uri.toString(), headers, cookies);
        handleDataSource(isCurrent, srcId, uri.toString(), headers, cookies, startPos, endPos);
    }

    private void handleDataSource(boolean isCurrent, long srcId, AssetFileDescriptor afd)
    private void handleDataSource(boolean isCurrent, long srcId, AssetFileDescriptor afd,
            long startPos, long endPos)
            throws IOException {
        if (afd.getDeclaredLength() < 0) {
            handleDataSource(isCurrent,
                    srcId,
                    afd.getFileDescriptor(),
                    0,
                    DataSourceDesc.LONG_MAX);
                    DataSourceDesc.LONG_MAX,
                    startPos,
                    endPos);
        } else {
            handleDataSource(isCurrent,
                    srcId,
                    afd.getFileDescriptor(),
                    afd.getStartOffset(),
                    afd.getDeclaredLength());
                    afd.getDeclaredLength(),
                    startPos,
                    endPos);
        }
    }

    private void handleDataSource(
            boolean isCurrent, long srcId,
            String path, Map<String, String> headers, List<HttpCookie> cookies)
            String path, Map<String, String> headers, List<HttpCookie> cookies,
            long startPos, long endPos)
            throws IOException {
        String[] keys = null;
        String[] values = null;
@@ -800,11 +813,12 @@ public final class MediaPlayer2Impl extends MediaPlayer2 {
                ++i;
            }
        }
        handleDataSource(isCurrent, srcId, path, keys, values, cookies);
        handleDataSource(isCurrent, srcId, path, keys, values, cookies, startPos, endPos);
    }

    private void handleDataSource(boolean isCurrent, long srcId,
            String path, String[] keys, String[] values, List<HttpCookie> cookies)
            String path, String[] keys, String[] values, List<HttpCookie> cookies,
            long startPos, long endPos)
            throws IOException {
        final Uri uri = Uri.parse(path);
        final String scheme = uri.getScheme();
@@ -818,7 +832,9 @@ public final class MediaPlayer2Impl extends MediaPlayer2 {
                Media2HTTPService.createHTTPService(path, cookies),
                path,
                keys,
                values);
                values,
                startPos,
                endPos);
            return;
        }

@@ -826,7 +842,7 @@ public final class MediaPlayer2Impl extends MediaPlayer2 {
        if (file.exists()) {
            FileInputStream is = new FileInputStream(file);
            FileDescriptor fd = is.getFD();
            handleDataSource(isCurrent, srcId, fd, 0, DataSourceDesc.LONG_MAX);
            handleDataSource(isCurrent, srcId, fd, 0, DataSourceDesc.LONG_MAX, startPos, endPos);
            is.close();
        } else {
            throw new IOException("handleDataSource failed.");
@@ -835,7 +851,8 @@ public final class MediaPlayer2Impl extends MediaPlayer2 {

    private native void nativeHandleDataSourceUrl(
            boolean isCurrent, long srcId,
            Media2HTTPService httpService, String path, String[] keys, String[] values)
            Media2HTTPService httpService, String path, String[] keys, String[] values,
            long startPos, long endPos)
            throws IOException;

    /**
@@ -849,23 +866,27 @@ public final class MediaPlayer2Impl extends MediaPlayer2 {
     */
    private void handleDataSource(
            boolean isCurrent, long srcId,
            FileDescriptor fd, long offset, long length) throws IOException {
        nativeHandleDataSourceFD(isCurrent, srcId, fd, offset, length);
            FileDescriptor fd, long offset, long length,
            long startPos, long endPos) throws IOException {
        nativeHandleDataSourceFD(isCurrent, srcId, fd, offset, length, startPos, endPos);
    }

    private native void nativeHandleDataSourceFD(boolean isCurrent, long srcId,
            FileDescriptor fd, long offset, long length) throws IOException;
            FileDescriptor fd, long offset, long length,
            long startPos, long endPos) throws IOException;

    /**
     * @throws IllegalStateException if it is called in an invalid state
     * @throws IllegalArgumentException if dataSource is not a valid Media2DataSource
     */
    private void handleDataSource(boolean isCurrent, long srcId, Media2DataSource dataSource) {
        nativeHandleDataSourceCallback(isCurrent, srcId, dataSource);
    private void handleDataSource(boolean isCurrent, long srcId, Media2DataSource dataSource,
            long startPos, long endPos) {
        nativeHandleDataSourceCallback(isCurrent, srcId, dataSource, startPos, endPos);
    }

    private native void nativeHandleDataSourceCallback(
            boolean isCurrent, long srcId, Media2DataSource dataSource);
            boolean isCurrent, long srcId, Media2DataSource dataSource,
            long startPos, long endPos);

    /**
     * @return true if there is a next data source, false otherwise.
+22 −11
Original line number Diff line number Diff line
@@ -283,7 +283,8 @@ static void process_media_player_call(
static void
android_media_MediaPlayer2_handleDataSourceUrl(
        JNIEnv *env, jobject thiz, jboolean isCurrent, jlong srcId,
        jobject httpServiceObj, jstring path, jobjectArray keys, jobjectArray values) {
        jobject httpServiceObj, jstring path, jobjectArray keys, jobjectArray values,
        jlong startPos, jlong endPos) {

    sp<MediaPlayer2> mp = getMediaPlayer(env, thiz);
    if (mp == NULL) {
@@ -300,7 +301,8 @@ android_media_MediaPlayer2_handleDataSourceUrl(
    if (tmp == NULL) {  // Out of memory
        return;
    }
    ALOGV("handleDataSourceUrl: path %s, srcId %lld", tmp, (long long)srcId);
    ALOGV("handleDataSourceUrl: path %s, srcId %lld, start %lld, end %lld",
          tmp, (long long)srcId, (long long)startPos, (long long)endPos);

    if (strncmp(tmp, "content://", 10) == 0) {
        ALOGE("handleDataSourceUrl: content scheme is not supported in native code");
@@ -313,6 +315,8 @@ android_media_MediaPlayer2_handleDataSourceUrl(
    dsd->mId = srcId;
    dsd->mType = DataSourceDesc::TYPE_URL;
    dsd->mUrl = tmp;
    dsd->mStartPositionMs = startPos;
    dsd->mEndPositionMs = endPos;

    env->ReleaseStringUTFChars(path, tmp);
    tmp = NULL;
@@ -342,8 +346,8 @@ android_media_MediaPlayer2_handleDataSourceUrl(
static void
android_media_MediaPlayer2_handleDataSourceFD(
        JNIEnv *env, jobject thiz, jboolean isCurrent, jlong srcId,
    jobject fileDescriptor, jlong offset, jlong length)
{
        jobject fileDescriptor, jlong offset, jlong length,
        jlong startPos, jlong endPos) {
    sp<MediaPlayer2> mp = getMediaPlayer(env, thiz);
    if (mp == NULL ) {
        jniThrowException(env, "java/lang/IllegalStateException", NULL);
@@ -355,8 +359,10 @@ android_media_MediaPlayer2_handleDataSourceFD(
        return;
    }
    int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
    ALOGV("handleDataSourceFD: srcId=%lld, fd=%d (%s), offset=%lld, length=%lld",
          (long long)srcId, fd, nameForFd(fd).c_str(), (long long)offset, (long long)length);
    ALOGV("handleDataSourceFD: srcId=%lld, fd=%d (%s), offset=%lld, length=%lld, "
          "start=%lld, end=%lld",
          (long long)srcId, fd, nameForFd(fd).c_str(), (long long)offset, (long long)length,
          (long long)startPos, (long long)endPos);

    struct stat sb;
    int ret = fstat(fd, &sb);
@@ -389,6 +395,8 @@ android_media_MediaPlayer2_handleDataSourceFD(
    dsd->mFD = fd;
    dsd->mFDOffset = offset;
    dsd->mFDLength = length;
    dsd->mStartPositionMs = startPos;
    dsd->mEndPositionMs = endPos;

    status_t err;
    if (isCurrent) {
@@ -402,7 +410,8 @@ android_media_MediaPlayer2_handleDataSourceFD(

static void
android_media_MediaPlayer2_handleDataSourceCallback(
    JNIEnv *env, jobject thiz, jboolean isCurrent, jlong srcId, jobject dataSource)
    JNIEnv *env, jobject thiz, jboolean isCurrent, jlong srcId, jobject dataSource,
    jlong startPos, jlong endPos)
{
    sp<MediaPlayer2> mp = getMediaPlayer(env, thiz);
    if (mp == NULL ) {
@@ -419,6 +428,8 @@ android_media_MediaPlayer2_handleDataSourceCallback(
    dsd->mId = srcId;
    dsd->mType = DataSourceDesc::TYPE_CALLBACK;
    dsd->mCallbackSource = callbackDataSource;
    dsd->mStartPositionMs = startPos;
    dsd->mEndPositionMs = endPos;

    status_t err;
    if (isCurrent) {
@@ -1442,17 +1453,17 @@ static const JNINativeMethod gMethods[] = {
    {
        "nativeHandleDataSourceUrl",
        "(ZJLandroid/media/Media2HTTPService;Ljava/lang/String;[Ljava/lang/String;"
        "[Ljava/lang/String;)V",
        "[Ljava/lang/String;JJ)V",
        (void *)android_media_MediaPlayer2_handleDataSourceUrl
    },
    {
        "nativeHandleDataSourceFD",
        "(ZJLjava/io/FileDescriptor;JJ)V",
        "(ZJLjava/io/FileDescriptor;JJJJ)V",
        (void *)android_media_MediaPlayer2_handleDataSourceFD
    },
    {
        "nativeHandleDataSourceCallback",
        "(ZJLandroid/media/Media2DataSource;)V",
        "(ZJLandroid/media/Media2DataSource;JJ)V",
        (void *)android_media_MediaPlayer2_handleDataSourceCallback
    },
    {"nativePlayNextDataSource", "(J)V",                        (void *)android_media_MediaPlayer2_playNextDataSource},