android.media.MediaFormat Java Examples

The following examples show how to use android.media.MediaFormat. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AACEncoder.java    From AndroidScreenShare with Apache License 2.0 7 votes vote down vote up
private void init() {
    mBufferInfo = new MediaCodec.BufferInfo();
    try {
        mEncoder = MediaCodec.createEncoderByType(MIME_TYPE);
        MediaFormat mediaFormat = MediaFormat.createAudioFormat(MIME_TYPE,
                sampleRate, channelCount);
        mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, bitRate);
        mediaFormat.setInteger(MediaFormat.KEY_AAC_PROFILE,
                KEY_AAC_PROFILE);
        mEncoder.configure(mediaFormat, null, null,
                MediaCodec.CONFIGURE_FLAG_ENCODE);
        mEncoder.start();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #2
Source File: MoviePlayer.java    From pause-resume-video-recording with Apache License 2.0 6 votes vote down vote up
/**
 * Selects the video track, if any.
 *
 * @return the track index, or -1 if no video track is found.
 */
private static int selectTrack(MediaExtractor extractor) {
    // Select the first video track we find, ignore the rest.
    int numTracks = extractor.getTrackCount();
    for (int i = 0; i < numTracks; i++) {
        MediaFormat format = extractor.getTrackFormat(i);
        String mime = format.getString(MediaFormat.KEY_MIME);
        if (mime.startsWith("video/")) {
            if (VERBOSE) {
                Log.d(TAG, "Extractor selected track " + i + " (" + mime + "): " + format);
            }
            return i;
        }
    }

    return -1;
}
 
Example #3
Source File: DashMediaExtractor.java    From MediaPlayer-Extended with Apache License 2.0 6 votes vote down vote up
@Override
public MediaFormat getTrackFormat(int index) {
    MediaFormat mediaFormat = super.getTrackFormat(index);
    if(mMp4Mode) {
        /* An MP4 that has been converted from a fragmented to an unfragmented container
         * through the isoparser library does only contain the current segment's runtime. To
         * return the total runtime, we take the value from the MPD instead.
         */
        mediaFormat.setLong(MediaFormat.KEY_DURATION, mMPD.mediaPresentationDurationUs);
    }
    if(mediaFormat.getString(MediaFormat.KEY_MIME).startsWith("video/")) {
        // Return the display aspect ratio as defined in the MPD (can be different from the encoded video size)
        mediaFormat.setFloat(MEDIA_FORMAT_EXTENSION_KEY_DAR,
                mAdaptationSet.hasPAR() ? mAdaptationSet.par : mRepresentation.calculatePAR());
    }
    return mediaFormat;
}
 
Example #4
Source File: VideoTrackTranscoderShould.java    From LiTr with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void addTrackWhenEncoderMediaFormatReceived() throws Exception {
    videoTrackTranscoder.lastExtractFrameResult = TrackTranscoder.RESULT_EOS_REACHED;
    videoTrackTranscoder.lastDecodeFrameResult = TrackTranscoder.RESULT_EOS_REACHED;
    videoTrackTranscoder.lastEncodeFrameResult = TrackTranscoder.RESULT_FRAME_PROCESSED;

    MediaFormat encoderMediaFormat = new MediaFormat();
    doReturn(MediaCodec.INFO_OUTPUT_FORMAT_CHANGED).when(encoder).dequeueOutputFrame(anyLong());
    doReturn(encoderMediaFormat).when(encoder).getOutputFormat();
    doReturn(VIDEO_TRACK).when(mediaTarget).addTrack(any(MediaFormat.class), anyInt());

    int result = videoTrackTranscoder.processNextFrame();

    ArgumentCaptor<MediaFormat> mediaFormatArgumentCaptor = ArgumentCaptor.forClass(MediaFormat.class);
    verify(mediaTarget).addTrack(mediaFormatArgumentCaptor.capture(), eq(VIDEO_TRACK));
    assertThat(mediaFormatArgumentCaptor.getValue(), is(encoderMediaFormat));

    assertThat(videoTrackTranscoder.targetTrack, is(VIDEO_TRACK));
    assertThat(result, is(TrackTranscoder.RESULT_OUTPUT_MEDIA_FORMAT_CHANGED));
}
 
Example #5
Source File: MediaExtractorUtils.java    From android-transcoder with Apache License 2.0 6 votes vote down vote up
public static TrackResult getFirstVideoAndAudioTrack(MediaExtractor extractor) {
    TrackResult trackResult = new TrackResult();
    trackResult.mVideoTrackIndex = -1;
    trackResult.mAudioTrackIndex = -1;
    int trackCount = extractor.getTrackCount();
    for (int i = 0; i < trackCount; i++) {
        MediaFormat format = extractor.getTrackFormat(i);
        String mime = format.getString(MediaFormat.KEY_MIME);
        if (trackResult.mVideoTrackIndex < 0 && mime.startsWith("video/")) {
            trackResult.mVideoTrackIndex = i;
            trackResult.mVideoTrackMime = mime;
            trackResult.mVideoTrackFormat = format;
        } else if (trackResult.mAudioTrackIndex < 0 && mime.startsWith("audio/")) {
            trackResult.mAudioTrackIndex = i;
            trackResult.mAudioTrackMime = mime;
            trackResult.mAudioTrackFormat = format;
        }
        if (trackResult.mVideoTrackIndex >= 0 && trackResult.mAudioTrackIndex >= 0) break;
    }
    if (trackResult.mVideoTrackIndex < 0 || trackResult.mAudioTrackIndex < 0) {
        throw new IllegalArgumentException("extractor does not contain video and/or audio tracks.");
    }
    return trackResult;
}
 
Example #6
Source File: VideoEncoderCore.java    From kickflip-android-sdk with Apache License 2.0 6 votes vote down vote up
/**
 * Configures encoder and muxer state, and prepares the input Surface.
 */
public VideoEncoderCore(int width, int height, int bitRate, Muxer muxer) throws IOException {
    mMuxer = muxer;
    mBufferInfo = new MediaCodec.BufferInfo();

    MediaFormat format = MediaFormat.createVideoFormat(MIME_TYPE, width, height);

    // Set some properties.  Failing to specify some of these can cause the MediaCodec
    // configure() call to throw an unhelpful exception.
    format.setInteger(MediaFormat.KEY_COLOR_FORMAT,
            MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
    format.setInteger(MediaFormat.KEY_BIT_RATE, bitRate);
    format.setInteger(MediaFormat.KEY_FRAME_RATE, FRAME_RATE);
    format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, IFRAME_INTERVAL);
    if (VERBOSE) Log.d(TAG, "format: " + format);

    // Create a MediaCodec encoder, and configure it with our format.  Get a Surface
    // we can use for input and wrap it with a class that handles the EGL work.
    mEncoder = MediaCodec.createEncoderByType(MIME_TYPE);
    mEncoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
    mInputSurface = mEncoder.createInputSurface();
    mEncoder.start();

    mTrackIndex = -1;
}
 
Example #7
Source File: AudioCodec.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example #8
Source File: ExtractMpegFramesTest.java    From Android-MediaCodec-Examples with Apache License 2.0 6 votes vote down vote up
/**
 * Selects the video track, if any.
 *
 * @return the track index, or -1 if no video track is found.
 */
private int selectTrack(MediaExtractor extractor) {
    // Select the first video track we find, ignore the rest.
    int numTracks = extractor.getTrackCount();
    for (int i = 0; i < numTracks; i++) {
        MediaFormat format = extractor.getTrackFormat(i);
        String mime = format.getString(MediaFormat.KEY_MIME);
        if (mime.startsWith("video/")) {
            if (VERBOSE) {
                Log.d(TAG, "Extractor selected track " + i + " (" + mime + "): " + format);
            }
            return i;
        }
    }

    return -1;
}
 
Example #9
Source File: AudioUtil.java    From VideoProcessor with Apache License 2.0 6 votes vote down vote up
/**
 * 调整aac音量
 *
 * @param volume [0,100]
 * @throws IOException
 */
public static void adjustAacVolume(Context context, VideoProcessor.MediaSource aacSource, String outPath, int volume
        , @Nullable VideoProgressListener listener) throws IOException {
    String name = "temp_aac_"+System.currentTimeMillis();
    File pcmFile = new File(VideoUtil.getVideoCacheDir(context), name + ".pcm");
    File pcmFile2 = new File(VideoUtil.getVideoCacheDir(context), name + "_2.pcm");
    File wavFile = new File(VideoUtil.getVideoCacheDir(context), name + ".wav");

    AudioUtil.decodeToPCM(aacSource, pcmFile.getAbsolutePath(), null, null);
    AudioUtil.adjustPcmVolume(pcmFile.getAbsolutePath(), pcmFile2.getAbsolutePath(), volume);
    MediaExtractor extractor = new MediaExtractor();
    aacSource.setDataSource(extractor);
    int trackIndex = VideoUtil.selectTrack(extractor, true);
    MediaFormat aacFormat = extractor.getTrackFormat(trackIndex);
    int sampleRate = aacFormat.getInteger(MediaFormat.KEY_SAMPLE_RATE);
    int oriChannelCount = aacFormat.getInteger(MediaFormat.KEY_CHANNEL_COUNT);
    int channelConfig = AudioFormat.CHANNEL_IN_MONO;
    if (oriChannelCount == 2) {
        channelConfig = AudioFormat.CHANNEL_IN_STEREO;
    }
    new PcmToWavUtil(sampleRate, channelConfig, oriChannelCount, AudioFormat.ENCODING_PCM_16BIT).pcmToWav(pcmFile2.getAbsolutePath(), wavFile.getAbsolutePath());
    AudioUtil.encodeWAVToAAC(wavFile.getPath(), outPath, aacFormat, listener);
}
 
Example #10
Source File: VideoCodecTask.java    From MultiMediaSample with Apache License 2.0 6 votes vote down vote up
/**
 * 初始化编码器
 */

private void initMediaEncode() {
    getEncodeColor();
    try {
        MediaFormat format = MediaFormat.createVideoFormat(MediaFormat.MIMETYPE_VIDEO_AVC, mDstWidth, mDstHeight);
        format.setInteger(MediaFormat.KEY_BIT_RATE, 1024 * 512);
        format.setInteger(MediaFormat.KEY_FRAME_RATE, 27);
        format.setInteger(MediaFormat.KEY_COLOR_FORMAT, mVideoEncodeColor);
        format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1);

        mMediaEncode = MediaCodec.createEncoderByType(MediaFormat.MIMETYPE_VIDEO_AVC);
        mMediaEncode.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (mMediaEncode == null) {
        Log.e(TAG, "create mMediaEncode failed");
        return;
    }
    mMediaEncode.start();

}
 
Example #11
Source File: TranscodeVideoUtil.java    From SimpleVideoEdit with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an encoder for the given format using the specified codec, taking input from a
 * surface.
 *
 * <p>The surface to use as input is stored in the given reference.
 *
 * @param codecName of the codec to use
 * @param format of the stream to be produced
 * @param surfaceReference to store the surface to use as input
 */
private MediaCodec createVideoEncoder(
        String codecName,
        MediaFormat format,
        AtomicReference<Surface> surfaceReference) {
    try {
        MediaCodec encoder = MediaCodec.createByCodecName(codecName);
        encoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
        // Must be called before start() is.
        surfaceReference.set(encoder.createInputSurface());
        encoder.start();
        return encoder;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example #12
Source File: Util.java    From APlayer with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 获得歌曲格式
 */
public static String getType(String mimeType) {
  if (mimeType.equals(MediaFormat.MIMETYPE_AUDIO_MPEG)) {
    return "mp3";
  } else if (mimeType.equals(MediaFormat.MIMETYPE_AUDIO_FLAC)) {
    return "flac";
  } else if (mimeType.equals(MediaFormat.MIMETYPE_AUDIO_AAC)) {
    return "aac";
  } else if (mimeType.contains("ape")) {
    return "ape";
  } else {
    try {
      if (mimeType.contains("audio/")) {
        return mimeType.substring(6, mimeType.length() - 1);
      } else {
        return mimeType;
      }
    } catch (Exception e) {
      return mimeType;
    }
  }
}
 
Example #13
Source File: MediaCodecAudioRenderer.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the framework {@link MediaFormat} that can be used to configure a {@link MediaCodec}
 * for decoding the given {@link Format} for playback.
 *
 * @param format The format of the media.
 * @param codecMimeType The MIME type handled by the codec.
 * @param codecMaxInputSize The maximum input size supported by the codec.
 * @param codecOperatingRate The codec operating rate, or {@link #CODEC_OPERATING_RATE_UNSET} if
 *     no codec operating rate should be set.
 * @return The framework media format.
 */
@SuppressLint("InlinedApi")
protected MediaFormat getMediaFormat(
    Format format, String codecMimeType, int codecMaxInputSize, float codecOperatingRate) {
  MediaFormat mediaFormat = new MediaFormat();
  // Set format parameters that should always be set.
  mediaFormat.setString(MediaFormat.KEY_MIME, codecMimeType);
  mediaFormat.setInteger(MediaFormat.KEY_CHANNEL_COUNT, format.channelCount);
  mediaFormat.setInteger(MediaFormat.KEY_SAMPLE_RATE, format.sampleRate);
  MediaFormatUtil.setCsdBuffers(mediaFormat, format.initializationData);
  // Set codec max values.
  MediaFormatUtil.maybeSetInteger(mediaFormat, MediaFormat.KEY_MAX_INPUT_SIZE, codecMaxInputSize);
  // Set codec configuration values.
  if (Util.SDK_INT >= 23) {
    mediaFormat.setInteger(MediaFormat.KEY_PRIORITY, 0 /* realtime priority */);
    if (codecOperatingRate != CODEC_OPERATING_RATE_UNSET) {
      mediaFormat.setFloat(MediaFormat.KEY_OPERATING_RATE, codecOperatingRate);
    }
  }
  return mediaFormat;
}
 
Example #14
Source File: ExoVlcUtil.java    From Exoplayer_VLC with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param media
 * @param vlcTracks
 * @param lib
 * @return
 */
static com.google.android.exoplayer.TrackInfo[] vlc2exoTracks(long duration,
		org.videolan.libvlc.Media.Track[] vlcTracks, LibVLC lib) {
	com.google.android.exoplayer.TrackInfo[] res = new com.google.android.exoplayer.TrackInfo[vlcTracks.length];
	System.out.println("ExoVlcUtil.vlc2exoTracks() vlcTracks = " + vlcTracks.length);
	// Media.Track
	for (int i = 0; i < res.length; i++) {
		MediaFormat mf = track2mediaFormat(vlcTracks[i]);

		res[i] = new TrackInfo(mf.getString(MediaFormat.KEY_MIME), duration);
		System.out.println("\t track " + i + " mime type =" + mf.getString(MediaFormat.KEY_MIME) + " duration ="
				+ duration);
	}
	/*
	 * System.out.println(">>>> ExoVlcUtil.vlc2exoTracks() vlcTracks.length = "
	 * +vlcTracks.length); long l; for (int i = 0; i < vlcTracks.length;
	 * i++) { org.videolan.libvlc.TrackInfo vt = vlcTracks[i];
	 * System.out.println("\t\t >>>>>Codec("+i+") "+vlcTracks[i].Codec);
	 * res[i] = new TrackInfo( vt.Codec, (l=lib.getLength()) == -1 ?
	 * com.google.android.exoplayer.C.UNKNOWN_TIME_US : l * MS_2_MICRO); }
	 */
	return res;
}
 
Example #15
Source File: AvcDecoder.java    From cameraMediaCodec with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public int tryConfig(Surface surface, byte[] sps, byte[] pps)
{
	int[] width = new int[1];
	int[] height = new int[1];
	
	AvcUtils.parseSPS(sps, width, height);
	
	mMF = MediaFormat.createVideoFormat(MIME_TYPE, width[0], height[0]);
	
	mMF.setByteBuffer("csd-0", ByteBuffer.wrap(sps));
	mMF.setByteBuffer("csd-1", ByteBuffer.wrap(pps));
	mMF.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, width[0] * height[0]);
	
	mMC.configure(mMF, surface, null, 0);
	Log.i("AvcDecoder", "Init, configure");
	mStatus = STATUS_IDLE;
	
	return 0;
}
 
Example #16
Source File: Format3GP.java    From Android-Audio-Recorder with Apache License 2.0 6 votes vote down vote up
public Format3GP(EncoderInfo info, File out) {
        MediaFormat format = new MediaFormat();

        // for high bitrate AMR_WB
        {
//            final int kBitRates[] = {6600, 8850, 12650, 14250, 15850, 18250, 19850, 23050, 23850};

//            format.setString(MediaFormat.KEY_MIME, "audio/amr-wb");
//            format.setInteger(MediaFormat.KEY_SAMPLE_RATE, info.sampleRate);
//            format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, info.channels);
//            format.setInteger(MediaFormat.KEY_BIT_RATE, 23850); // set maximum
        }

        // for low bitrate, AMR_NB
        {
//            final int kBitRates[] = {4750, 5150, 5900, 6700, 7400, 7950, 10200, 12200};

            format.setString(MediaFormat.KEY_MIME, "audio/3gpp");
            format.setInteger(MediaFormat.KEY_SAMPLE_RATE, info.sampleRate); // 8000 only supported
            format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, info.channels);
            format.setInteger(MediaFormat.KEY_BIT_RATE, 12200); // set maximum
        }

        create(info, format, out);
    }
 
Example #17
Source File: MediaCodecAudioRenderer.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void configureCodec(
    MediaCodecInfo codecInfo,
    MediaCodec codec,
    Format format,
    MediaCrypto crypto,
    float codecOperatingRate) {
  codecMaxInputSize = getCodecMaxInputSize(codecInfo, format, getStreamFormats());
  codecNeedsDiscardChannelsWorkaround = codecNeedsDiscardChannelsWorkaround(codecInfo.name);
  codecNeedsEosBufferTimestampWorkaround = codecNeedsEosBufferTimestampWorkaround(codecInfo.name);
  passthroughEnabled = codecInfo.passthrough;
  String codecMimeType = passthroughEnabled ? MimeTypes.AUDIO_RAW : codecInfo.codecMimeType;
  MediaFormat mediaFormat =
      getMediaFormat(format, codecMimeType, codecMaxInputSize, codecOperatingRate);
  codec.configure(mediaFormat, /* surface= */ null, crypto, /* flags= */ 0);
  if (passthroughEnabled) {
    // Store the input MIME type if we're using the passthrough codec.
    passthroughMediaFormat = mediaFormat;
    passthroughMediaFormat.setString(MediaFormat.KEY_MIME, format.sampleMimeType);
  } else {
    passthroughMediaFormat = null;
  }
}
 
Example #18
Source File: MediaExtractorUtils.java    From phoenix with Apache License 2.0 6 votes vote down vote up
public static TrackResult getFirstVideoAndAudioTrack(MediaExtractor extractor) {
    TrackResult trackResult = new TrackResult();
    trackResult.mVideoTrackIndex = -1;
    trackResult.mAudioTrackIndex = -1;
    int trackCount = extractor.getTrackCount();
    for (int i = 0; i < trackCount; i++) {
        MediaFormat format = extractor.getTrackFormat(i);
        String mime = format.getString(MediaFormat.KEY_MIME);
        if (trackResult.mVideoTrackIndex < 0 && mime.startsWith("video/")) {
            trackResult.mVideoTrackIndex = i;
            trackResult.mVideoTrackMime = mime;
            trackResult.mVideoTrackFormat = format;
        } else if (trackResult.mAudioTrackIndex < 0 && mime.startsWith("audio/")) {
            trackResult.mAudioTrackIndex = i;
            trackResult.mAudioTrackMime = mime;
            trackResult.mAudioTrackFormat = format;
        }
        if (trackResult.mVideoTrackIndex >= 0 && trackResult.mAudioTrackIndex >= 0) break;
    }
    if (trackResult.mVideoTrackIndex < 0 || trackResult.mAudioTrackIndex < 0) {
        throw new IllegalArgumentException("extractor does not contain video and/or audio tracks.");
    }
    return trackResult;
}
 
Example #19
Source File: AudioCodec.java    From deltachat-android with GNU General Public License v3.0 6 votes vote down vote up
private MediaCodec createMediaCodec(Context context, 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, Prefs.isHardCompressionEnabled(context)? BIT_RATE_WORSE : BIT_RATE_BALANCED);
  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;
}
 
Example #20
Source File: PassThroughTrackTranscoder.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public PassThroughTrackTranscoder(MediaExtractor extractor, int trackIndex,
                                  QueuedMuxer muxer, QueuedMuxer.SampleType sampleType) {
    mExtractor = extractor;
    mTrackIndex = trackIndex;
    mMuxer = muxer;
    mSampleType = sampleType;

    mActualOutputFormat = mExtractor.getTrackFormat(mTrackIndex);
    mMuxer.setOutputFormat(mSampleType, mActualOutputFormat);
    mBufferSize = mActualOutputFormat.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE);
    mBuffer = ByteBuffer.allocateDirect(mBufferSize).order(ByteOrder.nativeOrder());
}
 
