Search in sources :

Example 16 with MediaFormat

use of android.media.MediaFormat in project speechutils by Kaljurand.

the class MediaFormatFactory method createMediaFormat.

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static MediaFormat createMediaFormat(Type type, int sampleRate) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        MediaFormat format = new MediaFormat();
        // TODO: this causes a crash in MediaCodec.configure
        //format.setString(MediaFormat.KEY_FRAME_RATE, null);
        format.setInteger(MediaFormat.KEY_SAMPLE_RATE, sampleRate);
        format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
        if (type == Type.AAC) {
            format.setString(MediaFormat.KEY_MIME, "audio/mp4a-latm");
            // TODO: or 39?
            format.setInteger(MediaFormat.KEY_AAC_PROFILE, 2);
            format.setInteger(MediaFormat.KEY_BIT_RATE, 64000);
        } else if (type == Type.FLAC) {
            //format.setString(MediaFormat.KEY_MIME, MediaFormat.MIMETYPE_AUDIO_FLAC); // API=21
            format.setString(MediaFormat.KEY_MIME, "audio/flac");
            format.setInteger(MediaFormat.KEY_BIT_RATE, 64000);
        //TODO: use another bit rate, does not seem to have effect always
        //format.setInteger(MediaFormat.KEY_BIT_RATE, 128000);
        } else {
            format.setString(MediaFormat.KEY_MIME, "audio/amr-wb");
            format.setInteger(MediaFormat.KEY_BIT_RATE, 23050);
        }
        return format;
    }
    return null;
}
Also used : MediaFormat(android.media.MediaFormat) TargetApi(android.annotation.TargetApi)

Example 17 with MediaFormat

use of android.media.MediaFormat in project speechutils by Kaljurand.

the class EncodedAudioRecorder method recorderLoop.

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void recorderLoop(SpeechRecord speechRecord) {
    mNumBytesSubmitted = 0;
    mNumBytesDequeued = 0;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        MediaFormat format = MediaFormatFactory.createMediaFormat(MediaFormatFactory.Type.FLAC, getSampleRate());
        List<String> componentNames = AudioUtils.getEncoderNamesForType(format.getString(MediaFormat.KEY_MIME));
        for (String componentName : componentNames) {
            Log.i("component/format: " + componentName + "/" + format);
            MediaCodec codec = AudioUtils.createCodec(componentName, format);
            if (codec != null) {
                recorderEncoderLoop(codec, speechRecord);
                if (Log.DEBUG) {
                    AudioUtils.showMetrics(format, mNumBytesSubmitted, mNumBytesDequeued);
                }
                // TODO: we use the first one that is suitable
                break;
            }
        }
    }
}
Also used : MediaFormat(android.media.MediaFormat) MediaCodec(android.media.MediaCodec) TargetApi(android.annotation.TargetApi)

Example 18 with MediaFormat

use of android.media.MediaFormat in project speechutils by Kaljurand.

the class AudioUtils method getAvailableEncoders.

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static List<String> getAvailableEncoders(int sampleRate) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        MediaFormat format = MediaFormatFactory.createMediaFormat(MediaFormatFactory.Type.FLAC, sampleRate);
        MediaCodecList mcl = new MediaCodecList(MediaCodecList.REGULAR_CODECS);
        String encoderAsStr = mcl.findEncoderForFormat(format);
        List<String> encoders = new ArrayList<>();
        for (MediaCodecInfo info : mcl.getCodecInfos()) {
            if (info.isEncoder()) {
                if (info.getName().equals(encoderAsStr)) {
                    encoders.add("*** " + info.getName() + ": " + TextUtils.join(", ", info.getSupportedTypes()));
                } else {
                    encoders.add(info.getName() + ": " + TextUtils.join(", ", info.getSupportedTypes()));
                }
            }
        }
        return encoders;
    }
    return Collections.emptyList();
}
Also used : MediaFormat(android.media.MediaFormat) MediaCodecInfo(android.media.MediaCodecInfo) MediaCodecList(android.media.MediaCodecList) ArrayList(java.util.ArrayList) TargetApi(android.annotation.TargetApi)

Example 19 with MediaFormat

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

the class AudioCodec method createMediaCodec.

private MediaCodec createMediaCodec(int bufferSize) throws IOException {
    MediaCodec mediaCodec = MediaCodec.createEncoderByType("audio/mp4a-latm");
    MediaFormat mediaFormat = new MediaFormat();
    mediaFormat.setString(MediaFormat.KEY_MIME, "audio/mp4a-latm");
    mediaFormat.setInteger(MediaFormat.KEY_SAMPLE_RATE, SAMPLE_RATE);
    mediaFormat.setInteger(MediaFormat.KEY_CHANNEL_COUNT, CHANNELS);
    mediaFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, bufferSize);
    mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, BIT_RATE);
    mediaFormat.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
    try {
        mediaCodec.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
    } catch (Exception e) {
        Log.w(TAG, e);
        mediaCodec.release();
        throw new IOException(e);
    }
    return mediaCodec;
}
Also used : MediaFormat(android.media.MediaFormat) MediaCodec(android.media.MediaCodec) IOException(java.io.IOException) IOException(java.io.IOException)

