Search in sources :

Example 1 with AACLATMPacketizer

use of net.majorkernelpanic.streaming.rtp.AACLATMPacketizer in project libstreaming by fyhertz.

the class AACStream method configure.

public synchronized void configure() throws IllegalStateException, IOException {
    super.configure();
    mQuality = mRequestedQuality.clone();
    // Checks if the user has supplied an exotic sampling rate
    int i = 0;
    for (; i < AUDIO_SAMPLING_RATES.length; i++) {
        if (AUDIO_SAMPLING_RATES[i] == mQuality.samplingRate) {
            mSamplingRateIndex = i;
            break;
        }
    }
    // If he did, we force a reasonable one: 16 kHz
    if (i > 12)
        mQuality.samplingRate = 16000;
    if (mMode != mRequestedMode || mPacketizer == null) {
        mMode = mRequestedMode;
        if (mMode == MODE_MEDIARECORDER_API) {
            mPacketizer = new AACADTSPacketizer();
        } else {
            mPacketizer = new AACLATMPacketizer();
        }
        mPacketizer.setDestination(mDestination, mRtpPort, mRtcpPort);
        mPacketizer.getRtpSocket().setOutputStream(mOutputStream, mChannelIdentifier);
    }
    if (mMode == MODE_MEDIARECORDER_API) {
        testADTS();
        // All the MIME types parameters used here are described in RFC 3640
        // SizeLength: 13 bits will be enough because ADTS uses 13 bits for frame length
        // config: contains the object type + the sampling rate + the channel number
        // TODO: streamType always 5 ? profile-level-id always 15 ?
        mSessionDescription = "m=audio " + String.valueOf(getDestinationPorts()[0]) + " RTP/AVP 96\r\n" + "a=rtpmap:96 mpeg4-generic/" + mQuality.samplingRate + "\r\n" + "a=fmtp:96 streamtype=5; profile-level-id=15; mode=AAC-hbr; config=" + Integer.toHexString(mConfig) + "; SizeLength=13; IndexLength=3; IndexDeltaLength=3;\r\n";
    } else {
        // AAC LC
        mProfile = 2;
        mChannel = 1;
        mConfig = (mProfile & 0x1F) << 11 | (mSamplingRateIndex & 0x0F) << 7 | (mChannel & 0x0F) << 3;
        mSessionDescription = "m=audio " + String.valueOf(getDestinationPorts()[0]) + " RTP/AVP 96\r\n" + "a=rtpmap:96 mpeg4-generic/" + mQuality.samplingRate + "\r\n" + "a=fmtp:96 streamtype=5; profile-level-id=15; mode=AAC-hbr; config=" + Integer.toHexString(mConfig) + "; SizeLength=13; IndexLength=3; IndexDeltaLength=3;\r\n";
    }
}
Also used : AACLATMPacketizer(net.majorkernelpanic.streaming.rtp.AACLATMPacketizer) AACADTSPacketizer(net.majorkernelpanic.streaming.rtp.AACADTSPacketizer) SuppressLint(android.annotation.SuppressLint)

Example 2 with AACLATMPacketizer

use of net.majorkernelpanic.streaming.rtp.AACLATMPacketizer in project libstreaming by fyhertz.

the class AACStream method encodeWithMediaCodec.

@Override
@SuppressLint({ "InlinedApi", "NewApi" })
protected void encodeWithMediaCodec() throws IOException {
    final int bufferSize = AudioRecord.getMinBufferSize(mQuality.samplingRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT) * 2;
    ((AACLATMPacketizer) mPacketizer).setSamplingRate(mQuality.samplingRate);
    mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, mQuality.samplingRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize);
    mMediaCodec = MediaCodec.createEncoderByType("audio/mp4a-latm");
    MediaFormat format = new MediaFormat();
    format.setString(MediaFormat.KEY_MIME, "audio/mp4a-latm");
    format.setInteger(MediaFormat.KEY_BIT_RATE, mQuality.bitRate);
    format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
    format.setInteger(MediaFormat.KEY_SAMPLE_RATE, mQuality.samplingRate);
    format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
    format.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, bufferSize);
    mMediaCodec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
    mAudioRecord.startRecording();
    mMediaCodec.start();
    final MediaCodecInputStream inputStream = new MediaCodecInputStream(mMediaCodec);
    final ByteBuffer[] inputBuffers = mMediaCodec.getInputBuffers();
    mThread = new Thread(new Runnable() {

        @Override
        public void run() {
            int len = 0, bufferIndex = 0;
            try {
                while (!Thread.interrupted()) {
                    bufferIndex = mMediaCodec.dequeueInputBuffer(10000);
                    if (bufferIndex >= 0) {
                        inputBuffers[bufferIndex].clear();
                        len = mAudioRecord.read(inputBuffers[bufferIndex], bufferSize);
                        if (len == AudioRecord.ERROR_INVALID_OPERATION || len == AudioRecord.ERROR_BAD_VALUE) {
                            Log.e(TAG, "An error occured with the AudioRecord API !");
                        } else {
                            //Log.v(TAG,"Pushing raw audio to the decoder: len="+len+" bs: "+inputBuffers[bufferIndex].capacity());
                            mMediaCodec.queueInputBuffer(bufferIndex, 0, len, System.nanoTime() / 1000, 0);
                        }
                    }
                }
            } catch (RuntimeException e) {
                e.printStackTrace();
            }
        }
    });
    mThread.start();
    // The packetizer encapsulates this stream in an RTP stream and send it over the network
    mPacketizer.setInputStream(inputStream);
    mPacketizer.start();
    mStreaming = true;
}
Also used : AudioRecord(android.media.AudioRecord) MediaFormat(android.media.MediaFormat) AACLATMPacketizer(net.majorkernelpanic.streaming.rtp.AACLATMPacketizer) MediaCodecInputStream(net.majorkernelpanic.streaming.rtp.MediaCodecInputStream) ByteBuffer(java.nio.ByteBuffer) SuppressLint(android.annotation.SuppressLint) SuppressLint(android.annotation.SuppressLint)

Aggregations

SuppressLint (android.annotation.SuppressLint)2 AACLATMPacketizer (net.majorkernelpanic.streaming.rtp.AACLATMPacketizer)2 AudioRecord (android.media.AudioRecord)1 MediaFormat (android.media.MediaFormat)1 ByteBuffer (java.nio.ByteBuffer)1 AACADTSPacketizer (net.majorkernelpanic.streaming.rtp.AACADTSPacketizer)1 MediaCodecInputStream (net.majorkernelpanic.streaming.rtp.MediaCodecInputStream)1