Example #21
Source File: TLMediaEncoder.java    From TimeLapseRecordingSample with Apache License 2.0 5 votes vote down vote up
static MediaFormat readFormat(final DataInputStream in) {
	MediaFormat format = null;
	try {
		readHeader(in);
		in.readUTF();	// skip MediaFormat data for configure
		format = asMediaFormat(in.readUTF());
	} catch (IOException e) {
		Log.e(TAG_STATIC, "readFormat:", e);
	}
	if (DEBUG) Log.v(TAG_STATIC, "readFormat:format=" + format);
	return format;
}
 
Example #22
Source File: CameraRecorder.java    From AAVT with Apache License 2.0 5 votes vote down vote up
public void startRecord() throws IOException {
        synchronized (REC_LOCK){
            isRecordStarted=true;
            MediaFormat audioFormat=mConfig.getAudioFormat();
            mAudioEncoder=MediaCodec.createEncoderByType(audioFormat.getString(MediaFormat.KEY_MIME));
            mAudioEncoder.configure(audioFormat,null,null,MediaCodec.CONFIGURE_FLAG_ENCODE);
            MediaFormat videoFormat=mConfig.getVideoFormat();
            mVideoEncoder=MediaCodec.createEncoderByType(videoFormat.getString(MediaFormat.KEY_MIME));
            //此处不能用mOutputSurface,会configure失败
            mVideoEncoder.configure(videoFormat,null,null,MediaCodec.CONFIGURE_FLAG_ENCODE);
            mEncodeSurface=mVideoEncoder.createInputSurface();

            mAudioEncoder.start();
            mVideoEncoder.start();
            mMuxer=new MediaMuxer(mOutputPath,MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
            mRecordBufferSize = AudioRecord.getMinBufferSize(mRecordSampleRate,
                    mRecordChannelConfig, mRecordAudioFormat)*2;
//        buffer=new byte[bufferSize];
            mAudioRecord=new AudioRecord(MediaRecorder.AudioSource.MIC,mRecordSampleRate,mRecordChannelConfig,
                    mRecordAudioFormat,mRecordBufferSize);

            mAudioThread=new Thread(new Runnable() {
                @Override
                public void run() {
                    mAudioRecord.startRecording();
                    while (!audioEncodeStep(isTryStopAudio)){};
                    mAudioRecord.stop();
                }
            });
            mAudioThread.start();
            isRecordAudioStarted=true;
        }
    }
 
Example #23
Source File: Mp4Processor.java    From AAVT with Apache License 2.0 5 votes vote down vote up
private boolean videoEncodeStep(boolean isEnd){
    if(isEnd){
        mVideoEncoder.signalEndOfInputStream();
    }
    while (true){
        int mOutputIndex=mVideoEncoder.dequeueOutputBuffer(mVideoEncoderBufferInfo,TIME_OUT);
        AvLog.d("videoEncodeStep-------------------mOutputIndex="+mOutputIndex+"/"+mVideoEncoderBufferInfo.presentationTimeUs);
        if(mOutputIndex>=0){
            ByteBuffer buffer=getOutputBuffer(mVideoEncoder,mOutputIndex);
            if(mVideoEncoderBufferInfo.size>0){
                mMuxer.writeSampleData(mVideoEncoderTrack,buffer,mVideoEncoderBufferInfo);
            }
            mVideoEncoder.releaseOutputBuffer(mOutputIndex,false);
        }else if(mOutputIndex== MediaCodec.INFO_OUTPUT_FORMAT_CHANGED){
            MediaFormat format=mVideoEncoder.getOutputFormat();
            AvLog.d("video format -->"+format.toString());
            mVideoEncoderTrack=mMuxer.addTrack(format);
            mMuxer.start();
            synchronized (MUX_LOCK){
                MUX_LOCK.notifyAll();
            }
        }else if(mOutputIndex== MediaCodec.INFO_TRY_AGAIN_LATER){
            break;
        }
    }
    return false;
}
 
Example #24
Source File: AACHelper.java    From CameraV with GNU General Public License v3.0 5 votes vote down vote up
public boolean setEncoder(int sampleRate, int channels, int bitRate) throws Exception
{
    encoder = MediaCodec.createEncoderByType("audio/mp4a-latm");
    MediaFormat format = new MediaFormat();
    format.setString(MediaFormat.KEY_MIME, "audio/mp4a-latm");
    format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, channels);
    format.setInteger(MediaFormat.KEY_SAMPLE_RATE, sampleRate);
    format.setInteger(MediaFormat.KEY_BIT_RATE, bitRate * 1024);//AAC-HE 64kbps
    format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectHE);
    encoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
    
    initAudioRecord(sampleRate);
    
    return true;
}
 
