Move http media server to separate class

This commit is contained in:
hensm
2019-05-01 23:20:39 +01:00
committed by Matt Hensman
parent fc1dd28254
commit b44056a255
7 changed files with 104 additions and 54 deletions

73
app/src/MediaServer.ts Normal file
View File

@@ -0,0 +1,73 @@
"use strict";
import fs from "fs";
import http from "http";
import mime from "mime-types";
import EventEmitter from "events";
import { Message
, SendMessageCallback } from "./types";
export default class MediaServer extends EventEmitter {
private httpServer: http.Server;
constructor (
private filePath: string
, private port: number) {
super();
this.httpServer = http.createServer(this.requestListener.bind(this));
}
public start () {
this.httpServer.listen(this.port, () => {
this.emit("started");
});
}
public stop () {
if (this.httpServer && this.httpServer.listening) {
this.httpServer.close(() => {
this.emit("stopped");
});
}
}
private requestListener (
req: http.IncomingMessage
, res: http.ServerResponse) {
const { size: fileSize } = fs.statSync(this.filePath);
const { range } = req.headers;
const contentType = mime.lookup(this.filePath) || "video/mp4";
// Partial content HTTP 206
if (range) {
const bounds = range.substring(6).split("-");
const start = parseInt(bounds[0]);
const end = bounds[1] ? parseInt(bounds[1]) : fileSize - 1;
const chunkSize = (end - start) + 1;
res.writeHead(206, {
"Accept-Ranges": "bytes"
, "Content-Range": `bytes ${start}-${end}/${fileSize}`
, "Content-Length": chunkSize
, "Content-Type": contentType
});
fs.createReadStream(this.filePath, { start, end }).pipe(res);
} else {
res.writeHead(200, {
"Content-Length": fileSize
, "Content-Type": contentType
});
fs.createReadStream(this.filePath).pipe(res);
}
}
}

View File

@@ -8,6 +8,7 @@ import mime from "mime-types";
import path from "path";
import Media from "./Media";
import MediaServer from "./MediaServer";
import Session from "./Session";
import StatusListener from "./StatusListener";
import * as transforms from "./transforms";
@@ -27,11 +28,11 @@ events.EventEmitter.defaultMaxListeners = 50;
const browser = new dnssd.Browser(dnssd.tcp("googlecast"));
// Local media server
let httpServer: http.Server;
let mediaServer: MediaServer;
process.on("SIGTERM", () => {
if (httpServer) {
httpServer.close();
if (mediaServer) {
mediaServer.stop();
}
});
@@ -182,58 +183,32 @@ async function handleMessage (message: Message) {
}
case "bridge:/startHttpServer": {
case "bridge:/mediaServer/start": {
const { filePath, port } = message.data;
httpServer = http.createServer((req, res) => {
const { size: fileSize } = fs.statSync(filePath);
const { range } = req.headers;
mediaServer = new MediaServer(filePath, port);
mediaServer.start();
const contentType = mime.lookup(filePath) || "video/mp4";
// Partial content HTTP 206
if (range) {
const bounds = range.substring(6).split("-");
const start = parseInt(bounds[0]);
const end = bounds[1]
? parseInt(bounds[1])
: fileSize - 1;
const chunkSize = (end - start) + 1;
res.writeHead(206, {
"Accept-Ranges": "bytes"
, "Content-Range": `bytes ${start}-${end}/${fileSize}`
, "Content-Length": chunkSize
, "Content-Type": contentType
});
fs.createReadStream(filePath, { start, end }).pipe(res);
} else {
res.writeHead(200, {
"Content-Length": fileSize
, "Content-Type": contentType
});
fs.createReadStream(filePath).pipe(res);
}
});
httpServer.listen(port, () => {
mediaServer.on("started", () => {
sendMessage({
subject: "mediaCast:/httpServerStarted"
subject: "mediaCast:/mediaServer/started"
});
});
mediaServer.on("stopped", () => {
sendMessage({
subject: "mediaCast:/mediaServer/stopped"
});
})
break;
}
case "bridge:/stopHttpServer": {
if (httpServer) {
httpServer.close();
case "bridge:/mediaServer/stop": {
if (mediaServer) {
mediaServer.stop();
}
break;
}
}

View File

@@ -2,7 +2,7 @@
export interface Message {
subject: string;
data: any;
data?: any;
_id?: string;
}