Question

Here's another question for you :)

Basically i made a realtime streaming service, sending multiple jpegs to my android app, that decodes them as soon as he receives them.

// dIn is DataInputStream
// videoFeed is an ImageView
// bitmap is Bitmap
// hand is an Handler of the main thread

//CODE EXECUTED IN ANOTHER THERAD
byte[] inBuff = new byte[8];
byte[] imgBuff;
String inMsg;
while(socket.isConnected()) {
    dIn.readFully(inBuff);
    inMsg = new String(inBuff, "ASCII").trim();
    int size = Integer.parseInt(inMsg);
    imgBuff = new byte[size];
    dIn.readFully(imgBuff);
    out.write("SEND-NEXT-JPEG".getBytes("ASCII"));
    bitmap = BitmapFactory.decodeByteArray(imgBuff, 0, size);   
    hand.post(setImage);
    }
}

private Runnable setImage = new Runnable() {
    @Override
    public void run() {
        videoFeed.setImageBitmap(bitmap);
    }
};

The problem is that after about 10 or 20 jpegs are perfectly decoded in realtime, the app freezes for 400ms or so and then it continues to decode other 10/20 jpegs before another freeze...

I know that sending multiple jpegs it's not a good way for streaming video but i can only change the client (android app), not the server.

Do you have any idea for get a fluid video and avoid freezes? thanks!

Was it helpful?

Solution

Right now, you are using the three-parameter version of decodeByteArray(). Instead, switch to the four-parameter version, passing in a BitmapFactory.Options as the last value. On there, set inBitmap to be a Bitmap object that can be reused.

This requires you to maintain a small Bitmap object pool. It could be as simple as two Bitmap instances: the one that is presently being displayed and the one that you are preparing for the next "frame" of the video.

The catch is that, for API Level 18 and below, the Bitmap needs to be the same resolution (height and width in pixels). In your case, that's probably not a problem, as I would imagine that each of your bitmaps have the same resolution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top