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.

307 lines
7.5KB

  1. /** @file simple_client.c
  2. *
  3. * @brief This simple client demonstrates the basic features of JACK
  4. * as they would be used by many applications.
  5. */
  6. #include <stdio.h>
  7. #include <errno.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <math.h>
  11. #include <signal.h>
  12. #ifndef WIN32
  13. #include <unistd.h>
  14. #endif
  15. #include <jack/jack.h>
  16. #include <pthread.h>
  17. #include <time.h>
  18. #include <mqueue.h>
  19. #include <sys/types.h>
  20. #include <sys/ipc.h>
  21. #include <sys/msg.h>
  22. #include <sys/stat.h>
  23. #include <sys/wait.h>
  24. #include <sys/errno.h>
  25. #define Q_NAME "/tsq"
  26. #define Q_MSG_SIZE 10
  27. #define TABLE_SIZE (200)
  28. jack_port_t *output_port1, *output_port2;
  29. jack_client_t *client;
  30. pthread_t writerThread;
  31. FILE* filepointer;
  32. mqd_t *tsq;
  33. char msg_send[Q_MSG_SIZE];
  34. #ifndef M_PI
  35. #define M_PI (3.14159265)
  36. #endif
  37. typedef struct
  38. {
  39. float sine[TABLE_SIZE];
  40. int left_phase;
  41. int right_phase;
  42. }
  43. paTestData;
  44. static void signal_handler(int sig)
  45. {
  46. jack_client_close(client);
  47. fprintf(stderr, "signal received, exiting ...\n");
  48. pthread_kill(writerThread, -9);
  49. if( mq_close(tsq) < 0) {
  50. fprintf(filepointer, "[Q %d] close error %d %s\n", (int)*tsq, errno, strerror(errno));fflush(filepointer);
  51. } else {
  52. fprintf(filepointer, "[Q %d] close success\n", *tsq );fflush(filepointer);
  53. }
  54. exit(0);
  55. }
  56. /**
  57. * The process callback for this JACK application is called in a
  58. * special realtime thread once for each audio cycle.
  59. *
  60. * This client follows a simple rule: when the JACK transport is
  61. * running, copy the input port to the output. When it stops, exit.
  62. */
  63. void *worker_thread_listener_fileWriter()
  64. {
  65. struct timespec tim;
  66. tim.tv_sec = 0;
  67. tim.tv_nsec = 300000;
  68. mqd_t *tsq2 = mq_open(Q_NAME, O_RDWR | O_NONBLOCK);
  69. char msg_recv[Q_MSG_SIZE];
  70. while(1){
  71. if ( mq_receive(*tsq2, msg_recv, Q_MSG_SIZE, NULL) > 0) {
  72. fprintf(filepointer, "%s\n",msg_recv);fflush(filepointer);
  73. } else {
  74. if(errno != EAGAIN){
  75. fprintf(filepointer, "recv error %d %s %s\n", errno, strerror(errno), msg_recv);fflush(filepointer);
  76. }
  77. }
  78. nanosleep(&tim , NULL);
  79. }
  80. }
  81. int
  82. process (jack_nframes_t nframes, void *arg)
  83. {
  84. jack_default_audio_sample_t *out1, *out2;
  85. paTestData *data = (paTestData*)arg;
  86. int i;
  87. struct timespec sys_time;
  88. if (clock_gettime(CLOCK_REALTIME, &sys_time)) {
  89. fprintf(filepointer, " Clockrealtime Error\n");fflush(filepointer);
  90. }
  91. memset(msg_send, 0, Q_MSG_SIZE);
  92. sprintf (msg_send, "%x", (sys_time.tv_sec*1000000000ULL + sys_time.tv_nsec));
  93. if (mq_send(tsq, msg_send, Q_MSG_SIZE, NULL) < 0) {
  94. fprintf(filepointer, "send error %d %s %s\n", errno, strerror(errno), msg_send);fflush(filepointer);
  95. }
  96. out1 = (jack_default_audio_sample_t*)jack_port_get_buffer (output_port1, nframes);
  97. out2 = (jack_default_audio_sample_t*)jack_port_get_buffer (output_port2, nframes);
  98. for( i=0; i<nframes; i++ )
  99. {
  100. out1[i] = data->sine[data->left_phase]; /* left */
  101. out2[i] = data->sine[data->right_phase]; /* right */
  102. data->left_phase += 1;
  103. if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE;
  104. data->right_phase += 3; /* higher pitch so we can distinguish left and right. */
  105. if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE;
  106. }
  107. return 0;
  108. }
  109. /**
  110. * JACK calls this shutdown_callback if the server ever shuts down or
  111. * decides to disconnect the client.
  112. */
  113. void
  114. jack_shutdown (void *arg)
  115. {
  116. exit (1);
  117. }
  118. int
  119. main (int argc, char *argv[])
  120. {
  121. const char **ports;
  122. const char *client_name;
  123. const char *server_name = NULL;
  124. jack_options_t options = JackNullOption;
  125. jack_status_t status;
  126. paTestData data;
  127. int i;
  128. if (argc >= 2) { /* client name specified? */
  129. client_name = argv[1];
  130. if (argc >= 3) { /* server name specified? */
  131. server_name = argv[2];
  132. int my_option = JackNullOption | JackServerName;
  133. options = (jack_options_t)my_option;
  134. }
  135. } else { /* use basename of argv[0] */
  136. client_name = strrchr(argv[0], '/');
  137. if (client_name == 0) {
  138. client_name = argv[0];
  139. } else {
  140. client_name++;
  141. }
  142. }
  143. if( ! (filepointer = fopen("client_ts.log", "a")) ){
  144. printf("Error Opening file %d\n", errno);
  145. return -1;
  146. }
  147. struct mq_attr attr;
  148. attr.mq_flags = 0;
  149. attr.mq_maxmsg = 1000;
  150. attr.mq_msgsize = Q_MSG_SIZE;
  151. attr.mq_curmsgs = 0;
  152. if( mq_unlink(Q_NAME) < 0) {
  153. fprintf(filepointer, "unlink %s error %d %s\n", Q_NAME, errno, strerror(errno));fflush(filepointer);
  154. } else {
  155. fprintf(filepointer, "unlink %s success\n", Q_NAME );fflush(filepointer);
  156. }
  157. if ((*tsq = mq_open(Q_NAME, O_RDWR | O_CREAT | O_NONBLOCK | O_EXCL, 0666, &attr)) == -1) {
  158. fprintf(filepointer, "create error %s %d %s\n", Q_NAME, errno, strerror(errno));fflush(filepointer);
  159. } else {
  160. fprintf(filepointer, "create success %s\n", Q_NAME);fflush(filepointer);
  161. }
  162. if( pthread_create( &writerThread, NULL, (&worker_thread_listener_fileWriter), NULL) != 0 ) {
  163. fprintf(filepointer, "Error creating thread\n");fflush(filepointer);
  164. }
  165. for( i=0; i<TABLE_SIZE; i++ )
  166. {
  167. data.sine[i] = 0.2 * (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
  168. }
  169. data.left_phase = data.right_phase = 0;
  170. /* open a client connection to the JACK server */
  171. client = jack_client_open (client_name, options, &status, server_name);
  172. if (client == NULL) {
  173. fprintf (stderr, "jack_client_open() failed, "
  174. "status = 0x%2.0x\n", status);
  175. if (status & JackServerFailed) {
  176. fprintf (stderr, "Unable to connect to JACK server\n");
  177. }
  178. exit (1);
  179. }
  180. if (status & JackServerStarted) {
  181. fprintf (stderr, "JACK server started\n");
  182. }
  183. if (status & JackNameNotUnique) {
  184. client_name = jack_get_client_name(client);
  185. fprintf (stderr, "unique name `%s' assigned\n", client_name);
  186. }
  187. /* tell the JACK server to call `process()' whenever
  188. there is work to be done.
  189. */
  190. jack_set_process_callback (client, process, &data);
  191. /* tell the JACK server to call `jack_shutdown()' if
  192. it ever shuts down, either entirely, or if it
  193. just decides to stop calling us.
  194. */
  195. jack_on_shutdown (client, jack_shutdown, 0);
  196. /* create two ports */
  197. output_port1 = jack_port_register (client, "output1",
  198. JACK_DEFAULT_AUDIO_TYPE,
  199. JackPortIsOutput, 0);
  200. output_port2 = jack_port_register (client, "output2",
  201. JACK_DEFAULT_AUDIO_TYPE,
  202. JackPortIsOutput, 0);
  203. if ((output_port1 == NULL) || (output_port2 == NULL)) {
  204. fprintf(stderr, "no more JACK ports available\n");
  205. exit (1);
  206. }
  207. /* Tell the JACK server that we are ready to roll. Our
  208. * process() callback will start running now. */
  209. if (jack_activate (client)) {
  210. fprintf (stderr, "cannot activate client");
  211. exit (1);
  212. }
  213. /* Connect the ports. You can't do this before the client is
  214. * activated, because we can't make connections to clients
  215. * that aren't running. Note the confusing (but necessary)
  216. * orientation of the driver backend ports: playback ports are
  217. * "input" to the backend, and capture ports are "output" from
  218. * it.
  219. */
  220. ports = jack_get_ports (client, NULL, NULL,
  221. JackPortIsPhysical|JackPortIsInput);
  222. if (ports == NULL) {
  223. fprintf(stderr, "no physical playback ports\n");
  224. exit (1);
  225. }
  226. if (jack_connect (client, jack_port_name (output_port1), ports[0])) {
  227. fprintf (stderr, "cannot connect output ports\n");
  228. }
  229. if (jack_connect (client, jack_port_name (output_port2), ports[1])) {
  230. fprintf (stderr, "cannot connect output ports\n");
  231. }
  232. jack_free (ports);
  233. signal(SIGQUIT, signal_handler);
  234. signal(SIGTERM, signal_handler);
  235. signal(SIGHUP, signal_handler);
  236. signal(SIGINT, signal_handler);
  237. /* keep running until the Ctrl+C */
  238. while (1) {
  239. sleep (1);
  240. }
  241. jack_client_close (client);
  242. exit (0);
  243. }