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.

203 lines
6.1KB

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