Advertisement
Guest User

Go streaming server update yet again

a guest
Jan 7th, 2021
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.08 KB | None | 0 0
  1. func init() {
  2.     RouteHandler{
  3.         function: func(w http.ResponseWriter, r *http.Request) {
  4.             // Create multipart handler
  5.             mpw := multipart.NewWriter(w)
  6.             hdr := make(textproto.MIMEHeader)
  7.             hdr.Add("Content-Type", "image/png")
  8.  
  9.             // Write initial response
  10.             w.Header().Set("Content-Type", "multipart/x-mixed-replace; boundary="+mpw.Boundary())
  11.             w.WriteHeader(200)
  12.  
  13.             // Create a flusher to send frames
  14.             flusher, ok := w.(http.Flusher)
  15.             if !ok {
  16.                 return
  17.             }
  18.  
  19.             // If the request contains a token and the token maps to a valid "brain", start consuming frames from
  20.             // the brain and returning them to the client
  21.             params := r.URL.Query()
  22.             if val, ok := params["token"]; ok && len(val) > 0 {
  23.                 if b, ok := SharedMemory["brains"].(map[string]*brain.Brain)[val[0]]; ok && !b.CheckHasExit() {
  24.                     // Keep a checksum of the previous frame to avoid sending frames which haven't changed. Frames cannot
  25.                     // be compared directly (at least efficiently) as they are slices not arrays
  26.                     previousFrameChecksum := [16]byte{}
  27.  
  28.                     for {
  29.                         if !b.CheckHasExit() {
  30.                             frame, err := b.GetNextFrame(SharedMemory["conf"].(map[string]interface{})["DISPLAY_COL"].(color.Color))
  31.                             if err == nil && md5.Sum(frame) != previousFrameChecksum {
  32.                                 // Only write the frame if we succesfully read it and it's different to the previous
  33.                                 part, err := mpw.CreatePart(hdr)
  34.                                 if err != nil {
  35.                                     log.Println(err)
  36.                                     return
  37.                                 }
  38.                                 part.Write(frame)
  39.                                 //mpw.Close()
  40.  
  41.                                 // Update the checksum to this frame
  42.                                 previousFrameChecksum = md5.Sum(frame)
  43.  
  44.                                 // Flush the buffer to make sure the frame is sent ASAP
  45.                                 flusher.Flush()
  46.                             }
  47.                             // Limit the framerate to reduce CPU usage
  48.                             <-time.After(time.Duration(SharedMemory["conf"].(map[string]interface{})["FPS_LIMITER_INTERVAL"].(int)) * time.Millisecond)
  49.                         } else {
  50.                             // The brain has exit so there is no more we can do - we are braindead :P
  51.                             return
  52.                         }
  53.                     }
  54.                 }
  55.             }
  56.         },
  57.     }.Register("/stream", "/stream.png")
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement