format_g723.c 8.9 KB

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