Search in sources :

Example 6 with MediaFormat

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

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

use of android.media.MediaFormat in project platform_frameworks_base by android.

the class MediaPlayer method addSubtitleSource.

/** @hide */
public void addSubtitleSource(InputStream is, MediaFormat format) throws IllegalStateException {
    final InputStream fIs = is;
    final MediaFormat fFormat = format;
    if (is != null) {
        // way to implement timeouts in the future.
        synchronized (mOpenSubtitleSources) {
            mOpenSubtitleSources.add(is);
        }
    } else {
        Log.w(TAG, "addSubtitleSource called with null InputStream");
    }
    getMediaTimeProvider();
    // process each subtitle in its own thread
    final HandlerThread thread = new HandlerThread("SubtitleReadThread", Process.THREAD_PRIORITY_BACKGROUND + Process.THREAD_PRIORITY_MORE_FAVORABLE);
    thread.start();
    Handler handler = new Handler(thread.getLooper());
    handler.post(new Runnable() {

        private int addTrack() {
            if (fIs == null || mSubtitleController == null) {
                return MEDIA_INFO_UNSUPPORTED_SUBTITLE;
            }
            SubtitleTrack track = mSubtitleController.addTrack(fFormat);
            if (track == null) {
                return MEDIA_INFO_UNSUPPORTED_SUBTITLE;
            }
            // TODO: do the conversion in the subtitle track
            Scanner scanner = new Scanner(fIs, "UTF-8");
            String contents = scanner.useDelimiter("\\A").next();
            synchronized (mOpenSubtitleSources) {
                mOpenSubtitleSources.remove(fIs);
            }
            scanner.close();
            synchronized (mIndexTrackPairs) {
                mIndexTrackPairs.add(Pair.<Integer, SubtitleTrack>create(null, track));
            }
            Handler h = mTimeProvider.mEventHandler;
            int what = TimeProvider.NOTIFY;
            int arg1 = TimeProvider.NOTIFY_TRACK_DATA;
            Pair<SubtitleTrack, byte[]> trackData = Pair.create(track, contents.getBytes());
            Message m = h.obtainMessage(what, arg1, 0, trackData);
            h.sendMessage(m);
            return MEDIA_INFO_EXTERNAL_METADATA_UPDATE;
        }

        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) Scanner(java.util.Scanner) HandlerThread(android.os.HandlerThread) Message(android.os.Message) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Runnable(java.lang.Runnable) Handler(android.os.Handler) Pair(android.util.Pair)

Example 8 with MediaFormat

use of android.media.MediaFormat in project platform_frameworks_base by android.

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

use of android.media.MediaFormat in project ExoPlayer by google.

the class FormatTest method testConversionToFrameworkMediaFormatV16.

@SuppressLint("InlinedApi")
@TargetApi(16)
private static void testConversionToFrameworkMediaFormatV16(Format in) {
    MediaFormat out = in.getFrameworkMediaFormatV16();
    assertEquals(in.sampleMimeType, out.getString(MediaFormat.KEY_MIME));
    assertOptionalV16(out, MediaFormat.KEY_LANGUAGE, in.language);
    assertOptionalV16(out, MediaFormat.KEY_MAX_INPUT_SIZE, in.maxInputSize);
    assertOptionalV16(out, MediaFormat.KEY_WIDTH, in.width);
    assertOptionalV16(out, MediaFormat.KEY_HEIGHT, in.height);
    assertOptionalV16(out, MediaFormat.KEY_CHANNEL_COUNT, in.channelCount);
    assertOptionalV16(out, MediaFormat.KEY_SAMPLE_RATE, in.sampleRate);
    assertOptionalV16(out, MediaFormat.KEY_FRAME_RATE, in.frameRate);
    for (int i = 0; i < in.initializationData.size(); i++) {
        byte[] originalData = in.initializationData.get(i);
        ByteBuffer frameworkBuffer = out.getByteBuffer("csd-" + i);
        byte[] frameworkData = Arrays.copyOf(frameworkBuffer.array(), frameworkBuffer.limit());
        assertTrue(Arrays.equals(originalData, frameworkData));
    }
}
Also used : MediaFormat(android.media.MediaFormat) ByteBuffer(java.nio.ByteBuffer) SuppressLint(android.annotation.SuppressLint) SuppressLint(android.annotation.SuppressLint) TargetApi(android.annotation.TargetApi)

Example 10 with MediaFormat

use of android.media.MediaFormat in project ExoPlayer by google.

the class MediaCodecAudioRenderer method onOutputFormatChanged.

@Override
protected void onOutputFormatChanged(MediaCodec codec, MediaFormat outputFormat) throws ExoPlaybackException {
    boolean passthrough = passthroughMediaFormat != null;
    String mimeType = passthrough ? passthroughMediaFormat.getString(MediaFormat.KEY_MIME) : MimeTypes.AUDIO_RAW;
    MediaFormat format = passthrough ? passthroughMediaFormat : outputFormat;
    int channelCount = format.getInteger(MediaFormat.KEY_CHANNEL_COUNT);
    int sampleRate = format.getInteger(MediaFormat.KEY_SAMPLE_RATE);
    int[] channelMap;
    if (codecNeedsDiscardChannelsWorkaround && channelCount == 6 && this.channelCount < 6) {
        channelMap = new int[this.channelCount];
        for (int i = 0; i < this.channelCount; i++) {
            channelMap[i] = i;
        }
    } else {
        channelMap = null;
    }
    try {
        audioTrack.configure(mimeType, channelCount, sampleRate, pcmEncoding, 0, channelMap);
    } catch (AudioTrack.ConfigurationException e) {
        throw ExoPlaybackException.createForRenderer(e, getIndex());
    }
}
Also used : MediaFormat(android.media.MediaFormat)

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