Search in sources :

Example 1 with MediaFormat

use of android.media.MediaFormat in project android_frameworks_base by ResurrectionRemix.

the class MediaPlayer method addTimedTextSource.

/**
     * Adds an external timed text file (FileDescriptor).
     *
     * It is the caller's responsibility to close the file descriptor.
     * It is safe to do so as soon as this call returns.
     *
     * Currently supported format is SubRip. Note that a single external timed text source may
     * contain multiple tracks in it. One can find the total number of available tracks
     * using {@link #getTrackInfo()} to see what additional tracks become available
     * after this method call.
     *
     * @param fd the FileDescriptor for the file you want to play
     * @param offset the offset into the file where the data to be played starts, in bytes
     * @param length the length in bytes of the data to be played
     * @param mime The mime type of the file. Must be one of the mime types listed above.
     * @throws IllegalArgumentException if the mimeType is not supported.
     * @throws IllegalStateException if called in an invalid state.
     */
public void addTimedTextSource(FileDescriptor fd, long offset, long length, String mime) throws IllegalArgumentException, IllegalStateException {
    if (!availableMimeTypeForExternalSource(mime)) {
        throw new IllegalArgumentException("Illegal mimeType for timed text source: " + mime);
    }
    final FileDescriptor dupedFd;
    try {
        dupedFd = Libcore.os.dup(fd);
    } catch (ErrnoException ex) {
        Log.e(TAG, ex.getMessage(), ex);
        throw new RuntimeException(ex);
    }
    final MediaFormat fFormat = new MediaFormat();
    fFormat.setString(MediaFormat.KEY_MIME, mime);
    fFormat.setInteger(MediaFormat.KEY_IS_TIMED_TEXT, 1);
    // A MediaPlayer created by a VideoView should already have its mSubtitleController set.
    if (mSubtitleController == null) {
        setSubtitleAnchor();
    }
    if (!mSubtitleController.hasRendererFor(fFormat)) {
        // test and add not atomic
        Context context = ActivityThread.currentApplication();
        mSubtitleController.registerRenderer(new SRTRenderer(context, mEventHandler));
    }
    final SubtitleTrack track = mSubtitleController.addTrack(fFormat);
    synchronized (mIndexTrackPairs) {
        mIndexTrackPairs.add(Pair.<Integer, SubtitleTrack>create(null, track));
    }
    getMediaTimeProvider();
    final long offset2 = offset;
    final long length2 = length;
    final HandlerThread thread = new HandlerThread("TimedTextReadThread", Process.THREAD_PRIORITY_BACKGROUND + Process.THREAD_PRIORITY_MORE_FAVORABLE);
    thread.start();
    Handler handler = new Handler(thread.getLooper());
    handler.post(new Runnable() {

        private int addTrack() {
            final ByteArrayOutputStream bos = new ByteArrayOutputStream();
            try {
                Libcore.os.lseek(dupedFd, offset2, OsConstants.SEEK_SET);
                byte[] buffer = new byte[4096];
                for (long total = 0; total < length2; ) {
                    int bytesToRead = (int) Math.min(buffer.length, length2 - total);
                    int bytes = IoBridge.read(dupedFd, buffer, 0, bytesToRead);
                    if (bytes < 0) {
                        break;
                    } else {
                        bos.write(buffer, 0, bytes);
                        total += bytes;
                    }
                }
                Handler h = mTimeProvider.mEventHandler;
                int what = TimeProvider.NOTIFY;
                int arg1 = TimeProvider.NOTIFY_TRACK_DATA;
                Pair<SubtitleTrack, byte[]> trackData = Pair.create(track, bos.toByteArray());
                Message m = h.obtainMessage(what, arg1, 0, trackData);
                h.sendMessage(m);
                return MEDIA_INFO_EXTERNAL_METADATA_UPDATE;
            } catch (Exception e) {
                Log.e(TAG, e.getMessage(), e);
                return MEDIA_INFO_TIMED_TEXT_ERROR;
            } finally {
                try {
                    Libcore.os.close(dupedFd);
                } catch (ErrnoException e) {
                    Log.e(TAG, e.getMessage(), e);
                }
            }
        }

        public void run() {
            int res = addTrack();
            if (mEventHandler != null) {
                Message m = mEventHandler.obtainMessage(MEDIA_INFO, res, 0, null);
                mEventHandler.sendMessage(m);
            }
            thread.getLooper().quitSafely();
        }
    });
}
Also used : MediaFormat(android.media.MediaFormat) Context(android.content.Context) Message(android.os.Message) Handler(android.os.Handler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AssetFileDescriptor(android.content.res.AssetFileDescriptor) FileDescriptor(java.io.FileDescriptor) ErrnoException(android.system.ErrnoException) IOException(java.io.IOException) ErrnoException(android.system.ErrnoException) HandlerThread(android.os.HandlerThread) Runnable(java.lang.Runnable) Pair(android.util.Pair)

Example 2 with MediaFormat

use of android.media.MediaFormat in project android_frameworks_base by ResurrectionRemix.

the class VideoView method openVideo.

private void openVideo() {
    if (mUri == null || mSurfaceHolder == null) {
        // not ready for playback just yet, will try again later
        return;
    }
    // we shouldn't clear the target state, because somebody might have
    // called start() previously
    release(false);
    AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
    am.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    try {
        mMediaPlayer = new MediaPlayer();
        // TODO: create SubtitleController in MediaPlayer, but we need
        // a context for the subtitle renderers
        final Context context = getContext();
        final SubtitleController controller = new SubtitleController(context, mMediaPlayer.getMediaTimeProvider(), mMediaPlayer);
        controller.registerRenderer(new WebVttRenderer(context));
        controller.registerRenderer(new TtmlRenderer(context));
        controller.registerRenderer(new Cea708CaptionRenderer(context));
        controller.registerRenderer(new ClosedCaptionRenderer(context));
        mMediaPlayer.setSubtitleAnchor(controller, this);
        if (mAudioSession != 0) {
            mMediaPlayer.setAudioSessionId(mAudioSession);
        } else {
            mAudioSession = mMediaPlayer.getAudioSessionId();
        }
        mMediaPlayer.setOnPreparedListener(mPreparedListener);
        mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener);
        mMediaPlayer.setOnCompletionListener(mCompletionListener);
        mMediaPlayer.setOnErrorListener(mErrorListener);
        mMediaPlayer.setOnInfoListener(mInfoListener);
        mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);
        mCurrentBufferPercentage = 0;
        mMediaPlayer.setDataSource(mContext, mUri, mHeaders);
        mMediaPlayer.setDisplay(mSurfaceHolder);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaPlayer.setScreenOnWhilePlaying(true);
        mMediaPlayer.prepareAsync();
        for (Pair<InputStream, MediaFormat> pending : mPendingSubtitleTracks) {
            try {
                mMediaPlayer.addSubtitleSource(pending.first, pending.second);
            } catch (IllegalStateException e) {
                mInfoListener.onInfo(mMediaPlayer, MediaPlayer.MEDIA_INFO_UNSUPPORTED_SUBTITLE, 0);
            }
        }
        // we don't set the target state here either, but preserve the
        // target state that was there before.
        mCurrentState = STATE_PREPARING;
        attachMediaController();
    } catch (IOException ex) {
        Log.w(TAG, "Unable to open content: " + mUri, ex);
        mCurrentState = STATE_ERROR;
        mTargetState = STATE_ERROR;
        mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        return;
    } catch (IllegalArgumentException ex) {
        Log.w(TAG, "Unable to open content: " + mUri, ex);
        mCurrentState = STATE_ERROR;
        mTargetState = STATE_ERROR;
        mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        return;
    } finally {
        mPendingSubtitleTracks.clear();
    }
}
Also used : Context(android.content.Context) MediaFormat(android.media.MediaFormat) InputStream(java.io.InputStream) TtmlRenderer(android.media.TtmlRenderer) IOException(java.io.IOException) WebVttRenderer(android.media.WebVttRenderer) AudioManager(android.media.AudioManager) SubtitleController(android.media.SubtitleController) Cea708CaptionRenderer(android.media.Cea708CaptionRenderer) ClosedCaptionRenderer(android.media.ClosedCaptionRenderer) MediaPlayer(android.media.MediaPlayer)

