speexclient.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /***************************************************************************
  2. Copyright (C) 2004-2006 by Jean-Marc Valin
  3. Copyright (C) 2006 Commonwealth Scientific and Industrial Research
  4. Organisation (CSIRO) Australia
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. - Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. - Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. - Neither the name of the Xiph.org Foundation nor the names of its
  14. contributors may be used to endorse or promote products derived from
  15. this software without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  20. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  21. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  25. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. ****************************************************************************/
  28. #ifdef HAVE_CONFIG_H
  29. #include <config.h>
  30. #endif
  31. #include <stdlib.h>
  32. #include <sys/types.h>
  33. #include <sys/socket.h>
  34. #include <netinet/in.h>
  35. #include <arpa/inet.h>
  36. #include <netdb.h>
  37. #include <stdio.h>
  38. #include <unistd.h> /* close() */
  39. #include <string.h> /* memset() */
  40. #include "alsa_device.h"
  41. #include <speex/speex.h>
  42. #include <speex/speex_jitter.h>
  43. #include <speex/speex_preprocess.h>
  44. #include <speex/speex_echo.h>
  45. #include "speex_jitter_buffer.h"
  46. #include <sched.h>
  47. #define MAX_MSG 1500
  48. #define SAMPLING_RATE 16000
  49. #define FRAME_SIZE 320
  50. int main(int argc, char *argv[])
  51. {
  52. int sd, rc, n;
  53. int i;
  54. struct sockaddr_in cliAddr, remoteAddr;
  55. char msg[MAX_MSG];
  56. struct hostent *h;
  57. int local_port, remote_port;
  58. int nfds;
  59. struct pollfd *pfds;
  60. SpeexPreprocessState *preprocess;
  61. AlsaDevice *audio_dev;
  62. int tmp;
  63. if (argc != 5)
  64. {
  65. fprintf(stderr, "wrong options\n");
  66. exit(1);
  67. }
  68. h = gethostbyname(argv[2]);
  69. if(h==NULL) {
  70. fprintf(stderr, "%s: unknown host '%s' \n", argv[0], argv[1]);
  71. exit(1);
  72. }
  73. local_port = atoi(argv[3]);
  74. remote_port = atoi(argv[4]);
  75. printf("%s: sending data to '%s' (IP : %s) \n", argv[0], h->h_name,
  76. inet_ntoa(*(struct in_addr *)h->h_addr_list[0]));
  77. {
  78. remoteAddr.sin_family = h->h_addrtype;
  79. memcpy((char *) &remoteAddr.sin_addr.s_addr,
  80. h->h_addr_list[0], h->h_length);
  81. remoteAddr.sin_port = htons(remote_port);
  82. }
  83. /* socket creation */
  84. sd=socket(AF_INET, SOCK_DGRAM, 0);
  85. if(sd<0) {
  86. printf("%s: cannot open socket \n",argv[0]);
  87. exit(1);
  88. }
  89. /* bind any port */
  90. cliAddr.sin_family = AF_INET;
  91. cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  92. cliAddr.sin_port = htons(local_port);
  93. rc = bind(sd, (struct sockaddr *) &cliAddr, sizeof(cliAddr));
  94. if(rc<0) {
  95. printf("%s: cannot bind port\n", argv[0]);
  96. exit(1);
  97. }
  98. /* Setup audio device */
  99. audio_dev = alsa_device_open(argv[1], SAMPLING_RATE, 1, FRAME_SIZE);
  100. /* Setup the encoder and decoder in wideband */
  101. void *enc_state, *dec_state;
  102. enc_state = speex_encoder_init(&speex_wb_mode);
  103. tmp = 8;
  104. speex_encoder_ctl(enc_state, SPEEX_SET_QUALITY, &tmp);
  105. tmp = 2;
  106. speex_encoder_ctl(enc_state, SPEEX_SET_COMPLEXITY, &tmp);
  107. dec_state = speex_decoder_init(&speex_wb_mode);
  108. tmp = 1;
  109. speex_decoder_ctl(dec_state, SPEEX_SET_ENH, &tmp);
  110. SpeexBits enc_bits, dec_bits;
  111. speex_bits_init(&enc_bits);
  112. speex_bits_init(&dec_bits);
  113. struct sched_param param;
  114. /*param.sched_priority = 40; */
  115. param.sched_priority = sched_get_priority_min(SCHED_FIFO);
  116. if (sched_setscheduler(0,SCHED_FIFO,&param))
  117. perror("sched_setscheduler");
  118. int send_timestamp = 0;
  119. int recv_started=0;
  120. /* Setup all file descriptors for poll()ing */
  121. nfds = alsa_device_nfds(audio_dev);
  122. pfds = malloc(sizeof(*pfds)*(nfds+1));
  123. alsa_device_getfds(audio_dev, pfds, nfds);
  124. pfds[nfds].fd = sd;
  125. pfds[nfds].events = POLLIN;
  126. /* Setup jitter buffer using decoder */
  127. SpeexJitter jitter;
  128. speex_jitter_init(&jitter, dec_state, SAMPLING_RATE);
  129. /* Echo canceller with 200 ms tail length */
  130. SpeexEchoState *echo_state = speex_echo_state_init(FRAME_SIZE, 10*FRAME_SIZE);
  131. tmp = SAMPLING_RATE;
  132. speex_echo_ctl(echo_state, SPEEX_ECHO_SET_SAMPLING_RATE, &tmp);
  133. /* Setup preprocessor and associate with echo canceller for residual echo suppression */
  134. preprocess = speex_preprocess_state_init(FRAME_SIZE, SAMPLING_RATE);
  135. speex_preprocess_ctl(preprocess, SPEEX_PREPROCESS_SET_ECHO_STATE, echo_state);
  136. alsa_device_start(audio_dev);
  137. /* Infinite loop on capture, playback and receiving packets */
  138. while (1)
  139. {
  140. /* Wait for either 1) capture 2) playback 3) socket data */
  141. poll(pfds, nfds+1, -1);
  142. /* Received packets */
  143. if (pfds[nfds].revents & POLLIN)
  144. {
  145. /*fprintf (stderr, "x");*/
  146. n = recv(sd, msg, MAX_MSG, 0);
  147. int recv_timestamp = ((int*)msg)[1];
  148. int payload = ((int*)msg)[0];
  149. if ((payload & 0x80000000) == 0)
  150. {
  151. /* Put content of the packet into the jitter buffer, except for the pseudo-header */
  152. speex_jitter_put(&jitter, msg+8, n-8, recv_timestamp);
  153. recv_started = 1;
  154. }
  155. }
  156. /* Ready to play a frame (playback) */
  157. if (alsa_device_playback_ready(audio_dev, pfds, nfds))
  158. {
  159. short pcm[FRAME_SIZE];
  160. if (recv_started)
  161. {
  162. /* Get audio from the jitter buffer */
  163. speex_jitter_get(&jitter, pcm, NULL);
  164. } else {
  165. for (i=0;i<FRAME_SIZE;i++)
  166. pcm[i] = 0;
  167. }
  168. /* Playback the audio and reset the echo canceller if we got an underrun */
  169. if (alsa_device_write(audio_dev, pcm, FRAME_SIZE))
  170. speex_echo_state_reset(echo_state);
  171. /* Put frame into playback buffer */
  172. speex_echo_playback(echo_state, pcm);
  173. }
  174. /* Audio available from the soundcard (capture) */
  175. if (alsa_device_capture_ready(audio_dev, pfds, nfds))
  176. {
  177. short pcm[FRAME_SIZE], pcm2[FRAME_SIZE];
  178. char outpacket[MAX_MSG];
  179. /* Get audio from the soundcard */
  180. alsa_device_read(audio_dev, pcm, FRAME_SIZE);
  181. /* Perform echo cancellation */
  182. speex_echo_capture(echo_state, pcm, pcm2);
  183. for (i=0;i<FRAME_SIZE;i++)
  184. pcm[i] = pcm2[i];
  185. speex_bits_reset(&enc_bits);
  186. /* Apply noise/echo suppression */
  187. speex_preprocess_run(preprocess, pcm);
  188. /* Encode */
  189. speex_encode_int(enc_state, pcm, &enc_bits);
  190. int packetSize = speex_bits_write(&enc_bits, outpacket+8, MAX_MSG);
  191. /* Pseudo header: four null bytes and a 32-bit timestamp */
  192. ((int*)outpacket)[0] = htonl(0);
  193. ((int*)outpacket)[1] = send_timestamp;
  194. send_timestamp += FRAME_SIZE;
  195. rc = sendto(sd, outpacket, packetSize+8, 0,
  196. (struct sockaddr *) &remoteAddr,
  197. sizeof(remoteAddr));
  198. if(rc<0) {
  199. printf("cannot send audio data\n");
  200. close(sd);
  201. exit(1);
  202. }
  203. }
  204. }
  205. return 0;
  206. }