// Path: app/src/main/java/com/constantinnovationsinc/livemultimedia/app/MultimediaApp.java
// public class MultimediaApp extends Application {
//     private static final String TAG = MultimediaApp.class.getCanonicalName();
//     public  ArrayBlockingQueue<ByteBuffer> mSavedAudioData = null;
// 
//     public synchronized  void saveAudioData(ByteBuffer data) {
//         mSavedAudioData.add(data);
//     }
// 
//     public synchronized  ByteBuffer pullAudioData() {
//         return mSavedAudioData.poll();
//     }
// 
//     public synchronized  int capacity() {
//         return mSavedAudioData.remainingCapacity();
//     }
// 
//     @Override
//     public void onCreate() {
//         super.onCreate();
//         mSavedAudioData = new ArrayBlockingQueue<>(150);
//     }
// 
//     @Override
//     public void onLowMemory() {
//         super.onLowMemory();
//         Log.e(TAG, "Low Memory Warning!");
//     }
// 
//     @Override
//     public void onTerminate() {
//         super.onTerminate();
//         Log.e(TAG, "App terminating!");
//     }
// 
// }


import android.app.Application;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaCodec;
import android.media.MediaCodecInfo;
import android.media.MediaCodecList;
import android.media.MediaFormat;
import android.media.MediaMuxer;
import android.media.MediaRecorder;
import android.media.MediaPlayer.TrackInfo;
import android.util.Log;
import android.os.Build;
import android.os.Environment;
import com.constantinnovationsinc.livemultimedia.app.MultimediaApp;
import java.util.LinkedList;
import java.util.List;
import java.nio.ByteBuffer;
import java.io.File;
import java.io.IOException;
import java.nio.ByteOrder;
/*
*   Copyright 2015 Constant Innovations Inc
*
*    Licensed under the Apache License, Version 2.0 (the "License");
*    you may not use this file except in compliance with the License.
*    You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
*    Unless required by applicable law or agreed to in writing, software
*    distributed under the License is distributed on an "AS IS" BASIS,
*    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*    See the License for the specific language governing permissions and
*    limitations under the License.
*/
package com.constantinnovationsinc.livemultimedia.encoders;


public class AudioEncoder  implements Runnable{
    private static final String TAG = AudioEncoder.class.getName();
    private static final File OUTPUT_FILENAME_DIR = Environment.getExternalStorageDirectory();
    // parameters for the audio encoder
    private static final String AUDIO_MIME_TYPE = "audio/mp4a-latm";

    // Audio Encoder and Configuration
    //
    private MediaCodec mAudioEncoder;
    private MediaCodec.BufferInfo mAudioBufferInfo;
    private TrackInfo mAudioTrackInfo;
    private MediaFormat mAudioFormat;	// Configured with the options below
    private boolean mMuxerStarted;
    private TrackIndex mAudioTrackIndex = new TrackIndex();
    @SuppressWarnings("all")
    private final int kAACProfiles[] = {
            2 /* OMX_AUDIO_AACObjectLC */,
            5 /* OMX_AUDIO_AACObjectHE */,
            39 /* OMX_AUDIO_AACObjectELD */
    };
    @SuppressWarnings("all")
    private final int kSampleRates[] = { 8000, 11025, 22050, 44100, 48000 };
    @SuppressWarnings("all")
    private final int kBitRates[] = { 64000, 128000 };
    private static final int kNumInputBytes = 256 * 1024;
    private static final long kTimeoutUs = 10000;

    private AudioRecord mRecorder = null;
    private Boolean mAudioRecordingStopped = false;
    private int mAudioFrames = 0;
    private int mAudioFramesMax = 270;
    public MultimediaApp mApp = null;
    public  MediaMuxer mAudioMuxer = null;

    public AudioEncoder(Application app) {
// Path: app/src/main/java/com/constantinnovationsinc/livemultimedia/app/MultimediaApp.java
// public class MultimediaApp extends Application {
//     private static final String TAG = MultimediaApp.class.getCanonicalName();
//     public  ArrayBlockingQueue<ByteBuffer> mSavedAudioData = null;
// 
//     public synchronized  void saveAudioData(ByteBuffer data) {
//         mSavedAudioData.add(data);
//     }
// 
//     public synchronized  ByteBuffer pullAudioData() {
//         return mSavedAudioData.poll();
//     }
// 
//     public synchronized  int capacity() {
//         return mSavedAudioData.remainingCapacity();
//     }
// 
//     @Override
//     public void onCreate() {
//         super.onCreate();
//         mSavedAudioData = new ArrayBlockingQueue<>(150);
//     }
// 
//     @Override
//     public void onLowMemory() {
//         super.onLowMemory();
//         Log.e(TAG, "Low Memory Warning!");
//     }
// 
//     @Override
//     public void onTerminate() {
//         super.onTerminate();
//         Log.e(TAG, "App terminating!");
//     }
// 
// }


// Path: app/src/main/java/com/constantinnovationsinc/livemultimedia/encoders/AudioEncoder.java
import android.app.Application;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaCodec;
import android.media.MediaCodecInfo;
import android.media.MediaCodecList;
import android.media.MediaFormat;
import android.media.MediaMuxer;
import android.media.MediaRecorder;
import android.media.MediaPlayer.TrackInfo;
import android.util.Log;
import android.os.Build;
import android.os.Environment;
import com.constantinnovationsinc.livemultimedia.app.MultimediaApp;
import java.util.LinkedList;
import java.util.List;
import java.nio.ByteBuffer;
import java.io.File;
import java.io.IOException;
import java.nio.ByteOrder;

/*
*   Copyright 2015 Constant Innovations Inc
*
*    Licensed under the Apache License, Version 2.0 (the "License");
*    you may not use this file except in compliance with the License.
*    You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
*    Unless required by applicable law or agreed to in writing, software
*    distributed under the License is distributed on an "AS IS" BASIS,
*    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*    See the License for the specific language governing permissions and
*    limitations under the License.
*/
package com.constantinnovationsinc.livemultimedia.encoders;


public class AudioEncoder  implements Runnable{
    private static final String TAG = AudioEncoder.class.getName();
    private static final File OUTPUT_FILENAME_DIR = Environment.getExternalStorageDirectory();
    // parameters for the audio encoder
    private static final String AUDIO_MIME_TYPE = "audio/mp4a-latm";

    // Audio Encoder and Configuration
    //
    private MediaCodec mAudioEncoder;
    private MediaCodec.BufferInfo mAudioBufferInfo;
    private TrackInfo mAudioTrackInfo;
    private MediaFormat mAudioFormat;	// Configured with the options below
    private boolean mMuxerStarted;
    private TrackIndex mAudioTrackIndex = new TrackIndex();
    @SuppressWarnings("all")
    private final int kAACProfiles[] = {
            2 /* OMX_AUDIO_AACObjectLC */,
            5 /* OMX_AUDIO_AACObjectHE */,
            39 /* OMX_AUDIO_AACObjectELD */
    };
    @SuppressWarnings("all")
    private final int kSampleRates[] = { 8000, 11025, 22050, 44100, 48000 };
    @SuppressWarnings("all")
    private final int kBitRates[] = { 64000, 128000 };
    private static final int kNumInputBytes = 256 * 1024;
    private static final long kTimeoutUs = 10000;

    private AudioRecord mRecorder = null;
    private Boolean mAudioRecordingStopped = false;
    private int mAudioFrames = 0;
    private int mAudioFramesMax = 270;
    public MultimediaApp mApp = null;
    public  MediaMuxer mAudioMuxer = null;

    public AudioEncoder(Application app) {
        mApp = (MultimediaApp)app;