format_g723.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Old-style G.723 frame/timestamp format.
  5. *
  6. * Copyright (C) 1999, Mark Spencer
  7. *
  8. * Mark Spencer <markster@linux-support.net>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <asterisk/lock.h>
  14. #include <asterisk/channel.h>
  15. #include <asterisk/file.h>
  16. #include <asterisk/logger.h>
  17. #include <asterisk/sched.h>
  18. #include <asterisk/module.h>
  19. #include <arpa/inet.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <unistd.h>
  23. #include <errno.h>
  24. #include <string.h>
  25. #include <pthread.h>
  26. #include <sys/time.h>
  27. #include "../channels/adtranvofr.h"
  28. #define G723_MAX_SIZE 1024
  29. struct ast_filestream {
  30. /* First entry MUST be reserved for the channel type */
  31. void *reserved[AST_RESERVED_POINTERS];
  32. /* This is what a filestream means to us */
  33. int fd; /* Descriptor */
  34. struct ast_filestream *next;
  35. struct ast_frame *fr; /* Frame representation of buf */
  36. struct timeval orig; /* Original frame time */
  37. char buf[G723_MAX_SIZE + AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
  38. };
  39. static ast_mutex_t g723_lock = AST_MUTEX_INITIALIZER;
  40. static int glistcnt = 0;
  41. static char *name = "g723sf";
  42. static char *desc = "G.723.1 Simple Timestamp File Format";
  43. static char *exts = "g723|g723sf";
  44. static struct ast_filestream *g723_open(int fd)
  45. {
  46. /* We don't have any header to read or anything really, but
  47. if we did, it would go here. We also might want to check
  48. and be sure it's a valid file. */
  49. struct ast_filestream *tmp;
  50. if ((tmp = malloc(sizeof(struct ast_filestream)))) {
  51. if (ast_mutex_lock(&g723_lock)) {
  52. ast_log(LOG_WARNING, "Unable to lock g723 list\n");
  53. free(tmp);
  54. return NULL;
  55. }
  56. tmp->fd = fd;
  57. tmp->fr = (struct ast_frame *)tmp->buf;
  58. tmp->fr->data = tmp->buf + sizeof(struct ast_frame);
  59. tmp->fr->frametype = AST_FRAME_VOICE;
  60. tmp->fr->subclass = AST_FORMAT_G723_1;
  61. /* datalen will vary for each frame */
  62. tmp->fr->src = name;
  63. tmp->fr->mallocd = 0;
  64. tmp->lasttimeout = -1;
  65. tmp->orig.tv_usec = 0;
  66. tmp->orig.tv_sec = 0;
  67. glistcnt++;
  68. ast_mutex_unlock(&g723_lock);
  69. ast_update_use_count();
  70. }
  71. return tmp;
  72. }
  73. static struct ast_filestream *g723_rewrite(int fd, char *comment)
  74. {
  75. /* We don't have any header to read or anything really, but
  76. if we did, it would go here. We also might want to check
  77. and be sure it's a valid file. */
  78. struct ast_filestream *tmp;
  79. if ((tmp = malloc(sizeof(struct ast_filestream)))) {
  80. if (ast_mutex_lock(&g723_lock)) {
  81. ast_log(LOG_WARNING, "Unable to lock g723 list\n");
  82. free(tmp);
  83. return NULL;
  84. }
  85. tmp->fd = fd;
  86. tmp->owner = NULL;
  87. tmp->fr = NULL;
  88. tmp->lasttimeout = -1;
  89. tmp->orig.tv_usec = 0;
  90. tmp->orig.tv_sec = 0;
  91. glistcnt++;
  92. ast_mutex_unlock(&g723_lock);
  93. ast_update_use_count();
  94. } else
  95. ast_log(LOG_WARNING, "Out of memory\n");
  96. return tmp;
  97. }
  98. static struct ast_frame *g723_read(struct ast_filestream *s)
  99. {
  100. return NULL;
  101. }
  102. static void g723_close(struct ast_filestream *s)
  103. {
  104. struct ast_filestream *tmp, *tmpl = NULL;
  105. if (ast_mutex_lock(&g723_lock)) {
  106. ast_log(LOG_WARNING, "Unable to lock g723 list\n");
  107. return;
  108. }
  109. tmp = glist;
  110. while(tmp) {
  111. if (tmp == s) {
  112. if (tmpl)
  113. tmpl->next = tmp->next;
  114. else
  115. glist = tmp->next;
  116. break;
  117. }
  118. tmpl = tmp;
  119. tmp = tmp->next;
  120. }
  121. glistcnt--;
  122. if (s->owner) {
  123. s->owner->stream = NULL;
  124. if (s->owner->streamid > -1)
  125. ast_sched_del(s->owner->sched, s->owner->streamid);
  126. s->owner->streamid = -1;
  127. }
  128. ast_mutex_unlock(&g723_lock);
  129. ast_update_use_count();
  130. if (!tmp)
  131. ast_log(LOG_WARNING, "Freeing a filestream we don't seem to own\n");
  132. close(s->fd);
  133. free(s);
  134. s = NULL;
  135. }
  136. static int ast_read_callback(void *data)
  137. {
  138. u_int16_t size;
  139. u_int32_t delay = -1;
  140. int looper = 1;
  141. int retval = 0;
  142. int res;
  143. struct ast_filestream *s = data;
  144. /* Send a frame from the file to the appropriate channel */
  145. while(looper) {
  146. if (read(s->fd, &size, 2) != 2) {
  147. /* Out of data, or the file is no longer valid. In any case
  148. go ahead and stop the stream */
  149. s->owner->streamid = -1;
  150. return 0;
  151. }
  152. /* Looks like we have a frame to read from here */
  153. size = ntohs(size);
  154. if (size > G723_MAX_SIZE - sizeof(struct ast_frame)) {
  155. ast_log(LOG_WARNING, "Size %d is invalid\n", size);
  156. /* The file is apparently no longer any good, as we
  157. shouldn't ever get frames even close to this
  158. size. */
  159. s->owner->streamid = -1;
  160. return 0;
  161. }
  162. /* Read the data into the buffer */
  163. s->fr->offset = AST_FRIENDLY_OFFSET;
  164. s->fr->datalen = size;
  165. s->fr->data = s->buf + sizeof(struct ast_frame) + AST_FRIENDLY_OFFSET;
  166. if ((res = read(s->fd, s->fr->data , size)) != size) {
  167. ast_log(LOG_WARNING, "Short read (%d of %d bytes) (%s)!\n", res, size, strerror(errno));
  168. s->owner->streamid = -1;
  169. return 0;
  170. }
  171. /* Read the delay for the next packet, and schedule again if necessary */
  172. if (read(s->fd, &delay, 4) == 4)
  173. delay = ntohl(delay);
  174. else
  175. delay = -1;
  176. #if 0
  177. /* Average out frames <= 50 ms */
  178. if (delay < 50)
  179. s->fr->timelen = 30;
  180. else
  181. s->fr->timelen = delay;
  182. #else
  183. s->fr->samples = 240;
  184. #endif
  185. /* Unless there is no delay, we're going to exit out as soon as we
  186. have processed the current frame. */
  187. if (delay > VOFR_FUDGE) {
  188. looper = 0;
  189. /* If there is a delay, lets schedule the next event */
  190. if (delay != s->lasttimeout) {
  191. /* We'll install the next timeout now. */
  192. s->owner->streamid = ast_sched_add(s->owner->sched,
  193. delay - VOFR_FUDGE,
  194. ast_read_callback, s);
  195. s->lasttimeout = delay;
  196. } else
  197. /* Just come back again at the same time */
  198. retval = -1;
  199. }
  200. /* Lastly, process the frame */
  201. if (ast_write(s->owner, s->fr)) {
  202. ast_log(LOG_WARNING, "Failed to write frame\n");
  203. s->owner->streamid = -1;
  204. return 0;
  205. }
  206. }
  207. return retval;
  208. }
  209. static int g723_apply(struct ast_channel *c, struct ast_filestream *s)
  210. {
  211. /* Select our owner for this stream, and get the ball rolling. */
  212. s->owner = c;
  213. return 0;
  214. }
  215. static int g723_play(struct ast_filestream *s)
  216. {
  217. u_int32_t delay;
  218. /* Read and ignore the first delay */
  219. if (read(s->fd, &delay, 4) != 4) {
  220. /* Empty file */
  221. return 0;
  222. }
  223. ast_read_callback(s);
  224. return 0;
  225. }
  226. static int g723_write(struct ast_filestream *fs, struct ast_frame *f)
  227. {
  228. struct timeval now;
  229. u_int32_t delay;
  230. u_int16_t size;
  231. int res;
  232. if (fs->fr) {
  233. ast_log(LOG_WARNING, "Asked to write on a read stream??\n");
  234. return -1;
  235. }
  236. if (f->frametype != AST_FRAME_VOICE) {
  237. ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
  238. return -1;
  239. }
  240. if (f->subclass != AST_FORMAT_G723_1) {
  241. ast_log(LOG_WARNING, "Asked to write non-g723 frame!\n");
  242. return -1;
  243. }
  244. if (!(fs->orig.tv_usec || fs->orig.tv_sec)) {
  245. /* First frame should have zeros for delay */
  246. delay = 0;
  247. if (gettimeofday(&fs->orig, NULL)) {
  248. ast_log(LOG_WARNING, "gettimeofday() failed?? What is this? Y2k?\n");
  249. return -1;
  250. }
  251. } else {
  252. if (gettimeofday(&now, NULL)) {
  253. ast_log(LOG_WARNING, "gettimeofday() failed?? What is this? Y2k?\n");
  254. return -1;
  255. }
  256. delay = (now.tv_sec - fs->orig.tv_sec) * 1000 + (now.tv_usec - fs->orig.tv_usec) / 1000;
  257. delay = htonl(delay);
  258. fs->orig.tv_sec = now.tv_sec;
  259. fs->orig.tv_usec = now.tv_usec;
  260. }
  261. if (f->datalen <= 0) {
  262. ast_log(LOG_WARNING, "Short frame ignored (%d bytes long?)\n", f->datalen);
  263. return 0;
  264. }
  265. if ((res = write(fs->fd, &delay, 4)) != 4) {
  266. ast_log(LOG_WARNING, "Unable to write delay: res=%d (%s)\n", res, strerror(errno));
  267. return -1;
  268. }
  269. size = htons(f->datalen);
  270. if ((res =write(fs->fd, &size, 2)) != 2) {
  271. ast_log(LOG_WARNING, "Unable to write size: res=%d (%s)\n", res, strerror(errno));
  272. return -1;
  273. }
  274. if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
  275. ast_log(LOG_WARNING, "Unable to write frame: res=%d (%s)\n", res, strerror(errno));
  276. return -1;
  277. }
  278. return 0;
  279. }
  280. static int g723_seek(struct ast_filestream *fs, long sample_offset, int whence)
  281. {
  282. return -1;
  283. }
  284. static int g723_trunc(struct ast_filestream *fs)
  285. {
  286. return -1;
  287. }
  288. static long g723_tell(struct ast_filestream *fs)
  289. {
  290. return -1;
  291. }
  292. static char *g723_getcomment(struct ast_filestream *s)
  293. {
  294. return NULL;
  295. }
  296. int load_module()
  297. {
  298. return ast_format_register(name, exts, AST_FORMAT_G723_1,
  299. g723_open,
  300. g723_rewrite,
  301. g723_apply,
  302. g723_play,
  303. g723_write,
  304. g723_seek,
  305. g723_trunc,
  306. g723_tell,
  307. g723_read,
  308. g723_close,
  309. g723_getcomment);
  310. }
  311. int unload_module()
  312. {
  313. struct ast_filestream *tmp, *tmpl;
  314. if (ast_mutex_lock(&g723_lock)) {
  315. ast_log(LOG_WARNING, "Unable to lock g723 list\n");
  316. return -1;
  317. }
  318. tmp = glist;
  319. while(tmp) {
  320. if (tmp->owner)
  321. ast_softhangup(tmp->owner, AST_SOFTHANGUP_APPUNLOAD);
  322. tmpl = tmp;
  323. tmp = tmp->next;
  324. free(tmpl);
  325. }
  326. ast_mutex_unlock(&g723_lock);
  327. return ast_format_unregister(name);
  328. }
  329. int usecount()
  330. {
  331. int res;
  332. if (ast_mutex_lock(&g723_lock)) {
  333. ast_log(LOG_WARNING, "Unable to lock g723 list\n");
  334. return -1;
  335. }
  336. res = glistcnt;
  337. ast_mutex_unlock(&g723_lock);
  338. return res;
  339. }
  340. char *description()
  341. {
  342. return desc;
  343. }
  344. char *key()
  345. {
  346. return ASTERISK_GPL_KEY;
  347. }