问题描述
我想在视频被Android设备的摄像头捕获时进行加密,因为媒体播放器不允许我直接处理字节或缓冲区,所以我将使用Media Codec和Media muxer。 Media muxer发生的问题是通过参数输出文件阻止了我之前对信息进行加密,因此,我的问题是,是否有任何方法可以使用此方法或另一种方法将信息传递给输出文件之前进行加密。
public class VideoEncoderCore {
private static final String TAG = "";
private static final boolean VERBOSE = false;
// Todo: these ought to be configurable as well
private static final String MIME_TYPE = "video/avc"; // H.264 Advanced Video Coding
private static final int FRAME_RATE = 30; // 30fps
private static final int IFRAME_INTERVAL = 5; // 5 seconds between I-frames
private Surface mInputSurface;
private Mediamuxer mmuxer;
private MediaCodec mEncoder;
private MediaCodec.BufferInfo mBufferInfo;
private int mTrackIndex;
private boolean mmuxerStarted;
/**
* Configures encoder and muxer state,and prepares the input Surface.
*/
public VideoEncoderCore(int width,int height,int bitRate,File outputFile)
throws IOException {
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,MediaCodec.CONfigURE_FLAG_ENCODE);
mInputSurface = mEncoder.createInputSurface();
mEncoder.start();
// Create a Mediamuxer. We can't add the video track and start() the muxer here,// because our MediaFormat doesn't have the Magic Goodies. These can only be
// obtained from the encoder after it has started processing data.
//
// We're not actually interested in multiplexing audio. We just want to convert
// the raw H.264 elementary stream we get from MediaCodec into a .mp4 file.
mmuxer = new Mediamuxer(outputFile.toString(),Mediamuxer.OutputFormat.muxer_OUTPUT_MPEG_4);
mTrackIndex = -1;
mmuxerStarted = false;
}
/**
* Returns the encoder's input surface.
*/
public Surface getInputSurface() {
return mInputSurface;
}
/**
* Releases encoder resources.
*/
public void release() {
if (VERBOSE) Log.d(TAG,"releasing encoder objects");
if (mEncoder != null) {
mEncoder.stop();
mEncoder.release();
mEncoder = null;
}
if (mmuxer != null) {
// Todo: stop() throws an exception if you haven't fed it any data. Keep track
// of frames submitted,and don't call stop() if we haven't written anything.
mmuxer.stop();
mmuxer.release();
mmuxer = null;
}
}
/**
* Extracts all pending data from the encoder and forwards it to the muxer.
* <p>
* If endOfStream is not set,this returns when there is no more data to drain. If it
* is set,we send EOS to the encoder,and then iterate until we see EOS on the output.
* Calling this with endOfStream set should be done once,right before stopping the muxer.
* <p>
* We're just using the muxer to get a .mp4 file (instead of a raw H.264 stream). We're
* not recording audio.
*/
public void drainEncoder(boolean endOfStream) {
final int TIMEOUT_USEC = 10000;
if (VERBOSE) Log.d(TAG,"drainEncoder(" + endOfStream + ")");
if (endOfStream) {
if (VERBOSE) Log.d(TAG,"sending EOS to encoder");
mEncoder.signalEndOfInputStream();
}
ByteBuffer[] encoderOutputBuffers = mEncoder.getoutputBuffers();
while (true) {
int encoderStatus = mEncoder.dequeueOutputBuffer(mBufferInfo,TIMEOUT_USEC);
if (encoderStatus == MediaCodec.INFO_TRY_AGAIN_LATER) {
// no output available yet
if (!endOfStream) {
break; // out of while
} else {
if (VERBOSE) Log.d(TAG,"no output available,spinning to await EOS");
}
} else if (encoderStatus == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
// not expected for an encoder
encoderOutputBuffers = mEncoder.getoutputBuffers();
} else if (encoderStatus == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
// should happen before receiving buffers,and should only happen once
if (mmuxerStarted) {
throw new RuntimeException("format changed twice");
}
MediaFormat newFormat = mEncoder.getoutputFormat();
Log.d(TAG,"encoder output format changed: " + newFormat);
// Now that we have the Magic Goodies,start the muxer
mTrackIndex = mmuxer.addTrack(newFormat);
mmuxer.start();
mmuxerStarted = true;
} else if (encoderStatus < 0) {
Log.w(TAG,"unexpected result from encoder.dequeueOutputBuffer: " +
encoderStatus);
// let's ignore it
} else {
ByteBuffer encodedData = encoderOutputBuffers[encoderStatus];
if (encodedData == null) {
throw new RuntimeException("encoderOutputBuffer " + encoderStatus +
" was null");
}
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.
if (VERBOSE) Log.d(TAG,"ignoring BUFFER_FLAG_CODEC_CONfig");
mBufferInfo.size = 0;
}
if (mBufferInfo.size != 0) {
if (!mmuxerStarted) {
throw new RuntimeException("muxer hasn't started");
}
// adjust the ByteBuffer values to match BufferInfo (not needed?)
encodedData.position(mBufferInfo.offset);
encodedData.limit(mBufferInfo.offset + mBufferInfo.size);
mmuxer.writeSampleData(mTrackIndex,encodedData,mBufferInfo);
if (VERBOSE) {
Log.d(TAG,"sent " + mBufferInfo.size + " bytes to muxer,ts=" +
mBufferInfo.presentationTimeUs);
}
}
mEncoder.releaSEOutputBuffer(encoderStatus,false);
if ((mBufferInfo.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
if (!endOfStream) {
Log.w(TAG,"reached end of stream unexpectedly");
} else {
if (VERBOSE) Log.d(TAG,"end of stream reached");
}
break; // out of while
}
}
}
}
}
以上代码是从Grafika项目中提取的,该项目是一个Android图形和媒体黑客转储。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)