Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import struct
- import threading
- import wave
- import os
- import sys
- import time
- import subprocess
- import tempfile
- import select
- from copy import copy
- class Sender(threading.Thread):
- def __init__(self):
- super().__init__()
- TEMPDIR = tempfile.gettempdir() + "/"
- self.fifoin = TEMPDIR + "ffmpeg-fifo-in"
- self.start()
- def run(self):
- self.fd = open(self.fifoin, "rb")
- self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- while True:
- try:
- header = bytes([0x01, 0x18, 0x02, 0x03, 0x00]) # 48khz, 24-bit, stereo
- data = self.fd.read(1152)
- sendbuf = header + data
- self.sock.sendto(sendbuf, ("192.168.3.199", 4010))
- except Exception as e:
- print("Except")
- print(e)
- class Receiver(threading.Thread):
- def __init__(self):
- super().__init__()
- TEMPDIR = tempfile.gettempdir() + "/"
- self.fifo1 = TEMPDIR + "ffmpeg-fifo-1"
- self.fifo2 = TEMPDIR + "ffmpeg-fifo-2"
- self.fifoin = TEMPDIR + "ffmpeg-fifo-in"
- self.fifos = [self.fifo1, self.fifo2]
- try:
- try:
- os.remove(self.fifoin)
- except:
- pass
- os.mkfifo(self.fifoin)
- except:
- pass
- self.start()
- sender=Sender()
- def run(self):
- ffmpeg_command=['ffmpeg', '-use_wallclock_as_timestamps', 'true', '-f', 's24le', '-ac', '2', '-ar', '48000', '-i', self.fifo1,
- '-use_wallclock_as_timestamps', 'true', '-f', 's24le', '-ac', '2', '-ar', '48000', '-i', self.fifo2,
- '-filter_complex', '[0]aresample=async=99999[a0],[1]aresample=async=99999[a1],[a0][a1]amix', "-y", '-f', 's24le', '-ac', '2', '-ar', '48000', self.fifoin]
- print(ffmpeg_command)
- sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- sock.setsockopt(socket.SOL_SOCKET,socket.SO_RCVBUF,4096)
- sock.bind(("", 16401))
- recvbuf = bytearray(1157)
- framecount = [0,0]
- lastbuf = [bytes([0] * 1157), bytes([0] * 1157)]
- closed = 1
- while True:
- ready = select.select([sock], [], [], .2)
- if ready[0]:
- recvbuf, addr = sock.recvfrom(1157)
- if closed == 1:
- for fifo in self.fifos:
- try:
- try:
- os.remove(fifo)
- except:
- pass
- os.mkfifo(fifo)
- except:
- pass
- framecount = [0,0]
- print("data, starting ffmpeg")
- ffmpeg = subprocess.Popen (ffmpeg_command, shell=False, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
- fifo1_fd = os.open(self.fifo1, os.O_RDWR)
- fifo1_file = os.fdopen(fifo1_fd, 'wb', 0)
- fifo2_fd = os.open(self.fifo2, os.O_RDWR)
- fifo2_file = os.fdopen(fifo2_fd, 'wb', 0)
- closed = 0
- if addr[0] == "192.168.3.199":
- lastbuf[0] = copy(recvbuf[5:])
- fifo1_file.write(recvbuf[5:])
- framecount[0] = framecount[0] + 1
- if addr[0] == "192.168.3.119":
- lastbuf[1] = copy(recvbuf[5:])
- fifo2_file.write(recvbuf[5:])
- framecount[1] = framecount[1] + 1
- # Keep buffers roughly in sync while playing
- targetframes=max(framecount)
- if targetframes - framecount[0] > 8:
- while (targetframes - framecount[0]) > 0:
- fifo1_file.write(lastbuf[0])
- framecount[0] = framecount[0] + 1
- if targetframes - framecount[1] > 8:
- while (targetframes - framecount[1]) > 0:
- fifo2_file.write(lastbuf[1])
- framecount[1] = framecount[1] + 1
- else:
- if closed == 0:
- ffmpeg.kill()
- print("No data, killing ffmpeg")
- fifo1_file.close()
- fifo2_file.close()
- closed = 1
- receiver=Receiver()
- while True:
- time.sleep(50000)
Advertisement
Add Comment
Please, Sign In to add comment