android.media.MediaCodec Java Examples

The following examples show how to use android.media.MediaCodec. 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: UdpReceiverDecoderThread.java    From myMediaCodecPlayer-for-FPV with MIT License 7 votes vote down vote up
public UdpReceiverDecoderThread(Surface surface1, int port, Context context) {
    surface=surface1;
    mContext = context;
    this.port = port;
    nalu_data = new byte[NALU_MAXLEN];
    nalu_data_position = 0;
    settings= PreferenceManager.getDefaultSharedPreferences(mContext);
    DecoderMultiThread=settings.getBoolean("decoderMultiThread", true);
    userDebug=settings.getBoolean("userDebug", false);
    groundRecord=settings.getBoolean("groundRecording", false);
    if(userDebug){
        SharedPreferences.Editor editor=settings.edit();
        if(settings.getString("debugFile","").length()>=5000){
            editor.putString("debugFile","new Session !\n");
        }else{
            editor.putString("debugFile",settings.getString("debugFile","")+"\n\nnew Session !\n");
        }
        editor.commit();
    }
    info = new MediaCodec.BufferInfo();
}
 
Example #2
Source File: MediaCodecDecoder.java    From MediaPlayer-Extended with Apache License 2.0 7 votes vote down vote up
public MediaCodecDecoder(MediaExtractor extractor, boolean passive, int trackIndex,
                         OnDecoderEventListener listener)
        throws IllegalStateException, IOException
{
    // Apply the name of the concrete class that extends this base class to the logging tag
    // THis is really not a nice solution but there's no better one: http://stackoverflow.com/a/936724
    TAG = getClass().getSimpleName();

    if(extractor == null || trackIndex == INDEX_NONE) {
        throw new IllegalArgumentException("no track specified");
    }

    mExtractor = extractor;
    mPassive = passive;
    mTrackIndex = trackIndex;
    mFormat = extractor.getTrackFormat(mTrackIndex);

    mOnDecoderEventListener = listener;

    mCodec = MediaCodec.createDecoderByType(mFormat.getString(MediaFormat.KEY_MIME));

    mDecodingPTS = PTS_NONE;
}
 
Example #3
Source File: TLMediaVideoEncoder.java    From TimeLapseRecordingSample with Apache License 2.0 7 votes vote down vote up
@Override
protected MediaCodec internal_configure(MediaCodec previous_codec,
	final MediaFormat format) throws IOException {

	if (DEBUG) Log.v(TAG, "internal_configure:");
	format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);	// API >= 18
	format.setInteger(MediaFormat.KEY_BIT_RATE, mBitRate > 0 ? mBitRate : calcBitRate());
	format.setInteger(MediaFormat.KEY_FRAME_RATE, mFrameRate);
	format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, mIFrameIntervals);
	if (DEBUG) Log.i(TAG, "format: " + format);

	if (previous_codec == null)
		previous_codec = MediaCodec.createEncoderByType(MIME_TYPE);
	previous_codec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
	mSurface = previous_codec.createInputSurface();	// API >= 18
	return previous_codec;
}
 
Example #4
Source File: AudioTrackTranscoderShould.java    From LiTr with BSD 2-Clause "Simplified" License 7 votes vote down vote up
@Test
public void addTrackWhenEncoderMediaFormatReceived() throws Exception {
    audioTrackTranscoder.lastExtractFrameResult = TrackTranscoder.RESULT_EOS_REACHED;
    audioTrackTranscoder.lastDecodeFrameResult = TrackTranscoder.RESULT_EOS_REACHED;
    audioTrackTranscoder.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(AUDIO_TRACK).when(mediaTarget).addTrack(any(MediaFormat.class), anyInt());

    int result = audioTrackTranscoder.processNextFrame();

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

    assertThat(audioTrackTranscoder.targetTrack, is(AUDIO_TRACK));
    assertThat(result, is(TrackTranscoder.RESULT_OUTPUT_MEDIA_FORMAT_CHANGED));
}
 
