{"action":"created","issue":{"url":"https://api.github.com/repos/google/ExoPlayer/issues/756","labels_url":"https://api.github.com/repos/google/ExoPlayer/issues/756/labels{/name}","comments_url":"https://api.github.com/repos/google/ExoPlayer/issues/756/comments","events_url":"https://api.github.com/repos/google/ExoPlayer/issues/756/events","html_url":"https://github.com/google/ExoPlayer/issues/756","id":104837086,"number":756,"title":"Exoplayer can get stuck in preparing state when preparing HLS content with small ts chunk size","user":{"login":"elixiroflife4u","id":1160135,"avatar_url":"https://avatars.githubusercontent.com/u/1160135?v=3","gravatar_id":"","url":"https://api.github.com/users/elixiroflife4u","html_url":"https://github.com/elixiroflife4u","followers_url":"https://api.github.com/users/elixiroflife4u/followers","following_url":"https://api.github.com/users/elixiroflife4u/following{/other_user}","gists_url":"https://api.github.com/users/elixiroflife4u/gists{/gist_id}","starred_url":"https://api.github.com/users/elixiroflife4u/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/elixiroflife4u/subscriptions","organizations_url":"https://api.github.com/users/elixiroflife4u/orgs","repos_url":"https://api.github.com/users/elixiroflife4u/repos","events_url":"https://api.github.com/users/elixiroflife4u/events{/privacy}","received_events_url":"https://api.github.com/users/elixiroflife4u/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-09-04T06:27:43Z","updated_at":"2015-09-04T11:57:34Z","closed_at":"2015-09-04T11:56:03Z","body":"I have found a race condition bug inside HlsSampleSource in Exoplayer 1.3.3. It is specially more reproducible if the TS chunk is of small duration (~2 second). I do not know if this issue also affects Exoplayer 1.4.2 or dev branch. At this time, I am also unable to share a sample HLS content which shows the problem.\r\n\r\nI decided to raise an issue so that others who may have encountered this issue could try the workaround I have found, as well as to raise awareness of this issue potentially lurking in the 1.4.2/dev branch, and other renderer builders. I would also love to hear your thoughts in case there is a problem with the workaround.\r\n\r\n[Symptom of the problem]\r\nOn calling prepare on Hls, the exoplayer remains stuck in preparing state even though the TS chunk has been fully downloaded. However, the player can sometimes finish preparing if the chunk load complete comes after a call to HlsSampleSource to prepare.\r\n\r\n[Cause of the issue]\r\nI traced the problem traced down to the HlsSampleSource and was happening due to a race condition between call to prepare and onLoadCompleted. \r\nBasically, the prepare is called in a polling loop every 10msec. The chunk download proceeds independently, and the data is read by the TSExtractor. Once the TS extractor has enough data, it does some processing in it's consume function and sets up a flag. If this flag is then read by HlsSampleSource/HlsExtractorWrapper on the prepare call and everything is great. However, if the chunkOnLoadComplete for a TS chunk (in HlsSampleSource) happens to precede the prepare call, because it doesn't see any tracks, it proceeds to clear the extractor state which also clears the previous flag (see https://github.com/google/ExoPlayer/blob/r1.3.3/library/src/main/java/com/google/android/exoplayer/hls/HlsSampleSource.java#L356). Therefore on the next call of prepare, the HlsExtractorWrapper fails to find anything and fails to prepare and simply keeps trying without ever succeeding.\r\nIt seems to me that the code assumed that the chunk load complete (for a TS chunk) won't happen before the extractor/sample source has been prepared.\r\n\r\n[My solution to this issue]\r\nI put a simple if check here:\r\nhttps://github.com/google/ExoPlayer/blob/r1.3.3/library/src/main/java/com/google/android/exoplayer/hls/HlsSampleSource.java#L359 ( in the else), just before clearState() etc.\r\nBasically, the function returns immediately if the HlsSampleSource is not prepared and the currentLoadable is ts chunk.\r\n.....\r\n else {\r\nif(!prepared && isCurrentLoadableTsChunk) return; \r\n...\r\nThis prevents the clearing of state and allows the next call to prepare find what it is looking for and transition to next state.\r\n\r\nI am still testing it, but i haven't seen any problems with it thus far.\r\n"},"comment":{"url":"https://api.github.com/repos/google/ExoPlayer/issues/comments/137716830","html_url":"https://github.com/google/ExoPlayer/issues/756#issuecomment-137716830","issue_url":"https://api.github.com/repos/google/ExoPlayer/issues/756","id":137716830,"user":{"login":"ojw28","id":7881572,"avatar_url":"https://avatars.githubusercontent.com/u/7881572?v=3","gravatar_id":"","url":"https://api.github.com/users/ojw28","html_url":"https://github.com/ojw28","followers_url":"https://api.github.com/users/ojw28/followers","following_url":"https://api.github.com/users/ojw28/following{/other_user}","gists_url":"https://api.github.com/users/ojw28/gists{/gist_id}","starred_url":"https://api.github.com/users/ojw28/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ojw28/subscriptions","organizations_url":"https://api.github.com/users/ojw28/orgs","repos_url":"https://api.github.com/users/ojw28/repos","events_url":"https://api.github.com/users/ojw28/events{/privacy}","received_events_url":"https://api.github.com/users/ojw28/received_events","type":"User","site_admin":false},"created_at":"2015-09-04T11:57:34Z","updated_at":"2015-09-04T11:57:34Z","body":"I'd strongly encourage you to update to a newer version of the library by the way. There were significant stability improvements for HLS in 1.4.0 and 1.4.1."}}