[PD-cvs] pd/src s_audio_jack.c,1.5.4.8,1.5.4.9

Tim Blechmann timblech at users.sourceforge.net
Thu Jun 16 14:44:57 CEST 2005


Update of /cvsroot/pure-data/pd/src
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15144

Modified Files:
      Tag: devel_0_38
	s_audio_jack.c 
Log Message:
restore connection state if jack kicks us out


Index: s_audio_jack.c
===================================================================
RCS file: /cvsroot/pure-data/pd/src/s_audio_jack.c,v
retrieving revision 1.5.4.8
retrieving revision 1.5.4.9
diff -C2 -d -r1.5.4.8 -r1.5.4.9
*** s_audio_jack.c	13 May 2005 17:01:48 -0000	1.5.4.8
--- s_audio_jack.c	16 Jun 2005 12:44:55 -0000	1.5.4.9
***************
*** 1,5 ****
  
  
! /* ----------------------- Experimental routines for jack -------------- */
  #ifdef USEAPI_JACK
  
--- 1,5 ----
  
  
! /* ----------------------- Routines for jack ---------------------------- */
  #ifdef USEAPI_JACK
  
***************
*** 35,40 ****
  pthread_cond_t jack_sem;
  
! extern int sys_keepsched;
! extern int sys_callbackscheduler;
  
  static int
--- 35,40 ----
  pthread_cond_t jack_sem;
  
! t_int jack_save_connection_state(t_int* dummy);
! static void jack_restore_connection_state(void);
  
  static int
***************
*** 146,157 ****
  static t_int jack_shutdown_handler(t_int* none)
  {
  	jack_close_audio();
! 	post("jack most likely kicked us out ... trying to reconnect");
  	/* try to reconnect to jack server */
  	jack_open_audio(sys_inchannels, sys_outchannels,\
! 					sys_dacsr, sys_callbackscheduler);
  	return 0;
  }
  
  static void
  jack_shutdown (void *arg)
--- 146,166 ----
  static t_int jack_shutdown_handler(t_int* none)
  {
+ 	int i;
+ 	post("jack kicked us out ... trying to reconnect");
+ 	/* clean up */
  	jack_close_audio();
! 
  	/* try to reconnect to jack server */
  	jack_open_audio(sys_inchannels, sys_outchannels,\
! 		sys_dacsr, sys_callbackscheduler);
! 
! 	/* restore last connection state */
! 	jack_restore_connection_state();
! 
  	return 0;
  }
  
+ 
+ 
  static void
  jack_shutdown (void *arg)
***************
*** 167,170 ****
--- 176,186 ----
  }
  
+ static int jack_graph_order_callback(void* arg)
+ {
+ 	post("graph order");
+ 	sys_callback(jack_save_connection_state, NULL, 0);
+ 	return 0;
+ }
+ 
  
  static char** jack_get_clients(void)
***************
*** 174,177 ****
--- 190,194 ----
      int num_clients = 0;
      regex_t port_regex;
+ 	
  	jack_ports = jack_get_ports( jack_client, "", "", 0 );
      regcomp( &port_regex, "^[^:]*", REG_EXTENDED );
***************
*** 228,232 ****
      }
  
!     /*    for (i=0;i<num_clients;i++) post("client: %s",jack_client_names[i]); */
  
      free( jack_ports );
--- 245,249 ----
      }
  
! /* 	for (i=0;i<num_clients;i++) post("client: %s",jack_client_names[i]); */
  
      free( jack_ports );
***************
*** 251,254 ****
--- 268,273 ----
  	jack_ports = jack_get_ports( jack_client, regex_pattern,
  								 NULL, JackPortIsOutput);
+ 	
+ 	
  	if (jack_ports) 
  		for (i=0;jack_ports[i] != NULL && i < sys_inchannels;i++)      
***************
*** 264,270 ****
  			if (jack_connect (jack_client, jack_port_name (output_port[i]), jack_ports[i])) 
  				fprintf (stderr, "cannot connect output ports %s -> %s\n", jack_port_name (output_port[i]),jack_ports[i]);
