Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
Posts: 21
Registered: ‎10-10-2011
My Device: blackberry bold 9780
My Carrier: developer

buffering and playback pcm audio

Hi Sir,

 

i hahe problem in my SourceStream.read() method .problem is my read() method not buffering and playback

audio so please help me what wrong in my code and how to solve this issue.

 

 

Thanks

 

Anurag 

Developer
Posts: 541
Registered: ‎05-17-2009
My Device: Not Specified

Re: buffering and playback pcm audio

Is the problem in the simulator? PCM playback in the simulator is almost always choppy for me, while one the device it will almost always sound fine (although there is the occassional skipping of a few milleseconds of the beginning of the sound clip).

 

If the problem is just on the simulator, you might not want to worry about it a whole lot of it sounds fine on the device. If it's choppy on the device, can you show your code?

 

Scott

Contributor
Posts: 21
Registered: ‎10-10-2011
My Device: blackberry bold 9780
My Carrier: developer

Re: buffering and playback pcm audio

Hi Sir,

 

Thanks For reply me,

 

I have already  run our application Both Device and Simulator but i am not getting any voice or playback audio (My Device BlackBerry Bold 9780 OS 6.0.0).

i post you our code and debug message please cheack  what is the problem in my code .and help me .

 

My SourceStream.read method  code and debug error log .. please see below.

 

