Jump to content

Bitrate test


Angelblue05

Recommended Posts

Angelblue05

I've notice the bitrate test in the api/playback.

 

How does it work? Is it just based off if the GET request succeeds or not depending on the size provided?

Link to comment
Share on other sites

No, it downloads a known-size block of data and times it - returning a bitrate value to the caller.

 

You should be using it to determine the max bitrate to use for streaming requests.

  • Like 1
Link to comment
Share on other sites

Angelblue05

That is not at all what I'm seeing. The server sends a response 200 with an empty body.

 

56601b2015f01_bitratetest.png

 

I had the same issue in Kodi when I tested there. Just an empty response. What am I doing wrong?

Link to comment
Share on other sites

Oh, sorry.  At the pure API level, that is the call that needs to be timed.  In the ApiClients we have that call wrapped inside logic that makes the request, times the response and then converts that timing to a bitrate (the request is made with BYTES so the response time calculation needs to be multiplied by 8 for bits).

    protected void detectBitrateInternal(final long downloadBytes, final Response<Long> response) {

        QueryStringDictionary dict = new QueryStringDictionary();

        dict.Add("Size", downloadBytes);

        String address = GetApiUrl("Playback/BitrateTest", dict);

        HttpURLConnection conn = null;

        try
        {
            URL url = new URL(address);

            conn = (HttpURLConnection) url.openConnection();
            conn.setDoInput(true); // Allow Inputs
            conn.setUseCaches(false); // Don't use a Cached Copy
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Connection", "Keep-Alive");

            for (String key: this.HttpHeaders.keySet()){
                conn.setRequestProperty(key, this.HttpHeaders.get(key));
            }

            final long startTime = System.currentTimeMillis();

            try (InputStream inputStream = conn.getInputStream()){

                byte[] byteChunk = new byte[4096]; // Or whatever size you want to read in at a time.
                int n;

                while ( (n = inputStream.read(byteChunk)) > 0 ) {

                }

                double time = System.currentTimeMillis() - startTime;
                double bitrate = downloadBytes * 8;
                bitrate /= time;
                bitrate *= 1000;

                response.onResponse(Math.round(bitrate));
            }
            catch (IOException ioException){
                response.onError(ioException);
                return;
            }
        }
        catch (Exception ex)
        {
            response.onError(ex);
        }
    }

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...