- change how we increment timestamps for audio packet

This commit is contained in:
Richard Alam 2010-12-14 15:38:09 -05:00
parent eeee05608b
commit 71588b22de

View File

@ -57,8 +57,6 @@ public class SipToFlashAudioStream implements TranscodedAudioDataListener, RtpSt
private RtpStreamReceiver rtpStreamReceiver; private RtpStreamReceiver rtpStreamReceiver;
private StreamObserver observer; private StreamObserver observer;
private SipToFlashTranscoder transcoder; private SipToFlashTranscoder transcoder;
private long startTimestamp = 0;
private boolean sentMetadata = false; private boolean sentMetadata = false;
private IoBuffer mBuffer; private IoBuffer mBuffer;
private AudioData audioData; private AudioData audioData;
@ -142,7 +140,6 @@ public class SipToFlashAudioStream implements TranscodedAudioDataListener, RtpSt
audioBroadcastStream.start(); audioBroadcastStream.start();
processAudioData = true; processAudioData = true;
audioDataProcessor = new Runnable() { audioDataProcessor = new Runnable() {
public void run() { public void run() {
processAudioData(); processAudioData();
@ -158,15 +155,11 @@ public class SipToFlashAudioStream implements TranscodedAudioDataListener, RtpSt
byte[] pcmAudio = new byte[len]; byte[] pcmAudio = new byte[len];
int remaining = len; int remaining = len;
int offset = 0; int offset = 0;
// long startProc;
// boolean transcoded = false;
while (processAudioData) { while (processAudioData) {
try { try {
// startProc = System.currentTimeMillis();
// System.out.println("** Remaining[" + remaining + "," + offset + "] " + streamToFlash.available());
if (streamToFlash.available() > 1000) { if (streamToFlash.available() > 1000) {
long skipped = streamToFlash.skip(1000L); long skipped = streamToFlash.skip(1000L);
// System.out.println("** Skipping audio bytes[" + skipped + "]");
} }
int bytesRead = streamToFlash.read(pcmAudio, offset, remaining); int bytesRead = streamToFlash.read(pcmAudio, offset, remaining);
remaining -= bytesRead; remaining -= bytesRead;
@ -174,12 +167,9 @@ public class SipToFlashAudioStream implements TranscodedAudioDataListener, RtpSt
remaining = len; remaining = len;
offset = 0; offset = 0;
transcoder.transcode(pcmAudio, this); transcoder.transcode(pcmAudio, this);
// transcoded = true;
} else { } else {
offset += bytesRead; offset += bytesRead;
} }
// System.out.println("S2F transcode ms=" + (System.currentTimeMillis()-startProc) + " coded " + transcoded);
// transcoded = false;
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
@ -195,7 +185,6 @@ public class SipToFlashAudioStream implements TranscodedAudioDataListener, RtpSt
@Override @Override
public void onAudioDataReceived(byte[] audioData, int offset, int len) { public void onAudioDataReceived(byte[] audioData, int offset, int len) {
try { try {
// System.out.println("** Received[" + audioData.length + "]");
streamFromSip.write(audioData, offset, len); streamFromSip.write(audioData, offset, len);
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
@ -214,7 +203,6 @@ public class SipToFlashAudioStream implements TranscodedAudioDataListener, RtpSt
private void sendFakeMetadata(long timestamp) { private void sendFakeMetadata(long timestamp) {
if (!sentMetadata) { if (!sentMetadata) {
startTimestamp = System.currentTimeMillis();
/* /*
* Flash Player 10.1 requires us to send metadata for it to play audio. * Flash Player 10.1 requires us to send metadata for it to play audio.
* We create a fake one here to get it going. Red5 should do this automatically * We create a fake one here to get it going. Red5 should do this automatically
@ -225,7 +213,7 @@ public class SipToFlashAudioStream implements TranscodedAudioDataListener, RtpSt
mBuffer.flip(); mBuffer.flip();
Notify notifyData = new Notify(mBuffer); Notify notifyData = new Notify(mBuffer);
notifyData.setTimestamp((int)startTimestamp); notifyData.setTimestamp((int)timestamp);
notifyData.setSourceType(Constants.SOURCE_TYPE_LIVE); notifyData.setSourceType(Constants.SOURCE_TYPE_LIVE);
audioBroadcastStream.dispatchEvent(notifyData); audioBroadcastStream.dispatchEvent(notifyData);
notifyData.release(); notifyData.release();
@ -234,15 +222,19 @@ public class SipToFlashAudioStream implements TranscodedAudioDataListener, RtpSt
} }
private void pushAudio(byte[] audio, long timestamp) { private void pushAudio(byte[] audio, long timestamp) {
sendFakeMetadata(timestamp); sendFakeMetadata(timestamp);
mBuffer.clear(); mBuffer.clear();
mBuffer.put((byte) transcoder.getCodecId()); mBuffer.put((byte) transcoder.getCodecId());
mBuffer.put(audio); mBuffer.put(audio);
mBuffer.flip(); mBuffer.flip();
audioData.setSourceType(Constants.SOURCE_TYPE_LIVE); audioData.setSourceType(Constants.SOURCE_TYPE_LIVE);
audioData.setTimestamp((int)(System.currentTimeMillis() - startTimestamp)); /*
* Use timestamp increments passed in by codecs (i.e. 32 for nelly). This will force
* Flash Player to playback audio at proper timestamp. If we calculate timestamp using
* System.currentTimeMillis() - startTimestamp, the audio has tendency to drift and
* introduce delay. (ralam dec 14, 2010)
*/
audioData.setTimestamp((int)(timestamp));
audioData.setData(mBuffer); audioData.setData(mBuffer);
audioBroadcastStream.dispatchEvent(audioData); audioBroadcastStream.dispatchEvent(audioData);
audioData.release(); audioData.release();