Example #25
Source File: MediaCodecRenderer.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Processes a new output format.
 */
private void processOutputFormat() throws ExoPlaybackException {
  MediaFormat format = codec.getOutputFormat();
  if (codecAdaptationWorkaroundMode != ADAPTATION_WORKAROUND_MODE_NEVER
      && format.getInteger(MediaFormat.KEY_WIDTH) == ADAPTATION_WORKAROUND_SLICE_WIDTH_HEIGHT
      && format.getInteger(MediaFormat.KEY_HEIGHT) == ADAPTATION_WORKAROUND_SLICE_WIDTH_HEIGHT) {
    // We assume this format changed event was caused by the adaptation workaround.
    shouldSkipAdaptationWorkaroundOutputBuffer = true;
    return;
  }
  if (codecNeedsMonoChannelCountWorkaround) {
    format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
  }
  onOutputFormatChanged(codec, format);
}
 
Example #26
Source File: RemixAudioComposer.java    From Mp4Composer-android with MIT License 5 votes vote down vote up
RemixAudioComposer(MediaExtractor extractor, int trackIndex,
                   MediaFormat outputFormat, MuxRender muxer, float timeScale, boolean isPitchChanged,
                   long trimStartMs, long trimEndMs) {
    this.extractor = extractor;
    this.trackIndex = trackIndex;
    this.outputFormat = outputFormat;
    this.muxer = muxer;
    this.timeScale = timeScale;
    this.isPitchChanged = isPitchChanged;
    this.trimStartUs = TimeUnit.MILLISECONDS.toMicros(trimStartMs);
    this.trimEndUs = trimEndMs == -1 ? trimEndMs : TimeUnit.MILLISECONDS.toMicros(trimEndMs);
}
 
