Java Code Examples for android.media.MediaFormat#setInteger()

The following examples show how to use android.media.MediaFormat#setInteger() . 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: AudioEncoderCore.java    From Fatigue-Detection with MIT License 7 votes vote down vote up
public AudioEncoderCore(MMediaMuxer MMediaMuxer) throws IOException {
        super(MMediaMuxer);
        final MediaFormat audioFormat = MediaFormat.createAudioFormat(MIME_TYPE, SAMPLE_RATE, 1);
        audioFormat.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
        audioFormat.setInteger(MediaFormat.KEY_CHANNEL_MASK, AudioFormat.CHANNEL_IN_MONO);
        audioFormat.setInteger(MediaFormat.KEY_BIT_RATE, BIT_RATE);
        audioFormat.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
//		audioFormat.setLong(MediaFormat.KEY_MAX_INPUT_SIZE, inputFile.length());
//      audioFormat.setLong(MediaFormat.KEY_DURATION, (long)durationInMs );
        if (VERBOSE) Log.i(TAG, "format: " + audioFormat);
        mEncoder = MediaCodec.createEncoderByType(MIME_TYPE);
        mEncoder.configure(audioFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
        mEncoder.start();
        if (mAudioThread == null) {
            mAudioThread = new AudioThread();
            mAudioThread.start();
            capturing=true;
            stopped=false;
        }
    }
 
Example 2
Source File: Encoder.java    From AndroidScreenShare with Apache License 2.0 6 votes vote down vote up
private void initMediaCodec() {
    try {
        MediaFormat format = MediaFormat.createVideoFormat(MIME, videoW, videoH);
        format.setInteger(MediaFormat.KEY_COLOR_FORMAT,
                MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);//颜色格式
        format.setInteger(MediaFormat.KEY_BIT_RATE, videoBitrate);//码流
        format.setInteger(MediaFormat.KEY_FRAME_RATE, videoFrameRate);//帧数
        format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, -1); // 关键帧 5秒
        
        codec = MediaCodec.createEncoderByType(MIME);
        codec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
        mSurface = codec.createInputSurface();
        codec.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 3
Source File: AudioTrackTranscoderShould.java    From LiTr with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Before
public void setup() throws Exception {
    MockitoAnnotations.initMocks(this);

    sampleFrame = new Frame(BUFFER_INDEX, ByteBuffer.allocate(BUFFER_SIZE), bufferInfo);

    doReturn(sourceMediaFormat).when(mediaSource).getTrackFormat(anyInt());
    doReturn(true).when(decoder).isRunning();
    doReturn(true).when(encoder).isRunning();

    targetAudioFormat = new MediaFormat();
    targetAudioFormat.setString(MediaFormat.KEY_MIME, "audio/aac");
    targetAudioFormat.setInteger(MediaFormat.KEY_BIT_RATE, 128000);
    targetAudioFormat.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 2);
    targetAudioFormat.setInteger(MediaFormat.KEY_SAMPLE_RATE, 44100);

    // setting up and starting test target, which will be used for frame processing testing below
    audioTrackTranscoder = spy(new AudioTrackTranscoder(
            mediaSource, AUDIO_TRACK,
            mediaTarget, AUDIO_TRACK,
            targetAudioFormat,
            renderer,
            decoder,
            encoder));
    audioTrackTranscoder.start();
}
 
Example 4
Source File: TranscoderUtilsShould.java    From LiTr with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Before
public void setup() {
    MockitoAnnotations.initMocks(this);

    targetVideoFormat = new MediaFormat();
    targetVideoFormat.setString(MediaFormat.KEY_MIME, "video/avc");
    targetVideoFormat.setInteger(MediaFormat.KEY_WIDTH, 1280);
    targetVideoFormat.setInteger(MediaFormat.KEY_HEIGHT, 720);
    targetVideoFormat.setInteger(MediaFormat.KEY_BIT_RATE, 3300000);
    targetVideoFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 5);

    when(videoMediaFormat.containsKey(MediaFormat.KEY_MIME)).thenReturn(true);
    when(videoMediaFormat.getString(MediaFormat.KEY_MIME)).thenReturn("video/avc");
    when(videoMediaFormat.containsKey(MediaFormat.KEY_BIT_RATE)).thenReturn(true);
    when(videoMediaFormat.getInteger(MediaFormat.KEY_BIT_RATE)).thenReturn(VIDEO_BIT_RATE);
    when(videoMediaFormat.containsKey(MediaFormat.KEY_DURATION)).thenReturn(true);
    when(videoMediaFormat.getLong(MediaFormat.KEY_DURATION)).thenReturn(DURATION_US);
    when(videoMediaFormat.getInteger(MediaFormat.KEY_WIDTH)).thenReturn(VIDEO_WIDTH);
    when(videoMediaFormat.getInteger(MediaFormat.KEY_HEIGHT)).thenReturn(VIDEO_HEIGHT);

    when(audioMediaFormat.containsKey(MediaFormat.KEY_MIME)).thenReturn(true);
    when(audioMediaFormat.getString(MediaFormat.KEY_MIME)).thenReturn("audio/mp4a-latm");
    when(audioMediaFormat.containsKey(MediaFormat.KEY_DURATION)).thenReturn(true);
    when(audioMediaFormat.getLong(MediaFormat.KEY_DURATION)).thenReturn(DURATION_US);

    when(miscMediaFormat.containsKey(MediaFormat.KEY_MIME)).thenReturn(true);
    when(miscMediaFormat.getString(MediaFormat.KEY_MIME)).thenReturn("text/vnd.dvb.subtitle");

    when(mediaSource.getTrackFormat(0)).thenReturn(videoMediaFormat);
    when(mediaSource.getTrackFormat(1)).thenReturn(audioMediaFormat);
}
 
