NanoHttpd / nanohttpd

Tiny, easily embeddable HTTP server in Java.

Home Page:http://nanohttpd.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SMB Video/Audio not buffering.

ADJSwapnil opened this issue · comments

I am trying to streaming add streaming feature in android phone, so used nanohttpd library.

I used this library for streaming the video and audio files which are placed in my SMB server.
the video and audio are streaming but the buffering whole file in one shot, so its take so much time if video/audio are large in size.

I google it accordingly I got new thing that is setting the headers "Content-Range" which are by default provided by the method server. But the thing is I am not getting the "range" in headers.

Please help to buffer the video/audio as the buffer I provide in serve.
Help ASAP. I put the snippet below.

@Override
@SuppressWarnings("deprecation")
public Response serve(String uri, Method method, Map<String, String> headers, Map<String, String> params, Map<String, String> files) {
    SmbFile smbFile = null ;
    try {
        smbFile = new SmbFile(filePath, auth);
        smbFile.connect();
        smbFile.setConnectTimeout(15000);
        smbFile.setReadTimeout(1500);
    } catch (IOException e) {
        e.printStackTrace();
    }
    //range=bytes=619814-
    long range ;
    if (headers.containsKey("range")) {
        String contentRange = headers.get("range");
        Log.e("RANGE",""+contentRange);
        range = Integer.parseInt(contentRange.substring(contentRange.indexOf("=") + 1, contentRange.indexOf("-")));
    }else
        range = 0 ;

    byte[] buffer;
    long constantLength = 0;
    try {
        constantLength = smbFile.length();
    } catch (SmbException e) {
        e.printStackTrace();
    }
    long bufLength = 0;
    boolean isLastPart = false;
    try {
        SmbRandomAccessFile ff = new SmbRandomAccessFile (smbFile ,"rw" );
        long remainingChunk = ff.length() - range; //remaining
        if (remainingChunk < constantLength){
            bufLength = remainingChunk; //means last part
            isLastPart = true;
        }else
            bufLength = constantLength;

        if (range !=0)
            ff.seek(range);
        buffer = new byte[(int)bufLength];
        ff.read(buffer);
    } catch (IOException e) {
        e.printStackTrace();
        buffer = new byte[0];
    }
    Response response;
    response = new Response(Response.Status.PARTIAL_CONTENT,mimeType, new ByteArrayInputStream(buffer));
    response.addHeader("Content-Length", String.valueOf(constantLength));
    response.addHeader("Content-Range", String.format("bytes %s-%s/%s", range, ( range + bufLength ), String.valueOf(constantLength)));
    Log.e("SERVER","Inside server sent " + String.format("bytes %s-%s/%s", range,( range + bufLength ), String.valueOf(constantLength)));
    return response;
}