Example #27
Source File: AudioReader.java    From media-for-mobile with Apache License 2.0 5 votes vote down vote up
protected MediaCodec createAudioDecoder(MediaFormat inputFormat) {
    MediaCodec decoder = null;
    try {
        decoder = MediaCodec.createDecoderByType(getMimeTypeFor(inputFormat));
        decoder.configure(inputFormat, null, null, 0);
        decoder.start();

    } catch (IOException e) {
        e.printStackTrace();
    }
    return decoder;
}
 
Example #28
Source File: MediaTransformer.java    From LiTr with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Transform video and audio track(s): change resolution, frame rate, bitrate, etc. Video track transformation
 * uses default hardware accelerated codecs and OpenGL renderer.
 *
 * If overlay(s) are provided, video track(s) will be transcoded with parameters as close to source format as possible.
 *
 * @param requestId client defined unique id for a transformation request. If not unique, {@link IllegalArgumentException} will be thrown.
 * @param inputUri input video {@link Uri}
 * @param outputFilePath Absolute path of output media file
 * @param targetVideoFormat target format parameters for video track(s), null to keep them as is
 * @param targetAudioFormat target format parameters for audio track(s), null to keep them as is
 * @param listener {@link TransformationListener} implementation, to get updates on transformation status/result/progress
 * @param granularity progress reporting granularity. NO_GRANULARITY for per-frame progress reporting,
 *                    or positive integer value for number of times transformation progress should be reported
 * @param filters optional OpenGL filters to apply to video frames
 */
