Guest User

Untitled

a guest
Apr 15th, 2024
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. import socket
  2. import struct
  3. import threading
  4. import wave
  5. import os
  6. import sys
  7. import time
  8. import subprocess
  9. import tempfile
  10. import select
  11. from copy import copy
  12.  
  13. class Sender(threading.Thread):
  14. def __init__(self):
  15. super().__init__()
  16. TEMPDIR = tempfile.gettempdir() + "/"
  17. self.fifoin = TEMPDIR + "ffmpeg-fifo-in"
  18. self.start()
  19.  
  20. def run(self):
  21. self.fd = open(self.fifoin, "rb")
  22. self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  23. while True:
  24. try:
  25. header = bytes([0x01, 0x18, 0x02, 0x03, 0x00]) # 48khz, 24-bit, stereo
  26. data = self.fd.read(1152)
  27. sendbuf = header + data
  28. self.sock.sendto(sendbuf, ("192.168.3.199", 4010))
  29. except Exception as e:
  30. print("Except")
  31. print(e)
  32.  
  33. class Receiver(threading.Thread):
  34. def __init__(self):
  35. super().__init__()
  36. TEMPDIR = tempfile.gettempdir() + "/"
  37. self.fifo1 = TEMPDIR + "ffmpeg-fifo-1"
  38. self.fifo2 = TEMPDIR + "ffmpeg-fifo-2"
  39. self.fifoin = TEMPDIR + "ffmpeg-fifo-in"
  40. self.fifos = [self.fifo1, self.fifo2]
  41. try:
  42. try:
  43. os.remove(self.fifoin)
  44. except:
  45. pass
  46. os.mkfifo(self.fifoin)
  47. except:
  48. pass
  49. self.start()
  50. sender=Sender()
  51.  
  52. def run(self):
  53. ffmpeg_command=['ffmpeg', '-use_wallclock_as_timestamps', 'true', '-f', 's24le', '-ac', '2', '-ar', '48000', '-i', self.fifo1,
  54. '-use_wallclock_as_timestamps', 'true', '-f', 's24le', '-ac', '2', '-ar', '48000', '-i', self.fifo2,
  55. '-filter_complex', '[0]aresample=async=99999[a0],[1]aresample=async=99999[a1],[a0][a1]amix', "-y", '-f', 's24le', '-ac', '2', '-ar', '48000', self.fifoin]
  56. print(ffmpeg_command)
  57.  
  58. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  59. sock.setsockopt(socket.SOL_SOCKET,socket.SO_RCVBUF,4096)
  60. sock.bind(("", 16401))
  61.  
  62. recvbuf = bytearray(1157)
  63. framecount = [0,0]
  64. lastbuf = [bytes([0] * 1157), bytes([0] * 1157)]
  65.  
  66. closed = 1
  67. while True:
  68. ready = select.select([sock], [], [], .2)
  69. if ready[0]:
  70. recvbuf, addr = sock.recvfrom(1157)
  71. if closed == 1:
  72. for fifo in self.fifos:
  73. try:
  74. try:
  75. os.remove(fifo)
  76. except:
  77. pass
  78. os.mkfifo(fifo)
  79. except:
  80. pass
  81. framecount = [0,0]
  82. print("data, starting ffmpeg")
  83. ffmpeg = subprocess.Popen (ffmpeg_command, shell=False, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
  84. fifo1_fd = os.open(self.fifo1, os.O_RDWR)
  85. fifo1_file = os.fdopen(fifo1_fd, 'wb', 0)
  86. fifo2_fd = os.open(self.fifo2, os.O_RDWR)
  87. fifo2_file = os.fdopen(fifo2_fd, 'wb', 0)
  88. closed = 0
  89.  
  90. if addr[0] == "192.168.3.199":
  91. lastbuf[0] = copy(recvbuf[5:])
  92. fifo1_file.write(recvbuf[5:])
  93. framecount[0] = framecount[0] + 1
  94.  
  95. if addr[0] == "192.168.3.119":
  96. lastbuf[1] = copy(recvbuf[5:])
  97. fifo2_file.write(recvbuf[5:])
  98. framecount[1] = framecount[1] + 1
  99.  
  100. # Keep buffers roughly in sync while playing
  101. targetframes=max(framecount)
  102. if targetframes - framecount[0] > 8:
  103. while (targetframes - framecount[0]) > 0:
  104. fifo1_file.write(lastbuf[0])
  105. framecount[0] = framecount[0] + 1
  106.  
  107. if targetframes - framecount[1] > 8:
  108. while (targetframes - framecount[1]) > 0:
  109. fifo2_file.write(lastbuf[1])
  110. framecount[1] = framecount[1] + 1
  111. else:
  112. if closed == 0:
  113. ffmpeg.kill()
  114. print("No data, killing ffmpeg")
  115. fifo1_file.close()
  116. fifo2_file.close()
  117. closed = 1
  118. receiver=Receiver()
  119.  
  120. while True:
  121. time.sleep(50000)
Advertisement
Add Comment
Please, Sign In to add comment