Example #5
Source File: MediaEncoder.java    From PLDroidShortVideo with Apache License 2.0 7 votes vote down vote up
public MediaEncoder(final MediaMuxerWrapper muxer, final MediaEncoderListener listener) {
    if (listener == null) throw new NullPointerException("MediaDecoderListener is null");
    if (muxer == null) throw new NullPointerException("MediaExtractorWrapper is null");
    mWeakMuxer = new WeakReference<MediaMuxerWrapper>(muxer);
    muxer.addEncoder(this);
    mListener = listener;
    synchronized (mLock) {
        // create BufferInfo here for effectiveness(to reduce GC)
        mBufferInfo = new MediaCodec.BufferInfo();
        // wait for starting thread
        new Thread(this, getClass().getSimpleName()).start();
        try {
            mLock.wait();
        } catch (final InterruptedException e) {
        }
    }
}
 
Example #6
Source File: SurfaceEncoder.java    From AAVT with Apache License 2.0 7 votes vote down vote up
private void openVideoEncoder(){
    AvLog.d(TAG,"openVideoEncoder startTime-->");
    if(mVideoEncoder==null){
        try {
            MediaFormat format=convertVideoConfigToFormat(mConfig.mVideo);
            mVideoEncoder= MediaCodec.createEncoderByType(mConfig.mVideo.mime);
            mVideoEncoder.configure(format,null,null,MediaCodec.CONFIGURE_FLAG_ENCODE);
            super.setSurface(mVideoEncoder.createInputSurface());
            super.setOutputSize(mConfig.mVideo.width,mConfig.mVideo.height);
            mVideoEncoder.start();
            isEncodeStarted=true;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    AvLog.d(TAG,"openVideoEncoder endTime-->");
}
 
Example #7
Source File: EncodedAudioRecorder.java    From AlexaAndroid with GNU General Public License v2.0 7 votes vote down vote up
@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);
                }
                break; // TODO: we use the first one that is suitable
            }
        }
    }
}
 
Example #8
Source File: ScreenRecordThread.java    From ScreenCapture with MIT License 7 votes vote down vote up
/**
 * 硬解码获取实时帧数据并写入mp4文件
 *
 * @param index
 */
