switch_core_file.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
  3. * Copyright (C) 2005-2018, Anthony Minessale II <anthm@freeswitch.org>
  4. *
  5. * Version: MPL 1.1
  6. *
  7. * The contents of this file are subject to the Mozilla Public License Version
  8. * 1.1 (the "License"); you may not use this file except in compliance with
  9. * the License. You may obtain a copy of the License at
  10. * http://www.mozilla.org/MPL/
  11. *
  12. * Software distributed under the License is distributed on an "AS IS" basis,
  13. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  14. * for the specific language governing rights and limitations under the
  15. * License.
  16. *
  17. * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
  18. *
  19. * The Initial Developer of the Original Code is
  20. * Anthony Minessale II <anthm@freeswitch.org>
  21. * Portions created by the Initial Developer are Copyright (C)
  22. * the Initial Developer. All Rights Reserved.
  23. *
  24. * Contributor(s):
  25. * Chris Rienzo <chris@signalwire.com>
  26. * Seven Du <dujinfang@gmail.com>
  27. * Dragos Oancea <dragos@signalwire.com>
  28. *
  29. * switch_core_file.c -- tests file core functions
  30. *
  31. */
  32. #include <switch.h>
  33. #include <stdlib.h>
  34. #include <test/switch_test.h>
  35. FST_CORE_BEGIN("./conf")
  36. {
  37. FST_SUITE_BEGIN(switch_core_file)
  38. {
  39. FST_SETUP_BEGIN()
  40. {
  41. }
  42. FST_SETUP_END()
  43. FST_TEARDOWN_BEGIN()
  44. {
  45. }
  46. FST_TEARDOWN_END()
  47. FST_TEST_BEGIN(test_switch_core_file_close)
  48. {
  49. switch_file_handle_t fh = { 0 };
  50. switch_status_t status = SWITCH_STATUS_FALSE;
  51. static char filename[] = "/tmp/fs_unit_test.wav";
  52. static char hdr[] = /*simplest wav file*/
  53. "\x52\x49\x46\x46"
  54. "\x24\x00\x00\x00"
  55. "\x57\x41\x56\x45"
  56. "\x66\x6d\x74\x20"
  57. "\x10\x00\x00\x00"
  58. "\x01\x00\x01\x00"
  59. "\x44\xac\x00\x00"
  60. "\x88\x58\x01\x00"
  61. "\x02\x00\x10\x00"
  62. "\x64\x61\x74\x61"
  63. "\x00\x00";
  64. FILE *f = NULL;
  65. f = fopen(filename, "w");
  66. fst_check(f != NULL);
  67. fwrite(hdr, 1, sizeof(hdr) ,f);
  68. fclose(f);
  69. status = switch_core_file_open(&fh, filename, 1, 8000, SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT, NULL);
  70. fst_check(status == SWITCH_STATUS_SUCCESS);
  71. fst_check(switch_test_flag(&fh, SWITCH_FILE_OPEN));
  72. status = switch_core_file_pre_close(&fh);
  73. fst_check(!switch_test_flag(&fh, SWITCH_FILE_OPEN));
  74. fst_check(switch_test_flag(&fh, SWITCH_FILE_PRE_CLOSED));
  75. fst_check(status == SWITCH_STATUS_SUCCESS);
  76. status = switch_core_file_close(&fh);
  77. fst_check(status == SWITCH_STATUS_SUCCESS);
  78. fst_check(!switch_test_flag(&fh, SWITCH_FILE_PRE_CLOSED));
  79. status = switch_core_file_close(&fh);
  80. fst_check(status == SWITCH_STATUS_FALSE);
  81. unlink(filename);
  82. }
  83. FST_TEST_END()
  84. FST_TEST_BEGIN(test_switch_core_file_no_pre_close)
  85. {
  86. switch_file_handle_t fh = { 0 };
  87. switch_status_t status = SWITCH_STATUS_FALSE;
  88. static char filename[] = "/tmp/fs_unit_test.wav";
  89. static char hdr[] = /*simplest wav file*/
  90. "\x52\x49\x46\x46"
  91. "\x24\x00\x00\x00"
  92. "\x57\x41\x56\x45"
  93. "\x66\x6d\x74\x20"
  94. "\x10\x00\x00\x00"
  95. "\x01\x00\x01\x00"
  96. "\x44\xac\x00\x00"
  97. "\x88\x58\x01\x00"
  98. "\x02\x00\x10\x00"
  99. "\x64\x61\x74\x61"
  100. "\x00\x00";
  101. FILE *f = NULL;
  102. f = fopen(filename, "w");
  103. fst_check(f != NULL);
  104. fwrite(hdr, 1, sizeof(hdr) ,f);
  105. fclose(f);
  106. status = switch_core_file_open(&fh, filename, 1, 8000, SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT, NULL);
  107. fst_check(status == SWITCH_STATUS_SUCCESS);
  108. fst_check(switch_test_flag(&fh, SWITCH_FILE_OPEN));
  109. status = switch_core_file_close(&fh);
  110. fst_check(status == SWITCH_STATUS_SUCCESS);
  111. fst_check(!switch_test_flag(&fh, SWITCH_FILE_PRE_CLOSED));
  112. status = switch_core_file_close(&fh);
  113. fst_check(status == SWITCH_STATUS_FALSE);
  114. unlink(filename);
  115. }
  116. FST_TEST_END()
  117. FST_TEST_BEGIN(test_switch_core_file_write_mono)
  118. {
  119. switch_status_t status = SWITCH_STATUS_FALSE;
  120. switch_file_handle_t fhw = { 0 };
  121. static char filename[] = "/tmp/fs_write_unit_test.wav";
  122. int16_t *buf;
  123. int nr_frames = 3, i;
  124. switch_size_t len;
  125. len = 160;
  126. switch_malloc(buf, len * sizeof(int16_t));
  127. status = switch_core_file_open(&fhw, filename, 1, 8000, SWITCH_FILE_FLAG_WRITE | SWITCH_FILE_DATA_SHORT, NULL);
  128. fst_check(status == SWITCH_STATUS_SUCCESS);
  129. for (i = 0; i < nr_frames; i++) {
  130. memset(buf, i, len * sizeof(int16_t));
  131. switch_core_file_write(&fhw, buf, &len);
  132. }
  133. status = switch_core_file_close(&fhw);
  134. fst_check(status == SWITCH_STATUS_SUCCESS);
  135. switch_safe_free(buf);
  136. unlink(filename);
  137. }
  138. FST_TEST_END()
  139. FST_TEST_BEGIN(test_switch_core_file_write_mono_to_stereo)
  140. {
  141. switch_status_t status = SWITCH_STATUS_FALSE;
  142. switch_file_handle_t fhw = { 0 };
  143. static char filename[] = "/tmp/fs_write_unit_test.wav";
  144. int16_t *buf;
  145. int nr_frames = 3, i, want_channels = 2;
  146. switch_size_t len;
  147. len = 160;
  148. switch_malloc(buf, len * sizeof(int16_t));
  149. status = switch_core_file_open(&fhw, filename, want_channels, 8000, SWITCH_FILE_FLAG_WRITE | SWITCH_FILE_DATA_SHORT, NULL);
  150. fst_check(status == SWITCH_STATUS_SUCCESS);
  151. fhw.real_channels = 1;
  152. for (i = 0; i < nr_frames; i++) {
  153. memset(buf, i, len * sizeof(int16_t));
  154. status = switch_core_file_write(&fhw, buf, &len);
  155. fst_requires(status == SWITCH_STATUS_SUCCESS);
  156. }
  157. status = switch_core_file_close(&fhw);
  158. fst_check(status == SWITCH_STATUS_SUCCESS);
  159. switch_safe_free(buf);
  160. unlink(filename);
  161. }
  162. FST_TEST_END()
  163. FST_TEST_BEGIN(test_switch_core_file_open_max_audio_channels)
  164. {
  165. switch_file_handle_t fh = { 0 };
  166. switch_status_t status = SWITCH_STATUS_FALSE;
  167. static char filename[] = "/tmp/fs_unit_test.wav";
  168. static char hdr[] = /*simplest wav file, hardcoded 8 channels*/
  169. "\x52\x49\x46\x46"
  170. "\x24\x00\x00\x00"
  171. "\x57\x41\x56\x45"
  172. "\x66\x6d\x74\x20"
  173. "\x10\x00\x00\x00"
  174. "\x01\x00\x08\x00"
  175. "\x44\xac\x00\x00"
  176. "\x88\x58\x01\x00"
  177. "\x02\x00\x10\x00"
  178. "\x64\x61\x74\x61"
  179. "\x00\x00";
  180. FILE *f = NULL;
  181. f = fopen(filename, "w");
  182. fst_check(f != NULL);
  183. fwrite(hdr, 1, sizeof(hdr) ,f);
  184. fclose(f);
  185. switch_core_max_audio_channels(6);
  186. status = switch_core_file_open(&fh, filename, 1, 8000, SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT, NULL);
  187. fst_check(status == SWITCH_STATUS_FALSE);
  188. switch_core_max_audio_channels(8);
  189. status = switch_core_file_open(&fh, filename, 1, 8000, SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT, NULL);
  190. fst_check(status == SWITCH_STATUS_SUCCESS);
  191. status = switch_core_file_close(&fh);
  192. fst_check(status == SWITCH_STATUS_SUCCESS);
  193. unlink(filename);
  194. }
  195. FST_TEST_END()
  196. }
  197. FST_SUITE_END()
  198. }
  199. FST_CORE_END()