Skip to content

Commit

Permalink
Bug fix for AAC streaming.
Browse files Browse the repository at this point in the history
  • Loading branch information
fyhertz committed Oct 6, 2013
1 parent 791c8e0 commit 3518342
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 16 deletions.
43 changes: 32 additions & 11 deletions src/net/majorkernelpanic/streaming/audio/AACStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,16 @@ public AACStream() throws IOException {
if (!AACStreamingSupported()) {
Log.e(TAG,"AAC not supported on this phone");
throw new AACNotSupportedException();
} else {
Log.d(TAG,"AAC supported on this phone");
}

if (mMode == MODE_MEDIARECORDER_API)
if (mMode == MODE_MEDIARECORDER_API) {
mQuality = new AudioQuality(16000,32000);
mPacketizer = new AACADTSPacketizer();
else
} else {
mPacketizer = new AACLATMPacketizer();
}

}

Expand Down Expand Up @@ -125,16 +129,9 @@ public void start() throws IllegalStateException, IOException {
}

@Override
@SuppressLint("InlinedApi")
protected void encodeWithMediaRecorder() throws IOException {
testADTS();
((AACADTSPacketizer)mPacketizer).setSamplingRate(mActualSamplingRate);
setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
try {
Field name = MediaRecorder.OutputFormat.class.getField("AAC_ADTS");
setOutputFormat(name.getInt(null));
}
catch (Exception ignore) {}
super.encodeWithMediaRecorder();
}

Expand Down Expand Up @@ -274,8 +271,31 @@ public String generateSessionDescription() throws IllegalStateException, IOExcep
* @throws IOException
* @throws IllegalStateException
*/
@SuppressLint("InlinedApi")
private void testADTS() throws IllegalStateException, IOException {

setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
try {
Field name = MediaRecorder.OutputFormat.class.getField("AAC_ADTS");
setOutputFormat(name.getInt(null));
}
catch (Exception ignore) {
setOutputFormat(6);
}

// 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) {
break;
}
}
// If he did, we force a reasonable one: 24 kHz
if (i>12) {
Log.e(TAG,"Not a valid sampling rate: "+mQuality.samplingRate);
mQuality.samplingRate = 24000;
}

if (mSettings!=null) {
if (mSettings.contains("aac-"+mQuality.samplingRate)) {
String[] s = mSettings.getString("aac-"+mQuality.samplingRate, "").split(",");
Expand Down Expand Up @@ -305,13 +325,14 @@ private void testADTS() throws IllegalStateException, IOException {
mMediaRecorder.setAudioSamplingRate(mQuality.samplingRate);
mMediaRecorder.setAudioEncodingBitRate(mQuality.bitRate);
mMediaRecorder.setOutputFile(TESTFILE);
mMediaRecorder.setMaxDuration(1000);
mMediaRecorder.prepare();
mMediaRecorder.start();

// We record for 1 sec
// TODO: use the MediaRecorder.OnInfoListener
try {
Thread.sleep(1000);
Thread.sleep(2000);
} catch (InterruptedException e) {}

mMediaRecorder.stop();
Expand All @@ -331,7 +352,7 @@ private void testADTS() throws IllegalStateException, IOException {

raf.read(buffer,1,5);

mSamplingRateIndex = (buffer[1]&0x3C) >> 2;
mSamplingRateIndex = (buffer[1]&0x3C)>>2 ;
mProfile = ( (buffer[1]&0xC0) >> 6 ) + 1 ;
mChannel = (buffer[1]&0x01) << 2 | (buffer[2]&0xC0) >> 6 ;
mActualSamplingRate = AUDIO_SAMPLING_RATES[mSamplingRateIndex];
Expand Down
15 changes: 12 additions & 3 deletions src/net/majorkernelpanic/streaming/rtp/H264Packetizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ public class H264Packetizer extends AbstractPacketizer implements Runnable {
private long delay = 0, oldtime = 0;
private Statistics stats = new Statistics();
private byte[] sps = null, pps = null;

private int count = 0;


public H264Packetizer() throws IOException {
super();
socket.setClockFrequency(90000);
Expand Down Expand Up @@ -77,6 +79,8 @@ public void run() {
long duration = 0, delta2 = 0;
Log.d(TAG,"H264 packetizer started !");
stats.reset();
count = 0;

// This will skip the MPEG4 header if this step fails we can't stream anything :(
try {
byte buffer[] = new byte[4];
Expand Down Expand Up @@ -163,8 +167,13 @@ private void send() throws IOException, InterruptedException {

// The stream already contains NAL unit type 7 or 8, we don't need
// to add them to the stream ourselves
if (type == 7) sps = null;
if (type == 8) pps = null;
if (type == 7 || type == 8) {
count++;
if (count>4) {
sps = null;
pps = null;
}
}

// Updates the timestamp
ts += delay;
Expand Down
2 changes: 1 addition & 1 deletion src/net/majorkernelpanic/streaming/rtp/RtpSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public void run() {
if ((mTimestamps[mBufferOut]-mOldTimestamp)>0) {
stats.push(mTimestamps[mBufferOut]-mOldTimestamp);
long d = stats.average()/1000000;
Log.d(TAG,"delay: "+d+" d: "+(mTimestamps[mBufferOut]-mOldTimestamp)/1000000);
//Log.d(TAG,"delay: "+d+" d: "+(mTimestamps[mBufferOut]-mOldTimestamp)/1000000);
// We ensure that packets are sent at a constant and suitable rate no matter how the RtpSocket is used.
Thread.sleep(d);
}
Expand Down
1 change: 0 additions & 1 deletion src/net/majorkernelpanic/streaming/video/VideoStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ public VideoStream(int camera) {
setCamera(camera);
// TODO: Remove this when encoding with the MediaCodec API is ready
setMode(MODE_MEDIARECORDER_API);
Log.e(TAG,"MODE: "+mMode);
}

/**
Expand Down

0 comments on commit 3518342

Please sign in to comment.