Example 5
Source File: AudioEncoder.java    From LiveMultimedia with Apache License 2.0 5 votes vote down vote up
public synchronized void createAudioFormat() {
    if (Thread.currentThread().isInterrupted()) {
        release();
    }
    mAudioFormat  = new MediaFormat();
    mAudioFormat.setString(MediaFormat.KEY_MIME, AUDIO_MIME_TYPE);
    mAudioFormat.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
    mAudioFormat.setInteger(MediaFormat.KEY_SAMPLE_RATE, 44100);
    mAudioFormat.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
    mAudioFormat.setInteger(MediaFormat.KEY_BIT_RATE, 128000);
    mAudioFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 16384);
}
 
Example 6
Source File: MediaVideoEncoderRunable.java    From GLES2_AUDIO_VIDEO_RECODE with Apache License 2.0 5 votes vote down vote up
/**
 * 开始录制前的准备(目前由SohuMediaMuxerManager在主线程调用)
 *
 * @throws IOException
 */
@Override
public void prepare() throws IOException {
    LogUtils.d(TAG, "---prepare---");
    //
    mTrackIndex = -1;
    //
    mMuxerStarted = mIsEndOfStream = false;

    //-----------------MediaFormat-----------------------
    // mediaCodeC采用的是H.264编码
    final MediaFormat format = MediaFormat.createVideoFormat(MIME_TYPE, mWidth, mHeight);
    // 数据来源自surface
    format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
    // 视频码率
    format.setInteger(MediaFormat.KEY_BIT_RATE, calcBitRate());
    // fps
    format.setInteger(MediaFormat.KEY_FRAME_RATE, FRAME_RATE);
    //设置关键帧的时间
    format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 10);

    //-----------------Encoder-----------------------
    mMediaCodec = MediaCodec.createEncoderByType(MIME_TYPE);
    mMediaCodec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
    // get Surface for encoder input
    // this method only can call between #configure and #start
    mSurface = mMediaCodec.createInputSurface();
    //
    mMediaCodec.start();
    //
    LogUtils.i(TAG, "prepare finishing");
    if (mMediaEncoderListener != null) {
        try {
            mMediaEncoderListener.onPrepared(this);
        } catch (final Exception e) {
            Log.e(TAG, "prepare:", e);
        }
    }
}
 
Example 7
Source File: AvcEncoder.java    From VIA-AI with MIT License 5 votes vote down vote up
public AvcEncoder(EncodeParameters encodeParameters, EncodedFrameListener listener) throws IOException {

        mEncodeParameters = new EncodeParameters(encodeParameters);
        mediaCodec = MediaCodec.createEncoderByType("video/avc");
        MediaFormat mediaFormat = MediaFormat.createVideoFormat("video/avc", mEncodeParameters.getWidth(), mEncodeParameters.getHeight());
        mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, mEncodeParameters.getBitrate());
        mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, 30);
        mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, mEncodeParameters.getColor_format());
        mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1);
        mFrameListener = listener;
        mediaCodec.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
        if(mEncodeParameters.getColor_format()== MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface) {
            mInputSurface = mediaCodec.createInputSurface();
        }
    }
 
Example 8
Source File: Android16By9FormatStrategy.java    From android-transcoder with Apache License 2.0 5 votes vote down vote up
@Override
public MediaFormat createAudioOutputFormat(MediaFormat inputFormat) {
    if (mAudioBitrate == AUDIO_BITRATE_AS_IS || mAudioChannels == AUDIO_CHANNELS_AS_IS) return null;

    // Use original sample rate, as resampling is not supported yet.
    final MediaFormat format = MediaFormat.createAudioFormat(MediaFormatExtraConstants.MIMETYPE_AUDIO_AAC,
            inputFormat.getInteger(MediaFormat.KEY_SAMPLE_RATE), mAudioChannels);
    format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
    format.setInteger(MediaFormat.KEY_BIT_RATE, mAudioBitrate);
    return format;
}
 
Example 9
Source File: MainActivity.java    From AndroidPlayground with MIT License 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    findViewById(R.id.mCameraCapture).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View v) {
            startActivity(new Intent(MainActivity.this, CameraCaptureActivity.class));
        }
    });

    TextView tvInfo = (TextView) findViewById(R.id.mTvInfo);

    MediaFormat format = MediaFormat.createVideoFormat(MIME_TYPE, 1080, 720);

    // 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, 100000);
    format.setInteger(MediaFormat.KEY_FRAME_RATE, FRAME_RATE);
    format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, IFRAME_INTERVAL);

    MediaCodecInfo codecInfo = selectCodec(MIME_TYPE);
    MediaCodecInfo.CodecCapabilities capabilities = codecInfo.getCapabilitiesForType(MIME_TYPE);

    tvInfo.setText(
            "MaxSupportedInstances: " + capabilities.getMaxSupportedInstances() + "\n"
    );
}
 
Example 10
Source File: SurfaceEncoder.java    From AAVT with Apache License 2.0 5 votes vote down vote up
protected MediaFormat convertVideoConfigToFormat(MediaConfig.Video config){
    MediaFormat format=MediaFormat.createVideoFormat(config.mime,config.width,config.height);
    format.setInteger(MediaFormat.KEY_BIT_RATE,config.bitrate);
    format.setInteger(MediaFormat.KEY_FRAME_RATE,config.frameRate);
    format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL,config.iframe);
    format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
    return format;
}
 
Example 11
Source File: AudioTransCoder.java    From SimpleVideoEditor with Apache License 2.0 5 votes vote down vote up
private void prepare() throws IOException {
    encoder = MediaCodec.createEncoderByType("audio/mp4a-latm");
    MediaFormat format = MediaFormat.createAudioFormat("audio/mp4a-latm", sampleRate, channelCount);
    format.setInteger(MediaFormat.KEY_BIT_RATE, 96000);
    format.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 512 * 1024);
    format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
    encoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
    encoder.start();
}
 
Example 12
Source File: AudioEncoder.java    From LiveVideoBroadcaster with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param sampleRate recommended setting is 44100
 * @param channelCount recommended setting is 1
 * @param bitrate  recommended setting is 64000
 * @return
 */
public boolean startAudioEncoder(int sampleRate, int channelCount, int bitrate, int maxInputSize, IMediaMuxer muxerHandler) {
    mMuxerHandler = muxerHandler;
    MediaFormat audioFormat = MediaFormat.createAudioFormat(AUDIO_MIME_TYPE, sampleRate, channelCount);
    audioFormat.setInteger(
            MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
    audioFormat.setInteger(MediaFormat.KEY_BIT_RATE, bitrate);
    audioFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, maxInputSize);


    try {
        mAudioEncoder = MediaCodec.createEncoderByType(AUDIO_MIME_TYPE);
        mAudioEncoder.configure(
                audioFormat,
                null /* surface */,
                null /* crypto */,
                MediaCodec.CONFIGURE_FLAG_ENCODE);

        mAudioEncoder.start();
        mAudioInputBuffers = mAudioEncoder.getInputBuffers();
        mAudioOutputBuffers = mAudioEncoder.getOutputBuffers();
        start();
        return true;
    } catch (IOException | IllegalStateException e) {
        e.printStackTrace();
        mAudioEncoder = null;
    }
    return false;
}
 
