@@ -161,6 +161,8 @@ type Streamer struct {
161161 auth auth.Authenticator
162162 // promCounter allows enabling/disabling Prometheus packet metrics.
163163 promCounter bool
164+ // cacheSize is the number of bytes to keep in precache
165+ cacheSize int
164166}
165167
166168// ConnectionBroker represents a policy handler for new connections.
@@ -178,9 +180,10 @@ type ConnectionBroker interface {
178180// NewStreamer creates a new packet streamer.
179181// queue is an input packet queue.
180182// qsize is the length of each connection's queue (in packets).
183+ // cachesize is the size of the precache buffer, in number of packets
181184// broker handles policy enforcement
182185// stats is a statistics collector object.
183- func NewStreamer (name string , qsize uint , broker ConnectionBroker , auth auth.Authenticator ) * Streamer {
186+ func NewStreamer (name string , cachesize uint , qsize uint , broker ConnectionBroker , auth auth.Authenticator ) * Streamer {
184187 streamer := & Streamer {
185188 name : name ,
186189 broker : broker ,
@@ -189,6 +192,7 @@ func NewStreamer(name string, qsize uint, broker ConnectionBroker, auth auth.Aut
189192 stats : & metrics.DummyCollector {},
190193 request : make (chan * ConnectionRequest ),
191194 auth : auth ,
195+ cacheSize : int (cachesize ) * protocol .MpegTsPacketSize ,
192196 }
193197 // start the command eater
194198 go streamer .eatCommands ()
@@ -248,9 +252,10 @@ func (streamer *Streamer) eatCommands() {
248252// This routine will block; you should run it asynchronously like this:
249253//
250254// queue := make(chan protocol.MpegTsPacket, inputQueueSize)
251- // go func() {
252- // log.Fatal(streamer.Stream(queue))
253- // }
255+ //
256+ // go func() {
257+ // log.Fatal(streamer.Stream(queue))
258+ // }
254259//
255260// or simply:
256261//
@@ -271,6 +276,9 @@ func (streamer *Streamer) Stream(queue <-chan protocol.MpegTsPacket) error {
271276 Command : streamerCommandStart ,
272277 }
273278
279+ // prepare the precache buffer
280+ precache := util .CreateSlidingWindow (streamer .cacheSize )
281+
274282 logger .Logkv (
275283 "event" , eventStreamerStart ,
276284 "message" , "Starting streaming" ,
@@ -286,7 +294,10 @@ func (streamer *Streamer) Stream(queue <-chan protocol.MpegTsPacket) error {
286294 //log.Printf("Got packet (length %d):\n%s\n", len(packet), hex.Dump(packet))
287295 //log.Printf("Got packet (length %d)\n", len(packet))
288296
297+ precache .Put (packet )
298+
289299 for conn := range pool {
300+
290301 select {
291302 case conn .Queue <- packet :
292303 // packet distributed, done
@@ -338,6 +349,9 @@ func (streamer *Streamer) Stream(queue <-chan protocol.MpegTsPacket) error {
338349 )
339350 pool [request .Connection ] = true
340351 request .Ok = true
352+ // write precached data
353+ // TODO maybe don't write this directly, use the queue?
354+ request .Connection .writer .Write (precache .Get ())
341355 } else {
342356 logger .Logkv (
343357 "event" , eventStreamerError ,
0 commit comments