|
| 1 | +from homekit import AccessoryServer |
| 2 | +from homekit.model import CameraAccessory, ManagedRTPStreamService, MicrophoneService |
| 3 | +from homekit.model.characteristics.rtp_stream.setup_endpoints import Address, IPVersion |
| 4 | +from homekit.model.characteristics.rtp_stream.supported_audio_stream_configuration import \ |
| 5 | + SupportedAudioStreamConfiguration, AudioCodecConfiguration, AudioCodecType, AudioCodecParameters, BitRate, \ |
| 6 | + SampleRate |
| 7 | +from homekit.model.characteristics.rtp_stream.supported_rtp_configuration import SupportedRTPConfiguration, \ |
| 8 | + CameraSRTPCryptoSuite |
| 9 | +from homekit.model.characteristics.rtp_stream.supported_video_stream_configuration import \ |
| 10 | + SupportedVideoStreamConfiguration, VideoCodecConfiguration, VideoCodecParameters, H264Profile, H264Level, \ |
| 11 | + VideoAttributes |
| 12 | + |
| 13 | +import subprocess |
| 14 | +import base64 |
| 15 | + |
| 16 | +if __name__ == '__main__': |
| 17 | + try: |
| 18 | + httpd = AccessoryServer('demoserver.json') |
| 19 | + |
| 20 | + accessory = CameraAccessory('Testkamera', 'wiomoc', 'Demoserver', '0001', '0.1') |
| 21 | + |
| 22 | + |
| 23 | + # accessory.set_get_image_snapshot_callback( |
| 24 | + # lambda f: open('cam-preview.jpg', 'rb').read()) |
| 25 | + |
| 26 | + class StreamHandler: |
| 27 | + def __init__(self, controller_address, srtp_params_video, **_): |
| 28 | + self.srtp_params_video = srtp_params_video |
| 29 | + self.controller_address = controller_address |
| 30 | + self.ffmpeg_process = None |
| 31 | + |
| 32 | + def on_start(self, attrs): |
| 33 | + self.ffmpeg_process = subprocess.Popen( |
| 34 | + ['ffmpeg', '-re', |
| 35 | + '-f', 'avfoundation', |
| 36 | + '-r', '30.000030', '-i', '0:0', '-threads', '0', |
| 37 | + '-vcodec', 'libx264', '-an', '-pix_fmt', 'yuv420p', |
| 38 | + '-r', str(attrs.attributes.frame_rate), |
| 39 | + '-f', 'rawvideo', '-tune', 'zerolatency', '-vf', |
| 40 | + f'scale={attrs.attributes.width}:{attrs.attributes.height}', |
| 41 | + '-b:v', '300k', '-bufsize', '300k', |
| 42 | + '-payload_type', '99', '-ssrc', '32', '-f', 'rtp', |
| 43 | + '-srtp_out_suite', 'AES_CM_128_HMAC_SHA1_80', |
| 44 | + '-srtp_out_params', base64.b64encode( |
| 45 | + self.srtp_params_video.master_key + self.srtp_params_video.master_salt).decode('ascii'), |
| 46 | + f'srtp://{self.controller_address.ip_address}:{self.controller_address.video_rtp_port}' |
| 47 | + f'?rtcpport={self.controller_address.video_rtp_port}&localrtcpport={self.controller_address.video_rtp_port}' |
| 48 | + '&pkt_size=1378' |
| 49 | + ]) |
| 50 | + |
| 51 | + def on_end(self): |
| 52 | + if self.ffmpeg_process is not None: |
| 53 | + self.ffmpeg_process.terminate() |
| 54 | + |
| 55 | + def get_ssrc(self): |
| 56 | + return (32, 32) |
| 57 | + |
| 58 | + def get_address(self): |
| 59 | + return Address(IPVersion.IPV4, httpd.data.ip, self.controller_address.video_rtp_port, |
| 60 | + self.controller_address.audio_rtp_port) |
| 61 | + |
| 62 | + |
| 63 | + stream_service = ManagedRTPStreamService( |
| 64 | + StreamHandler, |
| 65 | + SupportedRTPConfiguration( |
| 66 | + [ |
| 67 | + CameraSRTPCryptoSuite.AES_CM_128_HMAC_SHA1_80, |
| 68 | + ]), |
| 69 | + SupportedVideoStreamConfiguration( |
| 70 | + VideoCodecConfiguration( |
| 71 | + VideoCodecParameters( |
| 72 | + [H264Profile.CONSTRAINED_BASELINE_PROFILE, H264Profile.MAIN_PROFILE, H264Profile.HIGH_PROFILE], |
| 73 | + [H264Level.L_3_1, H264Level.L_3_2, H264Level.L_4] |
| 74 | + ), [ |
| 75 | + VideoAttributes(1920, 1080, 30), |
| 76 | + VideoAttributes(320, 240, 15), |
| 77 | + VideoAttributes(1280, 960, 30), |
| 78 | + VideoAttributes(1280, 720, 30), |
| 79 | + VideoAttributes(1280, 768, 30), |
| 80 | + VideoAttributes(640, 480, 30), |
| 81 | + VideoAttributes(640, 360, 30) |
| 82 | + ])), |
| 83 | + SupportedAudioStreamConfiguration([ |
| 84 | + AudioCodecConfiguration(AudioCodecType.OPUS, |
| 85 | + AudioCodecParameters(1, BitRate.VARIABLE, SampleRate.KHZ_24)), |
| 86 | + AudioCodecConfiguration(AudioCodecType.AAC_ELD, |
| 87 | + AudioCodecParameters(1, BitRate.VARIABLE, SampleRate.KHZ_16)) |
| 88 | + ], 0)) |
| 89 | + accessory.services.append(stream_service) |
| 90 | + microphone_service = MicrophoneService() |
| 91 | + accessory.services.append(microphone_service) |
| 92 | + httpd.accessories.add_accessory(accessory) |
| 93 | + |
| 94 | + httpd.publish_device() |
| 95 | + print('published device and start serving') |
| 96 | + httpd.serve_forever() |
| 97 | + except KeyboardInterrupt: |
| 98 | + print('unpublish device') |
| 99 | + httpd.unpublish_device() |
0 commit comments