Example 13
Source File: AudioRecordThread.java    From PhotoMovie with Apache License 2.0 4 votes vote down vote up
private void recordOtherAudio(MediaExtractor extractor, MediaFormat format) throws IOException, IllegalArgumentException {
    MediaCodec decoder = null;
    try {
        decoder = MediaCodec.createDecoderByType(format.getString(MediaFormat.KEY_MIME));
    } catch (IOException | IllegalArgumentException e) {
        MLog.e(TAG, "Create audio decoder failed!", e);
        try {
            //wait addTrack
            mBarrier.await();
            mBarrier.await();
        } catch (InterruptedException e1) {
            e.printStackTrace();
        } catch (BrokenBarrierException e2) {
            e.printStackTrace();
        }
        throw e;
    }

    File dir = new File(mContext.getCacheDir(), "AudioRecord");
    dir.mkdirs();
    long time = System.currentTimeMillis();
    File pcmFile = new File(dir, "pcm_" + time + ".pcm");
    File wavFile = new File(dir, "wav_" + time + ".wav");

    int sampleRate = format.getInteger(MediaFormat.KEY_SAMPLE_RATE);
    int oriChannelCount = format.getInteger(MediaFormat.KEY_CHANNEL_COUNT);
    int channelConfig = AudioFormat.CHANNEL_IN_MONO;
    if (oriChannelCount == 2) {
        channelConfig = AudioFormat.CHANNEL_IN_STEREO;
    }

    MediaFormat encodeFormat = MediaFormat.createAudioFormat(MediaFormat.MIMETYPE_AUDIO_AAC, sampleRate, oriChannelCount);
    encodeFormat.setInteger(MediaFormat.KEY_BIT_RATE, getAudioBitrate(format));//比特率
    encodeFormat.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
    checkCsd(encodeFormat,
            MediaCodecInfo.CodecProfileLevel.AACObjectLC,
            sampleRate,
            oriChannelCount);

    String aacOutputPath = new File(mContext.getCacheDir(), "tempaac_" + System.currentTimeMillis() + ".aac").getAbsolutePath();

    decodeToPCM(decoder, extractor, format, pcmFile.getAbsolutePath(), mVideoDurationUs);
    new PcmToWavUtil(sampleRate, channelConfig, oriChannelCount, AudioFormat.ENCODING_PCM_16BIT).pcmToWav(pcmFile.getAbsolutePath(), wavFile.getAbsolutePath());

    encodeWAVToAAC(wavFile.getPath(), aacOutputPath, encodeFormat);

    MediaExtractor aacExtractor = new MediaExtractor();
    aacExtractor.setDataSource(aacOutputPath);
    int audioTrackIndex = selectTrack(aacExtractor, true);
    if (audioTrackIndex < 0) {
        throw new RuntimeException("No audio track!");
    }
    format = aacExtractor.getTrackFormat(audioTrackIndex);
    aacExtractor.selectTrack(audioTrackIndex);

    recordAAC(aacExtractor, format);
}
 
