int main(int argc, char *argv[]) { Q_INIT_RESOURCE(ui); SOCK_STARTUP(); int result = run(argc, argv); SOCK_CLEANUP(); return result; }
int main(int argc, char *argv[]) { // tweaked by SungboKang /////////////////////////////////////////////////// int frameCnt = 0; int tempSeparateH264FileNumber = 0; int thread_id; pthread_t controlThread; //////////////////////////////////////////////////////////////////////////// char * pImgBuff; SOCKET StreamSock; char strQuery[MAX_QUERY]; int AppKey; unsigned long DaemonId = 0; int ScanMode = SCAN_RAW_MODE; int idx = 0; long nRead; int ImageSize; int nRetCode; char tmp_img_file[_MAX_PATH]; SytJesVideoCodecTypeEnum current_codec; #ifdef DECODER_ON Cffm4l ffm4l; #endif int First_IFrame_Flag = 0; pImgBuff = (char*)malloc(MAX_PACK_SIZE); if(pImgBuff == NULL) return -1; #ifdef DECODER_ON if (ffm4l.CreateFfm4lDecoder(CODEC_ID_H264)!=FFM4L_COMMON_SUCCESS) { printf("ffmpeg Create failed\n"); free(pImgBuff); return -1; } #endif current_codec = JES_VIDEO_CODEC_H264; setuped_codec = JES_VIDEO_CODEC_H264; SOCK_STARTUP(); //============================================================================= //Get FlexWATCH System Information // printf("FwSysGetCgiWp\n"); // memset(pImgBuff, 0, MAX_PACK_SIZE); // nRead = FwSysGetCgiWp(VSMID, TARGET_IPADDR, TARGET_PORT, FW_USER_ID, FW_PASS_WD, pImgBuff, MAX_PACK_SIZE, wp_domain, wp_port); // if(nRead > 0) // printf("FwSysGetCgiWp=[%s]\n",pImgBuff); //============================================================================= //============================================================================= //Open Cgi Stream AppKey = rand(); #ifdef __HEADER_STREAM_MODE_SUPPORT sprintf( strQuery, STREAM_CGI_FMT_V30, VSMID, AppKey, "0", // Port Id List (0,1,2,3) 0x00, // 0x00 = Normal Mode, 0x0f = Header Only Mode PAUSE_TIME, FWCGI_VERSION); #else // Normal Mode Only sprintf( strQuery, STREAM_CGI_FMT_V30, VSMID, AppKey, "0,1", // Port Id List (0,1,2,3) PAUSE_TIME, FWCGI_VERSION); #endif StreamSock = FwOpenGetCgiWp(TARGET_IPADDR, TARGET_PORT, FW_USER_ID, FW_PASS_WD, strQuery, OPEN_TIMEOUT, wp_domain, wp_port); if(StreamSock == INVALID_SOCKET) { SOCK_CLEANUP(); free(pImgBuff); return 0; } //============================================================================= //============================================================================= // tweaked by SungboKang //// thread_id = pthread_create(&controlThread, NULL, Control_thread_function, NULL); // make the control thread if(thread_id < 0) // when thread creating doesnt work properly { perror("thread control creation error\n"); exit(-1); } // if playlist file exists already, wipe it out and make a new one sprintf(tmp_img_file, PLAYLIST_FILENAME); if(!FileExist(tmp_img_file)) { WriteM3U8("#EXTM3U"); sprintf(tmp_img_file, "#EXT-X-TARGETDURATION:%d", FRAMECOUNT/30); WriteM3U8(tmp_img_file); WriteM3U8("#EXT-X-MEDIA-SEQUENCE:0"); } #ifdef USE_FFMPEG_LIBRARY // Initialize ffmpeg library av_register_all(); #endif ///////////////////////////// // tweaked by GJ Yang // printf("%c[2J%c[0;0H",27,27); ///////////////////////////// // Get Cgi Stream //for(idx=0 ; idx < RECV_IMAGE_CNT ; idx++) while(1) { // tweaked by GJ Yang // printf("Getting frame...\n"); // printf("%c[s",27);fflush(stdout); //save the cursor location // printf("%c[%d;%dH",27, 31, 2);fflush(stdout); // printf("\033[%dmGetting frame...%2d\033[0m for",42, frameCnt); // printf(" \033[%dmVIDEO%d.h264\033[0m\n", 41, tempSeparateH264FileNumber); // printf("%c[u",27); fflush(stdout);//restore the cursor locarion /////////////////////////////////// nRetCode = FwRcvCgiStream(StreamSock, pImgBuff, MAX_PACK_SIZE, &ImageSize, &ScanMode, &DaemonId); /******************************** CODE ADDED BY SEYEON TECH START **********************************/ p_jpeg_usr_t pJesHeader; // oject of the class that contains the header information unsigned short JesHeaderSize; // char* pH264Image; int H264FrameSize; pJesHeader = (p_jpeg_usr_t)pImgBuff; // magic code // pJesHeader->start_of_jpg[0] : It mean JPEG or H.264 // pJesHeader->start_of_jpg[1] : It mean Sequence Number 0,1,2,3... JesHeaderSize = (unsigned short)(ntohs(pJesHeader->usr_length) + 2) + 1; // previously 2 // added 1 // IT WORKS!!! // tweaked by GJ Yang // printf("################################ Header Size: %d\n", JesHeaderSize); //////////////////////////////////// // modify here remove the first 0x00 always // look at research log, june 20, 2012 H264FrameSize = ImageSize - JesHeaderSize; pH264Image = pImgBuff + JesHeaderSize + 1; /******************************** CODE ADDED BY SEYEON TECH END ***********************************/ if( nRetCode < 0 ) { // tweaked by GJ Yang /////////////////// // printf("%c[%d;%dH",27, 32, 2);fflush(stdout); // printf("\033[%dmGetCgiStream Error=%d\033[0m\n", 41, nRetCode); //////////////////////////////////////// } else #ifdef DECODER_ON { sprintf(tmp_img_file, "img%d_3_%02d.raw",__LINE__, idx); if(IsJesPacketVideo((pjpeg_usr_t)pImgBuff) ) { current_codec = GetVideoCodecTypeOfJesPacket((pjpeg_usr_t)pImgBuff); if(current_codec == setuped_codec) { if(current_codec == JES_VIDEO_CODEC_JPEG) Decoding(&ffm4l, (pjpeg_usr_t)pImgBuff, tmp_img_file); else if(current_codec == JES_VIDEO_CODEC_H264) { if(IsJesPacketVideoH264IFrame((pjpeg_usr_t)pImgBuff) ) { if(First_IFrame_Flag == 0) First_IFrame_Flag = 1; } else if(First_IFrame_Flag == 0) continue; Decoding(&ffm4l, (pjpeg_usr_t)pImgBuff, tmp_img_file); } } else if(current_codec != setuped_codec) { CodecChange(&ffm4l, current_codec); } } } #else { if(IsJesPacketVideo((pjpeg_usr_t)pImgBuff) ) { if (GetVideoCodecTypeOfJesPacket((pjpeg_usr_t)pImgBuff) == JES_VIDEO_CODEC_JPEG) sprintf(tmp_img_file, "img%d_3_%02d.jpg",__LINE__, idx); else if(GetVideoCodecTypeOfJesPacket((pjpeg_usr_t)pImgBuff) == JES_VIDEO_CODEC_H264) { if(IsJesPacketVideoH264IFrame((pjpeg_usr_t)pImgBuff) ) { if(First_IFrame_Flag == 0) First_IFrame_Flag = 1; } else if(First_IFrame_Flag == 0) continue; // sprintf(tmp_img_file, "img%d_3_%02d.264",__LINE__, idx); // original function call. saves one frame into one file // sprintf(tmp_img_file, "VIDEO.h264"); // new function call. saves many frames into one file // tweaked by SungboKang // sprintf(tmp_img_file, "VIDEO%d.h264", tempSeparateH264FileNumber); // newer function call. saves many frames into numbered files /////////////////////////// } // SavePacket(pImgBuff, tmp_img_file, ImageSize); // original function call. saves one frame into one file WITH HEADER DATA SavePacket(pH264Image, tmp_img_file, H264FrameSize); // new function call. saves a frame without the JES headers frameCnt++; } } // tweaked by SungboKang /////////////////////////////////////////////////////////////////////// // assume that a IP Camera sends 30 frames at any circumstances. // In here, it runs every 10 second(300 frames) to make a separate H264 file if(frameCnt == FRAMECOUNT) { while(QueueIsFull()); // waits till the queue is not full Enqueue(tempSeparateH264FileNumber); // put data into the queue tempSeparateH264FileNumber = (tempSeparateH264FileNumber + 1) % MAX_QUEUE_N; // tempSeparateH264FileNumber = tempSeparateH264FileNumber + 1; // If the next file numbered with 'tempSeparateH264FileNumber' exists, wipe it out // It is probable to have a flaw related to authority. // In addition, FileExist() works for files up to 2GB only sprintf(tmp_img_file, "VIDEO%d.h264", tempSeparateH264FileNumber); if(FileExist(tmp_img_file)) { if(remove(tmp_img_file) < 0) { printf("%s file removing error\n", tmp_img_file); exit(-1); } } frameCnt = 0; First_IFrame_Flag = 0; // initialize Iframe checker // makes 30 h264 files then quit looping // It should be less than MAX_QUEUE_N, otherwise working infinitely // if(tempSeparateH264FileNumber == (MAX_QUEUE_N-1)) // break; } //////////////////////////////////////////////////////////////////////////////////////////////// #endif } //============================================================================= //============================================================================= // Control Cgi Stream memset(pImgBuff, 0, MAX_PACK_SIZE); //============================================================================= // Set Stream as Normal mode ( Header + Image ) // nRead = TestStCtrCgiV30Wp(VSMID, AppKey, DaemonId, "Normal", "0,1,2,3", PAUSE_TIME,TARGET_IPADDR, TARGET_PORT, FW_USER_ID, FW_PASS_WD, pImgBuff, MAX_PACK_SIZE, wp_domain, wp_port); //============================================================================= // Set Stream as Header mode (Header Only) // nRead = FwStCtrCgiV30Wp(VSMID, AppKey, DaemonId, "Header", "0,1,2,3", PAUSE_TIME,TARGET_IPADDR, TARGET_PORT, FW_USER_ID, FW_PASS_WD, pImgBuff, MAX_PACK_SIZE, wp_domain, wp_port); //============================================================================= // Add Camera 3,4 --> PortId (2,3) // nRead = TestStCtrCgiV30Wp(VSMID, AppKey, DaemonId, "Add", "2,3", PAUSE_TIME,TARGET_IPADDR, TARGET_PORT, FW_USER_ID, FW_PASS_WD, pImgBuff, MAX_PACK_SIZE, wp_domain, wp_port); //============================================================================= // Remove Camera 2 --> PortId (1) // nRead = TestStCtrCgiV30Wp(VSMID, AppKey, DaemonId, "Remove", "1", PAUSE_TIME,TARGET_IPADDR, TARGET_PORT, FW_USER_ID, FW_PASS_WD, pImgBuff, MAX_PACK_SIZE, wp_domain, wp_port); //============================================================================= // Set pause time 1000 msec // nRead = TestStCtrCgiV30Wp(VSMID, AppKey, DaemonId, "Set", NULL, 100,TARGET_IPADDR, TARGET_PORT, FW_USER_ID, FW_PASS_WD, pImgBuff, MAX_PACK_SIZE, wp_domain, wp_port); //============================================================================= // Get Cgi Stream #if 0 for(idx=0 ; idx < RECV_IMAGE_CNT ; idx++) { sprintf(tmp_img_file, "img%d_3_%02d.jpg",__LINE__, idx); #ifdef WIN32 tmp_img_filep=fopen(tmp_img_file, "wb"); #else tmp_img_filep=fopen(tmp_img_file, "wb"); #endif nRetCode = FwRcvCgiStream(StreamSock, pImgBuff, MAX_PACK_SIZE, &ImageSize, &ScanMode, &DaemonId); if( nRetCode < 0 ) { printf("GetCgiStream Error=%d\n", nRetCode); } else { // printf("-------- Jpeg Header --------\n"); // PrintJpegHeader(pImgBuff); printf("DaemondId : 0x%08lx\n", DaemonId); printf("FwModId(0~): 0x%04x\n", GET_FW_STREAM_MOD_ID( nRetCode )); printf("PortId(0~3): 0x%04x\n", GET_FW_STREAM_PORT_ID( nRetCode )); fwrite(pImgBuff, sizeof(char), ImageSize, tmp_img_filep); } fclose(tmp_img_filep); } //============================================================================= #endif //============================================================================= // Close Cgi Stream FwCloseCgiWp(StreamSock); //============================================================================= // nRead = TestCamGetCgiV30Wp(VSMID, TARGET_IPADDR, TARGET_PORT, 0, FW_USER_ID, FW_PASS_WD, pImgBuff, MAX_PACK_SIZE, wp_domain, wp_port); // if(nRead > 0) // printf("FwSysGetCgiWp=[%s]\n",pImgBuff); // tweaked by SungboKang ////////////////////////////////////////////////// setControlThreadEndFlag(TRUE); pthread_join(controlThread, NULL); // wait for the control thread // WriteM3U8("#EXT-X-ENDLIST"); /////////////////////////////////////////////////////////////////////////// SOCK_CLEANUP(); free(pImgBuff); return 0; }