Skip to content

Commit e0b2b86

Browse files
onitakesrgoni
authored andcommitted
Rebase fixes
1 parent 4b4be50 commit e0b2b86

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

cmd/restreamer/restreamer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func main() {
159159

160160
auth := auth.NewAuthenticator(streamdef.Authentication, config.UserList)
161161

162-
streamer := streaming.NewStreamer(streamdef.Serve, config.OutputBuffer, controller, auth)
162+
streamer := streaming.NewStreamer(streamdef.Serve, config.OutputBuffer, config.CacheSize, controller, auth)
163163
streamer.SetCollector(reg)
164164
streamer.SetNotifier(queue)
165165

configuration/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ type Configuration struct {
137137
Resources []Resource `json:"resources"`
138138
// Notifications defines event callbacks.
139139
Notifications []Notification `json:"notifications"`
140+
// CacheSize is the number of packets to precache to boost client stream
141+
// startup time.
142+
CacheSize uint `json:"cachesize"`
140143
}
141144

142145
// DefaultConfiguration creates and returns a configuration object

streaming/streamer.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)