resampleplay.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /**
  20. * \page page_pjmedia_samples_resampleplay_c Samples: Using Resample Port
  21. *
  22. * This example demonstrates how to use @ref PJMEDIA_RESAMPLE_PORT to
  23. * change the sampling rate of the media streams.
  24. *
  25. * This file is pjsip-apps/src/samples/resampleplay.c
  26. *
  27. * \includelineno resampleplay.c
  28. */
  29. #include <pjmedia.h>
  30. #include <pjlib-util.h>
  31. #include <pjlib.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include "util.h"
  35. /* For logging purpose. */
  36. #define THIS_FILE "resampleplay.c"
  37. static const char *desc =
  38. " FILE \n"
  39. " \n"
  40. " resampleplay.c \n"
  41. " \n"
  42. " PURPOSE \n"
  43. " \n"
  44. " Demonstrate how use resample port to play a WAV file to sound \n"
  45. " device using different sampling rate. \n"
  46. " \n"
  47. " USAGE \n"
  48. " \n"
  49. " resampleplay [options] FILE.WAV \n"
  50. " \n"
  51. " where options: \n"
  52. SND_USAGE
  53. " \n"
  54. " The WAV file could have mono or stereo channels with arbitrary \n"
  55. " sampling rate, but MUST contain uncompressed (i.e. 16bit) PCM. \n";
  56. int main(int argc, char *argv[])
  57. {
  58. pj_caching_pool cp;
  59. pjmedia_endpt *med_endpt;
  60. pj_pool_t *pool;
  61. pjmedia_port *file_port;
  62. pjmedia_port *resample_port;
  63. pjmedia_snd_port *snd_port;
  64. char tmp[10];
  65. pj_status_t status;
  66. int dev_id = -1;
  67. int sampling_rate = CLOCK_RATE;
  68. int channel_count = NCHANNELS;
  69. int samples_per_frame = NSAMPLES;
  70. int bits_per_sample = NBITS;
  71. //int ptime;
  72. //int down_samples;
  73. /* Must init PJLIB first: */
  74. status = pj_init();
  75. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  76. /* Get options */
  77. if (get_snd_options(THIS_FILE, argc, argv, &dev_id, &sampling_rate,
  78. &channel_count, &samples_per_frame, &bits_per_sample))
  79. {
  80. puts("");
  81. puts(desc);
  82. return 1;
  83. }
  84. if (!argv[pj_optind]) {
  85. puts("Error: no file is specified");
  86. puts(desc);
  87. return 1;
  88. }
  89. /* Must create a pool factory before we can allocate any memory. */
  90. pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
  91. /*
  92. * Initialize media endpoint.
  93. * This will implicitly initialize PJMEDIA too.
  94. */
  95. status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
  96. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  97. /* Create memory pool for our file player */
  98. pool = pj_pool_create( &cp.factory, /* pool factory */
  99. "app", /* pool name. */
  100. 4000, /* init size */
  101. 4000, /* increment size */
  102. NULL /* callback on error */
  103. );
  104. /* Create the file port. */
  105. status = pjmedia_wav_player_port_create( pool, argv[pj_optind], 0, 0,
  106. 0, &file_port);
  107. if (status != PJ_SUCCESS) {
  108. app_perror(THIS_FILE, "Unable to open file", status);
  109. return 1;
  110. }
  111. /* File must have same number of channels. */
  112. if (PJMEDIA_PIA_CCNT(&file_port->info) != (unsigned)channel_count) {
  113. PJ_LOG(3,(THIS_FILE, "Error: file has different number of channels. "
  114. "Perhaps you'd need -c option?"));
  115. pjmedia_port_destroy(file_port);
  116. return 1;
  117. }
  118. /* Calculate number of samples per frame to be taken from file port */
  119. //ptime = samples_per_frame * 1000 / sampling_rate;
  120. /* Create the resample port. */
  121. status = pjmedia_resample_port_create( pool, file_port,
  122. sampling_rate, 0,
  123. &resample_port);
  124. if (status != PJ_SUCCESS) {
  125. app_perror(THIS_FILE, "Unable to create resample port", status);
  126. return 1;
  127. }
  128. /* Create sound player port. */
  129. status = pjmedia_snd_port_create(
  130. pool, /* pool */
  131. dev_id, /* device */
  132. dev_id, /* device */
  133. sampling_rate, /* clock rate. */
  134. channel_count, /* # of channels. */
  135. samples_per_frame, /* samples per frame. */
  136. bits_per_sample, /* bits per sample. */
  137. 0, /* options */
  138. &snd_port /* returned port */
  139. );
  140. if (status != PJ_SUCCESS) {
  141. app_perror(THIS_FILE, "Unable to open sound device", status);
  142. return 1;
  143. }
  144. /* Connect resample port to sound device */
  145. status = pjmedia_snd_port_connect( snd_port, resample_port);
  146. if (status != PJ_SUCCESS) {
  147. app_perror(THIS_FILE, "Error connecting sound ports", status);
  148. return 1;
  149. }
  150. /* Dump memory usage */
  151. dump_pool_usage(THIS_FILE, &cp);
  152. /*
  153. * File should be playing and looping now, using sound device's thread.
  154. */
  155. /* Sleep to allow log messages to flush */
  156. pj_thread_sleep(100);
  157. printf("Playing %s at sampling rate %d (original file sampling rate=%d)\n",
  158. argv[pj_optind], sampling_rate,
  159. PJMEDIA_PIA_SRATE(&file_port->info));
  160. puts("");
  161. puts("Press <ENTER> to stop playing and quit");
  162. if (fgets(tmp, sizeof(tmp), stdin) == NULL) {
  163. puts("EOF while reading stdin, will quit now..");
  164. }
  165. /* Start deinitialization: */
  166. /* Destroy sound device */
  167. status = pjmedia_snd_port_destroy( snd_port );
  168. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  169. /* Destroy resample port.
  170. * This will destroy all downstream ports (e.g. the file port)
  171. */
  172. status = pjmedia_port_destroy( resample_port );
  173. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  174. /* Release application pool */
  175. pj_pool_release( pool );
  176. /* Destroy media endpoint. */
  177. pjmedia_endpt_destroy( med_endpt );
  178. /* Destroy pool factory */
  179. pj_caching_pool_destroy( &cp );
  180. /* Shutdown PJLIB */
  181. pj_shutdown();
  182. /* Done. */
  183. return 0;
  184. }