Beispiel #1
0
/* Find all of the records for _<name>._<protocol>.<target> */
radiodns_app_t *
radiodns_resolve_app(radiodns_t *context, const char *name, const char *protocol)
{
	char fqdn[MAXDNAME+1], dnbuf[MAXDNAME+1];
	radiodns_app_t *defapp, *namedapps, *app;
	ns_msg handle;
	ns_rr rr;
	int len, c, r;
	unsigned char *abuf;

	if(!context->target)
	{
		if(NULL == radiodns_resolve_target(context))
		{
			return NULL;
		}
	}
	if(!protocol)
	{
		protocol = "tcp";
	}
	if(strlen(name) + strlen(protocol) + strlen(context->target) + 4 > MAXDNAME)
	{
		errno = ENAMETOOLONG;
		return NULL;
	}
	sprintf(fqdn, "_%s._%s.%s", name, protocol, context->target);
	if(0 >= (len = res_query(fqdn, ns_c_in, ns_t_any, context->answer, RDNS_ANSWERBUFLEN)))
	{
		return NULL;
	}	
	if(0 > ns_initparse(context->answer, len, &handle))
	{
		return NULL;
	}
	if(0 > (len = ns_msg_count(handle, ns_s_an)))
	{
		return NULL;
	}
	defapp = NULL;
	namedapps = NULL;
	abuf = NULL;
	r = 0; /* -1 == some catchable error, -2 == catastrophic error */
	for(c = 0; c < len; c++)
	{
		if(ns_parserr(&handle, ns_s_an, c, &rr))
		{
			/* Parse failed? Hmm. */
			continue;
		}
		if(ns_rr_class(rr) != ns_c_in)
		{
			continue;
		}
		if(ns_rr_type(rr) == ns_t_ptr)
		{
			if(!abuf)
			{
				if(!(abuf = (unsigned char *) malloc(RDNS_ANSWERBUFLEN)))
				{
					r = -2;
					break;
				}
			}
			if(!(app = app_create()))
			{
				r = -2;
				break;
			}
			r = app_follow_ptr(app, abuf, handle, rr);
			if(r == 0)
			{
				app->next = namedapps;
				namedapps = app;
			}
			else if(r == -1)
			{
				radiodns_destroy_app(app);
				continue;
			}
			else
			{
				radiodns_destroy_app(app);
				break;
			}	
		}
		else if(ns_rr_type(rr) == ns_t_txt)
		{
			if(!defapp)
			{
				if(!(defapp = app_create()))
				{
					r = -2;
					break;
				}
			}
			if(-2 == (r = app_parse_txt(defapp, handle, rr, dnbuf)))
			{
				break;
			}
		}
		else if(ns_rr_type(rr) == ns_t_srv)
		{
			if(!defapp)
			{
				if(!(defapp = app_create()))
				{
					r = -2;
					break;
				}
			}
			if(!defapp->srv)
			{
				if(!(defapp->srv = (radiodns_srv_t *) calloc(len, sizeof(radiodns_srv_t))))
				{
					r = -2;
					break;
				}
			}
			r = app_parse_srv(defapp, handle, rr, dnbuf, &(defapp->srv[defapp->nsrv]));
			if(r == 0)
			{
				defapp->nsrv++;
			}
			else if(r == -2)
			{
				break;
			}
		}
	}
	free(abuf);
	if(r == -2)
	{
		radiodns_destroy_app(defapp);
		radiodns_destroy_app(namedapps);
		return NULL;
	}
	if(defapp)
	{
		if(defapp->nsrv)
		{
			defapp->next = namedapps;
			return defapp;
		}
		radiodns_destroy_app(defapp);
	}
	/* In the event that there actually wasn't anything worth returning,
	 * don't confuse matters by leaving errno set to something random.
	 */
	errno = 0;
	return namedapps;
}
int main(int argc, char* argv[]) {
	if( argc < 2 ) {
		exit_usage(argv[0]);
	}

	int bit_rate = 0,
		fps = 60,
		port = 8080,
		width = 0,
		height = 0;

	// Parse command line options
	for( int i = 1; i < argc-1; i+=2 ) {
		if( strlen(argv[i]) < 2 || i >= argc-2 || argv[i][0] != '-' ) {
			exit_usage(argv[0]);
		}

		switch( argv[i][1] ) {
			case 'b': bit_rate = atoi(argv[i+1]) * 1000; break;
			case 'p': port = atoi(argv[i+1]); break;
			case 's': sscanf(argv[i+1], "%dx%d", &width, &height); break;
			case 'f': fps = atoi(argv[i+1]); break;
			default: exit_usage(argv[0]);
		}
	}

	// Find target window
	char *window_title = argv[argc-1];
	HWND window = NULL;
	if( strcmp(window_title, "desktop") == 0 ) {
		window = GetDesktopWindow();
	}
	else if( strcmp(window_title, "cursor") == 0 ) {
		POINT cursor;
		GetCursorPos(&cursor);
		window = WindowFromPoint(cursor);
	}
	else {
		window = window_with_prefix(window_title);
	}

	if( !window ) {
		printf("No window with title starting with \"%s\"\n", window_title);
		return 0;
	}

	// Start the app
	app_t *app = app_create(window, port, bit_rate, width, height);

	char real_window_title[56];
	GetWindowTextA(window, real_window_title, sizeof(real_window_title));
	printf(
		"Window 0x%08x: \"%s\"\n"
		"Window size: %dx%d, output size: %dx%d, bit rate: %d kb/s\n\n"
		"Server started on: http://%s:%d/\n\n",
		window, real_window_title,
		app->grabber->width, app->grabber->height,
		app->encoder->out_width, app->encoder->out_height,
		app->encoder->context->bit_rate / 1000,
		server_get_host_address(app->server), app->server->port
	);

	app_run(app, fps);

	app_destroy(app);	

	return 0;
}
Beispiel #3
0
int main(int argc, char **argv) {
	int listen_fd;
	sp_session *session;
	static sp_session_config config;
	static sp_session_callbacks callbacks = {
		.logged_in		= &sess_callback_logged_in,
		.logged_out		= &sess_callback_logged_out,
		.metadata_updated	= &sess_callback_metadata_updated,
		.connection_error	= NULL,
		.message_to_user	= &sess_callback_message_to_user,
		.notify_main_thread	= &sess_callback_notify,
		.music_delivery		= &player_callback_frame_delivery,
		.play_token_lost	= &player_callback_playtoken_lost,
		.log_message		= &sess_callback_log_message,
		.end_of_track		= &player_callback_end_of_track,
		.streaming_error	= NULL,
		.userinfo_updated	= NULL,
		.start_playback		= &player_callback_start_playback,
		.stop_playback		= &player_callback_stop_playback,
		.get_audio_buffer_stats	= &player_callback_get_audio_buffer_stats,
		// libspotify 10
		.offline_status_updated	= &sess_callback_offline_status_updated,
		.offline_error		= &sess_callback_offline_error,
		// libspotify 11
		.credentials_blob_updated	= sess_callback_credentials_blob_updated,
		// libspotify 12
		.connectionstate_updated	= &sess_callback_connectionstate_updated,
		.scrobble_error			= NULL,
		.private_session_mode_changed	= NULL,
	};

	thread_main = pthread_self();

	/* Setup logging to stderr */
	openlog(LIBSPOTIFY_USERAGENT, LOG_PERROR, LOG_USER);

	/**
	 * Filter logging with one of these 
	 *
	setlogmask(LOG_UPTO(LOG_WARNING));
	setlogmask(LOG_UPTO(LOG_NOTICE));
	setlogmask(LOG_UPTO(LOG_DEBUG));
	 */
	setlogmask(LOG_UPTO(LOG_INFO));

	config.api_version = SPOTIFY_API_VERSION;
	config.cache_location = LIBSPOTIFY_CACHE_DIR;
	config.settings_location = LIBSPOTIFY_CACHE_DIR;
	config.application_key = g_appkey;
	config.application_key_size = sizeof(g_appkey);
	config.user_agent = LIBSPOTIFY_USERAGENT;
	config.callbacks = &callbacks;
	config.userdata = app_create();
	config.compress_playlists = 1;
	config.dont_save_metadata_for_playlists = 0;
	config.initially_unload_playlists = 0;
	config.device_id = NULL;

	syslog(LOG_DEBUG, "MAIN: Initializing libspotify");
	if(sp_session_create(&config, &session) != SP_ERROR_OK) {
		syslog(LOG_ERR, "MAIN: Unable to initialize libspotify");
		app_release();
		return -1;
	}

	app_set_session(session);
	if(argc == 4) {
		sp_link *link = sp_link_create_from_string(argv[3]);
		app_set_link(link);
	}

	/* This program will be run on mobile internet connections */
	sp_session_set_connection_type(session, SP_CONNECTION_TYPE_MOBILE_ROAMING);
	sp_session_set_connection_rules(session, SP_CONNECTION_RULE_NETWORK|SP_CONNECTION_RULE_NETWORK_IF_ROAMING|SP_CONNECTION_RULE_ALLOW_SYNC_OVER_MOBILE);
	sp_session_preferred_offline_bitrate(session, SP_BITRATE_160k, 0);

	if(argc < 2) {
		char username[256];

		if(sp_session_remembered_user(session, username, sizeof(username)) > 0)
			syslog(LOG_DEBUG, "MAIN: Attempting to login using stored credentials for user '%s'", username);

		if(sp_session_relogin(session) == SP_ERROR_NO_CREDENTIALS) {
			syslog(LOG_ERR, "MAIN: No credentials stored. Please run: %s <username> <password>", argv[0]);
			app_release();
			return -1;
		}
	}
	else {
		syslog(LOG_DEBUG, "MAIN: Attempting to login using command line credentials");
		sp_session_login(session, argv[1], argc == 3? argv[2]: NULL, 1, get_auth_blob());
	}

	if((listen_fd = net_create(CTRL_TCP_PORT)) < 0) {
		syslog(LOG_ERR, "MAIN: Failed to initialize external network");
		app_release();
		return -1;
	}

	mainloop(session, listen_fd);
	syslog(LOG_INFO, "MAIN: Outside main event loop, good bye!");

	net_release(listen_fd);
	app_release();

	return 0;
}