[asterisk-commits] mnicholson: branch group/res_fax r238186 - /team/group/res_fax/res/
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Wed Jan 6 14:52:35 CST 2010
Author: mnicholson
Date: Wed Jan 6 14:52:31 2010
New Revision: 238186
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=238186
Log:
Switch from usleep to the ast_timer api for timing.
Modified:
team/group/res_fax/res/res_fax_spandsp.c
Modified: team/group/res_fax/res/res_fax_spandsp.c
URL: http://svnview.digium.com/svn/asterisk/team/group/res_fax/res/res_fax_spandsp.c?view=diff&rev=238186&r1=238185&r2=238186
==============================================================================
--- team/group/res_fax/res/res_fax_spandsp.c (original)
+++ team/group/res_fax/res/res_fax_spandsp.c Wed Jan 6 14:52:31 2010
@@ -41,10 +41,11 @@
#include "asterisk/strings.h"
#include "asterisk/cli.h"
#include "asterisk/utils.h"
+#include "asterisk/timing.h"
#include "asterisk/res_fax.h"
#define SPANDSP_FAX_SAMPLES 160
-#define SPANDSP_FAX_POLL_TIME 20000 /* 20ms */
+#define SPANDSP_FAX_TIMER_RATE 8000 / SPANDSP_FAX_SAMPLES /* 50 ticks per second, 20ms, 160 samples per second */
static void *spandsp_fax_new(struct ast_fax_session *s, struct ast_fax_tech_token *token);
@@ -113,13 +114,11 @@
t30_state_t *t30_state;
t38_core_state_t *t38_core_state;
- pthread_t session_thread;
- int trigger_pipe[2];
+ struct ast_timer *timer;
AST_LIST_HEAD(frame_queue, ast_frame) read_frames;
};
static void session_destroy(struct spandsp_pvt *p);
-static void *session_thread(void *data);
static int t38_tx_packet_handler(t38_core_state_t *t38_core_state, void *data, const uint8_t *buf, int len, int count);
static void t30_phase_e_handler(t30_state_t *t30_state, void *data, int completion_code);
static void spandsp_log(int level, const char *msg);
@@ -136,54 +135,14 @@
t30_terminate(p->t30_state);
p->isdone = 1;
- if (p->session_thread != AST_PTHREADT_NULL) {
- pthread_join(p->session_thread, NULL);
- }
+ ast_timer_close(p->timer);
fax_release(&p->fax_state);
t38_terminal_release(&p->t38_state);
- if (p->trigger_pipe[0] != -1) {
- close(p->trigger_pipe[0]);
- close(p->trigger_pipe[1]);
- }
-
while ((f = AST_LIST_REMOVE_HEAD(&p->read_frames, frame_list))) {
ast_frfree(f);
}
-}
-
-static void *session_thread(void *data)
-{
- struct ast_fax_session *s = data;
- struct spandsp_pvt *p = s->tech_pvt;
- struct timeval now, last_frame;
- char byte = 0;
-
- now = ast_tvnow();
-
-#if SPANDSP_RELEASE_DATE >= 20080725
- /* spandsp 0.0.6 */
- while (t30_call_active(p->t30_state)) {
-#else
- while (!p->isdone) {
-#endif
- if (p->ist38) {
- last_frame = now;
- now = ast_tvnow();
- t38_terminal_send_timeout(&p->t38_state, ast_tvdiff_us(now, last_frame) / (1000000 / 8000));
- } else {
- write(p->trigger_pipe[1], &byte, 1);
- }
-
- usleep(SPANDSP_FAX_POLL_TIME);
- }
-
- /* trigger the read function to return NULL */
- s->state = AST_FAX_STATE_COMPLETE;
- write(p->trigger_pipe[1], &byte, 1);
-
- return NULL;
}
/*! \brief
@@ -199,7 +158,6 @@
};
struct ast_frame *f = &fax_frame;
- char byte = 0;
/* TODO: Asterisk does not provide means of resending the same packet multiple
@@ -211,11 +169,8 @@
return -1;
}
- AST_LIST_LOCK(&p->read_frames);
+ /* no need to lock, this all runs in the same thread */
AST_LIST_INSERT_TAIL(&p->read_frames, f, frame_list);
- AST_LIST_UNLOCK(&p->read_frames);
-
- write(p->trigger_pipe[1], &byte, 1);
return 0;
}
@@ -341,8 +296,6 @@
}
AST_LIST_HEAD_INIT(&p->read_frames);
- p->trigger_pipe[0] = -1;
- p->session_thread = AST_PTHREADT_NULL;
if (s->details->caps & AST_FAX_TECH_RECEIVE) {
caller_mode = 0;
@@ -353,12 +306,12 @@
goto e_free;
}
- if (pipe(p->trigger_pipe) < 0) {
- ast_log(LOG_ERROR, "Channel '%s' FAX session '%d' failed to create trigger pipe.\n", s->channame, s->id);
+ if (!(p->timer = ast_timer_open())) {
+ ast_log(LOG_ERROR, "Channel '%s' FAX session '%d' failed to create timing source.\n", s->channame, s->id);
goto e_free;
}
- s->fd = p->trigger_pipe[0];
+ s->fd = ast_timer_fd(p->timer);
if (s->details->caps & AST_FAX_TECH_T38) {
if ((s->details->caps & AST_FAX_TECH_AUDIO) == 0) {
@@ -404,7 +357,6 @@
uint8_t buffer[AST_FRIENDLY_OFFSET + SPANDSP_FAX_SAMPLES * sizeof(uint16_t)];
int16_t *buf = (int16_t *) (buffer + AST_FRIENDLY_OFFSET);
int samples;
- char byte;
struct ast_frame fax_frame = {
.frametype = AST_FRAME_VOICE,
@@ -414,19 +366,20 @@
struct ast_frame *f = &fax_frame;
+ ast_timer_ack(p->timer, 1);
+
/* XXX do we need to lock here? */
- if (s->state == AST_FAX_STATE_COMPLETE) {
- ast_log(LOG_WARNING, "FAX session '%d' is in the '%s' state.\n", s->id, ast_fax_state_to_str(s->state));
+ if (p->isdone) {
+ s->state = AST_FAX_STATE_COMPLETE;
+ ast_debug(5, "FAX session '%d' is complete.\n", s->id);
return NULL;
}
- read(p->trigger_pipe[0], &byte, 1);
-
if (p->ist38) {
- AST_LIST_LOCK(&p->read_frames);
- f = AST_LIST_REMOVE_HEAD(&p->read_frames, frame_list);
- AST_LIST_UNLOCK(&p->read_frames);
- return f;
+ t38_terminal_send_timeout(&p->t38_state, SPANDSP_FAX_SAMPLES);
+ if ((f = AST_LIST_REMOVE_HEAD(&p->read_frames, frame_list))) {
+ return f;
+ }
} else {
if ((samples = fax_tx(&p->fax_state, buf, SPANDSP_FAX_SAMPLES)) > 0) {
f->samples = samples;
@@ -435,7 +388,7 @@
}
}
- return NULL;
+ return &ast_null_frame;
}
/*! \brief Write a frame to the spandsp fax stack.
@@ -525,10 +478,9 @@
}
- /* create the session thread */
- p->isdone = 0;
- if (ast_pthread_create(&p->session_thread, NULL, session_thread, s) < 0) {
- ast_log(LOG_ERROR, "failed to create FAX thread for session: %d\n", s->id);
+ /* start the timer */
+ if (ast_timer_set_rate(p->timer, SPANDSP_FAX_TIMER_RATE)) {
+ ast_log(LOG_ERROR, "FAX session '%d' error setting rate on timing source.\n", s->id);
return -1;
}
@@ -560,11 +512,7 @@
/* prevent the phase E handler from running, this is not a real termination */
t30_set_phase_e_handler(p->t30_state, NULL, NULL);
- p->isdone = 1;
t30_terminate(p->t30_state);
- if (p->session_thread != AST_PTHREADT_NULL) {
- pthread_join(p->session_thread, NULL);
- }
s->details->option.switch_to_t38 = 1;
More information about the asterisk-commits
mailing list