FileDocCategorySizeDatePackage
NativeEncoder.javaAPI DocJMF 2.1.1e9283Mon May 12 12:21:02 BST 2003com.ibm.media.codec.audio.g723

NativeEncoder

public class NativeEncoder extends BufferedEncoder

Fields Summary
int
nativeData
private int
sample_count
private long
currentSeq
private long
timestamp
byte[]
pendingBuffer
Constructors Summary
public NativeEncoder()

    ////////////////////////////////////////////////////////////////////////////
    // Methods


      
       	supportedInputFormats = new AudioFormat[] {
	                        new AudioFormat(AudioFormat.LINEAR,
					        Format.NOT_SPECIFIED,
						16,
						1,
						AudioFormat.LITTLE_ENDIAN,
						AudioFormat.SIGNED,
                                                Format.NOT_SPECIFIED,
                                                Format.NOT_SPECIFIED,
                                                Format.byteArray
                                ) };

        defaultOutputFormats  = new AudioFormat[] {
	                        new AudioFormat(AudioFormat.G723)  /*,
			        new AudioFormat(AudioFormat.G723_RTP,
						8000,
						Format.NOT_SPECIFIED,
						1)*/};

        PLUGIN_NAME="G723 Encoder";
        historySize = 480;
	pendingFrames = 0;
	// for RTP, set default packetsize to 160 audiosamples for a
	// default ms/packet of 20ms. This works out to 33 octets.
	//note if this value changes in setPacketSize(), sample count
	// needs to be updated as well.
	packetSize = 24;
    
Methods Summary
protected intcalculateFramesNumber(int inputSize)

        return inputSize / 480;
    
protected intcalculateOutputSize(int inputSize)

        return calculateFramesNumber(inputSize) * 24 ;
    
public voidclose()
Clean up

        freeNative();
    
protected native booleancodecProcess(byte[] inpData, int readPtr, byte[] outData, int writePtr, int inpLength, int[] readBytes, int[] writeBytes, int[] frameNumber, int[] regions, int[] regionsTypes)

public voidcodecReset()

        resetNative();
    
private native voidfreeNative()

protected javax.media.Format[]getMatchingOutputFormats(javax.media.Format in)


        AudioFormat af =(AudioFormat) in;

        supportedOutputFormats = new AudioFormat[] {
                new AudioFormat(
                    AudioFormat.G723,
                    af.getSampleRate(),
                    Format.NOT_SPECIFIED,
                    1,
                    Format.NOT_SPECIFIED,
                    Format.NOT_SPECIFIED,
                    24*8,
                    Format.NOT_SPECIFIED,
                    Format.byteArray
                ) /*,

                new AudioFormat(
                    AudioFormat.G723_RTP,
                    8000,
                    Format.NOT_SPECIFIED,
		    1,
                    Format.NOT_SPECIFIED,
                    Format.NOT_SPECIFIED,
                    24*8,
                    Format.NOT_SPECIFIED,
                    Format.byteArray
                )   */

        };
        return  supportedOutputFormats;
    
private native voidinitNative()

public voidopen()
Initializes the codec.

    	try {
            JMFSecurityManager.loadLibrary("jmutil");
            JMFSecurityManager.loadLibrary("jmg723");
            initNative();
            return;

        } catch (Throwable t) {
            // can't load native implementation
            System.err.println("can not load "+PLUGIN_NAME);
            System.err.println("reason : "+t);
            throw new ResourceUnavailableException("can not load "+PLUGIN_NAME);
	}


    
public intprocess(javax.media.Buffer inputBuffer, javax.media.Buffer outputBuffer)

    // let the buffered encoder process the input buffer. this will
    // encode the data for us.Only RTP packetization will be desired
    // at this time.
    int retVal = super.process(inputBuffer, outputBuffer);

    // if it is RTP, packetize the data.
    if (outputFormat.getEncoding().equals(AudioFormat.G723_RTP)){

      // before we proceed for packetization, check for failure in
      // encoding and EOM
      if (retVal == BUFFER_PROCESSED_FAILED)
	return retVal;
      if (isEOM(inputBuffer) ) {
	propagateEOM(outputBuffer);
	return BUFFER_PROCESSED_OK;
      }

      // Now, if there are no pending frames, we are beginning
      // packetization of a new buffer.get a handle over the buffer to
      // be packetized
      if (pendingFrames == 0)
	pendingBuffer = (byte[])outputBuffer.getData();

      // start packetizing one frame at a time (240 samples)
      // the size of outputdata depends on the packet size set.
      byte[] outData = new byte[packetSize];
      outputBuffer.setData(outData);
      updateOutput(outputBuffer, outputFormat,packetSize, 0);
      outputBuffer.setSequenceNumber(currentSeq++);
      outputBuffer.setTimeStamp(timestamp);
      timestamp+=sample_count;

      System.arraycopy(pendingBuffer,
		       regions[pendingFrames],
		       outData,
		       0,
		       packetSize);

      if (pendingFrames + 1== frameNumber[0]){
	pendingFrames = 0;
	pendingBuffer = null;
	return BUFFER_PROCESSED_OK;
      }else
	pendingFrames++;

      return INPUT_BUFFER_NOT_CONSUMED;

    }//end of G723_RTP

    return retVal;
  
private native voidresetNative()