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.
source share