public void transform(@NonNull String requestId,
                      @NonNull Uri inputUri,
                      @NonNull String outputFilePath,
                      @Nullable MediaFormat targetVideoFormat,
                      @Nullable MediaFormat targetAudioFormat,
                      @NonNull TransformationListener listener,
                      @IntRange(from = GRANULARITY_NONE) int granularity,
                      @Nullable List<GlFilter> filters) {
    try {
        MediaSource mediaSource = new MediaExtractorMediaSource(context, inputUri);
        MediaTarget mediaTarget = new MediaMuxerMediaTarget(outputFilePath,
                                                            mediaSource.getTrackCount(),
                                                            mediaSource.getOrientationHint(),
                                                            MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
        Renderer renderer = new GlVideoRenderer(filters);
        Decoder decoder = new MediaCodecDecoder();
        Encoder encoder = new MediaCodecEncoder();
        transform(requestId,
                  mediaSource,
                  decoder,
                  renderer,
                  encoder,
                  mediaTarget,
                  targetVideoFormat,
                  targetAudioFormat,
                  listener,
                  granularity);
    } catch (MediaSourceException | MediaTargetException ex) {
        listener.onError(requestId, ex, null);
    }
}
 
Example #29
Source File: DefaultDataSinkChecks.java    From GIFCompressor with Apache License 2.0 5 votes vote down vote up
void checkOutputFormat(@NonNull MediaFormat format) {
    String mime = format.getString(MediaFormat.KEY_MIME);
    // Refer: http://developer.android.com/guide/appendix/media-formats.html#core
    // Refer: http://en.wikipedia.org/wiki/MPEG-4_Part_14#Data_streams
    if (!MediaFormatConstants.MIMETYPE_VIDEO_AVC.equals(mime)) {
        throw new InvalidOutputFormatException("Video codecs other than AVC is not supported, actual mime type: " + mime);
    }
}
 
Example #30
Source File: MediaCodecRenderer.java    From K-Sonic with MIT License 5 votes vote down vote up
/**
 * Processes a new output format.
 */
private void processOutputFormat() throws ExoPlaybackException {
  MediaFormat format = codec.getOutputFormat();
  if (codecNeedsAdaptationWorkaround
      && format.getInteger(MediaFormat.KEY_WIDTH) == ADAPTATION_WORKAROUND_SLICE_WIDTH_HEIGHT
      && format.getInteger(MediaFormat.KEY_HEIGHT) == ADAPTATION_WORKAROUND_SLICE_WIDTH_HEIGHT) {
    // We assume this format changed event was caused by the adaptation workaround.
    shouldSkipAdaptationWorkaroundOutputBuffer = true;
    return;
  }
  if (codecNeedsMonoChannelCountWorkaround) {
    format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
  }
  onOutputFormatChanged(codec, format);
}