Skip to content

Commit

Permalink
Remove port to play_url
Browse files Browse the repository at this point in the history
  • Loading branch information
postlund committed Jun 15, 2017
1 parent 6a1ef0c commit 0ad879d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
14 changes: 8 additions & 6 deletions pyatv/airplay/player.py
Expand Up @@ -14,32 +14,34 @@
TIMEOUT = 10


# TODO: Use net.HttpSession instead of ClientSession
# pylint: disable=too-few-public-methods
class AirPlayPlayer:
"""This class helps with playing media from an URL."""

def __init__(self, loop, session, address):
def __init__(self, loop, session, address, port=7000):
"""Initialize a new AirPlay instance."""
self.loop = loop
self.address = address
self.session = session
self.port = port

@asyncio.coroutine
def play_url(self, url, position=0, port=AIRPLAY_PORT):
def play_url(self, url, position=0):
"""Play media from an URL on the device."""
headers = {'User-Agent': 'MediaControl/1.0',
'Content-Type': 'text/parameters'}
body = "Content-Location: {}\nStart-Position: {}\n\n".format(
url, position)

address = self._url(port, 'play')
address = self._url(self.port, 'play')
_LOGGER.debug('AirPlay %s to %s', url, address)

resp = None
try:
resp = yield from self.session.post(
address, headers=headers, data=body, timeout=TIMEOUT)
yield from self._wait_for_media_to_end(port)
yield from self._wait_for_media_to_end()
finally:
if resp is not None:
resp.close()
Expand All @@ -50,8 +52,8 @@ def _url(self, port, command):
# Poll playback-info to find out if something is playing. It might take
# some time until the media starts playing, give it 5 seconds (attempts)
@asyncio.coroutine
def _wait_for_media_to_end(self, port):
address = self._url(port, 'playback-info')
def _wait_for_media_to_end(self):
address = self._url(self.port, 'playback-info')
attempts = 5
video_started = False
while True:
Expand Down
6 changes: 3 additions & 3 deletions pyatv/internal/apple_tv.py
Expand Up @@ -463,8 +463,7 @@ def play_url(self, url, **kwargs):
yield from self.verify_authenticated()

position = 0 if 'position' not in kwargs else int(kwargs['position'])
port = 7000 if 'port' not in kwargs else kwargs['port']
return (yield from self.player.play_url(url, position, port))
return (yield from self.player.play_url(url, position))


class AppleTVInternal(AppleTV):
Expand All @@ -486,7 +485,8 @@ def __init__(self, loop, session, details):
self._atv_metadata = MetadataInternal(self._apple_tv)
self._atv_push_updater = PushUpdaterInternal(loop, self._apple_tv)

airplay_player = player.AirPlayPlayer(loop, session, details.address)
airplay_player = player.AirPlayPlayer(
loop, session, details.address, details.airplay_port)
airplay_http = HttpSession(
session, 'http://{0}:{1}/'.format(
details.address, details.airplay_port))
Expand Down
6 changes: 3 additions & 3 deletions tests/test_airplay.py
Expand Up @@ -54,9 +54,9 @@ def test_play_video(self):
self.usecase.airplay_playback_idle()

session = ClientSession(loop=self.loop)
aplay = player.AirPlayPlayer(self.loop, session, '127.0.0.1')
yield from aplay.play_url(
STREAM, position=START_POSITION, port=self.app.port)
aplay = player.AirPlayPlayer(
self.loop, session, '127.0.0.1', port=self.app.port)
yield from aplay.play_url(STREAM, position=START_POSITION)

self.assertEqual(self.fake_atv.last_airplay_url, STREAM)
self.assertEqual(self.fake_atv.last_airplay_start, START_POSITION)
Expand Down

0 comments on commit 0ad879d

Please sign in to comment.