Exoplayer custom DataSource : read data from external network call

216 views Asked by At

I need to get data from native network call with CPP. from native i am getting chunks of data as i strat to receive the response in native functions and i am appending byte[] to ByteArrayOutputStream. i have created a custom datasource and in Read callback i am using ByteArrayOutputStream to read byte[] from based on offset. i am also using BlockingQueue to block the execution till we get enough byte[] to read.

The main issue i am having is in syncing the byte[] for exoplayer buffer. and when it works, it buffers a lot even with full byte[]. i am adding the code block for Open, Read and Close callbacks. Please let me know if i should try anything differently.

Open callback

@Override
    public long open(DataSpec dataSpec) {
        Log.e("data_source", "open : " + dataSpec.toString());
        transferInitializing(dataSpec);
        // initiate native call to get the data
        bytesAlreadyRead = 0;
        opened = true;
        transferStarted(dataSpec);
        // blocking execution to get content length
        return contentLength.take();
    }

Read callback

@Override
    public int read(byte[] buffer, int offset, int readLength) {
        // Get latest data from `ByteArrayOutputStream`
        byte[] data = getLatestData();
        if (data.length < contentLength && data.length < (readLength + bytesAlreadyRead)) {
            try {
                //Blocking the execution until we get enough bytes
                String response = responseQueue.take()
                data = getLatestData();
            } catch (Exception ex) {
                Log.e("data_source", ex.getMessage());
            }
        }

        int byteAvailableToRead = (int) (data.length - bytesAlreadyRead);
        int bytesReadThisTime = Math.min(byteAvailableToRead, readLength);
        System.arraycopy(data, bytesAlreadyRead, buffer, offset, bytesReadThisTime);
        bytesAlreadyRead += bytesReadThisTime;
        bytesTransferred(bytesReadThisTime);
        return bytesReadThisTime;
    }

Close callback

@Override
    public void close() {
        Log.e("data_source","close : ");
        if (opened) {
            opened = false;
            transferEnded();
        }
        uri = null;
    }
0

There are 0 answers