Streaming video or (stream of unknown length) using Akka-Http

I am working on akka-http (akka-http-experimental_2.11 - 0.4) for a pilot project. and I have not worked on Spray before.

I would like to stream mp4 video streaming (size may vary) in the browser. But I do not know how to create HttpEntity for HttpResponse (HttpEntity.Chunked?). I tried something dirty like this, which is not the right way, but it works in Firefox for only one 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, the server cannot process this request. Since this is an experimental project, there is not enough documentation for large file streaming.

I got a sample source code 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 a Producer for HttpEntity.Chunked. If someone can explain in plain language, this will be useful for understanding the API.

Thanks.

(PS: Someone, please create an Akka-Http tag in the stack overflow)

+5
source share
1 answer

I know this question is quite old, but in case you still need an answer: I wrote a small toy file server that sends large files via http using memory with IO mapping and encoded encoding.

https://gist.github.com/rklaehn/3f26c3f80e5870831f52#file-file-server-example

Basically, there is a method that generates Iterator [ByteString] from a file. Then you create a source from this iterator, create a ChunkStreamPart from each ByteString through the map, and send it in its path.

 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)) 
+3
source

Source: https://habr.com/ru/post/1200833/


All Articles