Created
January 1, 2020 13:05
-
-
Save orange888/8d78962b4e0ac73cd7483e5cd153118e to your computer and use it in GitHub Desktop.
Test script for sending jpeg data to LKV373 receivers
This file contains hidden or 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
#!/usr/bin/env python | |
import os | |
import sys | |
import socket | |
import time | |
import multiprocessing | |
from struct import * | |
try: | |
import netifaces | |
except: | |
# Add OS dependant advice | |
sys.stderr.write("Netifaces not found. Please install from https://pypi.python.org/pypi/netifaces" + newline()) | |
quit() | |
LOCAL_INTERFACE = 'en33' | |
MCAST_HEARTBEAT_PORT = 48689 | |
VIDEO_WIDTH = 1920 | |
VIDEO_HEIGHT = 1080 | |
VIDEO_FPS = 1 | |
try: | |
local_ip = netifaces.ifaddresses(LOCAL_INTERFACE)[2][0]['addr'] | |
except Exception as e: | |
sys.stderr.write("Unable to obtain IP address for " + input_source + newline()) | |
if debug_level is not None: | |
sys.stderr.write(error_header() + str(e[0]) + newline()) | |
quit() | |
def heartbeat(): | |
# Setup heartbeat tx socket | |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 2) | |
sock.setsockopt(socket.IPPROTO_IP, socket.IP_TOS, 0xfc) # unknown DSCP | |
sock.bind((local_ip, MCAST_HEARTBEAT_PORT)) | |
# HEARTBEAT FORMAT NOTES FROM http://danman.eu/blog/?p=110 | |
# ADDITIONAL NOTES: https://danman.eu/blog/reverse-engineering-lenkeng-hdmi-over-ip-extender/#comment-67 | |
# heartbeat_header = '5446367a60' # -63 = Sender, -60 = Receiver | |
# sender_receiver = '02' # 01 = Sender, 02 = Receiver | |
# heartbeat_padding = '0000' | |
# heartbeat_counter = '0000' # Counter not implemented | |
signal_present = 3 # present (!= 3 not present) | |
receiver_present = 2 # present (0 = not present) | |
signal_fps = VIDEO_FPS * 10 | |
uptime = time.time() | |
heartbeat = '5446367a630100000000000303030024'.decode('hex').ljust(27, "\0") | |
heartbeat += pack('>BHHHHHHLB', signal_present, VIDEO_WIDTH, VIDEO_HEIGHT, signal_fps, VIDEO_WIDTH, VIDEO_HEIGHT, | |
120, uptime, receiver_present) | |
# Pad the remaining zeros | |
heartbeat = heartbeat.ljust(512, "\0") | |
while 1: | |
# print heartbeat_sock.recv(512) | |
sock.sendto(heartbeat, ('<broadcast>', MCAST_HEARTBEAT_PORT)) | |
# data, addr = sock.recvfrom(1024) | |
# print "Rx:" + data | |
time.sleep(1) | |
def stream_transmit(): | |
MCAST_VIDEO_GROUP = '226.2.2.2' # sys.argv[1] | |
MCAST_VIDEO_PORT = 2068 # int(sys.argv[2]) | |
MCAST_AUDIO_PORT = 2067 # int(sys.argv[2]) | |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
sock.bind((local_ip, 0)) | |
frame = 0 | |
while True: | |
f = open("input.jpg") | |
f_size = os.fstat(f.fileno()).st_size | |
chunk = 0 | |
frame_msb = frame >> 8 | |
frame_lsb = frame & (1 << 8) - 1 | |
# print "frame msb:" + str(frame_msb) | |
# print "frame lsb:" + str(frame_lsb) | |
audio = pack('>xxxxBBxxxxxxxxxxxx', frame_msb, frame_lsb) | |
sock.sendto(audio, (MCAST_VIDEO_GROUP, MCAST_AUDIO_PORT)) | |
while True: | |
jpeg_data = f.read(1420) | |
is_end = f.tell() == f_size | |
chunk_msb = chunk >> 8 | |
chunk_lsb = chunk & (1 << 8) - 1 | |
# print "chunk msb:" + str(chunk_msb) | |
# print "chunk lsb:" + str(chunk_lsb) | |
if is_end: | |
chunk_msb |= 0x80 | |
data = pack('>BBBB', frame_msb, frame_lsb, chunk_msb, chunk_lsb) + jpeg_data | |
sock.sendto(data, (MCAST_VIDEO_GROUP, MCAST_VIDEO_PORT)) | |
if is_end: | |
break | |
time.sleep(0.0001) | |
chunk += 1 | |
frame += 1 | |
time.sleep(1 / VIDEO_FPS) | |
def main(): | |
heartbeat_thread = multiprocessing.Process(target=heartbeat, args=()) | |
stream_thread = multiprocessing.Process(target=stream_transmit, args=()) | |
heartbeat_thread.start() | |
# stream_thread.start() | |
while 1: | |
try: | |
time.sleep(0.5) | |
except KeyboardInterrupt: | |
heartbeat_thread.terminate() | |
# stream_thread.terminate() | |
quit() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment