00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <mxcpcMJPEGServerpushWriter.h>
00018
00019 #ifdef WIN32
00020 #include <fcntl.h>
00021 #include <io.h>
00022 #else
00023 #include <cstdio>
00024 #endif
00025 #include <cstring>
00026 #include <memory>
00027
00028
00029
00030 mxcpcMJPEGServerpushWriter::mxcpcMJPEGServerpushWriter(FILE* output_dest, const char* separator_string) {
00031
00032 if (output_dest != NULL)
00033 OutputDest = output_dest;
00034 else
00035 OutputDest = stdout;
00036
00037 SeparatorString = separator_string;
00038
00039 #ifdef O_BINARY // for Win32 (both mxvc and cygwin)
00040 setmode( fileno(OutputDest), O_BINARY );
00041 #endif
00042
00043 if (strlen(SeparatorString) > 0)
00044 fprintf (OutputDest, "Content-type: multipart/x-mixed-replace;boundary=\"%s\"\r\n", SeparatorString);
00045
00046 FrameNum = 0;
00047
00048 ErrorEncountered = false;
00049
00050 }
00051
00052 mxcpcMJPEGServerpushWriter::~mxcpcMJPEGServerpushWriter() {
00053 if (strlen(SeparatorString) > 0)
00054 fprintf (OutputDest, "\r\n--%s--\r\n", SeparatorString);
00055 fprintf (stderr, "Frames written: %d\n", FrameNum);
00056 }
00057
00058
00059 void mxcpcMJPEGServerpushWriter::beginFrame(void) {
00060 if (strlen(SeparatorString) > 0)
00061 fprintf (OutputDest, "\r\n--%s\r\nContent-type: image/jpeg\r\n\r\n", SeparatorString);
00062 }
00063
00064
00065 void mxcpcMJPEGServerpushWriter::endFrame(void) {
00066 FrameNum++;
00067 }
00068
00069
00070 void mxcpcMJPEGServerpushWriter::receiveJPEGBytes(mxcpc::u8 *buffer, int num) {
00071
00072 if (OutputDest == NULL) {
00073 ErrorEncountered = true;
00074 return;
00075 }
00076
00077 if (num < 1) {
00078 ErrorEncountered = true;
00079 return;
00080 }
00081
00082 if (fwrite(buffer, num, 1, OutputDest) != 1) {
00083 OutputDest = 0;
00084 ErrorEncountered = true;
00085 }
00086 }
00087