If you think the Android project streamvid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
* Copyright (C) 2011-2014 GUIGUI Simon, fyhertz@gmail.com
* /*fromwww.java2s.com*/
* This file is part of libstreaming (https://github.com/fyhertz/libstreaming)
*
* Spydroid is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this source code; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/package net.majorkernelpanic.streaming.rtp;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import android.annotation.SuppressLint;
import android.media.MediaCodec;
import android.media.MediaCodec.BufferInfo;
import android.media.MediaFormat;
import android.util.Log;
/**
* An InputStream that uses data from a MediaCodec.
* The purpose of this class is to interface existing RTP packetizers of
* libstreaming with the new MediaCodec API. This class is not thread safe !
*/
@SuppressLint("NewApi")
publicclass MediaCodecInputStream extends InputStream {
publicfinal String TAG = "MediaCodecInputStream";
private MediaCodec mMediaCodec = null;
private BufferInfo mBufferInfo = new BufferInfo();
private ByteBuffer[] mBuffers = null;
private ByteBuffer mBuffer = null;
privateint mIndex = -1;
privateboolean mClosed = false;
public MediaFormat mMediaFormat;
public MediaCodecInputStream(MediaCodec mediaCodec) {
mMediaCodec = mediaCodec;
mBuffers = mMediaCodec.getOutputBuffers();
}
@Override
publicvoid close() {
mClosed = true;
}
@Override
publicint read() throws IOException {
return 0;
}
@Override
publicint read(byte[] buffer, int offset, int length) throws IOException {
int min = 0;
try {
if (mBuffer==null) {
while (!Thread.interrupted() && !mClosed) {
mIndex = mMediaCodec.dequeueOutputBuffer(mBufferInfo, 500000);
if (mIndex>=0 ){
//Log.d(TAG,"Index: "+mIndex+" Time: "+mBufferInfo.presentationTimeUs+" size: "+mBufferInfo.size);
mBuffer = mBuffers[mIndex];
mBuffer.position(0);
break;
} elseif (mIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
mBuffers = mMediaCodec.getOutputBuffers();
} elseif (mIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
mMediaFormat = mMediaCodec.getOutputFormat();
Log.i(TAG,mMediaFormat.toString());
} elseif (mIndex == MediaCodec.INFO_TRY_AGAIN_LATER) {
Log.v(TAG,"No buffer available...");
//return 0;
} else {
Log.e(TAG,"Message: "+mIndex);
//return 0;
}
}
}
if (mClosed) thrownew IOException("This InputStream was closed");
min = length < mBufferInfo.size - mBuffer.position() ? length : mBufferInfo.size - mBuffer.position();
mBuffer.get(buffer, offset, min);
if (mBuffer.position()>=mBufferInfo.size) {
mMediaCodec.releaseOutputBuffer(mIndex, false);
mBuffer = null;
}
} catch (RuntimeException e) {
e.printStackTrace();
}
return min;
}
publicint available() {
if (mBuffer != null)
return mBufferInfo.size - mBuffer.position();
elsereturn 0;
}
public BufferInfo getLastBufferInfo() {
return mBufferInfo;
}
}