!   
!   
!   
  	free(jack_ports);
  	return 0;
--- 283,289 ----
  			if (jack_connect (jack_client, jack_port_name (output_port[i]), jack_ports[i])) 
  				fprintf (stderr, "cannot connect output ports %s -> %s\n", jack_port_name (output_port[i]),jack_ports[i]);
! 	
! 	
! 	
  	free(jack_ports);
  	return 0;
***************
*** 272,276 ****
  
  
! void jack_error(const char *desc) {
  	return;
  }
--- 291,296 ----
  
  
! static void jack_error(const char *desc) 
! {
  	return;
  }
***************
*** 285,292 ****
  	int new_jack = 0;
  	int srate;
! 
  	jack_dio_error = 0;
          
! 	if ((inchans == 0) && (outchans == 0)) return 0;
  
  	if (outchans > NUM_JACK_PORTS) {
--- 305,317 ----
  	int new_jack = 0;
  	int srate;
! 	int ret;
! 	
  	jack_dio_error = 0;
          
! 	if ((inchans == 0) && (outchans == 0))
! 	{
! 		ret = 0;
! 		goto bailout;
! 	}
  
  	if (outchans > NUM_JACK_PORTS) {
***************
*** 300,304 ****
  	}
  
! 	if (jack_client && sys_callbackscheduler != scheduler)
  	{
  		jack_client_close(jack_client);
--- 325,329 ----
  	}
  
! 	if (jack_client && scheduler != sys_getscheduler())
  	{
  		jack_client_close(jack_client);
***************
*** 306,311 ****
  	}
  	
! 	sys_keepsched = 0;
! 	sys_callbackscheduler = scheduler;
  
  	/* try to become a client of the JACK server (we allow two pd's)*/
--- 331,335 ----
  	}
  	
! 	sys_setscheduler(scheduler);
  
  	/* try to become a client of the JACK server (we allow two pd's)*/
***************
*** 319,323 ****
  		if (!jack_client) { // jack spits out enough messages already, do not warn
              sys_inchannels = sys_outchannels = 0;
!             return 1;
  		}
            
--- 343,348 ----
  		if (!jack_client) { // jack spits out enough messages already, do not warn
              sys_inchannels = sys_outchannels = 0;
! 			ret = 1;
! 			goto bailout;
  		}
            
***************
*** 339,342 ****
--- 364,371 ----
  		jack_set_xrun_callback (jack_client, jack_xrun, NULL);
  #endif
+ 
+ 		jack_set_graph_order_callback(jack_client,
+ 			jack_graph_order_callback, NULL);
+ 
            
  		/* tell the JACK server to call `srate()' whenever
***************
*** 358,362 ****
  			output_port[j] = NULL;
  		}
-           
  		new_jack = 1;
  	}
--- 387,390 ----
***************
*** 371,375 ****
                  
  	/* create the ports */
