Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. public static final String MIME_TYPE_AUDIO = "audio/mp4a-latm";
  2. public static final int SAMPLE_RATE = 44100;
  3. public static final int CHANNEL_COUNT = 1;
  4. public static final int CHANNEL_CONFIG = AudioFormat.CHANNEL_IN_MONO;
  5. public static final int BIT_RATE_AUDIO = 128000;
  6. public static final int SAMPLES_PER_FRAME = 1024 * 2;
  7. public static final int FRAMES_PER_BUFFER = 24;
  8. public static final int AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
  9. public static final int AUDIO_SOURCE = MediaRecorder.AudioSource.MIC;
  10. public static final int MAX_INPUT_SIZE = 16384 * 4;
  11. public static final int MAX_SAMPLE_SIZE = 256 * 1024;
  12.  
  13. private AudioRecord audioRecord;
  14. private ByteBuffer[] inputBuffers;
  15. private ByteBuffer inputBuffer;
  16. private MediaExtractor mediaExtractor;
  17.  
  18. private boolean audioSended = false;
  19. private boolean completed = false;
  20. private int sampleCount;
  21. private int iBufferSize;
  22.  
  23. public AudioEncoderCore(MovieMuxer muxer) throws IOException {
  24. this.muxer = muxer;
  25. bufferInfo = new MediaCodec.BufferInfo();
  26.  
  27. MediaFormat mediaFormat = null;
  28.  
  29. mediaFormat = MediaFormat.createAudioFormat(MIME_TYPE_AUDIO, SAMPLE_RATE, CHANNEL_COUNT);
  30. mediaFormat.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
  31. mediaFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, MAX_INPUT_SIZE);
  32. mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, BIT_RATE_AUDIO);
  33.  
  34. encoder = MediaCodec.createEncoderByType(MIME_TYPE_AUDIO);
  35. encoder.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
  36. encoder.start();
  37. iBufferSize = SAMPLES_PER_FRAME * FRAMES_PER_BUFFER;
  38.  
  39. // Ensure buffer is adequately sized for the AudioRecord
  40. // object to initialize
  41. int iMinBufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE, CHANNEL_CONFIG, AUDIO_FORMAT);
  42. if (iBufferSize < iMinBufferSize)
  43. iBufferSize = ((iMinBufferSize / SAMPLES_PER_FRAME) + 1) * SAMPLES_PER_FRAME * 2;
  44.  
  45. audioRecord = new AudioRecord(AUDIO_SOURCE, SAMPLE_RATE, CHANNEL_CONFIG, AUDIO_FORMAT, iBufferSize);
  46. audioRecord.startRecording();
  47.  
  48. }
  49.  
  50. public void sendDataFromMic(boolean endOfStream) {
  51. if (endOfStream)
  52. Log.d(TAG, "sendDataFromMic end of stream");
  53.  
  54. long audioPresentationTimeNs;
  55.  
  56. byte[] mTempBuffer = new byte[SAMPLES_PER_FRAME];
  57. audioPresentationTimeNs = System.nanoTime();
  58.  
  59. int iReadResult = audioRecord.read(mTempBuffer, 0, mTempBuffer.length);
  60.  
  61. if (iReadResult == AudioRecord.ERROR_BAD_VALUE || iReadResult == AudioRecord.ERROR_INVALID_OPERATION || iReadResult == 0) {
  62. Log.e(TAG, "audio buffer read error");
  63. } else {
  64. // send current frame data to encoder
  65. try {
  66. if (inputBuffers == null)
  67. inputBuffers = encoder.getInputBuffers();
  68.  
  69. //Sometimes can't get any available input buffer
  70. int inputBufferIndex = encoder.dequeueInputBuffer(100000);
  71. Log.d(TAG, "inputBufferIndex = " + inputBufferIndex);
  72. if (inputBufferIndex >= 0) {
  73. inputBuffer = inputBuffers[inputBufferIndex];
  74. inputBuffer.clear();
  75. inputBuffer.put(mTempBuffer, 0, iReadResult);
  76.  
  77. Log.d(TAG, "sending frame to audio encoder " + iReadResult + " bytes");
  78. encoder.queueInputBuffer(inputBufferIndex, 0, iReadResult, audioPresentationTimeNs / 1000,
  79. endOfStream ? MediaCodec.BUFFER_FLAG_END_OF_STREAM : 0);
  80. }
  81.  
  82.  
  83. } catch (Throwable t) {
  84. Log.e(TAG, "sendFrameToAudioEncoder exception");
  85. t.printStackTrace();
  86. }
  87. }
  88. }
  89.  
  90. public void drainEncoder() {
  91. ByteBuffer[] encoderOutputBuffers = encoder.getOutputBuffers();
  92. while (true) {
  93. int encoderStatus = encoder.dequeueOutputBuffer(bufferInfo, TIMEOUT_NSECS);
  94. if (encoderStatus == MediaCodec.INFO_TRY_AGAIN_LATER) {
  95. Log.d(TAG, "no output available, spinning to await EOS");
  96. break;
  97. } else if (encoderStatus == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED)
  98. encoderOutputBuffers = encoder.getOutputBuffers();
  99. else if (encoderStatus == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
  100. MediaFormat newFormat = encoder.getOutputFormat();
  101. Log.d(TAG, "encoder format changed: " + newFormat);
  102. trackIndex = muxer.addTrack(newFormat);
  103. } else if (muxer.isMuxerStarted()) {
  104. ByteBuffer encodedData = encoderOutputBuffers[encoderStatus];
  105. if (encodedData == null)
  106. throw new RuntimeException("encoded buffer " + encoderStatus + " was null");
  107.  
  108. if ((bufferInfo.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {
  109. Log.d(TAG, "ignoring BUFFER_FLAG_CODEC_CONFIG");
  110. bufferInfo.size = 0;
  111. }
  112.  
  113. if (bufferInfo.size != 0) {
  114. encodedData.position(bufferInfo.offset);
  115. encodedData.limit(bufferInfo.offset + bufferInfo.size);
  116.  
  117. synchronized (muxer) {
  118. muxer.writeSampleData(trackIndex, encodedData, bufferInfo);
  119. }
  120. Log.d(TAG, "sent " + bufferInfo.size + " bytes to muxer, ts=" +
  121. bufferInfo.presentationTimeUs + " track index=" + trackIndex);
  122. }
  123.  
  124. encoder.releaseOutputBuffer(encoderStatus, false);
  125.  
  126. if ((bufferInfo.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
  127. Log.d(TAG, "end of stream reached");
  128. completed = true;
  129. break; // out of while
  130. }
  131. }
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement