Search in sources :

Example 31 with MediaExtractor

use of android.media.MediaExtractor in project robolectric by robolectric.

the class ShadowMediaExtractorTest method setDataSource_emptyTracksWhenNotAdded.

@Test
public void setDataSource_emptyTracksWhenNotAdded() throws IOException {
    // Note: no data source data has been set with ShadowMediaExtractor.addTrack().
    MediaExtractor mediaExtractor = new MediaExtractor();
    mediaExtractor.setDataSource(path);
    assertThat(mediaExtractor.getTrackCount()).isEqualTo(0);
}
Also used : MediaExtractor(android.media.MediaExtractor) Test(org.junit.Test)

Example 32 with MediaExtractor

use of android.media.MediaExtractor in project Pix-Art-Messenger by kriztan.

the class MediaTranscoderEngine method transcodeVideo.

/**
 * Run video transcoding. Blocks current thread.
 * Audio data will not be transcoded; original stream will be wrote to output file.
 *
 * @param outputPath     File path to output transcoded video file.
 * @param formatStrategy Output format strategy.
 * @throws IOException                  when input or output file could not be opened.
 * @throws InvalidOutputFormatException when output format is not supported.
 * @throws InterruptedException         when cancel to transcode.
 */
public void transcodeVideo(String outputPath, MediaFormatStrategy formatStrategy) throws IOException, InterruptedException {
    if (outputPath == null) {
        throw new NullPointerException("Output path cannot be null.");
    }
    if (mInputFileDescriptor == null) {
        throw new IllegalStateException("Data source is not set.");
    }
    try {
        // NOTE: use single extractor to keep from running out audio track fast.
        mExtractor = new MediaExtractor();
        mExtractor.setDataSource(mInputFileDescriptor);
        mMuxer = new MediaMuxer(outputPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
        setupMetadata();
        setupTrackTranscoders(formatStrategy);
        runPipelines();
        mMuxer.stop();
    } finally {
        try {
            if (mVideoTrackTranscoder != null) {
                mVideoTrackTranscoder.release();
                mVideoTrackTranscoder = null;
            }
            if (mAudioTrackTranscoder != null) {
                mAudioTrackTranscoder.release();
                mAudioTrackTranscoder = null;
            }
            if (mExtractor != null) {
                mExtractor.release();
                mExtractor = null;
            }
        } catch (RuntimeException e) {
            // noinspection ThrowFromFinallyBlock
            throw new Error("Could not shutdown extractor, codecs and muxer pipeline.", e);
        }
        try {
            if (mMuxer != null) {
                mMuxer.release();
                mMuxer = null;
            }
        } catch (RuntimeException e) {
            Log.e(TAG, "Failed to release muxer.", e);
        }
    }
}
Also used : MediaExtractor(android.media.MediaExtractor) MediaMuxer(android.media.MediaMuxer)

Example 33 with MediaExtractor

use of android.media.MediaExtractor in project LanSoEditor_common by LanSoSdk.

the class AudioEncodeDecode method decodeAudio.

/**
 * 把原始音频流,包括MP3 或aac解码成裸码流保存起来, 小端模式
 * 中间可以读取各种音频信息,但没有读取.
 * 一般播放采用ffplay -f s16le -ar 44.1k -ac 2 hong222.pcm
 * @param srcAudioPath
 * @param dstAudioPcmPath
 * @return
 */
public static int decodeAudio(String srcAudioPath, String dstAudioPcmPath) {
    // int mChannels;
    // int mNumSamples;
    // int mSampleRate;
    // int mNumFrames;
    File tmpFile = new File(srcAudioPath);
    if (tmpFile.exists() == false)
        return -1;
    FileOutputStream fos = null;
    BufferedOutputStream bos = null;
    try {
        fos = new FileOutputStream(dstAudioPcmPath);
        bos = new BufferedOutputStream(fos);
    } catch (IOException ioe) {
        Log.e("lansongeditor", ioe.toString());
        return -1;
    }
    MediaExtractor extractor = new MediaExtractor();
    MediaFormat format = null;
    int i;
    try {
        extractor.setDataSource(srcAudioPath);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    int numTracks = extractor.getTrackCount();
    for (i = 0; i < numTracks; i++) {
        format = extractor.getTrackFormat(i);
        if (format.getString(MediaFormat.KEY_MIME).startsWith("audio/")) {
            extractor.selectTrack(i);
            break;
        }
    }
    if (i == numTracks) {
        extractor.release();
        Log.e("lansongedit", "No audio track found in " + srcAudioPath);
        try {
            bos.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            Log.e("lansongeditor", e1.toString());
            return -1;
        }
        return -1;
    }
    MediaCodec codec;
    try {
        codec = MediaCodec.createDecoderByType(format.getString(MediaFormat.KEY_MIME));
        codec.configure(format, null, null, 0);
        codec.start();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        // e.printStackTrace();
        Log.e("lansongedit", "No audio track found in " + srcAudioPath);
        if (extractor != null)
            extractor.release();
        try {
            bos.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            Log.e("lansongeditor", e1.toString());
            return -1;
        }
        return -1;
    }
    // size of the output buffer containing decoded samples.
    int decodedSamplesSize = 0;
    byte[] decodedSamples = null;
    ByteBuffer[] inputBuffers = codec.getInputBuffers();
    ByteBuffer[] outputBuffers = codec.getOutputBuffers();
    int sample_size;
    MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
    long presentation_time;
    boolean done_reading = false;
    // Set the size of the decoded samples buffer to 1MB (~6sec of a stereo stream at 44.1kHz).
    // For longer streams, the buffer size will be increased later on, calculating a rough
    // estimate of the total size needed to store all the samples in order to resize the buffer
    // only once.
    Boolean firstSampleData = true;
    while (true) {
        // read data from file and feed it to the decoder input buffers.
        int inputBufferIndex = codec.dequeueInputBuffer(100);
        if (!done_reading && inputBufferIndex >= 0) {
            sample_size = extractor.readSampleData(inputBuffers[inputBufferIndex], 0);
            if (firstSampleData && format.getString(MediaFormat.KEY_MIME).equals("audio/mp4a-latm") && sample_size == 2) {
                // For some reasons on some devices (e.g. the Samsung S3) you should not
                // provide the first two bytes of an AAC stream, otherwise the MediaCodec will
                // crash. These two bytes do not contain music data but basic info on the
                // stream (e.g. channel configuration and sampling frequency), and skipping them
                // seems OK with other devices (MediaCodec has already been configured and
                // already knows these parameters).
                extractor.advance();
            } else if (sample_size < 0) {
                // All samples have been read.
                codec.queueInputBuffer(inputBufferIndex, 0, 0, -1, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
                done_reading = true;
            } else {
                presentation_time = extractor.getSampleTime();
                codec.queueInputBuffer(inputBufferIndex, 0, sample_size, presentation_time, 0);
                extractor.advance();
            }
            firstSampleData = false;
        }
        // Get decoded stream from the decoder output buffers.
        int outputBufferIndex = codec.dequeueOutputBuffer(info, 100);
        if (outputBufferIndex >= 0 && info.size > 0) {
            if (decodedSamplesSize < info.size) {
                decodedSamplesSize = info.size;
                decodedSamples = new byte[decodedSamplesSize];
            }
            outputBuffers[outputBufferIndex].get(decodedSamples, 0, info.size);
            outputBuffers[outputBufferIndex].clear();
            try {
                bos.write(decodedSamples);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            codec.releaseOutputBuffer(outputBufferIndex, false);
        } else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
            outputBuffers = codec.getOutputBuffers();
        } else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
        // Subsequent data will conform to new format.
        // We could check that codec.getOutputFormat(), which is the new output format,
        // is what we expect.
        }
        if ((info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
            // lower pitch.)
            break;
        }
    }
    extractor.release();
    extractor = null;
    codec.stop();
    codec.release();
    codec = null;
    try {
        if (bos != null) {
            bos.close();
        }
        if (fos != null) {
            fos.close();
        }
    } catch (IOException ioe) {
        Log.e("lansongeditor", ioe.toString());
        return -1;
    }
    return 0;
}
Also used : MediaFormat(android.media.MediaFormat) IOException(java.io.IOException) MediaExtractor(android.media.MediaExtractor) ByteBuffer(java.nio.ByteBuffer) MediaCodec(android.media.MediaCodec) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 34 with MediaExtractor

use of android.media.MediaExtractor in project grafika by google.

the class MoviePlayer method play.

/**
 * Decodes the video stream, sending frames to the surface.
 * <p>
 * Does not return until video playback is complete, or we get a "stop" signal from
 * frameCallback.
 */
public void play() throws IOException {
    MediaExtractor extractor = null;
    MediaCodec decoder = null;
    // file exists so we can throw a better one if it's not there.
    if (!mSourceFile.canRead()) {
        throw new FileNotFoundException("Unable to read " + mSourceFile);
    }
    try {
        extractor = new MediaExtractor();
        extractor.setDataSource(mSourceFile.toString());
        int trackIndex = selectTrack(extractor);
        if (trackIndex < 0) {
            throw new RuntimeException("No video track found in " + mSourceFile);
        }
        extractor.selectTrack(trackIndex);
        MediaFormat format = extractor.getTrackFormat(trackIndex);
        // Create a MediaCodec decoder, and configure it with the MediaFormat from the
        // extractor.  It's very important to use the format from the extractor because
        // it contains a copy of the CSD-0/CSD-1 codec-specific data chunks.
        String mime = format.getString(MediaFormat.KEY_MIME);
        decoder = MediaCodec.createDecoderByType(mime);
        decoder.configure(format, mOutputSurface, null, 0);
        decoder.start();
        doExtract(extractor, trackIndex, decoder, mFrameCallback);
    } finally {
        // release everything we grabbed
        if (decoder != null) {
            decoder.stop();
            decoder.release();
            decoder = null;
        }
        if (extractor != null) {
            extractor.release();
            extractor = null;
        }
    }
}
Also used : MediaFormat(android.media.MediaFormat) MediaCodec(android.media.MediaCodec) FileNotFoundException(java.io.FileNotFoundException) MediaExtractor(android.media.MediaExtractor)

Example 35 with MediaExtractor

use of android.media.MediaExtractor in project Signal-Android by signalapp.

the class AudioTrackConverter method create.

@Nullable
static AudioTrackConverter create(@NonNull final MediaInput input, final long timeFrom, final long timeTo, final int audioBitrate) throws IOException {
    final MediaExtractor audioExtractor = input.createExtractor();
    final int audioInputTrack = getAndSelectAudioTrackIndex(audioExtractor);
    if (audioInputTrack == -1) {
        audioExtractor.release();
        return null;
    }
    return new AudioTrackConverter(audioExtractor, audioInputTrack, timeFrom, timeTo, audioBitrate);
}
Also used : MediaExtractor(android.media.MediaExtractor) Nullable(androidx.annotation.Nullable)

Aggregations

MediaExtractor (android.media.MediaExtractor)35 MediaFormat (android.media.MediaFormat)20 IOException (java.io.IOException)9 MediaCodec (android.media.MediaCodec)8 ByteBuffer (java.nio.ByteBuffer)8 Test (org.junit.Test)8 File (java.io.File)7 Size (android.util.Size)5 Nullable (androidx.annotation.Nullable)5 RequiresApi (androidx.annotation.RequiresApi)4 BufferInfo (android.media.MediaCodec.BufferInfo)2 MediaMuxer (android.media.MediaMuxer)2 NonNull (androidx.annotation.NonNull)2 WorkerThread (androidx.annotation.WorkerThread)2 ByteString (com.google.protobuf.ByteString)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DecryptableUriMediaInput (org.thoughtcrime.securesms.media.DecryptableUriMediaInput)2 MediaInput (org.thoughtcrime.securesms.media.MediaInput)2 AssetFileDescriptor (android.content.res.AssetFileDescriptor)1 MediaMetadataRetriever (android.media.MediaMetadataRetriever)1