!         
  	for (j = 0; j < inchans; j++) {
  		sprintf(port_name, "input%d", j);
--- 399,403 ----
                  
  	/* create the ports */
! 
  	for (j = 0; j < inchans; j++) {
  		sprintf(port_name, "input%d", j);
***************
*** 385,397 ****
  	/* tell the JACK server that we are ready to roll */
   
! 	if (new_jack) {
! 		if (jack_activate (jack_client)) {
              fprintf (stderr, "cannot activate client\n");
              sys_inchannels = sys_outchannels = 0;
!             return 1;
  		}
!           
  		memset(jack_outbuf,0,sizeof(jack_outbuf));
!           
  		if (jack_client_names[0])
              jack_connect_ports(jack_client_names[0]);
--- 413,428 ----
  	/* tell the JACK server that we are ready to roll */
   
! 	if (new_jack) 
! 	{
! 		if (jack_activate (jack_client)) 
! 		{
              fprintf (stderr, "cannot activate client\n");
              sys_inchannels = sys_outchannels = 0;
!             ret = 1;
! 			goto bailout;
  		}
! 
  		memset(jack_outbuf,0,sizeof(jack_outbuf));
! 		
  		if (jack_client_names[0])
              jack_connect_ports(jack_client_names[0]);
***************
*** 422,425 ****
--- 453,461 ----
  
  	return 0;
+ 		
+  bailout:
+ 	/* tb: if we can't start jack, go to traditional scheduler */
+ 	sys_setscheduler(0);
+ 	return ret;
  }
  
***************
*** 429,436 ****
--- 465,480 ----
  	if (jack_client)
  	{
+ 		int i;
+ 		sys_setscheduler(0);
  		jack_deactivate(jack_client);
  		jack_started = 0;
  		jack_client_close(jack_client);
  		jack_client = NULL;
+ 
+ 		for (i = 0; i <NUM_JACK_PORTS; i++) 
+ 		{
+ 			input_port[i] = NULL;
+ 			output_port[i] = NULL;
+ 		}
  	}
  }
***************
*** 501,503 ****
--- 545,635 ----
  }
  
+ 
+ /* ports connected to the inputs */
+ static const char ** jack_in_connections[NUM_JACK_PORTS];
+ /* ports connected to the outputs */
+ static const char ** jack_out_connections[NUM_JACK_PORTS];
+ 
+ /* tb: save the current state of pd's jack connections */
+ /* static t_int jack_shutdown_handler(t_int* none) */
+ t_int jack_save_connection_state(t_int* dummy)
+ {
+ 	int i;
+ 	return 0;
+ 	for (i = 0; i != NUM_JACK_PORTS; ++i)
+ 	{
+ 		post("%d", i);
+ 		/* saving the inputs connections */
+ 		if (jack_in_connections[i])
+ 			free(jack_in_connections[i]);
+ 		
+ 		if (i < sys_inchannels)
+ 			jack_in_connections[i] = 
+ 				jack_port_get_all_connections(jack_client, input_port[i]);
+ 		else
+ 			jack_in_connections[i] = 0;
+ 
+ 		/* saving the inputs connections */
+ 		if (jack_out_connections[i])
+ 			free(jack_out_connections[i]);
+ 		
+ 		if (i < sys_outchannels)
+ 			jack_out_connections[i] = 
+ 				jack_port_get_all_connections(jack_client, output_port[i]);
+ 		else
+ 			jack_out_connections[i] = 0;
+ 	}
+ 	return 0;
+ }
+ 
+ static void jack_restore_connection_state(void)
+ {
+ 	int i, j, status;
+ 	
+ 	post("restoring connections");
+ 	for (i = 0; i != NUM_JACK_PORTS; ++i)
+ 	{
+ 		/* restoring the inputs connections */
+ 		if (jack_in_connections[i])
+ 		{
+ 			j = 0;
+ 			while (1)
+ 			{
+ 				const char* src_port = jack_in_connections[i][j];
+ 				if (!src_port)
+ 					/* we've connected all incoming ports */
+ 					break;
+ 				
+ 				status = jack_connect(jack_client, src_port, 
+ 					jack_port_name(input_port[i]));
+ 				if (status)
+ 					post("cannot connect input ports %s -> %s", 
+ 						src_port, jack_port_name (input_port[i]));
+ 				++j;
+ 			}
+ 			
+ 		}
+ 
+ 		/* restoring the output connections */
+ 		if (jack_in_connections[i])
+ 		{
+ 			j = 0;
+ 			while (1)
+ 			{
+ 				const char* dst_port = jack_out_connections[i][j];
+ 				if (!dst_port)
+ 					/* we've connected all outgoing ports */
+ 					break;
+ 				
+ 				status = jack_connect(jack_client, jack_port_name(input_port[i]),
+ 					dst_port);
+ 				if (status)
+ 					post("cannot connect output ports %s -> %s", 
+ 						jack_port_name(output_port[i]), dst_port);
+ 				++j;
+ 			}
+ 		}
+ 	}
+ }
+ 
  #endif /* JACK */





More information about the Pd-cvs mailing list