00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <mxcpcMJPEGWriter.h>
00021
00022 #include <memory>
00023 #include <cstring>
00024
00025
00026
00027 mxcpcMJPEGWriter::mxcpcMJPEGWriter(const char *filename,
00028 const char *separator)
00029 : mxcpcBinaryFileWriter(filename) {
00030
00031 FrameNum = 0;
00032
00033 installSeparator(separator);
00034 }
00035
00036
00037 mxcpcMJPEGWriter::mxcpcMJPEGWriter(int file_descriptor,
00038 const char *separator)
00039 : mxcpcBinaryFileWriter(file_descriptor) {
00040
00041 FrameNum = 0;
00042
00043 installSeparator(separator);
00044 }
00045
00046
00047 void mxcpcMJPEGWriter::installSeparator(const char *separator) {
00048
00049 char txt[200];
00050
00051 if(separator) {
00052 try {
00053 Separator = new char[std::strlen(separator) + 1];
00054 std::strcpy(Separator, separator);
00055 } catch(std::bad_alloc) {
00056 Separator = 0;
00057 ErrorEncountered = true;
00058 }
00059 }
00060 else {
00061 Separator = 0;
00062 }
00063
00064 if(Separator && !ErrorEncountered) {
00065 std::strcpy(txt, "Content-type: multipart/x-mixed-replace;boundary=\"");
00066 receiveJPEGBytes((mxcpc::u8 *)txt, std::strlen(txt));
00067 receiveJPEGBytes((mxcpc::u8 *)Separator, std::strlen(Separator));
00068 std::strcpy(txt, "\"\r\n");
00069 receiveJPEGBytes((mxcpc::u8 *)txt, std::strlen(txt));
00070 }
00071 }
00072
00073
00074 mxcpcMJPEGWriter::~mxcpcMJPEGWriter() {
00075
00076 char txt[200];
00077
00078 if(Separator && !ErrorEncountered) {
00079 std::strcpy(txt, "\r\n--");
00080 receiveJPEGBytes((mxcpc::u8 *)txt, std::strlen(txt));
00081 receiveJPEGBytes((mxcpc::u8 *)Separator, std::strlen(Separator));
00082 std::strcpy(txt, "--\r\n");
00083 receiveJPEGBytes((mxcpc::u8 *)txt, std::strlen(txt));
00084 }
00085
00086 delete Separator;
00087 }
00088
00089
00090
00091 void mxcpcMJPEGWriter::beginFrame(void) {
00092
00093 char txt[200];
00094
00095 if(Separator && !ErrorEncountered) {
00096 std::strcpy(txt, "\r\n--");
00097 receiveJPEGBytes((mxcpc::u8 *)txt, std::strlen(txt));
00098 receiveJPEGBytes((mxcpc::u8 *)Separator, std::strlen(Separator));
00099 std::strcpy(txt, "\r\nContent-type: image/jpeg\r\n\r\n");
00100 receiveJPEGBytes((mxcpc::u8 *)txt, std::strlen(txt));
00101 }
00102 }
00103
00104
00105 void mxcpcMJPEGWriter::endFrame(void) {
00106
00107 if(ErrorEncountered) return;
00108
00109 FrameNum++;
00110 }
00111
00112
00113 void mxcpcMJPEGWriter::receiveJPEGBytes(const mxcpc::u8 *buffer,
00114 int num) {
00115
00116 receiveStreamBytes(buffer, num);
00117 }