mTrackIndex = mMuxer.addTrack(newFormat); mMuxer.start(); mMuxerStarted = true;
format.setString(MediaFormat.KEY_MIME, MediaFormat.MIMETYPE_AUDIO_AAC); int trackIndex = muxer.addTrack(format); muxer.start();
private void onSetOutputFormat() { if (mVideoFormat == null || mAudioFormat == null) return; mListener.onDetermineOutputFormat(); mVideoTrackIndex = mMuxer.addTrack(mVideoFormat); Log.v(TAG, "Added track #" + mVideoTrackIndex + " with " + mVideoFormat.getString(MediaFormat.KEY_MIME) + " to muxer"); mAudioTrackIndex = mMuxer.addTrack(mAudioFormat); Log.v(TAG, "Added track #" + mAudioTrackIndex + " with " + mAudioFormat.getString(MediaFormat.KEY_MIME) + " to muxer"); mMuxer.start(); mStarted = true; if (mByteBuffer == null) { mByteBuffer = ByteBuffer.allocate(0); } mByteBuffer.flip(); Log.v(TAG, "Output format determined, writing " + mSampleInfoList.size() + " samples / " + mByteBuffer.limit() + " bytes to muxer."); MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo(); int offset = 0; for (SampleInfo sampleInfo : mSampleInfoList) { sampleInfo.writeToBufferInfo(bufferInfo, offset); mMuxer.writeSampleData(getTrackIndexForSampleType(sampleInfo.mSampleType), mByteBuffer, bufferInfo); offset += sampleInfo.mSize; } mSampleInfoList.clear(); mByteBuffer = null; }
/** * assign encoder to muxer * @param format * @return minus value indicate error */ /*package*/ synchronized int addTrack(final MediaFormat format) { if (mIsStarted) throw new IllegalStateException("muxer already started"); final int trackIx = mMediaMuxer.addTrack(format); if (DEBUG) Log.i(TAG, "addTrack:trackNum=" + mEncoderCount + ",trackIx=" + trackIx + ",format=" + format); return trackIx; }
/** * assign encoder to muxer * @param format * @return minus value indicate error */ /*package*/ synchronized int addTrack(final MediaFormat format) { if (mIsStarted) throw new IllegalStateException("muxer already started"); final int trackIx = mMediaMuxer.addTrack(format); if (DEBUG) Log.i(TAG, "addTrack:trackNum=" + mEncoderCount + ",trackIx=" + trackIx + ",format=" + format); return trackIx; }
/** * assign encoder to muxer * @param format * @return minus value indicate error */ /*package*/ synchronized int addTrack(final MediaFormat format) { if (mIsStarted) throw new IllegalStateException("muxer already started"); final int trackIx = mMediaMuxer.addTrack(format); if (DEBUG) Log.i(TAG, "addTrack:trackNum=" + mEncoderCount + ",trackIx=" + trackIx + ",format=" + format); return trackIx; }
/** * assign encoder to muxer * @param format * @return minus value indicate error */ /*package*/ synchronized int addTrack(final MediaFormat format) { if (mIsStarted) throw new IllegalStateException("muxer already started"); final int trackIx = mMediaMuxer.addTrack(format); if (DEBUG) Log.i(TAG, "addTrack:trackNum=" + mEncoderCount + ",trackIx=" + trackIx + ",format=" + format); return trackIx; }
@Override public int addTrack(@NonNull final MediaFormat format) { return mMuxer.addTrack(format); }
public int addTrack(MediaFormat trackFormat) { if (mStarted) { throw new IllegalStateException(); } synchronized (mMuxer) { int track = mMuxer.addTrack(trackFormat); if (++mNumTracks == mExpectedNumTracks) { mMuxer.start(); mStarted = true; } return track; } }
public int addTrack(MediaFormat trackFormat) { if (mStarted) { throw new IllegalStateException(); } synchronized (mMuxer) { int track = mMuxer.addTrack(trackFormat); if (++mNumTracks == mExpectedNumTracks) { mMuxer.start(); mStarted = true; } return track; } }
/** * assign encoder to muxer * * @param format * @return minus value indicate error */ synchronized int addTrack(final MediaFormat format) { if (isStarted) { throw new IllegalStateException("muxer already started"); } final int trackIx = mediaMuxer.addTrack(format); Log.i(TAG, "addTrack:trackNum=" + encoderCount + ",trackIx=" + trackIx + ",format=" + format); String mime = format.getString(MediaFormat.KEY_MIME); if (!mime.startsWith("video/")) { audioTrackIndex = trackIx; } return trackIx; }
/** * assign encoder to muxer * * @param format * @return minus value indicate error */ synchronized int addTrack(final MediaFormat format) { if (isStarted) { throw new IllegalStateException("muxer already started"); } final int trackIx = mediaMuxer.addTrack(format); Log.i(TAG, "addTrack:trackNum=" + encoderCount + ",trackIx=" + trackIx + ",format=" + format); String mime = format.getString(MediaFormat.KEY_MIME); if (!mime.startsWith("video/")) { audioTrackIndex = trackIx; } return trackIx; }
@Override public void getVideoData(ByteBuffer h264Buffer, MediaCodec.BufferInfo info) { if (recording) { if (info.flags == MediaCodec.BUFFER_FLAG_KEY_FRAME && !canRecord && videoFormat != null && audioFormat != null) { videoTrack = mediaMuxer.addTrack(videoFormat); audioTrack = mediaMuxer.addTrack(audioFormat); mediaMuxer.start(); canRecord = true; } if (canRecord) mediaMuxer.writeSampleData(videoTrack, h264Buffer, info); } if (streaming) getH264DataRtp(h264Buffer, info); }
@Override public void getVideoData(ByteBuffer h264Buffer, MediaCodec.BufferInfo info) { if (recording) { if (info.flags == MediaCodec.BUFFER_FLAG_KEY_FRAME && !canRecord && videoFormat != null && audioFormat != null) { videoTrack = mediaMuxer.addTrack(videoFormat); audioTrack = mediaMuxer.addTrack(audioFormat); mediaMuxer.start(); canRecord = true; } if (canRecord) mediaMuxer.writeSampleData(videoTrack, h264Buffer, info); } if (streaming) getH264DataRtp(h264Buffer, info); }
@Override public void getVideoData(ByteBuffer h264Buffer, MediaCodec.BufferInfo info) { if (recording) { if (info.flags == MediaCodec.BUFFER_FLAG_KEY_FRAME && !canRecord && videoFormat != null && audioFormat != null) { videoTrack = mediaMuxer.addTrack(videoFormat); audioTrack = mediaMuxer.addTrack(audioFormat); mediaMuxer.start(); canRecord = true; } if (canRecord) mediaMuxer.writeSampleData(videoTrack, h264Buffer, info); } if (streaming) getH264DataRtp(h264Buffer, info); }
@Override public void getVideoData(ByteBuffer h264Buffer, MediaCodec.BufferInfo info) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 && recording) { if (info.flags == MediaCodec.BUFFER_FLAG_KEY_FRAME && !canRecord && videoFormat != null && audioFormat != null) { videoTrack = mediaMuxer.addTrack(videoFormat); audioTrack = mediaMuxer.addTrack(audioFormat); mediaMuxer.start(); canRecord = true; } if (canRecord) mediaMuxer.writeSampleData(videoTrack, h264Buffer, info); } if (streaming) getH264DataRtp(h264Buffer, info); }
private void resetOutputFormat() { // should happen before receiving buffers, and should only happen once if (mMuxerStarted) { throw new IllegalStateException("output format already changed!"); } MediaFormat newFormat = mMediaCodec.getOutputFormat(); Log.i(TAG, "output format changed.\n new format: " + newFormat.toString()); mVideoTrackIndex = mMuxer.addTrack(newFormat); mMuxer.start(); mMuxerStarted = true; Log.i(TAG, "started media muxer, videoIndex=" + mVideoTrackIndex); }
private void addTrackAndReadyToStart(int type) { if (trackCnt == 2) { return; } if (videoTrackIndex == null && type == FramePool.Frame.TYPE_VIDEO) { MediaFormat videoOutputMediaFormat = params.getVideoOutputMediaFormat(); videoOutputMediaFormat.getByteBuffer("csd-0"); // SPS videoOutputMediaFormat.getByteBuffer("csd-1"); // PPS videoTrackIndex = mMuxer.addTrack(videoOutputMediaFormat); trackCnt++; } else if (audioTrackIndex == null && type == FramePool.Frame.TYPE_AUDIO) { MediaFormat audioOutputMediaFormat = params.getAudioOutputMediaFormat(); audioTrackIndex = mMuxer.addTrack(audioOutputMediaFormat); trackCnt++; } if (trackCnt == 2) { frameSender.sendStartMessage(); } }
public boolean prepareVideo(){ try { mVideoBufferInfo = new MediaCodec.BufferInfo(); MediaFormat format = MediaFormat.createVideoFormat(MIME_TYPE, WIDTH, HEIGHT); //1. 设置一些属性。没有指定其中的一些可能会导致MediaCodec.configure()调用抛出一个无用的异常。 format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface); format.setInteger(MediaFormat.KEY_BIT_RATE, BIT_RATE);//比特率(比特率越高,音视频质量越高,编码文件越大) format.setInteger(MediaFormat.KEY_FRAME_RATE, FRAMES_PER_SECOND);//设置帧速 format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, IFRAME_INTERVAL);//设置关键帧间隔时间 //2.创建一个MediaCodec编码器,并配置格式。获取一个我们可以用于输入的表面,并将其封装到处理EGL工作的类中。 mVideoMediaCodec = MediaCodec.createEncoderByType("video/avc"); mVideoMediaCodec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE); mInputSurface = mVideoMediaCodec.createInputSurface(); mVideoMediaCodec.start(); MediaFormat newFormat = mVideoMediaCodec.getOutputFormat(); mWriteVideoTrackIndex = mediaMuxer.addTrack(newFormat); mediaMuxer.start(); } catch (IOException e) { e.printStackTrace(); } return true; }