From 5f1c21b7e98b66e10159184b633dde5fc606227f Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 5 May 2020 23:33:27 +0000 Subject: [PATCH 1/2] Fix exception on missing video fields Make logging consistent --- pyhap/camera.py | 67 +++++++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/pyhap/camera.py b/pyhap/camera.py index 155aad92..5e09add6 100644 --- a/pyhap/camera.py +++ b/pyhap/camera.py @@ -223,6 +223,8 @@ ) '''Template for the ffmpeg command.''' +logger = logging.getLogger(__name__) + class Camera(Accessory): """An Accessory that can negotiated camera stream settings with iOS and start a @@ -310,7 +312,7 @@ def get_supported_audio_stream_config(audio_params): codec = AUDIO_CODEC_TYPES['AACELD'] bitrate = AUDIO_CODEC_PARAM_BIT_RATE_TYPES['VARIABLE'] else: - logging.warning('Unsupported codec %s', param_type) + logger.warning('Unsupported codec %s', param_type) continue param_samplerate = codec_param['samplerate'] @@ -321,7 +323,7 @@ def get_supported_audio_stream_config(audio_params): elif param_samplerate == 24: samplerate = AUDIO_CODEC_PARAM_SAMPLE_RATE_TYPES['KHZ_24'] else: - logging.warning('Unsupported sample rate %s', param_samplerate) + logger.warning('Unsupported sample rate %s', param_samplerate) continue param_tlv = tlv.encode(AUDIO_CODEC_PARAM_TYPES['CHANNEL'], b'\x01', @@ -332,7 +334,7 @@ def get_supported_audio_stream_config(audio_params): configs += tlv.encode(SUPPORTED_AUDIO_CODECS_TAG, config_tlv) if not has_supported_codec: - logging.warning('Client does not support any audio codec that iOS supports.') + logger.warning('Client does not support any audio codec that iOS supports.') codec = AUDIO_CODEC_TYPES['OPUS'] bitrate = AUDIO_CODEC_PARAM_BIT_RATE_TYPES['VARIABLE'] @@ -483,20 +485,25 @@ async def _start_stream(self, objs, reconfigure): # pylint: disable=unused-argu video_rtp_param = video_objs.get(VIDEO_TYPES['RTP_PARAM']) if video_rtp_param: video_rtp_param_objs = tlv.decode(video_rtp_param) - # TODO: Optionals, handle the case where they are missing - opts['v_ssrc'] = struct.unpack(' Date: Tue, 5 May 2020 23:42:34 +0000 Subject: [PATCH 2/2] Address flake8 --- pyhap/camera.py | 55 +++++++++++++++++++++------------- tests/test_accessory_driver.py | 2 +- 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/pyhap/camera.py b/pyhap/camera.py index 5e09add6..d02cff71 100644 --- a/pyhap/camera.py +++ b/pyhap/camera.py @@ -541,8 +541,10 @@ async def _start_stream(self, objs, reconfigure): # pylint: disable=unused-argu if success: self.streaming_status = STREAMING_STATUS['STREAMING'] else: - logger.error('[%s] Faled to start/reconfigure stream, deleting session.', - session_id) + logger.error( + '[%s] Failed to start/reconfigure stream, deleting session.', + session_id + ) del self.sessions[session_id] self.streaming_status = STREAMING_STATUS['AVAILABLE'] @@ -567,8 +569,11 @@ async def _stop_stream(self, objs): session_info = self.sessions.get(session_id) if not session_info: - logger.error('Requested to stop stream for session %s, but no ' - 'such session was found', session_id) + logger.error( + 'Requested to stop stream for session %s, but no ' + 'such session was found', + session_id + ) return await self.stop_stream(session_info) @@ -647,16 +652,18 @@ def set_endpoints(self, value): audio_master_key = audio_info_objs[SETUP_SRTP_PARAM['MASTER_KEY']] audio_master_salt = audio_info_objs[SETUP_SRTP_PARAM['MASTER_SALT']] - logger.debug('Received endpoint configuration:' - '\nsession_id: %s\naddress: %s\nis_ipv6: %s' - '\ntarget_video_port: %s\ntarget_audio_port: %s' - '\nvideo_crypto_suite: %s\nvideo_srtp: %s' - '\naudio_crypto_suite: %s\naudio_srtp: %s', - session_id, address, is_ipv6, target_video_port, target_audio_port, - video_crypto_suite, - to_base64_str(video_master_key + video_master_salt), - audio_crypto_suite, - to_base64_str(audio_master_key + audio_master_salt)) + logger.debug( + 'Received endpoint configuration:' + '\nsession_id: %s\naddress: %s\nis_ipv6: %s' + '\ntarget_video_port: %s\ntarget_audio_port: %s' + '\nvideo_crypto_suite: %s\nvideo_srtp: %s' + '\naudio_crypto_suite: %s\naudio_srtp: %s', + session_id, address, is_ipv6, target_video_port, target_audio_port, + video_crypto_suite, + to_base64_str(video_master_key + video_master_salt), + audio_crypto_suite, + to_base64_str(audio_master_key + audio_master_salt) + ) # Configure the SetupEndpoints response @@ -772,8 +779,11 @@ async def start_stream(self, session_info, stream_config): :return: True if and only if starting the stream command was successful. :rtype: ``bool`` """ - logger.debug('[%s] Starting stream with the following parameters: %s', - session_info['id'], stream_config) + logger.debug( + '[%s] Starting stream with the following parameters: %s', + session_info['id'], + stream_config + ) cmd = self.start_stream_cmd.format(**stream_config).split() logger.debug('Executing start stream command: "%s"', ' '.join(cmd)) @@ -788,8 +798,11 @@ async def start_stream(self, session_info, stream_config): session_info['process'] = process - logger.info('[%s] Started stream process - PID %d', - session_info['id'], process.pid) + logger.info( + '[%s] Started stream process - PID %d', + session_info['id'], + process.pid + ) return True @@ -814,8 +827,10 @@ async def stop_stream(self, session_info): # pylint: disable=no-self-use ffmpeg_process.communicate(), timeout=2.0) logger.debug('Stream command stderr: %s', stderr) except asyncio.TimeoutError: - logger.error('Timeout while waiting for the stream process ' - 'to terminate. Trying with kill.') + logger.error( + 'Timeout while waiting for the stream process ' + 'to terminate. Trying with kill.' + ) ffmpeg_process.kill() await ffmpeg_process.wait() logger.debug('Stream process stopped.') diff --git a/tests/test_accessory_driver.py b/tests/test_accessory_driver.py index 7dfbc8ff..75de5a0c 100644 --- a/tests/test_accessory_driver.py +++ b/tests/test_accessory_driver.py @@ -55,7 +55,7 @@ def test_persist_load(): def test_service_callbacks(driver): - bridge = Bridge(driver,"mybridge") + bridge = Bridge(driver, "mybridge") acc = Accessory(driver, 'TestAcc', aid=2) acc2 = Accessory(driver, 'TestAcc2', aid=3)