int mainService(int argc, char *argv[])
{
	char	sourceURL[1024] = "";
	char	configFile[1024] = "";
	int		c = 0;
	int 	numEncoders = 1;


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

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

    setgLogFile(&gMain, currentlogFile);
    setConfigFileName(&gMain, configFile);

	addConfigVariable(&gMain, "SourceURL");
	addConfigVariable(&gMain, "NumEncoders");
	addConfigVariable(&gMain, "AutomaticReconnectSecs");
	addConfigVariable(&gMain, "AutoConnect");
	addConfigVariable(&gMain, "LogLevel");
	addConfigVariable(&gMain, "LogFile");


    readConfigFile(&gMain, 0);

	strcpy(sourceURL, gMain.gSourceURL);

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

        g[i]->encoderNumber = i + 1;
        setgLogFile(g[i], gMain.gLogFile);
        setConfigFileName(g[i], gMain.gConfigFileName);
		addBasicEncoderSettings(g[i]);
        initializeGlobals(g[i]);
        edcast_init(g[i]);
		connectToServer(g[i]);
    }

	pthread_create(&sourceThread, NULL, &startSourceThread, sourceURL);
	pthread_create(&reconnectThread, NULL, &startReconnectThread, NULL);
	pthread_create(&decodeThread, NULL, &startDecodeThread, NULL);
	pthread_join(sourceThread, NULL);
	pthread_join(decodeThread, NULL);
	return 1;
}
Example #2
0
int
main (int argc, char *argv[])

{
	jack_client_t *client;
	jack_thread_info_t thread_info;
	int c;
	char	jackClientName[1024] = "";
	int ret = 0;
	struct stat buf;
	int i;

	int longopt_index = 0;
	extern int optind, opterr;
	int show_usage = 0;
	char *optstring = "p:n:c:d:f:b:B:h";
	struct option long_options[] = {
		{ "portmatch", 1, 0, 'p' },
		{ "name", 1, 0, 'n' },
		{ "config", 1, 0, 'c' },
		{ "help", 0, 0, 'h' },
		{ "duration", 1, 0, 'd' },
		{ "file", 1, 0, 'f' },
		{ "bitdepth", 1, 0, 'b' },
		{ "bufsize", 1, 0, 'B' },
		{ 0, 0, 0, 0 }
	};

	signal(SIGINT, sigHandler);

	memset (&thread_info, 0, sizeof (thread_info));
	thread_info.rb_size = DEFAULT_RB_SIZE;
	opterr = 0;


	memset(&g, '\000', sizeof(g));
	
	setServerStatusCallback(&g, serverStatusCallback);
	setGeneralStatusCallback(&g, generalStatusCallback);
	setWriteBytesCallback(&g, writeBytesCallback);

	addBasicEncoderSettings(&g);
	strcpy(jackClientName, "edcast");
	while ((c = getopt_long (argc, argv, optstring, long_options, &longopt_index)) != -1) {
		switch (c) {
		case 1:
			/* getopt signals end of '-' options */
			break;
		case 'p':
			strcpy(portMatch, optarg);
			getFirstTwo = 1;
			break;
		case 'n':
			strcpy(jackClientName, optarg);
			break;
		case 'c':
			i = stat (optarg, &buf );
			if ( i != 0 ) {
				printf("Cannot open config file (%s)\n", optarg);
				exit(1);
			}
			setConfigFileName(&g, optarg);
			readConfigFile(&g, 1);
			break;
		case 'h':
			show_usage++;
			break;
		case 'd':
			thread_info.duration = atoi (optarg);
			break;
		case 'f':
			thread_info.path = optarg;
			break;
		case 'b':
			thread_info.bitdepth = atoi (optarg);
			break;
		case 'B':
			thread_info.rb_size = atoi (optarg);
			break;
		default:
			fprintf (stderr, "error\n");
			show_usage++;
			break;
		}
	}

	if (show_usage || ((optind == argc) && (!getFirstTwo))) {
		fprintf (stderr, "usage: edcast -c configfile [ -n jack_client_name ] [ -p portmatch ] [ jackport1 [ jackport2 ... ]]\n");
		fprintf(stderr, "Where:\n");
		fprintf(stderr, "-c : config file that specified encoding settings\n");
		fprintf(stderr, "-n : name used to register with jackd\n");
		fprintf(stderr, "-p : if specified, edcast will connect to the first two output ports matching this name.\n");
		fprintf(stderr, "jackportX : explicitly specify a jack port name\n");
		exit (1);
	}



	if ((client = jack_client_new (jackClientName)) == 0) {
		fprintf (stderr, "jack server not running?\n");
		exit (1);
	}

	thread_info.client = client;
	if (getFirstTwo) {
		thread_info.channels = 2;
	}
	else {
		thread_info.channels = argc - optind;
	}
	thread_info.can_process = 0;

	setLogFlags();

	setup_edcast_thread (&thread_info);

	jack_set_process_callback (client, process, &thread_info);
	jack_on_shutdown (client, jack_shutdown, &thread_info);

	if (jack_activate (client)) {
		fprintf (stderr, "cannot activate client");
	}

	setup_ports (thread_info.channels, &argv[optind], &thread_info);

	run_edcast_thread (&thread_info);

	jack_client_close (client);

	jack_ringbuffer_free (rb[0]);
	jack_ringbuffer_free (rb[1]);

	exit (0);
}
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");
	}
	
}