Example 3 with MediaFormat

use of android.media.MediaFormat in project android_frameworks_base by ResurrectionRemix.

the class MediaDecoder method onStart.

private void onStart() throws Exception {
    if (mOpenGLEnabled) {
        getRenderTarget().focus();
    }
    mMediaExtractor = new MediaExtractor();
    mMediaExtractor.setDataSource(mContext, mUri, null);
    mVideoTrackIndex = -1;
    mAudioTrackIndex = -1;
    for (int i = 0; i < mMediaExtractor.getTrackCount(); i++) {
        MediaFormat format = mMediaExtractor.getTrackFormat(i);
        if (DEBUG) {
            Log.i(LOG_TAG, "Uri " + mUri + ", track " + i + ": " + format);
        }
        if (DecoderUtil.isVideoFormat(format) && mVideoTrackIndex == -1) {
            mVideoTrackIndex = i;
        } else if (DecoderUtil.isAudioFormat(format) && mAudioTrackIndex == -1) {
            mAudioTrackIndex = i;
        }
    }
    if (mVideoTrackIndex == -1 && mAudioTrackIndex == -1) {
        throw new IllegalArgumentException("Couldn't find a video or audio track in the provided file");
    }
    if (mVideoTrackIndex != -1) {
        MediaFormat videoFormat = mMediaExtractor.getTrackFormat(mVideoTrackIndex);
        mVideoTrackDecoder = mOpenGLEnabled ? new GpuVideoTrackDecoder(mVideoTrackIndex, videoFormat, this) : new CpuVideoTrackDecoder(mVideoTrackIndex, videoFormat, this);
        mVideoTrackDecoder.init();
        mMediaExtractor.selectTrack(mVideoTrackIndex);
        if (Build.VERSION.SDK_INT >= 17) {
            retrieveDefaultRotation();
        }
    }
    if (mAudioTrackIndex != -1) {
        MediaFormat audioFormat = mMediaExtractor.getTrackFormat(mAudioTrackIndex);
        mAudioTrackDecoder = new AudioTrackDecoder(mAudioTrackIndex, audioFormat, this);
        mAudioTrackDecoder.init();
        mMediaExtractor.selectTrack(mAudioTrackIndex);
    }
    if (mStartMicros > 0) {
        mMediaExtractor.seekTo(mStartMicros, MediaExtractor.SEEK_TO_PREVIOUS_SYNC);
    }
    mStarted = true;
    mListener.onDecodingStarted();
}
Also used : MediaFormat(android.media.MediaFormat) MediaExtractor(android.media.MediaExtractor)

