Search in sources :

Example 61 with MediaFormat

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

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 62 with MediaFormat

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

the class DisplaySinkService method updateSurfaceFromUi.

private void updateSurfaceFromUi(SurfaceHolder holder) {
    Surface surface = null;
    int width = 0, height = 0;
    if (holder != null && !holder.isCreating()) {
        surface = holder.getSurface();
        if (surface.isValid()) {
            final Rect frame = holder.getSurfaceFrame();
            width = frame.width();
            height = frame.height();
        } else {
            surface = null;
        }
    }
    synchronized (mSurfaceAndCodecLock) {
        if (mSurface == surface && mSurfaceWidth == width && mSurfaceHeight == height) {
            return;
        }
        mSurface = surface;
        mSurfaceWidth = width;
        mSurfaceHeight = height;
        if (mCodec != null) {
            mCodec.stop();
            mCodec = null;
            mCodecInputBuffers = null;
            mCodecBufferInfo = null;
        }
        if (mSurface != null) {
            MediaFormat format = MediaFormat.createVideoFormat("video/avc", mSurfaceWidth, mSurfaceHeight);
            try {
                mCodec = MediaCodec.createDecoderByType("video/avc");
            } catch (IOException e) {
                throw new RuntimeException("failed to create video/avc decoder", e);
            }
            mCodec.configure(format, mSurface, null, 0);
            mCodec.start();
            mCodecBufferInfo = new BufferInfo();
        }
        mTransportHandler.post(new Runnable() {

            @Override
            public void run() {
                sendSinkStatus();
            }
        });
    }
}
Also used : MediaFormat(android.media.MediaFormat) Rect(android.graphics.Rect) BufferInfo(android.media.MediaCodec.BufferInfo) IOException(java.io.IOException) Surface(android.view.Surface)

Example 63 with MediaFormat

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

the class Camera2RecordingTest method validateRecording.

private void validateRecording(Size sz, int expectedDurationMs) throws Exception {
    File outFile = new File(mOutMediaFileName);
    assertTrue("No video is recorded", outFile.exists());
    MediaExtractor extractor = new MediaExtractor();
    try {
        extractor.setDataSource(mOutMediaFileName);
        long durationUs = 0;
        int width = -1, height = -1;
        int numTracks = extractor.getTrackCount();
        final String VIDEO_MIME_TYPE = "video";
        for (int i = 0; i < numTracks; i++) {
            MediaFormat format = extractor.getTrackFormat(i);
            String mime = format.getString(MediaFormat.KEY_MIME);
            if (mime.contains(VIDEO_MIME_TYPE)) {
                Log.i(TAG, "video format is: " + format.toString());
                durationUs = format.getLong(MediaFormat.KEY_DURATION);
                width = format.getInteger(MediaFormat.KEY_WIDTH);
                height = format.getInteger(MediaFormat.KEY_HEIGHT);
                break;
            }
        }
        Size videoSz = new Size(width, height);
        assertTrue("Video size doesn't match, expected " + sz.toString() + " got " + videoSz.toString(), videoSz.equals(sz));
        int duration = (int) (durationUs / 1000);
        if (VERBOSE) {
            Log.v(TAG, String.format("Video duration: recorded %dms, expected %dms", duration, expectedDurationMs));
        }
        // TODO: Don't skip this for video snapshot
        if (!mStaticInfo.isHardwareLevelLegacy()) {
            assertTrue(String.format("Camera %s: Video duration doesn't match: recorded %dms, expected %dms.", mCamera.getId(), duration, expectedDurationMs), Math.abs(duration - expectedDurationMs) < DURATION_MARGIN * expectedDurationMs);
        }
    } finally {
        extractor.release();
        if (!DEBUG_DUMP) {
            outFile.delete();
        }
    }
}
Also used : MediaFormat(android.media.MediaFormat) Size(android.util.Size) MediaExtractor(android.media.MediaExtractor) File(java.io.File)

Example 64 with MediaFormat

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

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 65 with MediaFormat

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

the class Camera2RecordingTest method validateRecording.

private void validateRecording(Size sz, int expectedDurationMs) throws Exception {
    File outFile = new File(mOutMediaFileName);
    assertTrue("No video is recorded", outFile.exists());
    MediaExtractor extractor = new MediaExtractor();
    try {
        extractor.setDataSource(mOutMediaFileName);
        long durationUs = 0;
        int width = -1, height = -1;
        int numTracks = extractor.getTrackCount();
        final String VIDEO_MIME_TYPE = "video";
        for (int i = 0; i < numTracks; i++) {
            MediaFormat format = extractor.getTrackFormat(i);
            String mime = format.getString(MediaFormat.KEY_MIME);
            if (mime.contains(VIDEO_MIME_TYPE)) {
                Log.i(TAG, "video format is: " + format.toString());
                durationUs = format.getLong(MediaFormat.KEY_DURATION);
                width = format.getInteger(MediaFormat.KEY_WIDTH);
                height = format.getInteger(MediaFormat.KEY_HEIGHT);
                break;
            }
        }
        Size videoSz = new Size(width, height);
        assertTrue("Video size doesn't match, expected " + sz.toString() + " got " + videoSz.toString(), videoSz.equals(sz));
        int duration = (int) (durationUs / 1000);
        if (VERBOSE) {
            Log.v(TAG, String.format("Video duration: recorded %dms, expected %dms", duration, expectedDurationMs));
        }
        // TODO: Don't skip this for video snapshot
        if (!mStaticInfo.isHardwareLevelLegacy()) {
            assertTrue(String.format("Camera %s: Video duration doesn't match: recorded %dms, expected %dms.", mCamera.getId(), duration, expectedDurationMs), Math.abs(duration - expectedDurationMs) < DURATION_MARGIN * expectedDurationMs);
        }
    } finally {
        extractor.release();
        if (!DEBUG_DUMP) {
            outFile.delete();
        }
    }
}
Also used : MediaFormat(android.media.MediaFormat) Size(android.util.Size) MediaExtractor(android.media.MediaExtractor) File(java.io.File)

Aggregations

MediaFormat (android.media.MediaFormat)87 IOException (java.io.IOException)27 ByteBuffer (java.nio.ByteBuffer)18 MediaExtractor (android.media.MediaExtractor)16 MediaCodec (android.media.MediaCodec)15 InputStream (java.io.InputStream)12 TargetApi (android.annotation.TargetApi)11 Context (android.content.Context)10 Handler (android.os.Handler)10 HandlerThread (android.os.HandlerThread)10 Message (android.os.Message)10 Pair (android.util.Pair)10 Runnable (java.lang.Runnable)10 SuppressLint (android.annotation.SuppressLint)9 File (java.io.File)8 BufferInfo (android.media.MediaCodec.BufferInfo)7 FileInputStream (java.io.FileInputStream)7 MediaPlayer (android.media.MediaPlayer)6 Surface (android.view.Surface)6 AssetFileDescriptor (android.content.res.AssetFileDescriptor)5