This discussion is archived
3 Replies Latest reply: Jan 27, 2011 8:40 AM by captfoss RSS

Is SourceDataLine.write(byte[] bytes) very cpu demanding?

833786 Newbie
Currently Being Moderated
Hello all
I'm working with a Java application that receives video streams from ip cameras (h.264 streams).
I'm using Xuggler to receive and decode the streams.

I'm having a big problem with the audio because it's falling behind in time comparing to images.

I have a AudioManager that handles the Java sound.

The AudioManager has a Queue that the streamReader sends its audio packages to, and a looping thread that constantly pops from the queue and write the raw bytes to the SourceDataLine.

Optimally, the queue should contain a maximum of one audio package, but it tends to start growing.
During the first minute it manages to keep it empty, but then it starts to fill.

I am constantly monitoring the sourceDataLine.available(). At startup, it starts at 5000, but decrease very fast down to 0, and that's where my problems start and the queue starts to grow.

I never imagined that this would be a problem and thought it would be quite easy to write those byte arrays to it.

I tried to set the buffer size higher, but Linux doesn't give me more to work with. I also realize that increasing the buffer size will just delay the problem.

Is there any way I can do this in a more effective way?

The hardware I'm running this on is a dual core Atom board.

I will be thankful if someone can give me hints for how to solve this.

Best regards
Per-Jarle Sæther
  • 1. Re: Is SourceDataLine.write(byte[] bytes) very cpu demanding?
    captfoss Pro
    Currently Being Moderated
    830783 wrote:
    I am constantly monitoring the sourceDataLine.available().
    Dataline.available doesn't work the way it's supposed to 99% of the time. It's best practice to avoid using it...
    830783 wrote:
    At startup, it starts at 5000, but decrease very fast down to 0, and that's where my problems start and the queue starts to grow.
    Is your SDL actually rendering the data properly?

    More than likely, I'd suspect the issue is that you're not writing data until SDL.available tells you there's room for it... and because "available" is problematic, you really should see how much room is available on the line by trying to write as much data as you have available to it...
  • 2. Re: Is SourceDataLine.write(byte[] bytes) very cpu demanding?
    833786 Newbie
    Currently Being Moderated
    Hello, and thank you for the reply

    I'm using the available method just to monitor the line.

    the code for writing the bytes to the audioLine is like this:



    while (true) {
    if (!queue.isEmpty()){ // queue is a ConcurrentLinkedQueue<byte[]>
    byte[] rawBytes = queue.pop();
    audioLine.write(rawBytes, 0, rawBytes.length);
    }
    }

    I keep track of the queue size by manually counting what I add and what I pop because the size() method can't be trusted.

    When the sourceDataLine.available() starts to go close to 0, the size of the queue starts to grow

    /Per-Jarle
  • 3. Re: Is SourceDataLine.write(byte[] bytes) very cpu demanding?
    captfoss Pro
    Currently Being Moderated
    830783 wrote:
    while (true) {
    if (!queue.isEmpty()){ // queue is a ConcurrentLinkedQueue<byte[]>
    byte[] rawBytes = queue.pop();
    audioLine.write(rawBytes, 0, rawBytes.length);
    }
    }
    First off, that's bad code because it assumes audioLine will write the entire buffer and it may not. So you'd be losing data in that instance.

    I have no advise to offer without being able to see, essentially, the bulk of your code... and make sure to use code tags when posting code so it'll be formatted correctly.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points