...
https://github.com/rs/zerolog for logging. We’ll stick to this package throughout this project.
https://github.com/vladimirvivien/go4vl to access the Video4Linux2 API.
func main ()
main()
line 6462
opens the device /dev/video0 which is closed when the app terminates.line 7169
reads the pixel format of the device and stores width and height globally. See Components of interest for details on the sensor. Note the difference between the image width (720) and the number of bytes per line (736) for this sensor. Each line is padded with two empty bytes and we have to work with the actual data size of 736 x 540 pixels. The sensor is set to deliver 8 Bit values per pixel by default, so each byte in gthe frame represents one pixel.line 8280
starts the camline 8886
stores the channel fro reading frames from the video device in a global variableline 9290
addsfunc frameSrv()
as handler for the http endpoint/stream
to the default http multiplexerline 9391
starts listening on port 55553 for incoming resource requests. It will start a newframeSrv
-Handler as go routine for each incoming request.
...
It first creates a HTTP multipart/x-mixed-replace keep-alive stream and sets it’s content type to image/jpeg
. Each camera frame is sent as jpeg to the http.ResponseWriter
of this connection.
line 40
It creates a buffer for frames from the video device.line 41
create a buffer an image struct used to convert the frame to jpeg.line 4443
is a loop that waits for a new frame from the video device and. Each frame is received right into the buffer of previously created image.line 45 44
creates a new partWriter for the multipartWriterline 51
copies the received frame from the video device into the prepared imageline 52
55
encodes the image as jpeg into the partWriter
...