Skip to content

Commit

Permalink
Use same server for media and subtitles
Browse files Browse the repository at this point in the history
  • Loading branch information
palaviv committed Oct 30, 2016
1 parent 0261896 commit 732e68e
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions caster.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import socket
import os
from socketserver import ThreadingMixIn
import mimetypes

import pychromecast
import readchar
Expand All @@ -18,8 +19,10 @@ def get_internal_ip(dst_ip):
return s.getsockname()[0]


mimetypes.add_type("text/vtt", ".vtt")


class RequestHandler(BaseHTTPRequestHandler):
content_type = "video/mp4"
chunk_size = 1024

""" Handle HTTP requests for files which do not need transcoding """
Expand All @@ -31,6 +34,8 @@ def parse_range(self, file_size):

def do_GET(self):
self.protocol_version = "HTTP/1.1"
content_type = mimetypes.guess_type(self.path)
print(self.path, content_type)

with open(self.path, "rb") as f:
f.seek(0, 2)
Expand All @@ -46,7 +51,7 @@ def do_GET(self):
self.send_response(200)
start_range, end_range = 0, file_size
self.send_header('Content-Length', end_range - start_range + 1)
self.send_header("Content-type", self.content_type)
self.send_header("Content-type", content_type[0])
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()

Expand All @@ -72,10 +77,6 @@ def handle_one_request(self):
pass


class SubRequestHandler(RequestHandler):
content_type = "text/vtt"


class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
pass

Expand Down Expand Up @@ -135,16 +136,12 @@ def main():
server_thread = threading.Thread(target=server.serve_forever)
server_thread.start()

mc = dev.media_controller

if subs:
sub_server = HTTPServer((server_ip, 0), SubRequestHandler)
sub_server_thread = threading.Thread(target=sub_server.handle_request)
sub_server_thread.start()
subtitles_url = "http://{IP}:{PORT}/{URI}".format(IP=server_ip, PORT=sub_server.server_port, URI=subs)
subtitles_url = "http://{IP}:{PORT}/{URI}".format(IP=server_ip, PORT=server.server_port, URI=subs)
else:
subtitles_url = None

mc = dev.media_controller

media_url = "http://{IP}:{PORT}/{URI}".format(IP=server_ip, PORT=server.server_port, URI=file_path)
mc.play_media(media_url, 'video/mp4', title=os.path.basename(file_path), subtitles=subtitles_url,
current_time=seek)
Expand All @@ -157,9 +154,5 @@ def main():
server_thread.join()
server.server_close()

if subs:
sub_server_thread.join()
sub_server.server_close()

if __name__ == "__main__":
main()

0 comments on commit 732e68e

Please sign in to comment.