Forked from anonymous/gist:7aa72601f708150a2870
Last active
December 1, 2015 07:31
-
-
Save luciferous/1846c46b9870cbd729ce to your computer and use it in GitHub Desktop.
HttpStreamingServer for mpeg2 videos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.twitter.finagle.http | |
import com.twitter.conversions.time._ | |
import com.twitter.finagle.http.{Request, Response, Status} | |
import com.twitter.finagle.{Http, Service} | |
import com.twitter.io.{Buf, Reader, Writer} | |
import com.twitter.util.{Await, Future, Base64StringEncoder} | |
import sys.process._ | |
object HttpStreamingServer extends App { | |
val proc = Process("./test.sh") | |
def pio(p: Promise[Reader]) = new ProcessIO(_ => (), | |
stdout => p.setValue(Reader.fromStream(stdout)), | |
_ => ()) | |
val service = new Service[Request, Response] { | |
def apply(request: Request) = { | |
val p = new Promise[Reader] | |
proc.run(pio(p)) | |
p.map { reader => | |
val response = Response(request.version, Status.Ok, reader) | |
response.setContentType("video/mp2t") | |
response | |
} | |
} | |
} | |
Await.result(Http.server | |
// Translate buffered writes into HTTP chunks. | |
.withStreaming(enabled = true) | |
// Listen on port 8080. | |
.serve("0.0.0.0:8080", service) | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment