I am working on the akka-http(akka-http-experimental_2.11 - 0.4) for an experimental project. and I have not worked on Spray before.
I would like to stream an mp4 video(size can vary) to browser. But I dont know how to create the HttpEntity for the HttpResponse (HttpEntity.Chunked ?). I have tried something dirty like this, which is not a right way to do, but this works in Firefox for only single request.
def output = Source.fromFile("C:\\Users\\karthik\\Downloads\\big_buck_bunny.mp4")(scala.io.Codec.ISO8859)
lazy val video = HttpResponse(entity = HttpEntity.Chunked(MediaTypes.`video/mp4`, Flow(output.map(_.toByte).map(a => ChunkStreamPart(ByteString(a)))).toProducer(materializer)))
When I open the same url in another tab or browser, server cant process that request. Since this is an experimental project, there is not enough documentation for large file streaming.
I got the sample source code form https://github.com/akka/akka/blob/release-2.3-dev/akka-http-core/src/test/scala/akka/http/TestServer.scala
I need to know how to create the Producer for HttpEntity.Chunked. If any one can explain in simple terms, that will be useful for understanding the API.
Thank you.
(P.S: Some one please create Akka-Http tag in Stack Overflow)
I know this question is pretty old, but in case you still need an answer: I have written a little toy file server that sends large files via http using memory mapped IO and chunked encoding.
https://gist.github.com/rklaehn/3f26c3f80e5870831f52#file-file-server-example
Basically there is a method that generates an Iterator[ByteString] from a file. Then you create a source from that iterator, create a ChunkStreamPart from each ByteString via map, and send it on its way.
val mappedByteBuffer = map(path)
val iterator = new ByteBufferIterator(mappedByteBuffer, 4096)
val chunks = Source(() => iterator).map(ChunkStreamPart.apply)
HttpResponse(entity = HttpEntity.Chunked(MediaTypes.`application/octet-stream`, chunks))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With