void writeBytesCallback(void *gbl, void *pValue) {
    oddcastGlobals *g = (oddcastGlobals *)gbl;
    //mainWindow->writeBytesCallback(g->encoderNumber, pValue);

	/* pValue is a long */
	static long startTime[MAX_ENCODERS];
	static long endTime[MAX_ENCODERS];
	static long bytesWrittenInterval[MAX_ENCODERS];
	static long totalBytesWritten[MAX_ENCODERS];
	static int  initted = 0;
	char        kBPSstr[255] = "";

	if(!initted) {
		initted = 1;
		memset(&startTime, '\000', sizeof(startTime));
		memset(&endTime, '\000', sizeof(endTime));
		memset(&bytesWrittenInterval, '\000', sizeof(bytesWrittenInterval));
		memset(&totalBytesWritten, '\000', sizeof(totalBytesWritten));
	}

	if(g->encoderNumber != 0) {
		int     enc_index = g->encoderNumber - 1;
		long    bytesWritten = (long) pValue;

		if(bytesWritten == -1) {
			strcpy(kBPSstr, "");
			outputStatusCallback(g, (void *)kBPSstr);
			startTime[enc_index] = 0;
			return;
		}

		if(startTime[enc_index] == 0) {
			startTime[enc_index] = time(&(startTime[enc_index]));
			bytesWrittenInterval[enc_index] = 0;
		}

		bytesWrittenInterval[enc_index] += bytesWritten;
		totalBytesWritten[enc_index] += bytesWritten;
		endTime[enc_index] = time(&(endTime[enc_index]));
		if((endTime[enc_index] - startTime[enc_index]) > 4) {
			int     bytespersec = bytesWrittenInterval[enc_index] / (endTime[enc_index] - startTime[enc_index]);
			long    kBPS = (bytespersec * 8) / 1000;
			if(strlen(g->gMountpoint) > 0) {
				sprintf(kBPSstr, "%ld Kbps (%s)", kBPS, g->gMountpoint);
			}
			else {
				sprintf(kBPSstr, "%ld Kbps", kBPS);
			}

			outputStatusCallback(g, (void *)kBPSstr);
			startTime[enc_index] = 0;
		}
	}

} 
Exemple #2
0
void * startReconnectThread(void *url) {
	
	reconnectLoop = 1;
	reconnectThreadRunning = 1;
	
	while (reconnectLoop) {
		time_t  currentTime;

		currentTime = time(&currentTime);
		for(int i = 0; i < gMain.gNumEncoders; i++) {
			if(g[i]->forcedDisconnect) {
				LogMessage(g[i], LOG_DEBUG, "Reconnecting disconnected encoder.");

				int timeout = getReconnectSecs(g[i]);
				int timediff = currentTime - g[i]->forcedDisconnectSecs;
				if(timediff > timeout) {
					g[i]->forcedDisconnect = false;
					if(!g[i]->weareconnected) {
						setForceStop(g[i], 0);
						if(!connectToServer(g[i])) {
							g[i]->forcedDisconnect = true;
							g[i]->forcedDisconnectSecs = time(&(g[i]->forcedDisconnectSecs));
						}
						LogMessage(g[i], LOG_DEBUG, "Done Reconnecting disconnected encoder.");
					}
				}
				else {
					char    buf[255] = "";
					sprintf(buf, "Connecting in %d seconds", timeout - timediff);
					outputStatusCallback(g[i], buf);
				}
			}
		}
#ifdef WIN32
		Sleep(1000);
#else
		sleep(1);
#endif
	}
	reconnectThreadRunning = 0;
	return(NULL);
}
int main(int argc, char *argv[])
{
	char	sourceURL[1024] = "";
	char	configFile[1024] = "";
	int		c = 0;
	int 	numEncoders = 1;
	bool	background = false;

	while ((c = getopt(argc, argv, "bnh")) != -1) {
		switch (c) {
			case 'h':
				usage();
				break;
			case 'b':
				background = true;
				nostdout = true;
			case 'n':
				nostdout = true;
				break;
			default:
				usage();
		}
	}

	if (background) {
		fprintf(stderr, "Backgrounding process...\n");
		/* Shamelessly ripped from ICES */
		int ret = 0;
		/* Start up new session, to lose old session and process group */
		switch (fork()) {
			case 0: 	break; /* child continues */
			case -1: 	perror ("fork"); 
						ret = -1;
			default:
						exit (ret);
		}

		/* Disassociate process group and controlling terminal */
		setsid();

		/* Become a NON-session leader so that a */
		/* control terminal can't be reacquired */
		switch (fork()) {
			case 0: 	break; /* child continues */
			case -1: 	perror ("fork"); 
						ret = -1;
			default:
						exit (ret);
		}
	}

    char    currentlogFile[1024] = "";
    char    logPrefix[255] = "streamTranscoder";

	int		configFilesCreated = 0;
	int		encoderconfigFilesCreated = 0;

    
    sprintf(configFile, "%s", logPrefix);
    sprintf(currentlogFile, "%s", logPrefix);

    setDefaultLogFileName(currentlogFile);
    setgLogFile(&gMain, currentlogFile);
    setConfigFileName(&gMain, configFile);
    addConfigVariable(&gMain, "SourceURL");
    addConfigVariable(&gMain, "NumEncoders");
    addConfigVariable(&gMain, "AutomaticReconnectSecs");
    addConfigVariable(&gMain, "AutoConnect");
    addConfigVariable(&gMain, "LogLevel");
    addConfigVariable(&gMain, "LogFile");

    char    tmpLog[1024] = "";
	sprintf(tmpLog, "%s_%d.cfg", logPrefix, 0);
	if (!file_exists(tmpLog)) {
		printf("Creating config file %s\n", tmpLog);
		configFilesCreated = 1;
	}
	
    readConfigFile(&gMain, 0);


    for(int i = 0; i < gMain.gNumEncoders; i++) {
        if(!g[i]) {
            g[i] = (oddcastGlobals *) malloc(sizeof(oddcastGlobals));
            memset(g[i], '\000', sizeof(oddcastGlobals));
        	initializeGlobals(g[i]);
            addBasicEncoderSettings(g[i]);
        }
		else {
        	initializeGlobals(g[i]);
		}
    

        g[i]->encoderNumber = i + 1;
        char    currentlogFile[1024] = "";
        char    logPrefix[255] = "streamTranscoder";

        sprintf(currentlogFile, "%s_%d", logPrefix, g[i]->encoderNumber);

        setDefaultLogFileName(currentlogFile);
        setgLogFile(g[i], currentlogFile);
        setConfigFileName(g[i], gMain.gConfigFileName);

		sprintf(tmpLog, "%s.cfg", currentlogFile);
		if (!file_exists(tmpLog)) {
			printf("Creating config file %s\n", tmpLog);
			configFilesCreated = 1;
			encoderconfigFilesCreated = 1;
		}

        oddcastv3_init(g[i]);
    }

	if (configFilesCreated) {
		printf("One or more config files were created.\n");
		printf("Source settings are contained in %s_0.cfg\n", logPrefix);
		printf("All encoder settings are contained in %s_X.cfg, where X is the encoder number.\n", logPrefix);
		printf("\nYou must manually edit these config files before continuing.\n");
		if (encoderconfigFilesCreated) {
			printf("Your next step should probably be to ****edit %s_X.cfg where X = 1 to number of encoders**** you've defined, then rerun streamTranscoder\n", logPrefix);
		}
		else {
			printf("Your next step should probably be to ****edit %s_0.cfg and change the number of encoders****, then rerun streamTranscoder\n", logPrefix);
		}
		exit(1);
	}
    outputStatusCallback(&gMain, (void *)"Ready to connect");

	for(int i = 0; i < gMain.gNumEncoders; i++) {
		if (!connectToServer(g[i])) {
			char buf[255] = "";
			sprintf(buf, "Disconnected from server");
			g[i]->forcedDisconnect = true;
			g[i]->forcedDisconnectSecs = time(&(g[i]->forcedDisconnectSecs));
			g[i]->serverStatusCallback(g[i], (void *) buf);
		}
	}


	pthread_create(&sourceThread, NULL, &startSourceThread, gMain.gSourceURL);
	pthread_create(&reconnectThread, NULL, &startReconnectThread, NULL);
	pthread_create(&decodeThread, NULL, &startDecodeThread, NULL);

	sleep(2);
	if (!nostdout) {
		printf("streamTranscoder started.\n");
	}
	pthread_join(sourceThread, NULL);
	pthread_join(decodeThread, NULL);
	if (!nostdout) {
		fprintf(stdout, "Done...\n");
	}
	
}