Motion - Webcam Code Overview

Webcam Code Overview

Webcam

Defined in webcam.h

The webcam_buffer struct references the picture that gets streamed
struct webcam_buffer
unsigned char *ptr This is a pointer to the jpeg encodes images which is being streamed as the next mjpeg frame
int ref Need to find out
long size Size of the jpeg image

The webcam struct contains the info related to ONE connected client. It is used as part of a chain that points to the next and previous client. Each camera thread has a webcam struct member in the global cnt structure. Ie. the syntax is often

cnt->webcam.something

struct webcam
int socket the socket on which the webcam listens for incoming clients
FILE *fwrite a file handle linked to the outbound socket
struct webcam_buffer *tmpbuffer See sbove struct
References the picture that gets streamed
long filepos Position in the buffer
int nr Number of the client connected
unsigned long int last Timestamp of last sent frame
struct webcam *prev Pointer to previous client in chain. If none it is NULL
struct webcam *next Pointer to next client in chain. If none it is NULL

Initialisation

int webcam_init(struct context *cnt)

Is run from motion.c during the long init of the motion_loop before it enters the main loop. The following happens.

  • cnt->webcam.socket gets assigned to the incoming socket handle given by http_bindsock()
    • setup socket handle (socket)
    • link socket to localhost or any interface (as given by config)
    • bind socket
    • listen to socket (wait for incoming requests from clients)
    • Return handle to socket
  • cnt->webcam.next and cnt->webcam.prev gets assigned to NULL indicating that there is only one struct in the chain and no clients connected.
  • Return the socket. motion_loop uses this only to check that all went well.

Termination

  • event_stop_webcam() is called when motion_loop terminates and calls event(EVENT_STOP...)
    • webcam_stop() is called if webcam port is not 0 = disabled
      • close(cnt->webcam.socket)
      • webcam_close()
        • cnt->webcam.next is the entry point to the chain of clients and the function now runs through each webcam struct in the chain (each client)
        • if there is a picture buffer it is freed
        • Any open outbound file handle (linked to socket) is closed
        • The client socket is closed.
        • The client struct in the chain is freed

The webcam loop

The webcam loop is a subloop of the motion_loop.

The starting point of the webcam subloop is the function webcam_put
int webcam_put(struct context *cnt, unsigned char *image)

The motion_loop initiates the webcam_put according to these rules.

  • In setup mode the webcam_put is called each time a new frame is grabbed from camera. It is the special setup mode which is fed to the webcam_put.
  • In normal mode Motion always calls webcam_put once per second.
  • If the config option webcam_motion is on motion detected frames (which is not already sent from previous rule) are sent to the webcam_put.
  • If the config option webcam_motion is not on all grabbed images are sent to webcam_put.

webcam_put loop

The following section explains what happens in the webcam_put loop.

  • Check to see if we have reached the maximum number of allowed clients per camera.
    • If OK to add more we
      • Call http_acceptsock() to check for clients and get create a socket for him. Note that we only add one new client for each picture frame. http_acceptsock() returns a socket handle that we can use to send data to the client.
      • Call webcam_add_client() to create the webcam struct for the new client and add it to the chain of webcam structs.
      • cnt->stream_count in incremented. This variable keeps track of how many clients are connected to the webcam stream.
  • We now call webcam_flush() the first time.
  • webcam_check_write runs though the chain until it finds a tmpbuffer pointer which is NULL. If it finds a webcam struct with no tmpbuffer then ...
    • webcam_tmpbuffer() is called which initializes and mallocs the tmpbuffer. The size reserved for the image is the YUV420P, or greyscale given picture size (H x W x 1.5 or H x W). The returned value is a pointer to the tmpbuffer struct and gets assigned to the tmpbuffer
    • put_picture_memory() function is called which converts the current image to a jpeg frame. The size if this frame is returned and assigned to tmpbuffer->size. &tmpbuffer->ptr points to the jpeg image.
    • \r\n is appended to the buffer and its size increased by 2 bytes.
    • webcam_add_write() is called

Topic not finished. I take a little at a time

-- KennethLavrsen - 12 Jul 2005
Topic revision: r4 - 12 Jul 2005, KennethLavrsen
Copyright © 1999-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Please do not email Kenneth for support questions (read why). Use the Support Requests page or join the Mailing List.
This website only use harmless session cookies. See Cookie Policy for details. By using this website you accept the use of these cookies.