jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
6.2KB

  1. /** @file simple_session_client.c
  2. *
  3. * @brief This simple client demonstrates the most basic features of JACK
  4. * as they would be used by many applications.
  5. * this version also adds session manager functionality.
  6. */
  7. #include <stdio.h>
  8. #include <errno.h>
  9. #ifndef WIN32
  10. #include <unistd.h>
  11. #endif
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <inttypes.h>
  15. #include <jack/jack.h>
  16. #include <jack/types.h>
  17. #include <jack/session.h>
  18. jack_port_t *input_port;
  19. jack_port_t *output_port;
  20. jack_client_t *client;
  21. int simple_quit = 0;
  22. /**
  23. * The process callback for this JACK application is called in a
  24. * special realtime thread once for each audio cycle.
  25. *
  26. * This client does nothing more than copy data from its input
  27. * port to its output port. It will exit when stopped by
  28. * the user (e.g. using Ctrl-C on a unix-ish operating system)
  29. */
  30. int
  31. process (jack_nframes_t nframes, void *arg)
  32. {
  33. jack_default_audio_sample_t *in, *out;
  34. in = jack_port_get_buffer (input_port, nframes);
  35. out = jack_port_get_buffer (output_port, nframes);
  36. memcpy (out, in,
  37. sizeof (jack_default_audio_sample_t) * nframes);
  38. return 0;
  39. }
  40. void
  41. session_callback (jack_session_event_t *event, void *arg)
  42. {
  43. char retval[100];
  44. printf ("session notification\n");
  45. printf ("path %s, uuid %s, type: %s\n", event->session_dir, event->client_uuid, event->type == JackSessionSave ? "save" : "quit");
  46. snprintf (retval, 100, "jack_simple_session_client %s", event->client_uuid);
  47. event->command_line = strdup (retval);
  48. jack_session_reply( client, event );
  49. if (event->type == JackSessionSaveAndQuit) {
  50. simple_quit = 1;
  51. }
  52. jack_session_event_free (event);
  53. }
  54. /**
  55. * JACK calls this shutdown_callback if the server ever shuts down or
  56. * decides to disconnect the client.
  57. */
  58. void
  59. jack_shutdown (void *arg)
  60. {
  61. exit (1);
  62. }
  63. int
  64. main (int argc, char *argv[])
  65. {
  66. const char **ports;
  67. const char *client_name = "simple";
  68. jack_status_t status;
  69. /* open a client connection to the JACK server */
  70. if( argc == 1 )
  71. client = jack_client_open (client_name, JackNullOption, &status );
  72. else if( argc == 2 )
  73. client = jack_client_open (client_name, JackSessionID, &status, argv[1] );
  74. if (client == NULL) {
  75. fprintf (stderr, "jack_client_open() failed, "
  76. "status = 0x%2.0x\n", status);
  77. if (status & JackServerFailed) {
  78. fprintf (stderr, "Unable to connect to JACK server\n");
  79. }
  80. exit (1);
  81. }
  82. if (status & JackServerStarted) {
  83. fprintf (stderr, "JACK server started\n");
  84. }
  85. if (status & JackNameNotUnique) {
  86. client_name = jack_get_client_name(client);
  87. fprintf (stderr, "unique name `%s' assigned\n", client_name);
  88. }
  89. /* tell the JACK server to call `process()' whenever
  90. there is work to be done.
  91. */
  92. jack_set_process_callback (client, process, 0);
  93. /* tell the JACK server to call `jack_shutdown()' if
  94. it ever shuts down, either entirely, or if it
  95. just decides to stop calling us.
  96. */
  97. jack_on_shutdown (client, jack_shutdown, 0);
  98. /* tell the JACK server to call `session_callback()' if
  99. the session is saved.
  100. */
  101. jack_set_session_callback (client, session_callback, NULL);
  102. /* display the current sample rate.
  103. */
  104. printf ("engine sample rate: %" PRIu32 "\n",
  105. jack_get_sample_rate (client));
  106. /* create two ports */
  107. input_port = jack_port_register (client, "input",
  108. JACK_DEFAULT_AUDIO_TYPE,
  109. JackPortIsInput, 0);
  110. output_port = jack_port_register (client, "output",
  111. JACK_DEFAULT_AUDIO_TYPE,
  112. JackPortIsOutput, 0);
  113. if ((input_port == NULL) || (output_port == NULL)) {
  114. fprintf(stderr, "no more JACK ports available\n");
  115. exit (1);
  116. }
  117. /* Tell the JACK server that we are ready to roll. Our
  118. * process() callback will start running now. */
  119. if (jack_activate (client)) {
  120. fprintf (stderr, "cannot activate client");
  121. exit (1);
  122. }
  123. /* Connect the ports. You can't do this before the client is
  124. * activated, because we can't make connections to clients
  125. * that aren't running. Note the confusing (but necessary)
  126. * orientation of the driver backend ports: playback ports are
  127. * "input" to the backend, and capture ports are "output" from
  128. * it.
  129. */
  130. /* only do the autoconnect when not reloading from a session.
  131. * in case of a session reload, the SM will restore our connections
  132. */
  133. if (argc==1) {
  134. ports = jack_get_ports (client, NULL, NULL,
  135. JackPortIsPhysical|JackPortIsOutput);
  136. if (ports == NULL) {
  137. fprintf(stderr, "no physical capture ports\n");
  138. exit (1);
  139. }
  140. if (jack_connect (client, ports[0], jack_port_name (input_port))) {
  141. fprintf (stderr, "cannot connect input ports\n");
  142. }
  143. free (ports);
  144. ports = jack_get_ports (client, NULL, NULL,
  145. JackPortIsPhysical|JackPortIsInput);
  146. if (ports == NULL) {
  147. fprintf(stderr, "no physical playback ports\n");
  148. exit (1);
  149. }
  150. if (jack_connect (client, jack_port_name (output_port), ports[0])) {
  151. fprintf (stderr, "cannot connect output ports\n");
  152. }
  153. free (ports);
  154. }
  155. /* keep running until until we get a quit event */
  156. while (!simple_quit)
  157. #ifdef WIN32
  158. Sleep(1*1000);
  159. #else
  160. sleep(1);
  161. #endif
  162. jack_client_close (client);
  163. exit (0);
  164. }