Play Iteratee

I am writing a web framework using scala and Play. I rely on Iteratees for actual streaming, but I ran into a problem trying to prevent the greedy client from loading data too fast and consuming the stream for all clients. To do this, I tried to create an Enumeratee that will throttle how fast Enumerator produces data. Here my enumeratee looks like

val throttlingIteratee = Iteratee.foldM[Array[Byte], Array[Byte]](new Array[Byte](0)) { (result, chunk) => val prom = Promise[Array[Byte]]() timer.schedule(new TimerTask{ def run() = prom.success(result ++ chunk) },1000) prom.future } private val chunker = Enumeratee.grouped( Traversable.take[Array[Byte]](31792) &>> throttlingIteratee ) 

The idea is that I use a timer task to create an Intergate throttle and a pair with the Enumeratee.grouped function. This seems to work quite well, but it's hard for me to figure out what value to use for the size of the piece. I want this to produce pieces at about the same speed as the sound. My audio file is encoded at 82kpbs, and I tried to calculate it in terms of bytes, but the values ​​that I come up with seem too small and the sound plays faster than the data is being transmitted.

My question is double. Is my basic approach good? And if so, how can I adjust the block size in terms of the bit rate of the audio file.

+6
source share
1 answer

A few things:

  • 1 kbps = 1000 bits per second = 125 bytes per second. So in your case, 10,250 bytes per second.
  • I see no problems with your code. I assume that you have removed the failover code for clarity.
  • I do not know your use case, but I would suggest that setting up this type of throttling might be easier to do in a proxy or web server, and not in your code.
0
source

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


All Articles