Example 14
Source File: MediaCodecVideoRenderer.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the framework {@link MediaFormat} that should be used to configure the decoder.
 *
 * @param format The {@link Format} of media.
 * @param codecMimeType The MIME type handled by the codec.
 * @param codecMaxValues Codec max values that should be used when configuring the decoder.
 * @param codecOperatingRate The codec operating rate, or {@link #CODEC_OPERATING_RATE_UNSET} if
 *     no codec operating rate should be set.
 * @param deviceNeedsNoPostProcessWorkaround Whether the device is known to do post processing by
 *     default that isn't compatible with ExoPlayer.
 * @param tunnelingAudioSessionId The audio session id to use for tunneling, or {@link
 *     C#AUDIO_SESSION_ID_UNSET} if tunneling should not be enabled.
 * @return The framework {@link MediaFormat} that should be used to configure the decoder.
 */
@SuppressLint("InlinedApi")
protected MediaFormat getMediaFormat(
    Format format,
    String codecMimeType,
    CodecMaxValues codecMaxValues,
    float codecOperatingRate,
    boolean deviceNeedsNoPostProcessWorkaround,
    int tunnelingAudioSessionId) {
  MediaFormat mediaFormat = new MediaFormat();
  // Set format parameters that should always be set.
  mediaFormat.setString(MediaFormat.KEY_MIME, codecMimeType);
  mediaFormat.setInteger(MediaFormat.KEY_WIDTH, format.width);
  mediaFormat.setInteger(MediaFormat.KEY_HEIGHT, format.height);
  MediaFormatUtil.setCsdBuffers(mediaFormat, format.initializationData);
  // Set format parameters that may be unset.
  MediaFormatUtil.maybeSetFloat(mediaFormat, MediaFormat.KEY_FRAME_RATE, format.frameRate);
  MediaFormatUtil.maybeSetInteger(mediaFormat, MediaFormat.KEY_ROTATION, format.rotationDegrees);
  MediaFormatUtil.maybeSetColorInfo(mediaFormat, format.colorInfo);
  if (MimeTypes.VIDEO_DOLBY_VISION.equals(format.sampleMimeType)) {
    // Some phones require the profile to be set on the codec.
    // See https://github.com/google/ExoPlayer/pull/5438.
    Pair<Integer, Integer> codecProfileAndLevel = MediaCodecUtil.getCodecProfileAndLevel(format);
    if (codecProfileAndLevel != null) {
      MediaFormatUtil.maybeSetInteger(
          mediaFormat, MediaFormat.KEY_PROFILE, codecProfileAndLevel.first);
    }
  }
  // Set codec max values.
  mediaFormat.setInteger(MediaFormat.KEY_MAX_WIDTH, codecMaxValues.width);
  mediaFormat.setInteger(MediaFormat.KEY_MAX_HEIGHT, codecMaxValues.height);
  MediaFormatUtil.maybeSetInteger(
      mediaFormat, MediaFormat.KEY_MAX_INPUT_SIZE, codecMaxValues.inputSize);
  // 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);
    }
  }
  if (deviceNeedsNoPostProcessWorkaround) {
    mediaFormat.setInteger("no-post-process", 1);
    mediaFormat.setInteger("auto-frc", 0);
  }
  if (tunnelingAudioSessionId != C.AUDIO_SESSION_ID_UNSET) {
    configureTunnelingV21(mediaFormat, tunnelingAudioSessionId);
  }
  return mediaFormat;
}
 
