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.

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