Example 4 with MediaFormat

use of android.media.MediaFormat in project android_frameworks_base by DirtyUnicorns.

the class MediaPlayer method addTimedTextSource.

/**
     * Adds an external timed text file (FileDescriptor).
     *
     * It is the caller's responsibility to close the file descriptor.
     * It is safe to do so as soon as this call returns.
     *
     * Currently supported format is SubRip. Note that a single external timed text source may
     * contain multiple tracks in it. One can find the total number of available tracks
     * using {@link #getTrackInfo()} to see what additional tracks become available
     * after this method call.
     *
     * @param fd the FileDescriptor for the file you want to play
     * @param offset the offset into the file where the data to be played starts, in bytes
     * @param length the length in bytes of the data to be played
     * @param mime The mime type of the file. Must be one of the mime types listed above.
     * @throws IllegalArgumentException if the mimeType is not supported.
     * @throws IllegalStateException if called in an invalid state.
     */
public void addTimedTextSource(FileDescriptor fd, long offset, long length, String mime) throws IllegalArgumentException, IllegalStateException {
    if (!availableMimeTypeForExternalSource(mime)) {
        throw new IllegalArgumentException("Illegal mimeType for timed text source: " + mime);
    }
    final FileDescriptor dupedFd;
    try {
        dupedFd = Libcore.os.dup(fd);
    } catch (ErrnoException ex) {
        Log.e(TAG, ex.getMessage(), ex);
        throw new RuntimeException(ex);
    }
    final MediaFormat fFormat = new MediaFormat();
    fFormat.setString(MediaFormat.KEY_MIME, mime);
    fFormat.setInteger(MediaFormat.KEY_IS_TIMED_TEXT, 1);
    // A MediaPlayer created by a VideoView should already have its mSubtitleController set.
    if (mSubtitleController == null) {
        setSubtitleAnchor();
    }
    if (!mSubtitleController.hasRendererFor(fFormat)) {
        // test and add not atomic
        Context context = ActivityThread.currentApplication();
        mSubtitleController.registerRenderer(new SRTRenderer(context, mEventHandler));
    }
    final SubtitleTrack track = mSubtitleController.addTrack(fFormat);
    synchronized (mIndexTrackPairs) {
        mIndexTrackPairs.add(Pair.<Integer, SubtitleTrack>create(null, track));
    }
    getMediaTimeProvider();
    final long offset2 = offset;
    final long length2 = length;
    final HandlerThread thread = new HandlerThread("TimedTextReadThread", Process.THREAD_PRIORITY_BACKGROUND + Process.THREAD_PRIORITY_MORE_FAVORABLE);
    thread.start();
    Handler handler = new Handler(thread.getLooper());
    handler.post(new Runnable() {

        private int addTrack() {
            final ByteArrayOutputStream bos = new ByteArrayOutputStream();
            try {
                Libcore.os.lseek(dupedFd, offset2, OsConstants.SEEK_SET);
                byte[] buffer = new byte[4096];
                for (long total = 0; total < length2; ) {
                    int bytesToRead = (int) Math.min(buffer.length, length2 - total);
                    int bytes = IoBridge.read(dupedFd, buffer, 0, bytesToRead);
                    if (bytes < 0) {
                        break;
                    } else {
                        bos.write(buffer, 0, bytes);
                        total += bytes;
                    }
                }
                Handler h = mTimeProvider.mEventHandler;
                int what = TimeProvider.NOTIFY;
                int arg1 = TimeProvider.NOTIFY_TRACK_DATA;
                Pair<SubtitleTrack, byte[]> trackData = Pair.create(track, bos.toByteArray());
                Message m = h.obtainMessage(what, arg1, 0, trackData);
                h.sendMessage(m);
                return MEDIA_INFO_EXTERNAL_METADATA_UPDATE;
            } catch (Exception e) {
                Log.e(TAG, e.getMessage(), e);
                return MEDIA_INFO_TIMED_TEXT_ERROR;
            } finally {
                try {
                    Libcore.os.close(dupedFd);
                } catch (ErrnoException e) {
                    Log.e(TAG, e.getMessage(), e);
                }
            }
        }

        public void run() {
            int res = addTrack();
            if (mEventHandler != null) {
                Message m = mEventHandler.obtainMessage(MEDIA_INFO, res, 0, null);
                mEventHandler.sendMessage(m);
            }
            thread.getLooper().quitSafely();
        }
    });
}
Also used : MediaFormat(android.media.MediaFormat) Context(android.content.Context) Message(android.os.Message) Handler(android.os.Handler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AssetFileDescriptor(android.content.res.AssetFileDescriptor) FileDescriptor(java.io.FileDescriptor) ErrnoException(android.system.ErrnoException) IOException(java.io.IOException) ErrnoException(android.system.ErrnoException) HandlerThread(android.os.HandlerThread) Runnable(java.lang.Runnable) Pair(android.util.Pair)

Example 5 with MediaFormat

use of android.media.MediaFormat in project android_frameworks_base by DirtyUnicorns.

the class MediaDecoder method onStart.

private void onStart() throws Exception {
    if (mOpenGLEnabled) {
        getRenderTarget().focus();
    }
    mMediaExtractor = new MediaExtractor();
    mMediaExtractor.setDataSource(mContext, mUri, null);
    mVideoTrackIndex = -1;
    mAudioTrackIndex = -1;
    for (int i = 0; i < mMediaExtractor.getTrackCount(); i++) {
        MediaFormat format = mMediaExtractor.getTrackFormat(i);
        if (DEBUG) {
            Log.i(LOG_TAG, "Uri " + mUri + ", track " + i + ": " + format);
        }
        if (DecoderUtil.isVideoFormat(format) && mVideoTrackIndex == -1) {
            mVideoTrackIndex = i;
        } else if (DecoderUtil.isAudioFormat(format) && mAudioTrackIndex == -1) {
            mAudioTrackIndex = i;
        }
    }
    if (mVideoTrackIndex == -1 && mAudioTrackIndex == -1) {
        throw new IllegalArgumentException("Couldn't find a video or audio track in the provided file");
    }
    if (mVideoTrackIndex != -1) {
        MediaFormat videoFormat = mMediaExtractor.getTrackFormat(mVideoTrackIndex);
        mVideoTrackDecoder = mOpenGLEnabled ? new GpuVideoTrackDecoder(mVideoTrackIndex, videoFormat, this) : new CpuVideoTrackDecoder(mVideoTrackIndex, videoFormat, this);
        mVideoTrackDecoder.init();
        mMediaExtractor.selectTrack(mVideoTrackIndex);
        if (Build.VERSION.SDK_INT >= 17) {
            retrieveDefaultRotation();
        }
    }
    if (mAudioTrackIndex != -1) {
        MediaFormat audioFormat = mMediaExtractor.getTrackFormat(mAudioTrackIndex);
        mAudioTrackDecoder = new AudioTrackDecoder(mAudioTrackIndex, audioFormat, this);
        mAudioTrackDecoder.init();
        mMediaExtractor.selectTrack(mAudioTrackIndex);
    }
    if (mStartMicros > 0) {
        mMediaExtractor.seekTo(mStartMicros, MediaExtractor.SEEK_TO_PREVIOUS_SYNC);
    }
    mStarted = true;
    mListener.onDecodingStarted();
}
Also used : MediaFormat(android.media.MediaFormat) MediaExtractor(android.media.MediaExtractor)

Aggregations

MediaFormat (android.media.MediaFormat)130 IOException (java.io.IOException)29 MediaCodec (android.media.MediaCodec)24 ByteBuffer (java.nio.ByteBuffer)22 MediaExtractor (android.media.MediaExtractor)18 SuppressLint (android.annotation.SuppressLint)16 Handler (android.os.Handler)15 Test (org.junit.Test)14 TargetApi (android.annotation.TargetApi)13 InputStream (java.io.InputStream)12 Context (android.content.Context)10 HandlerThread (android.os.HandlerThread)10 Message (android.os.Message)10 Pair (android.util.Pair)10 Format (com.google.android.exoplayer2.Format)10 Runnable (java.lang.Runnable)10 Nullable (androidx.annotation.Nullable)9 File (java.io.File)9 BufferInfo (android.media.MediaCodec.BufferInfo)8 Surface (android.view.Surface)8