Guest User

Untitled

a guest
Oct 13th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. """
  4. /*
  5. * Copyright (C) 2016 Jones Chi
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. """
  20.  
  21. from threading import Thread
  22. from subprocess import Popen, PIPE, STDOUT
  23. import select, socket
  24. import SocketServer
  25.  
  26. HOST = ''
  27. PORT = 53515
  28. IP = '192.168.0.11'
  29.  
  30. bufferSize = 1024
  31. meta_data = '{"port":%d,"name":"PyReceiver @ %s","id":"%s","width":1280,"height":960,"mirror":"h264","audio":"pcm","subtitles":"text/vtt","proxyHeaders":true,"hls":false,"upsell":true}' % (PORT, IP, IP)
  32.  
  33. SAVE_TO_FILE = False
  34. class MyTCPHandler(SocketServer.BaseRequestHandler):
  35. def handle(self):
  36. if SAVE_TO_FILE:
  37. f = open('video.raw', 'wb')
  38. p = Popen(['ffplay', '-framerate', '30', '-'], stdin=PIPE, stdout=PIPE)
  39. #p = Popen(['gst-launch-1.0', 'fdsrc', '!', 'h264parse', '!', 'avdec_h264', '!', 'autovideosink'], stdin=PIPE, stdout=PIPE)
  40. skiped_metadata = False
  41. while True:
  42. data = self.request.recv(bufferSize)
  43. if data == None or len(data) <= 0:
  44. break
  45. if not skiped_metadata:
  46. print "Client connected, addr: ", self.client_address[0]
  47. if data.find('\r\n\r\n') > 0:
  48. last_ctrl = data.find('\r\n\r\n') + 4
  49. print 'Recv control data: ', data[0:last_ctrl]
  50. if len(data) > last_ctrl:
  51. p.stdin.write(data[last_ctrl:])
  52. if SAVE_TO_FILE:
  53. f.write(data[last_ctrl:])
  54. skiped_metadata = True
  55. else:
  56. p.stdin.write(data)
  57. if SAVE_TO_FILE:
  58. f.write(data)
  59. p.kill()
  60. if SAVE_TO_FILE:
  61. f.close()
  62.  
  63. def resp_hello(ip, port):
  64. send_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  65. send_sock.sendto(meta_data, (ip, port))
  66.  
  67. def handle_discovery():
  68. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  69. s.bind(('', PORT))
  70. s.setblocking(0)
  71. while True:
  72. result = select.select([s],[],[])
  73. if len(result[0]) <= 0:
  74. continue
  75. msg, address = result[0][0].recvfrom(bufferSize)
  76. print 'Receive broadcast msg: ', msg
  77. if msg == 'hello':
  78. print 'Got discover msg, src ip: %s, port: %d' % (address[0], address[1])
  79. resp_hello(address[0], address[1])
  80.  
  81.  
  82. if __name__ == "__main__":
  83. server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
  84. server_thread = Thread(target=server.serve_forever)
  85. server_thread.daemon = True
  86. server_thread.start()
  87.  
  88. handle_discovery()
  89. server.shutdown()
Add Comment
Please, Sign In to add comment