private void encodeToVideoTrack(int index) {
    // 获取到的实时帧视频数据
    ByteBuffer encodedData = mEncoder.getOutputBuffer(index);

    if ((mBufferInfo.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {
        // The codec config data was pulled out and fed to the muxer
        // when we got
        // the INFO_OUTPUT_FORMAT_CHANGED status.
        // Ignore it.
        Log.d(TAG, "ignoring BUFFER_FLAG_CODEC_CONFIG");
        mBufferInfo.size = 0;
    }
    if (mBufferInfo.size == 0) {
        Log.d(TAG, "info.size == 0, drop it.");
        encodedData = null;
    } else {
        Log.d(TAG, "got buffer, info: size=" + mBufferInfo.size + ", presentationTimeUs="
                + mBufferInfo.presentationTimeUs + ", offset=" + mBufferInfo.offset);
    }
    if (encodedData != null) {
        mMuxer.writeSampleData(mVideoTrackIndex, encodedData, mBufferInfo);
    }
}
 
Example #9
Source File: ScreenRecordThread.java    From ScreenCapture with MIT License 7 votes vote down vote up
private void recordVirtualDisplay() {
    while (!mQuit.get()) {
        int index = mEncoder.dequeueOutputBuffer(mBufferInfo, TIMEOUT_US);
        Log.i(TAG, "dequeue output buffer index=" + index);
        if (index == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
            // 后续输出格式变化
            resetOutputFormat();
        } else if (index == MediaCodec.INFO_TRY_AGAIN_LATER) {
            // 请求超时
            Log.d(TAG, "retrieving buffers time out!");
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
            }
        } else if (index >= 0) {
            // 有效输出
            if (!mMuxerStarted) {
                Log.d(TAG, "mMuxerStarted not started");
                throw new IllegalStateException("MediaMuxer dose not call addTrack(format) ");
            }
            encodeToVideoTrack(index);

            mEncoder.releaseOutputBuffer(index, false);
        }
    }
}
 
Example #10
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 #11
Source File: StreamingAudioEncoder.java    From live-transcribe-speech-engine with Apache License 2.0 7 votes vote down vote up
@Override
public void init(int sampleRateHz, CodecAndBitrate codecAndBitrate, boolean allowVbr)
    throws EncoderException, IOException {
  codecType = lookupCodecType(codecAndBitrate);
  if (codecType == CodecType.UNSPECIFIED || codecType == CodecType.OGG_OPUS) {
    throw new EncoderException("Codec not set properly.");
  }
  if (codecType == CodecType.AMRWB && sampleRateHz != 16000) {
    throw new EncoderException("AMR-WB encoder requires a sample rate of 16kHz.");
  }
  MediaCodecInfo codecInfo = searchAmongAndroidSupportedCodecs(getMime(codecType));
  if (codecInfo == null) {
    throw new EncoderException("Encoder not found.");
  }
  this.codec = MediaCodec.createByCodecName(codecInfo.getName());

  MediaFormat format = getMediaFormat(codecAndBitrate, sampleRateHz);
  codec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
  codec.start();
  initBuffers();

  addedHeader = false;
  successfullyFlushed = false;
  formatChangeReportedOnce = false;
}
 
Example #12
Source File: TestAudioEncoder.java    From AndroidInstantVideo with Apache License 2.0 7 votes vote down vote up
public void write() {
    MediaCodecInputStream mediaCodecInputStream = aacEncoder.getMediaCodecInputStream();
    MediaCodecInputStream.readAll(mediaCodecInputStream, writeBuffer, new MediaCodecInputStream.OnReadAllCallback() {
        boolean shouldAddPacketHeader = true;
        byte[] header = new byte[7];
        @Override
        public void onReadOnce(byte[] buffer, int readSize, MediaCodec.BufferInfo bufferInfo) {
            if (readSize <= 0) {
                return;
            }
            try {
                Loggers.d("TestAudioEncoder", String.format("onReadOnce: readSize:%d, bufferInfo:%d", readSize, bufferInfo.size));
                if (shouldAddPacketHeader) {
                    Loggers.d("TestAudioEncoder", String.format("onReadOnce: add packet header"));
                    AACEncoder.addADTStoPacket(header, 7 + bufferInfo.size);
                    os.write(header);
                }
                os.write(buffer, 0, readSize);
                os.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
            shouldAddPacketHeader = readSize >= bufferInfo.size;
        }
    });
}
 
Example #13
Source File: MediaCodecRenderer.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param trackType The track type that the renderer handles. One of the {@code C.TRACK_TYPE_*}
 *     constants defined in {@link C}.
 * @param mediaCodecSelector A decoder selector.
 * @param drmSessionManager For use with encrypted media. May be null if support for encrypted
 *     media is not required.
 * @param playClearSamplesWithoutKeys Encrypted media may contain clear (un-encrypted) regions.
 *     For example a media file may start with a short clear region so as to allow playback to
 *     begin in parallel with key acquisition. This parameter specifies whether the renderer is
 *     permitted to play clear regions of encrypted media files before {@code drmSessionManager}
 *     has obtained the keys necessary to decrypt encrypted regions of the media.
 * @param assumedMinimumCodecOperatingRate A codec operating rate that all codecs instantiated by
 *     this renderer are assumed to meet implicitly (i.e. without the operating rate being set
 *     explicitly using {@link MediaFormat#KEY_OPERATING_RATE}).
 */
public MediaCodecRenderer(
    int trackType,
    MediaCodecSelector mediaCodecSelector,
    @Nullable DrmSessionManager<FrameworkMediaCrypto> drmSessionManager,
    boolean playClearSamplesWithoutKeys,
    float assumedMinimumCodecOperatingRate) {
  super(trackType);
  Assertions.checkState(Util.SDK_INT >= 16);
  this.mediaCodecSelector = Assertions.checkNotNull(mediaCodecSelector);
  this.drmSessionManager = drmSessionManager;
  this.playClearSamplesWithoutKeys = playClearSamplesWithoutKeys;
  this.assumedMinimumCodecOperatingRate = assumedMinimumCodecOperatingRate;
  buffer = new DecoderInputBuffer(DecoderInputBuffer.BUFFER_REPLACEMENT_MODE_DISABLED);
  flagsOnlyBuffer = DecoderInputBuffer.newFlagsOnlyInstance();
  formatHolder = new FormatHolder();
  decodeOnlyPresentationTimestamps = new ArrayList<>();
  outputBufferInfo = new MediaCodec.BufferInfo();
  codecReconfigurationState = RECONFIGURATION_STATE_NONE;
  codecReinitializationState = REINITIALIZATION_STATE_NONE;
  codecOperatingRate = CODEC_OPERATING_RATE_UNSET;
  rendererOperatingRate = 1f;
}
 
Example #14
Source File: MediaCodecVideoRenderer.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected @KeepCodecResult int canKeepCodec(
    MediaCodec codec, MediaCodecInfo codecInfo, Format oldFormat, Format newFormat) {
  if (codecInfo.isSeamlessAdaptationSupported(
          oldFormat, newFormat, /* isNewFormatComplete= */ true)
      && newFormat.width <= codecMaxValues.width
      && newFormat.height <= codecMaxValues.height
      && getMaxInputSize(codecInfo, newFormat) <= codecMaxValues.inputSize) {
    return oldFormat.initializationDataEquals(newFormat)
        ? KEEP_CODEC_RESULT_YES_WITHOUT_RECONFIGURATION
        : KEEP_CODEC_RESULT_YES_WITH_RECONFIGURATION;
  }
  return KEEP_CODEC_RESULT_NO;
}
 
Example #15
Source File: MediaCodecVideoTrackRenderer.java    From Exoplayer_VLC with Apache License 2.0 6 votes vote down vote up
@TargetApi(21)
private void renderOutputBufferTimedV21(MediaCodec codec, int bufferIndex, long releaseTimeNs) {
  maybeNotifyVideoSizeChanged();
  TraceUtil.beginSection("releaseOutputBufferTimed");
  codec.releaseOutputBuffer(bufferIndex,false);// releaseTimeNs);
  TraceUtil.endSection();
  codecCounters.renderedOutputBufferCount++;
  maybeNotifyDrawnToSurface();
}
 
Example #16
Source File: MediaCodecVideoRenderer.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) {
  String codecMimeType = codecInfo.codecMimeType;
  codecMaxValues = getCodecMaxValues(codecInfo, format, getStreamFormats());
  MediaFormat mediaFormat =
      getMediaFormat(
          format,
          codecMimeType,
          codecMaxValues,
          codecOperatingRate,
          deviceNeedsNoPostProcessWorkaround,
          tunnelingAudioSessionId);
  if (surface == null) {
    Assertions.checkState(shouldUseDummySurface(codecInfo));
    if (dummySurface == null) {
      dummySurface = DummySurface.newInstanceV17(context, codecInfo.secure);
    }
    surface = dummySurface;
  }
  codec.configure(mediaFormat, surface, crypto, 0);
  if (Util.SDK_INT >= 23 && tunneling) {
    tunnelingOnFrameRenderedListener = new OnFrameRenderedListenerV23(codec);
  }
}
 
Example #17
Source File: MediaEncoder.java    From MegviiFacepp-Android-SDK with Apache License 2.0 6 votes vote down vote up
/**
     * Method to set byte array to the MediaCodec encoder
     * @param buffer
     * @param length length of byte array, zero means EOS.
     * @param presentationTimeUs
     */
    protected void encode(final ByteBuffer buffer, final int length, final long presentationTimeUs) {
    	if (!mIsCapturing) return;
        final ByteBuffer[] inputBuffers = mMediaCodec.getInputBuffers();
        while (mIsCapturing) {
	        final int inputBufferIndex = mMediaCodec.dequeueInputBuffer(TIMEOUT_USEC);
	        if (inputBufferIndex >= 0) {
	            final ByteBuffer inputBuffer = inputBuffers[inputBufferIndex];
	            inputBuffer.clear();
	            if (buffer != null) {
	            	inputBuffer.put(buffer);
	            }
//	            if (DEBUG) Log.v(TAG, "encode:queueInputBuffer");
	            if (length <= 0) {
	            	// send EOS
	            	mIsEOS = true;
	            	if (DEBUG) Log.i(TAG, "send BUFFER_FLAG_END_OF_STREAM");
	            	mMediaCodec.queueInputBuffer(inputBufferIndex, 0, 0,
	            		presentationTimeUs, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
		            break;
	            } else {
	            	mMediaCodec.queueInputBuffer(inputBufferIndex, 0, length,
	            		presentationTimeUs, 0);
	            }
	            break;
	        } else if (inputBufferIndex == MediaCodec.INFO_TRY_AGAIN_LATER) {
	        	// wait for MediaCodec encoder is ready to encode
	        	// nothing to do here because MediaCodec#dequeueInputBuffer(TIMEOUT_USEC)
	        	// will wait for maximum TIMEOUT_USEC(10msec) on each call
	        }
        }
    }
 
Example #18
Source File: MediaCodecVideoRenderer.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected @KeepCodecResult int canKeepCodec(
    MediaCodec codec, MediaCodecInfo codecInfo, Format oldFormat, Format newFormat) {
  if (areAdaptationCompatible(codecInfo.adaptive, oldFormat, newFormat)
      && newFormat.width <= codecMaxValues.width
      && newFormat.height <= codecMaxValues.height
      && getMaxInputSize(codecInfo, newFormat) <= codecMaxValues.inputSize) {
    return oldFormat.initializationDataEquals(newFormat)
        ? KEEP_CODEC_RESULT_YES_WITHOUT_RECONFIGURATION
        : KEEP_CODEC_RESULT_YES_WITH_RECONFIGURATION;
  }
  return KEEP_CODEC_RESULT_NO;
}
 
Example #19
Source File: AudioTrackTranscoder.java    From EZFilter with MIT License 6 votes vote down vote up
@Override
public void setup() throws IOException {
    mExtractor.selectTrack(mTrackIndex);
    mEncoder = MediaCodec.createEncoderByType(mOutputFormat.getString(MediaFormat.KEY_MIME));
    mEncoder.configure(mOutputFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
    mEncoder.start();
    mEncoderStarted = true;

    final MediaFormat inputFormat = mExtractor.getTrackFormat(mTrackIndex);
    mDecoder = MediaCodec.createDecoderByType(inputFormat.getString(MediaFormat.KEY_MIME));
    mDecoder.configure(inputFormat, null, null, 0);
    mDecoder.start();
    mDecoderStarted = true;

    mAudioChannel = new AudioChannel(mDecoder, mEncoder, mOutputFormat);
}
 
Example #20
Source File: MediaCodecWrapper.java    From patrol-android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Write a media sample to the decoder.
 *
 * A "sample" here refers to a single atomic access unit in the media stream. The definition
 * of "access unit" is dependent on the type of encoding used, but it typically refers to
 * a single frame of video or a few seconds of audio. {@link MediaExtractor}
 * extracts data from a stream one sample at a time.
 *
 * @param extractor  Instance of {@link MediaExtractor} wrapping the media.
 *
 * @param presentationTimeUs The time, relative to the beginning of the media stream,
 * at which this buffer should be rendered.
 *
 * @param flags  Flags to pass to the decoder. See {@link MediaCodec#queueInputBuffer(int,
 * int, int, long, int)}
 *
 * @throws MediaCodec.CryptoException
 */
public boolean writeSample(final MediaExtractor extractor,
        final boolean isSecure,
        final long presentationTimeUs,
        int flags) {
    boolean result = false;
    boolean isEos = false;

    if (!mAvailableInputBuffers.isEmpty()) {
        int index = mAvailableInputBuffers.remove();
        ByteBuffer buffer = mInputBuffers[index];

        // reads the sample from the file using extractor into the buffer
        int size = extractor.readSampleData(buffer, 0);
        if (size <= 0) {
            flags |= MediaCodec.BUFFER_FLAG_END_OF_STREAM;
        }

        // Submit the buffer to the codec for decoding. The presentationTimeUs
        // indicates the position (play time) for the current sample.
        if (!isSecure) {
            mDecoder.queueInputBuffer(index, 0, size, presentationTimeUs, flags);
        } else {
            extractor.getSampleCryptoInfo(cryptoInfo);
            mDecoder.queueSecureInputBuffer(index, 0, cryptoInfo, presentationTimeUs, flags);
        }

        result = true;
    }
    return result;
}
 
Example #21
Source File: ExtractorRendererBuilder.java    From AndroidTvDemo with Apache License 2.0 6 votes vote down vote up
@Override
public void buildRenderers(DemoPlayer player) {
  Allocator allocator = new DefaultAllocator(BUFFER_SEGMENT_SIZE);
  Handler mainHandler = player.getMainHandler();

  // Build the video and audio renderers.
  DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(mainHandler, null);
  DataSource dataSource = new DefaultUriDataSource(context, bandwidthMeter, userAgent);
  ExtractorSampleSource sampleSource = new ExtractorSampleSource(uri, dataSource, allocator,
      BUFFER_SEGMENT_COUNT * BUFFER_SEGMENT_SIZE, mainHandler, player, 0);
  MediaCodecVideoTrackRenderer videoRenderer = new MediaCodecVideoTrackRenderer(context,
      sampleSource, MediaCodecSelector.DEFAULT, MediaCodec.VIDEO_SCALING_MODE_SCALE_TO_FIT, 5000,
      mainHandler, player, 50);
  MediaCodecAudioTrackRenderer audioRenderer = new MediaCodecAudioTrackRenderer(sampleSource,
      MediaCodecSelector.DEFAULT, null, true, mainHandler, player,
      AudioCapabilities.getCapabilities(context), AudioManager.STREAM_MUSIC);
  TrackRenderer textRenderer = new TextTrackRenderer(sampleSource, player,
      mainHandler.getLooper());

  // Invoke the callback.
  TrackRenderer[] renderers = new TrackRenderer[DemoPlayer.RENDERER_COUNT];
  renderers[DemoPlayer.TYPE_VIDEO] = videoRenderer;
  renderers[DemoPlayer.TYPE_AUDIO] = audioRenderer;
  renderers[DemoPlayer.TYPE_TEXT] = textRenderer;
  player.onRenderers(renderers, bandwidthMeter);
}
 
Example #22
Source File: Mp4Processor.java    From AAVT with Apache License 2.0 5 votes vote down vote up
private ByteBuffer getInputBuffer(MediaCodec codec, int index){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        return codec.getInputBuffer(index);
    }else{
        return codec.getInputBuffers()[index];
    }
}
 
Example #23
Source File: Mp4Movie.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
public void addSample(int trackIndex, long offset, MediaCodec.BufferInfo bufferInfo) {
    if (trackIndex < 0 || trackIndex >= tracks.size()) {
        return;
    }
    Track track = tracks.get(trackIndex);
    track.addSample(offset, bufferInfo);
}
 
Example #24
Source File: RecordController.java    From rtmp-rtsp-stream-client-java with Apache License 2.0 5 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public void recordAudio(ByteBuffer audioBuffer, MediaCodec.BufferInfo audioInfo) {
  if (status == Status.RECORDING) {
    updateFormat(this.audioInfo, audioInfo);
    mediaMuxer.writeSampleData(audioTrack, audioBuffer, this.audioInfo);
  }
}
 
Example #25
Source File: SdlEncoder.java    From sdl_java_suite with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Surface prepareEncoder () {

		mBufferInfo = new MediaCodec.BufferInfo();

		MediaFormat format = MediaFormat.createVideoFormat(_MIME_TYPE, frameWidth,
				frameHeight);

		// 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, frameRate);
		format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, frameInterval);

		// 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.
		//
		// If you want to have two EGL contexts -- one for display, one for
		// recording --
		// you will likely want to defer instantiation of CodecInputSurface
		// until after the
		// "display" EGL context is created, then modify the eglCreateContext
		// call to
		// take eglGetCurrentContext() as the share_context argument.
		try {
			mEncoder = MediaCodec.createEncoderByType(_MIME_TYPE);
		} catch (Exception e) {e.printStackTrace();}

		if(mEncoder != null) {
		   mEncoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
           return mEncoder.createInputSurface();
		} else {
			return null;
		}
	}
 
Example #26
Source File: VideoComposer.java    From GPUVideo-android with MIT License 5 votes vote down vote up
private int drainEncoder() {
    if (isEncoderEOS) return DRAIN_STATE_NONE;
    int result = encoder.dequeueOutputBuffer(bufferInfo, 0);
    switch (result) {
        case MediaCodec.INFO_TRY_AGAIN_LATER:
            return DRAIN_STATE_NONE;
        case MediaCodec.INFO_OUTPUT_FORMAT_CHANGED:
            if (actualOutputFormat != null) {
                throw new RuntimeException("Video output format changed twice.");
            }
            actualOutputFormat = encoder.getOutputFormat();
            muxRender.setOutputFormat(MuxRender.SampleType.VIDEO, actualOutputFormat);
            muxRender.onSetOutputFormat();
            return DRAIN_STATE_SHOULD_RETRY_IMMEDIATELY;
        case MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED:
            encoderOutputBuffers = encoder.getOutputBuffers();
            return DRAIN_STATE_SHOULD_RETRY_IMMEDIATELY;
    }
    if (actualOutputFormat == null) {
        throw new RuntimeException("Could not determine actual output format.");
    }

    if ((bufferInfo.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
        isEncoderEOS = true;
        bufferInfo.set(0, 0, 0, bufferInfo.flags);
    }
    if ((bufferInfo.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {
        // SPS or PPS, which should be passed by MediaFormat.
        encoder.releaseOutputBuffer(result, false);
        return DRAIN_STATE_SHOULD_RETRY_IMMEDIATELY;
    }
    muxRender.writeSampleData(MuxRender.SampleType.VIDEO, encoderOutputBuffers[result], bufferInfo);
    writtenPresentationTimeUs = bufferInfo.presentationTimeUs;
    encoder.releaseOutputBuffer(result, false);
    return DRAIN_STATE_CONSUMED;
}
 
Example #27
Source File: MicrophoneEncoder.java    From cineio-broadcast-android with MIT License 5 votes vote down vote up
private void sendAudioToEncoder(boolean endOfStream) {
        // send current frame data to encoder
        try {
            ByteBuffer[] inputBuffers = mMediaCodec.getInputBuffers();
            audioInputBufferIndex = mMediaCodec.dequeueInputBuffer(-1);
            if (audioInputBufferIndex >= 0) {
                ByteBuffer inputBuffer = inputBuffers[audioInputBufferIndex];
                inputBuffer.clear();
                audioInputLength = mAudioRecord.read(inputBuffer, SAMPLES_PER_FRAME * 2);
                audioAbsolutePtsUs = (System.nanoTime()) / 1000L;
                // We divide audioInputLength by 2 because audio samples are
                // 16bit.
                audioAbsolutePtsUs = getJitterFreePTS(audioAbsolutePtsUs, audioInputLength / 2);

                if (audioInputLength == AudioRecord.ERROR_INVALID_OPERATION)
                    Log.e(TAG, "Audio read error: invalid operation");
                if (audioInputLength == AudioRecord.ERROR_BAD_VALUE)
                    Log.e(TAG, "Audio read error: bad value");
//                if (VERBOSE)
//                    Log.i(TAG, "queueing " + audioInputLength + " audio bytes with pts " + audioAbsolutePtsUs);
                if (endOfStream) {
                    if (VERBOSE) Log.i(TAG, "EOS received in sendAudioToEncoder");
                    mMediaCodec.queueInputBuffer(audioInputBufferIndex, 0, audioInputLength, audioAbsolutePtsUs, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
                } else {
                    mMediaCodec.queueInputBuffer(audioInputBufferIndex, 0, audioInputLength, audioAbsolutePtsUs, 0);
                }
            }
        } catch (Throwable t) {
            Log.e(TAG, "_offerAudioEncoder exception");
            t.printStackTrace();
        }
    }
 
Example #28
Source File: PlayerActivity.java    From android-tv-leanback with Apache License 2.0 5 votes vote down vote up
private void preparePlayer() {
    Log.d(TAG, "preparePlayer()");
    SampleSource sampleSource =
            new FrameworkSampleSource(this, Uri.parse(mVideo.getContentUrl()), /* headers */ null, RENDERER_COUNT);

    // Build the track renderers
    videoRenderer = new MediaCodecVideoTrackRenderer(sampleSource, MediaCodec.VIDEO_SCALING_MODE_SCALE_TO_FIT);
    TrackRenderer audioRenderer = new MediaCodecAudioTrackRenderer(sampleSource);

    // Setup the player
    player = ExoPlayer.Factory.newInstance(RENDERER_COUNT, 1000, 5000);
    player.addListener(this);
    player.prepare(videoRenderer, audioRenderer);
    if (mIsOnTv) {
        // This PlayerControl must stay in sync with PlaybackOverlayFragment.
        // We created methods such as PlaybackOverlayFragment.pressPlay() to request
        // that the fragment change the playback state. When the fragment receives a playback
        // request, it updates the UI and then calls a method in this activity according to
        // PlaybackOverlayFragment.OnPlayPauseClickedListener.
        playerControl = new PlayerControl(player);
    } else {
        // Build the player controls
        mediaController.setMediaPlayer(new PlayerControl(player));
        mediaController.setEnabled(true);
    }
    maybeStartPlayback();
}
 
Example #29
Source File: EncoderDebugger.java    From spydroid-ipcamera with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Instantiates and starts the encoder.
 */
private void configureEncoder()  {
	mEncoder = MediaCodec.createByCodecName(mEncoderName);
	MediaFormat mediaFormat = MediaFormat.createVideoFormat(MIME_TYPE, mWidth, mHeight);
	mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, BITRATE);
	mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, FRAMERATE);	
	mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, mEncoderColorFormat);
	mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1);
	mEncoder.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
	mEncoder.start();
}
 
Example #30
Source File: MediaCodecAudioRenderer.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected boolean processOutputBuffer(long positionUs, long elapsedRealtimeUs, MediaCodec codec,
    ByteBuffer buffer, int bufferIndex, int bufferFlags, long bufferPresentationTimeUs,
    boolean shouldSkip) throws ExoPlaybackException {
  if (passthroughEnabled && (bufferFlags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {
    // Discard output buffers from the passthrough (raw) decoder containing codec specific data.
    codec.releaseOutputBuffer(bufferIndex, false);
    return true;
  }

  if (shouldSkip) {
    codec.releaseOutputBuffer(bufferIndex, false);
    decoderCounters.skippedOutputBufferCount++;
    audioSink.handleDiscontinuity();
    return true;
  }

  try {
    if (audioSink.handleBuffer(buffer, bufferPresentationTimeUs)) {
      codec.releaseOutputBuffer(bufferIndex, false);
      decoderCounters.renderedOutputBufferCount++;
      return true;
    }
  } catch (AudioSink.InitializationException | AudioSink.WriteException e) {
    throw ExoPlaybackException.createForRenderer(e, getIndex());
  }
  return false;
}