Example 20 with MediaFormat

use of android.media.MediaFormat in project android_packages_apps_Gallery2 by LineageOS.

the class VideoUtils method genVideoUsingMuxer.

/**
 * @param srcPath the path of source video file.
 * @param dstPath the path of destination video file.
 * @param startMs starting time in milliseconds for trimming. Set to
 *            negative if starting from beginning.
 * @param endMs end time for trimming in milliseconds. Set to negative if
 *            no trimming at the end.
 * @param useAudio true if keep the audio track from the source.
 * @param useVideo true if keep the video track from the source.
 * @throws IOException
 */
private static void genVideoUsingMuxer(String srcPath, String dstPath, int startMs, int endMs, boolean useAudio, boolean useVideo) throws IOException {
    // Set up MediaExtractor to read from the source.
    MediaExtractor extractor = new MediaExtractor();
    extractor.setDataSource(srcPath);
    int trackCount = extractor.getTrackCount();
    // Set up MediaMuxer for the destination.
    MediaMuxer muxer;
    muxer = new MediaMuxer(dstPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
    // Set up the tracks and retrieve the max buffer size for selected
    // tracks.
    HashMap<Integer, Integer> indexMap = new HashMap<Integer, Integer>(trackCount);
    int bufferSize = -1;
    for (int i = 0; i < trackCount; i++) {
        MediaFormat format = extractor.getTrackFormat(i);
        String mime = format.getString(MediaFormat.KEY_MIME);
        boolean selectCurrentTrack = false;
        if (mime.startsWith("audio/") && useAudio) {
            selectCurrentTrack = true;
        } else if (mime.startsWith("video/") && useVideo) {
            selectCurrentTrack = true;
        }
        if (selectCurrentTrack) {
            extractor.selectTrack(i);
            try {
                int dstIndex = muxer.addTrack(format);
                indexMap.put(i, dstIndex);
                if (format.containsKey(MediaFormat.KEY_MAX_INPUT_SIZE)) {
                    int newSize = format.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE);
                    bufferSize = newSize > bufferSize ? newSize : bufferSize;
                }
            } catch (IllegalArgumentException e) {
                Log.e(LOGTAG, "Unsupported format '" + mime + "'");
                throw new IOException("Muxer does not support " + mime);
            }
        }
    }
    if (bufferSize < 0) {
        bufferSize = DEFAULT_BUFFER_SIZE;
    }
    // Set up the orientation and starting time for extractor.
    MediaMetadataRetriever retrieverSrc = new MediaMetadataRetriever();
    retrieverSrc.setDataSource(srcPath);
    String degreesString = retrieverSrc.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);
    if (degreesString != null) {
        int degrees = Integer.parseInt(degreesString);
        if (degrees >= 0) {
            muxer.setOrientationHint(degrees);
        }
    }
    if (startMs > 0) {
        extractor.seekTo(startMs * 1000, MediaExtractor.SEEK_TO_CLOSEST_SYNC);
    }
    // Copy the samples from MediaExtractor to MediaMuxer. We will loop
    // for copying each sample and stop when we get to the end of the source
    // file or exceed the end time of the trimming.
    int offset = 0;
    int trackIndex = -1;
    ByteBuffer dstBuf = ByteBuffer.allocate(bufferSize);
    BufferInfo bufferInfo = new BufferInfo();
    try {
        muxer.start();
        while (true) {
            bufferInfo.offset = offset;
            bufferInfo.size = extractor.readSampleData(dstBuf, offset);
            if (bufferInfo.size < 0) {
                Log.d(LOGTAG, "Saw input EOS.");
                bufferInfo.size = 0;
                break;
            } else {
                bufferInfo.presentationTimeUs = extractor.getSampleTime();
                if (endMs > 0 && bufferInfo.presentationTimeUs > (endMs * 1000)) {
                    Log.d(LOGTAG, "The current sample is over the trim end time.");
                    break;
                } else {
                    bufferInfo.flags = extractor.getSampleFlags();
                    trackIndex = extractor.getSampleTrackIndex();
                    muxer.writeSampleData(indexMap.get(trackIndex), dstBuf, bufferInfo);
                    extractor.advance();
                }
            }
        }
        muxer.stop();
    } catch (IllegalStateException e) {
        // Swallow the exception due to malformed source.
        Log.w(LOGTAG, "The source video file is malformed");
        File f = new File(dstPath);
        if (f.exists()) {
            f.delete();
        }
        throw e;
    } finally {
        muxer.release();
    }
    return;
}
Also used : MediaFormat(android.media.MediaFormat) BufferInfo(android.media.MediaCodec.BufferInfo) HashMap(java.util.HashMap) IOException(java.io.IOException) MediaExtractor(android.media.MediaExtractor) MediaMuxer(android.media.MediaMuxer) ByteBuffer(java.nio.ByteBuffer) MediaMetadataRetriever(android.media.MediaMetadataRetriever) RandomAccessFile(java.io.RandomAccessFile) IsoFile(com.coremedia.iso.IsoFile) File(java.io.File)

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