Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Beginning of refactor for MediaRemote protocol
This is the start of moving the DAAP protocol logic into its own place so that MediaRemote protocol can be implemented alongside it. More changes must be made, for instance, a new fake Apple TV must be created so that functional tests can be shared between the two protocol implementations. Also, the AirPlay parts in the test suite should be extracted to its own FakeAirPlayDevice (or similiar) to make re-use of tests easier.
- Loading branch information
Showing
18 changed files
with
242 additions
and
236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
"""Implementation of external API for AirPlay.""" | ||
|
||
import logging | ||
import asyncio | ||
import binascii | ||
|
||
from pyatv.interface import AirPlay | ||
|
||
from pyatv.airplay.srp import (SRPAuthHandler, new_credentials) | ||
from pyatv.airplay.auth import (AuthenticationVerifier, DeviceAuthenticator) | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class AirPlayAPI(AirPlay): | ||
"""Implementation of API for AirPlay support.""" | ||
|
||
def __init__(self, http, airplay_player): | ||
"""Initialize a new AirPlayInternal instance.""" | ||
self.player = airplay_player | ||
self.identifier = None | ||
self.srp = SRPAuthHandler() | ||
self.verifier = AuthenticationVerifier(http, self.srp) | ||
self.auther = DeviceAuthenticator(http, self.srp) | ||
|
||
@asyncio.coroutine | ||
def generate_credentials(self): | ||
"""Create new credentials for authentication. | ||
Credentials that have been authenticated shall be saved and loaded with | ||
load_credentials before playing anything. If credentials are lost, | ||
authentication must be performed again. | ||
""" | ||
identifier, seed = new_credentials() | ||
return '{0}:{1}'.format(identifier, seed.decode().upper()) | ||
|
||
@asyncio.coroutine | ||
def load_credentials(self, credentials): | ||
"""Load existing credentials.""" | ||
split = credentials.split(':') | ||
self.identifier = split[0] | ||
self.srp.initialize(binascii.unhexlify(split[1])) | ||
_LOGGER.debug('Loaded AirPlay credentials: %s', credentials) | ||
|
||
def verify_authenticated(self): | ||
"""Check if loaded credentials are verified.""" | ||
return self.verifier.verify_authed() | ||
|
||
def start_authentication(self): | ||
"""Begin authentication proces (show PIN on screen).""" | ||
return self.auther.start_authentication() | ||
|
||
def finish_authentication(self, pin): | ||
"""End authentication process with PIN code.""" | ||
return self.auther.finish_authentication(self.identifier, pin) | ||
|
||
@asyncio.coroutine | ||
def play_url(self, url, **kwargs): | ||
"""Play media from an URL on the device. | ||
Note: This method will not yield until the media has finished playing. | ||
The Apple TV requires the request to stay open during the entire | ||
play duration. | ||
""" | ||
# If credentials have been loaded, do device verification first | ||
if self.identifier: | ||
yield from self.verify_authenticated() | ||
|
||
position = 0 if 'position' not in kwargs else int(kwargs['position']) | ||
return (yield from self.player.play_url(url, position)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Implementation of the DMAP protocol used by older devices.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.