// Start streaming process func (w *Webcam) StartStreaming() error { buf_count := C.uint32_t(256) res, err := C.mmapRequestBuffers(C.int(w.fd), &buf_count) if res < 0 { return err } if uint32(buf_count) < 2 { return errors.New("Insufficient buffer memory") } w.buffers = make([]buffer, uint32(buf_count)) for index, buf := range w.buffers { var length C.uint32_t var start unsafe.Pointer res, err := C.mmapQueryBuffer(C.int(w.fd), C.uint32_t(index), &length, &start) if res < 0 { if err != nil { return err } else { return errors.New("Failed to map memory") } } buf.start = start buf.length = uint32(length) w.buffers[index] = buf } for index, _ := range w.buffers { res, err = C.mmapEnqueueBuffer(C.int(w.fd), C.uint32_t(index)) if res < 0 { return errors.New("Failed to enqueue buffer") } } res, err = C.startStreaming(C.int(w.fd)) if res < 0 { return err } return nil }
// Read a single frame from the webcam // If frame cannot be read at the moment // function will return empty array func (w *Webcam) ReadFrame() ([]byte, error) { var index C.uint32_t var length C.uint32_t res, err := C.mmapDequeueBuffer(C.int(w.fd), &index, &length) if res < 0 { return nil, err } else if res > 0 { return nil, nil } buffer := w.buffers[int(index)] result := C.GoBytes(buffer.start, C.int(length)) res, err = C.mmapEnqueueBuffer(C.int(w.fd), index) if res < 0 { return result, err } else { return result, nil } }