All pastes #824193 Raw Edit

Miscellany

public c v1 · immutable
·published 2007-12-19 21:04 UTC
rendered paste body
/****************************************************************************** * writerThrFxn ******************************************************************************/void *writerThrFxn(void *arg){    OutBufferElement   outFlush      = { WRITER_FLUSH };    unsigned int       initMask      = 0;    WriterEnv         *envp          = (WriterEnv *) arg;    void              *status        = THREAD_SUCCESS;    FILE              *outputFp;    OutBufferElement   oe;	int msg;	static int unique_file_number=0;	char output_file[32];	char file_data[16];	static int file_is_open=FALSE;	initMask |= VIDEOFILEINITIALIZED;			#ifdef FLASH_DRIVE_TEST	// mount the flash drive and open the file for writing	DBG("Mounting flash drive onto /media/ext_hdd\n");	//	system("mount /dev/sda1 /media/LEXAR");	system("mount /dev/sdb1 /media/ext_hdd");#endif    /* Signal that initialization is done and wait for other threads */    Rendezvous_meet(envp->hRendezvous);    DBG("Entering writer main loop.\n");    while(TRUE) {		// grab our message		msg = gblGetWriterMessage();		if (msg == WRITER_OPEN_NEW_FILE) {			/* make sure any previous file handle is closed *//* 			if (file_is_open) { */			if (outputFp != NULL) {				DBG("Closing current file %s\n",output_file);				if (!fclose(outputFp))					ERR("Error closing file %s\n",output_file);			}			/* format the new filename */			sprintf(output_file, "%s_%d.264", output_filename,unique_file_number);			unique_file_number++;			outputFp = fopen(output_file, "w");/* 			gblSetNumToWrite(0); */			DBG("Opening file %s...\n",output_file);			if (outputFp == NULL) {				ERR("Fail\n");				cleanup(THREAD_FAILURE);			}			/* we've opened the file, tell the video thread it can write to it*/			gblSetWriterMessage(WRITER_NO_MSG);			gblSetRecord(TRUE);/* 			file_is_open = TRUE; */			DBG("Success\n");		}		else if (msg == WRITER_CLOSE_FILE) {			DBG("Attempting to close file %s\n",output_file);			gblSetWriterMessage(WRITER_NO_MSG);			gblSetRecord(FALSE);/* 			if (file_is_open) { */			if (outputFp) {				DBG("Closing file %s\n",output_file);				fclose(outputFp);			}			else {				DBG("File was already closed\n");			}/* 			file_is_open = FALSE; *//* 			/\* Make sure the video thread isn't stuck in FifoUtil_get() *\/ *//* 			FifoUtil_put(&envp->inFifo, &outFlush); */		}		else {			/* Get an encoded buffer from the video thread */			if (FifoUtil_get(&envp->outFifo, &oe) == FIFOUTIL_FAILURE) {        				breakLoop(THREAD_FAILURE);			}			/* Is the video thread flushing the pipe? */			if (oe.id == WRITER_FLUSH) {				DBG("Breaking out of loop\n");				breakLoop(THREAD_SUCCESS);			}			/* Store the encoded frame to disk */			if (oe.id != WRITER_PRIME && oe.frameSize) {/* 				if (file_is_open) { */				if (outputFp) {					DBG("writing %d bytes into the file...\n",oe.frameSize);					if (fwrite(oe.encodedBuffer, oe.frameSize, 1, outputFp) != 1) {						ERR("Error writing the encoded data to video file\n");						breakLoop(THREAD_FAILURE);					}					/* 				sprintf(file_data,"%d\n",gblGetNumToWrite()); */					/* 				gblSetNumToWrite(gblGetNumToWrite()+1); */					/* 				DBG("Writing %s",file_data); */					/* 				if (fwrite(file_data, strlen(file_data), 1, outputFp) != 1) { */					/* 					ERR("Error with sequential number...\n"); */					/* 					breakLoop(THREAD_FAILURE); */					/* 				} */				}				else {					DBG("In writer, but not writing...+++++\n");				}			}			/* Send back the buffer to the video thread */			oe.id = 0;			if (FifoUtil_put(&envp->inFifo, &oe) == FIFOUTIL_FAILURE) {				ERR("Failed to put buffer in output fifo\n");				breakLoop(THREAD_FAILURE);			}		}	}	cleanup:	DBG("quitting from writer.c\n");    /* Make sure the other threads aren't waiting for init to complete */    Rendezvous_force(envp->hRendezvous);    /* Make sure the video thread isn't stuck in FifoUtil_get() */    FifoUtil_put(&envp->inFifo, &outFlush);    if (initMask & VIDEOFILEINITIALIZED && outputFp != NULL) {        fclose(outputFp);		//DBG("Closing file %s\n",envp->videoFile);		DBG("Closing file %s\n",output_file);    }#ifdef FLASH_DRIVE_TEST	DBG("Unmounting /media/ext_hdd\n");	//	system("umount /media/LEXAR");	system("umount /media/ext_hdd");#endif    return status;}