Repository navigation
Description
I work for some chromecast-ready apps and CastPlayer make it a lot of easier but I found a lot of troubles with it too:
1. Wrong getCurrentWindowIndex after setting media sources when CastPlayer is connected to Chromecast device.
There is use case where you connect to your chromecast device and after that you select media source.
How to recreate:
- connect to Chromecast device by tapping MediaRouteButton button
- create CastPlayer and set sources (castPlayer.loadItems(mediaItems, window, time, Player.REPEAT_MODE_OFF)) after some time (you can do it with some button)
- get currentWindowIndex (it's always 0)
Cause:
StatusListener don't invoke onSessionStarted which don't invoke setRemoteMediaClient when we set new sources so statusListener is no added to remoteMediaClient.
Solution:
Add statusListener to remoteMediaClient in contructor:
if (remoteMediaClient != null) {
try {
remoteMediaClient.addListener(statusListener);
remoteMediaClient.addProgressListener(statusListener, PROGRESS_REPORT_PERIOD_MS);
updateInternalState();
} catch (NullPointerException ignored) {
}
}2. I get 0 from getCurrentWindowIndex() which is not true.
How to recreate:
- invoke getCurrentWindowIndex() in onPositionDiscontinuity
Cause:
We get the value from fetchCurrentWindowIndex() when there is no media session. So current window is not actually 0.
Solution:
Change 0 to other value in fetchCurrentWindowIndex() when there is no media session. You will be able to recognize the real current window index from "no media session":
/**
* Retrieves the current item index from {@code mediaStatus} and maps it into a window index. If
* there is no media session, returns -1.
*/
private static int fetchCurrentWindowIndex(@Nullable MediaStatus mediaStatus) {
Integer currentItemId = mediaStatus != null
? mediaStatus.getIndexById(mediaStatus.getCurrentItemId()) : null;
return currentItemId != null ? currentItemId : -1;
}You should change getDuration and some other methods (isCurrentWindowDynamic(), isCurrentWindowSeekable()) in this case too to avoid some exceptions:
@Override
public long getDuration() {
return currentTimeline.isEmpty() ? C.TIME_UNSET
: currentTimeline.getWindow(getCurrentWindowIndex() < 0 ? 0 : getCurrentWindowIndex(),
window).getDurationMs();
}3. ArrayIndexOutOfBoundsException: length=2; index=2 when setting new media sources and some other problems with wrong current position.
How to recreate:
- create a pleyer with 3 sources
- seekTo 3'rd sources
- loadItems() with 2 sources
Cause:
The getCurrentWindowIndex() returns old value for some time.
Solution:
Update currentWindowIndex and pendingSeekWindowIndex values in loadItem and loadItems:
/**
* Loads a single item media queue. If no session is available, does nothing.
*
* @param item The item to load.
* @param positionMs The position at which the playback should start in milliseconds relative to
* the start of the item at {@code startIndex}. If {@link C#TIME_UNSET} is passed, playback
* starts at position 0.
* @return The Cast {@code PendingResult}, or null if no session is available.
*/
public PendingResult<MediaChannelResult> loadItem(MediaQueueItem item, long positionMs) {
pendingSeekWindowIndex = 0;
currentWindowIndex = 0;
return loadItems(new MediaQueueItem[]{item}, 0, positionMs, REPEAT_MODE_OFF);
}
/**
* Loads a media queue. If no session is available, does nothing.
*
* @param items The items to load.
* @param startIndex The index of the item at which playback should start.
* @param positionMs The position at which the playback should start in milliseconds relative to
* the start of the item at {@code startIndex}. If {@link C#TIME_UNSET} is passed, playback
* starts at position 0.
* @param repeatMode The repeat mode for the created media queue.
* @return The Cast {@code PendingResult}, or null if no session is available.
*/
public PendingResult<MediaChannelResult> loadItems(MediaQueueItem[] items, int startIndex,
long positionMs, @RepeatMode int repeatMode) {
if (remoteMediaClient != null) {
positionMs = positionMs != C.TIME_UNSET ? positionMs : 0;
waitingForInitialTimeline = true;
pendingSeekWindowIndex = startIndex;
currentWindowIndex = startIndex;
return remoteMediaClient.queueLoad(items, startIndex, getCastRepeatMode(repeatMode),
positionMs, null);
}
return null;
}4. There is no indicator to get to know that the media sources are finished.
#4130
I have no solution for now.
Hope it is helpful :) If you need some more info (videos with examples or something like that) let me know.
I will do pull request soon.
Activity
AquilesCanta commentedon Apr 23, 2018
Hi @mkaflowski, thanks for this great report. I still have to look into the issues, I keep them in mind but there are other things that require attention with higher priority right now. Sorry about it. We have been considering writing our own receiver app using shaka player to control casting end-to-end, which would provide a more definitive solutions to the issues we have run into.
Regarding 2, this is something that generated some discussion when reviewing, and applies to ExoPlayer as well. The conclusion of said discussion was that , in compliance with the Player documentation, we return 0 and it is up to the user to check the existence of a session. This might change, but for the time being, the value -1 (or C.INDEX_UNSET, which we would use instead) is not documented in Player, which means it should not be returned by its implementations.
Regarding 3, when seeking, you need to check that the position you are seeking to is available in the current timeline.
Regarding 4, it is more or less explained in the referred issue. For the time being, the only solution is to wait for IDLE. It is not yet clear to me when this does not work, but let's keep any discussions in the other dedicated thread.
It is important to keep issues as self contained (and dedicated) as possible. This helps in keeping a high quality issue history. So, if you are okay with it, let's move each individual point to its own issue and close this one.
mkaflowski commentedon Apr 23, 2018
Thank you for the answer. Some things could be confusing for new devs but I quite understand that.
Regarding 3, the problem is that after loadNewItems we get old current window index. For example when I play 3rd source, load 2 sources by loadNewItems and playing index 1 when I invoke getDuration() the CastPlayer try to return me duration for index 3 which doesn't exist and the 3 is out of bounds (because there are only 2 sources).
With the point 4 the problem is that that it is true that on finish I get IDLE but there is one more case when you get READY->IDLE. It's like on the video in the other topic:
READY(while playing) -> still READY(disconnected from Chromecast by user) -> IDLE(connecting to Chromecast) -> READY(playing) -> IDLE (finished)
So if I want to detect if track has finished to mark it somehow I could get into reconnecting change. I will try to find some solutions in a few days. I can send you some new video with logs to present it better.
mrHerintsoaHasina commentedon Jun 29, 2018
Hi, I have another problem when using the CastPlayer and you can test it with the cast demo app.
I choose 3 medias. I connect the app to the cast receiver after clicking the cast button in the action bar. I play the first media. When it is finished, the second media is played. When this second is finished, the third starts to be played but the UI shows the second media as played. I think this bug is related to a wrong currentWindowIndex value when the cast receiver go to the next track after the current track is finished. I need to have a fixing solution. Thanks !
mkaflowski commentedon Dec 17, 2019
Update for release r2.11.0:
Ad 1: I think it is fixed.
Ad 2: Not fixed - the same solution: just update pendingSeekWindowIndex and currentWindowIndex.
Additionally edit updateInternalStateAndNotifyIfChanged to not return 0 because it glitches when you try to change tracks very quickly (for example next->next->next):
Ad 3: I think it is fixed.