Android Open Source - voicelink Media Codec Input Stream






From Project

Back to project page voicelink.

License

The source code is released under:

Apache License

If you think the Android project voicelink 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
 * //from   ww w  .ja v a 2s  .  c om
 * 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.audio.recieve;

import java.io.IOException;
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")
public class MediaCodecInputStream   {

  public final String TAG = "MediaCodecInputStream"; 

  private MediaCodec mMediaCodec = null;
  private BufferInfo mBufferInfo = new BufferInfo();
  public ByteBuffer[] mMediaCodecOutputBuffers = null;
  private ByteBuffer mBuffer = null;
  private int mIndex = -1;
  private boolean mClosed = false;
  
  public MediaFormat mMediaFormat;

  public MediaCodecInputStream(MediaCodec mediaCodec) {
    mMediaCodec = mediaCodec;
    mMediaCodecOutputBuffers = mMediaCodec.getOutputBuffers();
  }

  public void close() {
    mClosed = true;
  }

  

  public int 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 = mMediaCodecOutputBuffers[mIndex];
            mBuffer.position(0);
            break;
          } else if (mIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
            mMediaCodecOutputBuffers = mMediaCodec.getOutputBuffers();
          } else if (mIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
            mMediaFormat = mMediaCodec.getOutputFormat();
            Log.i(TAG,mMediaFormat.toString());
          } else if (mIndex == MediaCodec.INFO_TRY_AGAIN_LATER) {
            Log.v(TAG,"No buffer available...");
            //return 0;
          } else {
            Log.e(TAG,"Message: "+mIndex);
            //return 0;
          }
        }      
      }
      
      if (mClosed) throw new 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;
  }
  
  public int available() {
    if (mBuffer != null) 
      return mBufferInfo.size - mBuffer.position();
    else 
      return 0;
  }

  public BufferInfo getLastBufferInfo() {
    return mBufferInfo;
  }

}




Java Source Code List

net.audio.example2.MainActivity.java
net.audio.example2.testActivity.java
net.audio.recieve.AACLATMunPacketizer.java
net.audio.recieve.AACStream.java
net.audio.recieve.AudioQuality.java
net.audio.recieve.AudioRecieveManage.java
net.audio.recieve.MediaCodecInputStream.java
net.audio.recieve.RecieveSocket.java
net.audio.send.AACLATMPacketizer.java
net.audio.send.AACStream.java
net.audio.send.AudioQuality.java
net.audio.send.AudioSendManage.java
net.audio.send.MediaCodecInputStream.java
net.audio.send.SendSocket.java