Search code examples
androidandroid-tvexoplayer

Occasionally getting "Too much time to handle tune request" error


I've built a Android TV Live Channels app and in general it works well. Occasionally, however, I will receive a crash report from a user that gives me an error like below:

02-11 13:32:21.135 29165-29165/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.felkertech.sample.channelsurfer, PID: 29165
    java.lang.RuntimeException: Too much time to handle tune request. (2160ms > 2000ms) Consider handling the tune request in a separate thread.
    at android.media.tv.ITvInputSessionWrapper.executeMessage(ITvInputSessionWrapper.java:166)
    at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:37)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

I don't really know how I can fix this as the error doesn't reference any of my code. Additionally, I can't reproduce this error consistently.

Here is part of my onTune method. Note that it does call a method from another class to actually get the video:

    @Override
public boolean onTune(Uri channelUri) {
    notifyVideoUnavailable(TvInputManager.VIDEO_UNAVAILABLE_REASON_TUNING);
    setOverlayViewEnabled(true);
    Log.d(TAG, "Tuning to " + channelUri.toString());
    String[] projection = {TvContract.Channels.COLUMN_DISPLAY_NAME, TvContract.Channels.COLUMN_ORIGINAL_NETWORK_ID,
            TvContract.Channels.COLUMN_SERVICE_ID, TvContract.Channels.COLUMN_TRANSPORT_STREAM_ID,
            TvContract.Channels.COLUMN_INPUT_ID, TvContract.Channels.COLUMN_DISPLAY_NUMBER};
    //Now look up this channel in the DB
    try (Cursor cursor = tvInputProvider.getContentResolver().query(channelUri, projection, null, null, null)) {
        if (cursor == null || cursor.getCount() == 0) {
            return false;
        }
        cursor.moveToNext();
        Channel channel = new Channel()
                .setNumber(cursor.getString(cursor.getColumnIndex(TvContract.Channels.COLUMN_DISPLAY_NUMBER)))
                .setName(cursor.getString(cursor.getColumnIndex(TvContract.Channels.COLUMN_DISPLAY_NAME)))
                .setOriginalNetworkId(cursor.getInt(cursor.getColumnIndex(TvContract.Channels.COLUMN_ORIGINAL_NETWORK_ID)))
                .setTransportStreamId(cursor.getInt(cursor.getColumnIndex(TvContract.Channels.COLUMN_TRANSPORT_STREAM_ID)))
                .setServiceId(cursor.getInt(cursor.getColumnIndex(TvContract.Channels.COLUMN_SERVICE_ID)))
                .setVideoHeight(1080)
                .setVideoWidth(1920);
        this.currentChannel = channel;
        TvInputManager mTvInputManager = (TvInputManager) tvInputProvider.getApplicationContext().getSystemService(Context.TV_INPUT_SERVICE);
        if(mTvInputManager.isParentalControlsEnabled()) {
            TvContentRating blockedRating = null;
            for(int i=0;i<tvInputProvider.getProgramRightNow(channel).getContentRatings().length;i++) {
                blockedRating = (mTvInputManager.isRatingBlocked(tvInputProvider.getProgramRightNow(channel).getContentRatings()[i]) && blockedRating == null)?tvInputProvider.getProgramRightNow(channel).getContentRatings()[i]:null;
            }
            if(blockedRating != null) {
                notifyContentBlocked(blockedRating);
            }
        }
        notifyContentAllowed();
        return tvInputProvider.onTune(channel);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

tvInputPlayer.onTune:

public boolean onTune(Channel channel) {
    this.currentChannel = channel;
    this.lastTune = new Date();
    Toast.makeText(SampleTvInputProvider.this, "Tuning to "+channel.getName()+" with program "+getProgramRightNow(channel).getTitle()+" at "+getProgramRightNow(channel).getInternalProviderData(), Toast.LENGTH_SHORT).show();
    Log.d(TAG, "Tuning to " + channel.getName());
    Log.d(TAG, "Playing "+getProgramRightNow(channel).getTitle());
    Log.d(TAG, "Play the video "+getProgramRightNow(channel).getInternalProviderData());

    //Only my local channels will have the ability to be time shifted, so I should update that every tuning.
    //Timeshifting only works for API >= 23
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (isLocal()) {
            getSession().notifyTimeShiftStatusChanged(TvInputManager.TIME_SHIFT_STATUS_AVAILABLE);
        } else {
            //If it's not a local channel, I cannot pause or seek in the program
            getSession().notifyTimeShiftStatusChanged(TvInputManager.TIME_SHIFT_STATUS_UNAVAILABLE);
        }
    }

    play(getProgramRightNow(channel).getInternalProviderData());
    if(currentChannel.getNumber().equals("4")) {
        Handler h = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                setOverlayEnabled(true);
            }
        };
        h.sendEmptyMessageDelayed(0, 16);
    }
    return true;
}

The method play loads a URL in an ExoPlayer. I feel like this is potentially where buffering will cause the ExoPlayer to lag slightly enough to create this problem, but I don't know how I could fix this.

Does anyone know?

Thanks.


Solution

  • I had a similar issue recently and via timing the blocks of code I isolated it to the Query:

    tvInputProvider.getContentResolver().query(.....
    

    This query will occasionally exceed 2 seconds upon startup of the Live Channels app, if your channel is the first one tuned. I removed the query (by keeping my own table) and the problem was avoided.