Example 15
Source File: AudioTrackConverter.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
private AudioTrackConverter(
        final @NonNull MediaExtractor audioExtractor,
        final int audioInputTrack,
        long timeFrom,
        long timeTo,
        int audioBitrate) throws IOException {

    mTimeFrom = timeFrom;
    mTimeTo = timeTo;
    mAudioExtractor = audioExtractor;
    mAudioBitrate = audioBitrate;

    final MediaCodecInfo audioCodecInfo = MediaConverter.selectCodec(OUTPUT_AUDIO_MIME_TYPE);
    if (audioCodecInfo == null) {
        // Don't fail CTS if they don't have an AAC codec (not here, anyway).
        Log.e(TAG, "Unable to find an appropriate codec for " + OUTPUT_AUDIO_MIME_TYPE);
        throw new FileNotFoundException();
    }
    if (VERBOSE) Log.d(TAG, "audio found codec: " + audioCodecInfo.getName());

    final MediaFormat inputAudioFormat = mAudioExtractor.getTrackFormat(audioInputTrack);
    mInputDuration = inputAudioFormat.containsKey(MediaFormat.KEY_DURATION) ? inputAudioFormat.getLong(MediaFormat.KEY_DURATION) : 0;

    final MediaFormat outputAudioFormat =
            MediaFormat.createAudioFormat(
                    OUTPUT_AUDIO_MIME_TYPE,
                    inputAudioFormat.getInteger(MediaFormat.KEY_SAMPLE_RATE),
                    inputAudioFormat.getInteger(MediaFormat.KEY_CHANNEL_COUNT));
    outputAudioFormat.setInteger(MediaFormat.KEY_BIT_RATE, audioBitrate);
    outputAudioFormat.setInteger(MediaFormat.KEY_AAC_PROFILE, OUTPUT_AUDIO_AAC_PROFILE);
    outputAudioFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 16 * 1024);

    // Create a MediaCodec for the desired codec, then configure it as an encoder with
    // our desired properties. Request a Surface to use for input.
    mAudioEncoder = createAudioEncoder(audioCodecInfo, outputAudioFormat);
    // Create a MediaCodec for the decoder, based on the extractor's format.
    mAudioDecoder = createAudioDecoder(inputAudioFormat);

    mAudioDecoderInputBuffers = mAudioDecoder.getInputBuffers();
    mAudioDecoderOutputBuffers = mAudioDecoder.getOutputBuffers();
    mAudioEncoderInputBuffers = mAudioEncoder.getInputBuffers();
    mAudioEncoderOutputBuffers = mAudioEncoder.getOutputBuffers();
    mAudioDecoderOutputBufferInfo = new MediaCodec.BufferInfo();
    mAudioEncoderOutputBufferInfo = new MediaCodec.BufferInfo();

    if (mTimeFrom > 0) {
        mAudioExtractor.seekTo(mTimeFrom * 1000, MediaExtractor.SEEK_TO_PREVIOUS_SYNC);
        Log.i(TAG, "Seek audio:" + mTimeFrom + " " + mAudioExtractor.getSampleTime());
    }
}
 
Example 16
Source File: MediaCodecVideoRenderer.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
@TargetApi(21)
private static void configureTunnelingV21(MediaFormat mediaFormat, int tunnelingAudioSessionId) {
  mediaFormat.setFeatureEnabled(CodecCapabilities.FEATURE_TunneledPlayback, true);
  mediaFormat.setInteger(MediaFormat.KEY_AUDIO_SESSION_ID, tunnelingAudioSessionId);
}
 
Example 17
Source File: TransformationPresenter.java    From LiTr with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void startVideoOverlayTransformation(@NonNull SourceMedia sourceMedia,
                                            @NonNull TargetMedia targetMedia,
                                            @NonNull TargetVideoConfiguration targetVideoConfiguration,
                                            @NonNull TransformationState transformationState) {
    if (targetMedia.getIncludedTrackCount() < 1) {
        return;
    }

    if (targetMedia.targetFile.exists()) {
        targetMedia.targetFile.delete();
    }

    transformationState.requestId = UUID.randomUUID().toString();
    MediaTransformationListener transformationListener = new MediaTransformationListener(context,
            transformationState.requestId,
            transformationState);

    try {
        MediaTarget mediaTarget = new MediaMuxerMediaTarget(targetMedia.targetFile.getPath(),
                targetMedia.getIncludedTrackCount(),
                targetVideoConfiguration.rotation,
                MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);

        List<TrackTransform> trackTransforms = new ArrayList<>(targetMedia.tracks.size());
        MediaSource mediaSource = new MediaExtractorMediaSource(context, sourceMedia.uri);

        for (TargetTrack targetTrack : targetMedia.tracks) {
            if (!targetTrack.shouldInclude) {
                continue;
            }
            MediaFormat mediaFormat = createMediaFormat(targetTrack);
            if (mediaFormat != null && targetTrack.format instanceof VideoTrackFormat) {
                mediaFormat.setInteger(KEY_ROTATION, targetVideoConfiguration.rotation);
            }
            TrackTransform.Builder trackTransformBuilder = new TrackTransform.Builder(mediaSource,
                    targetTrack.sourceTrackIndex,
                    mediaTarget)
                    .setTargetTrack(trackTransforms.size())
                    .setTargetFormat(mediaFormat)
                    .setEncoder(new MediaCodecEncoder())
                    .setDecoder(new MediaCodecDecoder());
            if (targetTrack.format instanceof VideoTrackFormat) {
                // adding background bitmap first, to ensure that video renders on top of it
                List<GlFilter> filters = new ArrayList<>();
                if (targetMedia.backgroundImageUri != null) {
                    GlFilter backgroundImageFilter = TransformationUtil.createGlFilter(context,
                            targetMedia.backgroundImageUri,
                            new PointF(1, 1),
                            new PointF(0.5f, 0.5f),
                            0);
                    filters.add(backgroundImageFilter);
                }

                GlFrameRenderFilter frameRenderFilter = new FreeTransformFrameRenderFilter(new PointF(0.25f, 0.25f), new PointF(0.65f, 0.55f), 30);
                filters.add(frameRenderFilter);

                trackTransformBuilder.setRenderer(new GlVideoRenderer(filters));
            }

            trackTransforms.add(trackTransformBuilder.build());
        }

        mediaTransformer.transform(transformationState.requestId,
                trackTransforms,
                transformationListener,
                MediaTransformer.GRANULARITY_DEFAULT);
    } catch (MediaTransformationException ex) {
        Log.e(TAG, "Exception when trying to perform track operation", ex);
    }
}
 
