Motion - Webcam Short Write Handling Patch

Webcam Short Write Handling Patch


The patch re-implements the handling of so called "short writes" in the webcam code. This was broken in the implementation of WebcamCompressInMemory. Version 2 of the patch also removes all stream writes (fwrite, fflush), since mixing these with the lower-level "write" function is very dangerous. Version 3 of the patch corrects an error in the arguments to the 'write' command, which were incorrect when any "partial writes" were done.

The symptom was that some people had trouble getting the webcam stream to work. This was especially true for large size formats, and particularly with the Palantir client.

This patch maintains the advantages of the WebcamCompressInMemory.

Description of Patch

The patch actually implements a handling of short writes which is even smarter than the original code.

The writing to the client takes place in the webcam_flush() function in webcam.c.

The old code would write 2048 bytes at a time to the client. After each write it would check how many bytes were actually written and send the remaining. It would finish up one client before it went on to the next.

The WebcamCompressInMemory had to reimplement this code because the old code was based on the use of a tmpfile. It simply tried to write the whole shebang in one go. But the new code forgot to check for the actual byte count returned by the function write() and write the remaining bytes.

This patch will write to the first client as much as possible. It will then continue to the next client while the first is getting ready for more data. And then the next client, etc. When all clients have been fed, the webcam_flush goes back to the first client, feeding any remaining bytes (if any), and on to the next client. It continues until all clients have received all the data they are willing to accept. This should be much faster and should not create any waits or intensive retries.

Stream writes (fwrite, fflush) were used in two places. They were used to generate the initial HTTP connection message, and were used to write out separators between successive images.

For the first case, the patch causes a "tmpbuffer" to be created which contains the HTTP initial message. This buffer is later written out to the client through the (normal) webcam_flush code.

For the second case, each time a new tmpbuffer is created for a new jpeg image, the image separator is placed into the buffer just ahead of the image. It turns out that this approach is also much faster / more efficient than the previous one.

Installation of Patch

Download the patch and copy it to the source directory.

And then install with

patch < webcam_short_write_handling_patch.diff

Then go through the normal: make clean, make, make install

Change History of Patch

Initial version.

Version 2.0 - added additional changes to remove all stream writes (fwrite, fflush), which can cause trouble when mixed with straight writes.

Version 3.0 - found and fixed a problem with the arguments to the 'write' command. This was the basic cause of the Palantir client's failure.

Discussion and Comments

Topic revision: r9 - 13 Aug 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.