private OutputStream _out = null;
mypackage.ByteBuffer buffer = new mypackage.ByteBuffer(32000, 32000);
private static final String content = "audio/pcm";
private SourceStream mInput = new SourceStream() {
private boolean priority_set = false;
private static final int rowSize = 320;
private static final int streamLength = 128000;
private byte[] tempBuffer = new byte[160];
ContentDescriptor mContentDescriptor = new ContentDescriptor(content);

 

public int read(byte[] b, int offset, int length) throws IOException {
if (mStartTime == 0) {
mStartTime = System.currentTimeMillis();
}
int lWrittenLenth = 0;
if (mStartTime == 0) {
mStartTime = System.currentTimeMillis();
}
try {
if (!priority_set
&& Thread.currentThread().getPriority() != Thread.MAX_PRIORITY) {
Thread.currentThread().setPriority(
Thread.MAX_PRIORITY);
priority_set = true;
}
while (lWrittenLenth <= (rowSize * 5)) {
long ts = getCurTs();
if (mPlayerTs == -1) {
mPlayerTs = ts;
}
RtpPacket packet = null;
long diff;
while ((diff = (ts - mPlayerTs)) >= 0) {
if (diff > 400) {
mPlayerTs = ts - 400;
}
packet = mSession.recvPacket((int) mPlayerTs);
if (packet != null) {
_out.write("here".getBytes());
_out.flush();
// ///////////// DECODING /////////////
System.arraycopy(packet.getBytes(), 12,
tempBuffer, 0, 160);
byte[] dencoded = new byte[tempBuffer.length * 2];
for (int i = 0, j = 0; i < tempBuffer.length; i++) {
int s = tempBuffer[i];
int res = G711.ulaw2linear(s);
dencoded[j++] = (byte) (res);
dencoded[j++] = (byte) (res >> 8);
}
lWrittenLenth += rowSize;
// ////////////////////////////////////
buffer.enqueue(dencoded);
}
mPlayerTs += 160;
}
}} catch (Exception e) {}
return lWrittenLenth;
}

 

///////////////////  Debug Log///////////////////

 

0.0] s=SBCSIPUAS SIP STACK v1.0
[0.0] t=0 0
[0.0] m=audio 50872 RTP/AVP 0
[0.0] c=IN IP4 208.65.111.132
[0.0] a=rtpmap:0 PCMU/8000
[0.0] a=sqn:0
[0.0] a=cdsc:1 image udptl t38
[0.0] a=sendrecv
[0.0] CSeq: 2 ACK
[0.0] To: <sip:00919412880575@208.65.111.132>;tag=7fn44wmzjaigeh6b.i
[[0.0] MN: slewVolume(50331648, 0, 1)=0
[0.0] MP: h=50331648,s=PL: +paus(120):297
[0.0] MN: pause0(50331648)=0
[0.0] MP: h=50331648,s=PL: -> PA
[0.0] SIM cache not loaded
[0.0] MP: h=50331648,s=PA: volChange 0
[0.0] MP: h=50331648,s=PA: +mPC
[0.0] MP: h=50331648,s=PA: -> LD
[0.0] MP: h=50331648,s=LD: -mPC
[0.0] 1326614806122 :: SIPTransactionStack: creating new Response SIP/2.0 200 OK
[0.0] MP: h=50331648,s=LD: -paus
[0.0] AR: remove source 14
[0.0] AR: setAudioMode 32
[0.0] MP: h=50331648,s=LD: +unld(120):297
[0.0] MP: h=50331648,s=LD: -> UG
[0.0] MN: unload0(50331648)=0 pauseHandle=0

 

[0.0] MN: MEDIA_STOPPED received

 

[0.0] MN: handle=50331648 bitmapHandle=0 thumbnailHandle=2147483647
[0.0] MP: h=50331648,s=UG: +mStp(120):120
[0.0] MP: h=50331648,s=UG: +unlC
[0.0] MN: finalize0(50331648)
[0.0] MN: unload 1
[0.0] MP: h=50331648,s=UG: -> UD
[0.0] MSM[1]: release
[0.0] MSM[1]: Stream 3595 sink done
[0.0] MSM[1]: Stream Streaming session: 3595 sendSourceEnded called
[0.0] MP: h=50331648,s=UD: -unld
[0.0] 1326614806318 :: SIPTransactionStack: creating new Response SIP/2.0 200 OK
[0.0] 1326614806335 :: null sipServerResponse!
[0.0] MSM[1]: RT exit
[0.0] [STREAM] releasing buffer [index: 0] [handle: 3595]
[0.0] MSM[1]: already released
[0.0] MSM[1]: already released
[0.0] MSM[1]: already released
[0.0] MP: h=50331648,s=UD: -unlC
[0.0] MP: h=50331648,s=UD: -mStp
[0.0] AR: add source 12
[0.0] AR: setAudioMode 9
[0.0] MSM[1]: created session
[0.0] MSM[1]: ----- initializeStream
[0.0] [STREAM] allocating buffer [index: 0] [size: 65536]
[0.0] [STREAM] allocating stream [index: 0] [size: 65536]
[0.0] [STREAM] allocated stream [index: 0] [handle: 3851] [size: 65536]
[0.0] MN: setRecordParameters(1,20)=0
[0.0] MSM[1]: Starting streaming session 3851
[0.0] 1326614806537 :: EventScanner going to sleep!
[0.0] AudioStream-Warn:received stats :Count=869
[0.0] MSM[1]: Stream 3851 new data
[0.0] MSM[1]: Stream 3851 new data
[0.0] MSM[1]: Stream 3851 new data
[0.0] MSM[1]: Stream 3851 new data
[0.0] MSM[1]: Stream 3851 new data
[0.0] MSM[1]: Stream 3851 new data
[0.0] MSM[1]: Stream 3851 new data
[0.0] jortp-Info:mDiffCurrent=-1081520957
[0.0] MSM[1]: Stream 3851 new data
[0.0] jortp-Info:mDiffCurrent=-1081520797
[

 

Thanks

 

Anurag

 

Developer
Posts: 541
Registered: ‎05-17-2009
My Device: Not Specified

Re: buffering and playback pcm audio

Have you considered using ByteArrayInputStreamDataSource? Read is already implemented in that class.
Contributor
Posts: 21
Registered: ‎10-10-2011
My Device: blackberry bold 9780
My Carrier: developer

Re: buffering and playback pcm audio

Hi Sir ,

thanks for replied me.

 

T already try to ByteArrayInputStreamDataSource but i am facing same issue.

in log message we found error  " MEDIA_STOPPED received"  i unable to find 

this issue why media stopped what wrong in my code. 

 

 

Thanks

Saurabh

Contributor
Posts: 21
Registered: ‎10-10-2011
My Device: blackberry bold 9780
My Carrier: developer

Re: buffering and playback pcm audio

Hi sir,

 

 

Please sir reply,

 

  please sir tell me what is the problem in my  given  above  code  . what is the problem in my sourcestream.read() method  when we run this code it throw the exception  Media Exception...

 

Thread [bs(244)id=5166080] (Suspended (exception MediaException)) 
Manager.createPlayerImpl(DataSource, boolean) line: 1072 
Manager.createPlayer(DataSource) line: 966 
RecvStream.start() line: 221

 

..........

 

Please sir cheack and help me.

 

thanks 

Anurag

Developer
Posts: 541
Registered: ‎05-17-2009
My Device: Not Specified

Re: buffering and playback pcm audio

I'm not the best person to help with this. The buffering I do is much different than what you have shown here, so I can't be much help in finding any errors in that code you posted, but I will try and help.

 

Is the MediaException object giving you a message? Try and catch the exception and display it with toString(). From the API, it states MediaException is "Thrown if a Player cannot be created for the given DataSource." The error seems to occur when the player is being created, which indicates to me that the DataSource can't be used to initialize the player.

 

It might help if you posted the code for RecvStream.start() as well.

 

Scott

Developer
Posts: 541
Registered: ‎05-17-2009
My Device: Not Specified

Re: buffering and playback pcm audio

I don't think read() is called during createPlayer(). I believe it wouldn't be called until after you called player.realize().

 

Scott

Contributor
Posts: 21
Registered: ‎10-10-2011
My Device: blackberry bold 9780
My Carrier: developer

Re: buffering and playback pcm audio

Hi Sir,

 

I appreciates your time and help. I post my whole RecvStream.java class  sir my problem is that my read () meathod not working .  and  unable to playback audio. so please sir help me how to work my SourceStream.read()

method.

I post some deboug message again.  please see below.

 

..........................Recvstream.java/.................

...............

 

public class RecvStream implements PlayerListener {
private Player mPlayer;
private SendStream mSendStream;
private RtpSession mSession;
private long mStartTime = 0;
static Timer mTimer;
private TimerTask rtpCollector;
private LabelField status;

private long mPlayerTs=-1;

private byte[] dencoded = null;
private Player player = null;
private int available = 0;
private int curPos = 0;
//private long startTime;
private int totalLen;
private static final String content = "audio/x-pcm";
private boolean stopFlag = false;
/**
* Creates a new MyScreen object
*/
private final static Logger mLog = JOrtpFactory.instance().createLogger("SoftphoneCore");
private OutputStream os = null;
mypackage.ByteBuffer buffer = new mypackage.ByteBuffer(32000, 32000);

private SourceStream input = new SourceStream() {
private ContentDescriptor mContentDescriptor = new ContentDescriptor(
content);

private static final int defSize = 320;

public int read(byte[] b, int offset, int length) throws IOException {
if (mStartTime == 0) {
mStartTime = System.currentTimeMillis();
}
int lWrittenLenth = 0;
os.write("size:".getBytes());
os.write(new Integer(buffer.getSize()).toString().getBytes());                      
os.write("\r\n".getBytes());
os.flush();
byte[] buf = buffer.dequeue();

if(buf !=null )
{
System.out.println("buffer item recivennnn //////"+buf);
mLog.info("buffer item recive //////"+buf);
os.write(buf);
os.flush();
os.write("read.size".getBytes());
System.out.println("read.size".getBytes());
os.write(new Integer(b.length).toString().getBytes());
os.write("\tsize:".getBytes());
os.write(new Integer(buffer.getSize()).toString().getBytes());
os.write("\ttaken:".getBytes());
os.write(new Integer(buf.length).toString().getBytes());
os.write("\ttime:".getBytes());
os.write(new Long(System.currentTimeMillis()).toString().getBytes());
os.write("\r\n".getBytes());
os.flush();
}
return lWrittenLenth;
}

public ContentDescriptor getContentDescriptor() {
return mContentDescriptor;
}

public long getContentLength() {
return 64000;
}

public int getSeekType() {
return SourceStream.SEEKABLE_TO_START;
}
public int getTransferSize() {
return defSize * 1000;
}

public long seek(long where) throws IOException {
return where;
// throw new IOException("not seekable");
}

/*public long tell() {
return mStartTime == 0 ? 0
: (System.currentTimeMillis() - mStartTime);
// return 0;
}*/

public long tell() {
if (mStartTime==0) return 0;
return (System.currentTimeMillis() - mStartTime);
}


public Control getControl(String controlType) {
return null;
}

public Control[] getControls() {
return null;
}
};
private int getCurTs() {
if (mStartTime == 0) {
mStartTime = System.currentTimeMillis();
}
// return (int) ((System.currentTimeMillis() - mStartTime) ); // 8
return (int) System.currentTimeMillis();
}
public RecvStream(RtpSession session,SendStream sendStream) {

mSession=session;
mSendStream = sendStream;
}

public void start() throws IOException, MediaException {
if (null == player) {

player = Manager.createPlayer(new DataSource(null) {
SourceStream[] mStream = {input };

public void connect() throws IOException {
}

public void disconnect() {
}

public String getContentType() {
return content;
}

public SourceStream[] getStreams() {
return mStream;
}

public void start() throws IOException {
}

public void stop() throws IOException {
}

public Control getControl(String controlType) {
return null;
}

public Control[] getControls() {
return null;
}

});
// ///////////////////// DataSource ///////////////
// ///////////////ByteArrayInputStream/////////////////////////////
/* ByteArrayInputStream theInput = new
ByteArrayInputStream(dencoded);
player = Manager.createPlayer(theInput, "audio/x-pcm");*/
// ///////////////ByteArrayInputStream/////////////////////////////

mTimer = new Timer();
mTimer.scheduleAtFixedRate(rtpCollector = new TimerTask() {
private byte[] tempBuffer = new byte[160];
private boolean priority_set = false;
private int rowSize = 320;
public void run() {
if (mStartTime == 0) {
mStartTime = System.currentTimeMillis();
}
int lWrittenLenth = 0;
try {
if (!priority_set
&& Thread.currentThread().getPriority() != Thread.MAX_PRIORITY) {
Thread.currentThread().setPriority(
Thread.MAX_PRIORITY);
priority_set = true;
}
while (lWrittenLenth <= (rowSize * 5)) {
long ts = getCurTs();
if (mPlayerTs == -1) {
mPlayerTs = ts;
}
RtpPacket packet = null;
long diff;
while ((diff = (ts - mPlayerTs)) >= 0) {
if (diff > 400) {
mPlayerTs = ts - 400;
}
packet = mSession.recvPacket((int) mPlayerTs);

System.out.println("saurabh recive RtpPacket /////"+packet);

if (packet != null) {
System.arraycopy(packet.getBytes(), 12,
tempBuffer, 0, 160);
byte[] dencoded = new byte[tempBuffer.length * 2];

for (int i = 0, j = 0; i < tempBuffer.length; i++) {
int s = tempBuffer[i];
int res = G711.ulaw2linear(s);
dencoded[j++] = (byte) (res);
dencoded[j++] = (byte) (res >> 8);
}
lWrittenLenth += rowSize;
buffer.enqueue(dencoded);
System.out.println("saurabh tempBuffer packet////"+dencoded);
}
mPlayerTs += 160;
}
}
} catch (Exception e) {
}
}}, 0, 10);


stopFlag = false;
curPos = 0;
status.setText("Playing...");
player.addPlayerListener(this);
player.realize();
AudioPathControl lPathCtr = (AudioPathControl) player
.getControl("net.rim.device.api.media.control.AudioPathControl");
lPathCtr.setAudioPath(AudioPathControl.AUDIO_PATH_HANDSET);
player.prefetch();
player.start();
}
}

private void restartPlayer() {
if (player != null) {
try {
if (player.getState() != Player.CLOSED) {
player.stop();
player.start();
}
} catch (MediaException e) {
}
}
}

public void stop() {
if (player == null)
return;// nothing to stop

try {
if (player.getState() == Player.STARTED) {
player.stop();
}
if (player.getState() != Player.CLOSED) {
player.close();
}
player = null;
status.setText("Stopped!");

} catch (MediaException e) {
}
}

public void playerUpdate(Player player, String event, Object eventData) {
if (event.equals(PlayerListener.STOPPED)
|| event.equals(PlayerListener.END_OF_MEDIA)) {
if (!stopFlag) {
restartPlayer();
}
}
}

public int getPlayLevel() {
if (player != null) {
return ((VolumeControl) player.getControl("VolumeControl"))
.getLevel();
} else {
return 0;
}
}
public void setPlayLevel(int level) {
if (player != null) {
((VolumeControl) player.getControl("VolumeControl"))
.setLevel(level);
}
}
public void enableSpeaker(boolean value) {
if (player == null)
return;// just ignore
AudioPathControl lPathCtr = (AudioPathControl) player
.getControl("net.rim.device.api.media.control.AudioPathControl");
try {
lPathCtr.setAudioPath(value ? AudioPathControl.AUDIO_PATH_HANDSFREE
: AudioPathControl.AUDIO_PATH_HANDSET);
} catch (Throwable e) {
}
}

public boolean isSpeakerEnabled() {
if (player == null)
return false;// just ignore
AudioPathControl lPathCtr = (AudioPathControl) player
.getControl("net.rim.device.api.media.control.AudioPathControl");
return lPathCtr.getAudioPath() == AudioPathControl.AUDIO_PATH_HANDSFREE;
}

public long getPlayerTs() {
return mPlayerTs;
}
}

 

 

 .............See Debug Message..............

 

[0.0] [STREAM] releasing buffer [index: 0] [handle: 2569]
[0.0] MSM[0]: already released
[0.0] MP: h=16777216,s=UD: -unlC
[0.0] MP: h=16777216,s=UD: -mStp
[0.0] AR: add source 11
[0.0] AR: setAudioMode 9
[0.0] MSM[1]: created session
[0.0] MSM[1]: ----- initializeStream
[0.0] [STREAM] allocating buffer [index: 0] [size: 65536]
[0.0] [STREAM] allocating stream [index: 0] [size: 65536]
[0.0] [STREAM] allocated stream [index: 0] [handle: 2825] [size: 65536]
[0.0] MN: setRecordParameters(1,20)=0
[0.0] MSM[1]: Starting streaming session 2825
[0.0] 1328620428109 :: EventScanner going to sleep!
[0.0] No stack trace
[0.0] jortp-Info:JitterBufferControl started, mCurrentSize=480.0 mDiffCurrent=-1475487426
[0.0] jortp-Info:mDiffCurrent=-1475487266
[0.0] jortp-Info:mDiffCurrent=-1475487106
[0.0] ACS Portrait
[0.0] jortp-Warn:Incoming payload type changed to 0
[0.0] jortp-Info:mDiffCurrent=-1475486946
[0.0] saurabh recive RtpPacket /////Seq=12765 Ts=46235
[0.0] saurabh tempBuffer packet////[B@190edc92
[0.0] saurabh recive RtpPacket /////null
[0.0] saurabh recive RtpPacket /////Seq=12766 Ts=46475
[0.0] saurabh recive RtpPacket /////null
[0.0] saurabh recive RtpPacket /////Seq=12767 Ts=46635
[0.0] saurabh tempBuffer packet////[B@3b804940
[0.0] 1328620428671 :: Removing clientTransaction sip4me.gov.nist.siplite.stack.ClientTransaction@c879216c[0.0]
[0.0] 1328620428671 :: Removing clientTransaction sip4me.gov.nist.siplite.stack.ClientTransaction@691b7aac[0.0]
[0.0] jortp-Warn:Incoming payload type changed to 0
[0.0] jortp-Info:mDiffCurrent=-1475486786
[0.0] saurabh recive RtpPacket /////Seq=12769 Ts=46955
[0.0] MSM[1]: Stream 2825 new data
[0.0] saurabh tempBuffer packet////[B@3f5ac7f3
[0.0] saurabh recive RtpPacket /////Seq=12770 Ts=47115
[0.0] saurabh tempBuffer packet////[B@f0e27c5
[0.0] MSM[1]: Stream 2825 new data
[0.0] jortp-Info:mDiffCurrent=-1475486626
[0.0] saurabh recive RtpPacket /////Seq=12772 Ts=47435
[0.0] saurabh tempBuffer packet////[B@ac36902f
[0.0] jortp-Info:mDiffCurrent=-1475486466
[0.0] jortp-Warn:Incoming payload type changed to 0
[0.0] saurabh recive RtpPacket /////Seq=12774 Ts=47755
[0.0] saurabh recive RtpPacket /////null
[0.0] jortp-Warn:Incoming payload type changed to 0
[0.0] jortp-Info:mDiffCurrent=-1475486306
[0.0] saurabh recive RtpPacket /////null
[0.0] jortp-Info:mDiffCurrent=-1475486146
[0.0] saurabh recive RtpPacket /////null
[0.0] jortp-Info:mDiffCurrent=-1475485986
[0.0] saurabh recive RtpPacket /////Seq=12775 Ts=48555
[0.0] saurabh recive RtpPacket /////null
[0.0] jortp-Warn:Incoming payload type changed to 0
[0.0] saurabh recive RtpPacket /////null
[0.0] MSM[1]: Stream 2825 new data

[0.0] MSM[1]: Stream 2825 new data

[0.0] MSM[1]: Stream 2825 new data

[0.0] MSM[1]: Stream 2825 new data

 

 

Thanks

 

Saurabh

 

Developer
Posts: 541
Registered: ‎05-17-2009
My Device: Not Specified

Re: buffering and playback pcm audio

[ Edited ]

I still don't see how read() could possibly cause MediaException. The stack trail for the exception leads back to the line:

 

player = Manager.createPlayer(new DataSource(null) {

 

 

This leads me to believe the way you have formatted this DataSource has a problem. What gives you the impression that read() has anything to do with the MediaException being thrown?

 

One issue I see with the implementation of read(), is that you do not populate b with any bytes. Just about anything implementing read() from SourceStream will require that b be given a value. If read() returns a length of 3000, and b has anything but 3000 bytes, then you will probably get an exception being thrown.

 

Scott