Example 18
Source File: AudioEncoder.java    From DeviceConnect-Android with MIT License 4 votes vote down vote up
@Override
protected void prepare() throws IOException {
    AudioQuality audioQuality = getAudioQuality();

    String mimeType = audioQuality.getMimeType();

    List<MediaCodecInfo> infoList = getMediaCodecInfo(mimeType);
    if (infoList.isEmpty()) {
        throw new IOException(mimeType + " not supported.");
    }

    MediaFormat format = MediaFormat.createAudioFormat(audioQuality.getMimeType(),
            audioQuality.getSamplingRate(), audioQuality.getChannelCount());
    format.setString(MediaFormat.KEY_MIME, audioQuality.getMimeType());
    format.setInteger(MediaFormat.KEY_SAMPLE_RATE, audioQuality.getSamplingRate());
    format.setInteger(MediaFormat.KEY_BIT_RATE, audioQuality.getBitRate());
    format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, audioQuality.getChannelCount());
    format.setInteger(MediaFormat.KEY_CHANNEL_MASK, audioQuality.getChannel());
    format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
    if (audioQuality.getMaxInputSize() > 0) {
        format.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, audioQuality.getMaxInputSize());
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        // 0: realtime priority
        // 1: non-realtime priority (best effort).
        format.setInteger(MediaFormat.KEY_PRIORITY, 0x00);
    }

    if (DEBUG) {
        Log.d(TAG, "List of MediaCodeInfo supported by MediaCodec.");
        for (MediaCodecInfo info : infoList) {
            Log.d(TAG, "  " + info.getName());
        }
        Log.i(TAG, "---");
        Log.i(TAG, "MIME_TYPE: " + audioQuality.getMimeType());
        Log.i(TAG, "SAMPLE_RATE: " + audioQuality.getSamplingRate());
        Log.i(TAG, "CHANNEL: " + audioQuality.getChannelCount());
        Log.i(TAG, "FORMAT: " + audioQuality.getFormat());
        Log.i(TAG, "BIT_RATE: " + audioQuality.getBitRate());
        Log.i(TAG, "---");
    }

    mMediaCodec = MediaCodec.createEncoderByType(audioQuality.getMimeType());
    mMediaCodec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
}
 
Example 19
Source File: MediaFormatUtil.java    From MediaSDK with Apache License 2.0 2 votes vote down vote up
/**
 * Sets a {@link MediaFormat} integer value. Does nothing if {@code value} is {@link
 * Format#NO_VALUE}.
 *
 * @param format The {@link MediaFormat} being configured.
 * @param key The key to set.
 * @param value The value to set.
 */
public static void maybeSetInteger(MediaFormat format, String key, int value) {
  if (value != Format.NO_VALUE) {
    format.setInteger(key, value);
  }
}
 
Example 20
Source File: MediaFormatUtil.java    From TelePlus-Android with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Sets a {@link MediaFormat} integer value. Does nothing if {@code value} is {@link
 * Format#NO_VALUE}.
 *
 * @param format The {@link MediaFormat} being configured.
 * @param key The key to set.
 * @param value The value to set.
 */
public static void maybeSetInteger(MediaFormat format, String key, int value) {
  if (value != Format.NO_VALUE) {
    format.setInteger(key, value);
  }
}