Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plex playing playlist of movies fails due to timeout #69815

Closed
charlie89 opened this issue Apr 10, 2022 · 5 comments · Fixed by #69821
Closed

Plex playing playlist of movies fails due to timeout #69815

charlie89 opened this issue Apr 10, 2022 · 5 comments · Fixed by #69821
Assignees

Comments

@charlie89
Copy link

The problem

When using the service Media player: Play media to play all videos of a playlist (or collection) on a plex client (Nvidia Shield) it runs into a timeout and doesn't start playing.

Note: I'm not using chromecast but the plex integration to directly play on a client because this gives me access to the playlist via the plex app on my phone and i can view/add/rearrange playlist items.
The issue only affects playlists with videos, playlists with music work fine.

I already did a whole bunch of investigation:
With 8 videos in the playlist it works and takes about 20 seconds to start playing the first video, but i now have 35 videos in the playlist so it takes longer and produces an error. I figured out that the python-plexapi module runs into it's default 30 second timeout, so i added a file ~/.config/plexapi/config.ini with a timeout of 5 minutes (300 seconds) like described here.
With the increased timeout playing the 35 video playlist works, but it takes about 75 seconds for it to start playing. As my playlist will grow in the future i would have to wait even longer for it to start playing.

When using the official plex app on my android phone to play the playlist on the Nvidia Shield it starts playing instantly, so i know there's a way to make it faster.
For testing purposes i created a small script which uses the plexapi package directly to stream the playlist:

from plexapi.server import PlexServer

plex = PlexServer(PLEX_URL, PLEX_TOKEN)
client = plex.client('SHIELD Android TV')
playlist = plex.playlist('kardio-workout')
pq = plex.createPlayQueue(playlist)
client.playMedia(pq)

And again it took 75 seconds to start playing and it was the function createPlayQueue which takes so long. Then i captured the network-traffic of the plex app on my android phone (including breaking tls encryption with mitmproxy) to compare it's api requests with those from plexapi.
The difference is that plexapi by default uses includeRelated=1 in function createPlayQueue (see source) to include related videos and then the request from the plex server takes so long cause plex probably searches for related videos for every video in the playlist. When changing line 6 of the above script to pq = plex.createPlayQueue(playlist, includeRelated=0) it starts playing instantly.

I don't even want plex to play related videos, just play those videos in the specified playlist. Probably that includeRelated flag may be used to get similar results, especially when using voice assistants (which i don't use).
The homeassistant code doesn't set the includeRelated flag currently, so it's always on.

A solution would be to add a optional flag related (or so) to the JSON contained in media_content_id which then sets the includeRelated flag for plexapi's createPlayQueue function. Basically just like shuffle works.
Or does someone have a better solution?

What version of Home Assistant Core has the issue?

core-2022.4.1

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Container

Integration causing the issue

plex

Link to integration documentation on our website

https://www.home-assistant.io/integrations/plex/

Diagnostics information

No response

Example YAML snippet

service: media_player.play_media
data:
  media_content_type: PLAYLIST
  media_content_id: '{ "playlist_name": "kardio-workout", "shuffle": "1" }'
target:
  entity_id: media_player.plex_plex_for_android_tv_shield_android_tv

Anything in the logs that might be useful for us?

Logger: homeassistant.core
Source: components/plex/server.py:605
First occurred: 20:16:25 (1 occurrences)
Last logged: 20:16:25

Error executing service: <ServiceCall media_player.play_media (c:23adbd7cd117dea6ee929828f1131a57): entity_id=['media_player.plex_plex_for_android_tv_shield_android_tv'], extra=, media_type=PLAYLIST, media_id={ "playlist_name": "kardio-workout", "shuffle": "1", "includeRelated": 0 }>
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 449, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 444, in _make_request
    httplib_response = conn.getresponse()
  File "/usr/local/lib/python3.9/http/client.py", line 1377, in getresponse
    response.begin()
  File "/usr/local/lib/python3.9/http/client.py", line 320, in begin
    version, status, reason = self._read_status()
  File "/usr/local/lib/python3.9/http/client.py", line 281, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/usr/local/lib/python3.9/socket.py", line 704, in readinto
    return self._sock.recv_into(b)
  File "/usr/local/lib/python3.9/ssl.py", line 1241, in recv_into
    return self.read(nbytes, buffer)
  File "/usr/local/lib/python3.9/ssl.py", line 1099, in read
    return self._sslobj.read(len, buffer)
socket.timeout: The read operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/requests/adapters.py", line 440, in send
    resp = conn.urlopen(
  File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 785, in urlopen
    retries = retries.increment(
  File "/usr/local/lib/python3.9/site-packages/urllib3/util/retry.py", line 550, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "/usr/local/lib/python3.9/site-packages/urllib3/packages/six.py", line 770, in reraise
    raise value
  File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 703, in urlopen
    httplib_response = self._make_request(
  File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 451, in _make_request
    self._raise_timeout(err=e, url=url, timeout_value=read_timeout)
  File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 340, in _raise_timeout
    raise ReadTimeoutError(
urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='redacted.plex.direct', port=54321): Read timed out. (read timeout=30)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/core.py", line 1652, in catch_exceptions
    await coro_or_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1671, in _execute_service
    await cast(Callable[[ServiceCall], Awaitable[None]], handler.job.target)(
  File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 204, in handle_service
    await service.entity_service_call(
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 677, in entity_service_call
    future.result()  # pop exception if have
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 955, in async_request_call
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 714, in _handle_entity_call
    await result
  File "/usr/src/homeassistant/homeassistant/components/media_player/__init__.py", line 722, in async_play_media
    await self.hass.async_add_executor_job(
  File "/usr/local/lib/python3.9/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/src/homeassistant/homeassistant/components/plex/media_player.py", line 484, in play_media
    result = process_plex_payload(
  File "/usr/src/homeassistant/homeassistant/components/plex/services.py", line 176, in process_plex_payload
    playqueue = plex_server.create_playqueue(media, shuffle=shuffle)
  File "/usr/src/homeassistant/homeassistant/components/plex/server.py", line 605, in create_playqueue
    return plexapi.playqueue.PlayQueue.create(self._plex_server, media, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/plexapi/playqueue.py", line 192, in create
    data = server.query(path, method=server._session.post)
  File "/usr/local/lib/python3.9/site-packages/plexapi/server.py", line 672, in query
    response = method(url, headers=headers, timeout=timeout, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 577, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 529, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 645, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/requests/adapters.py", line 532, in send
    raise ReadTimeout(e, request=request)
requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='redacted.plex.direct', port=54321): Read timed out. (read timeout=30)

Additional information

No response

@probot-home-assistant
Copy link

plex documentation
plex source
(message by IssueLinks)

@probot-home-assistant
Copy link

Hey there @jjlawren, mind taking a look at this issue as it has been labeled with an integration (plex) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

@jjlawren
Copy link
Contributor

Thanks for the very thorough investigation! Definitely not a use-case I've run into myself, but seems simple enough to address.

@jjlawren
Copy link
Contributor

@charlie89 I'm curious if this works better for you in 2022.4.2.

@charlie89
Copy link
Author

@jjlawren That fixed it, thanks. The video now starts playing basically instantly.

@github-actions github-actions bot locked and limited conversation to collaborators May 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants