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.

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