--- linphone-1.0.0pre5.orig/mediastreamer/jackcard.c 2004-11-04 11:55:18.000000000 +0100 +++ linphone-1.0.0pre5/mediastreamer/jackcard.c 2004-12-01 06:41:32.000000000 +0100 @@ -34,8 +34,6 @@ #define WRITEBUFFERSIZE 524288 #define BSIZE 512 -//#define DEBUG - /** * This is the shutdown callback for this JACK application. * It is called by JACK if the server ever shuts down or @@ -47,19 +45,14 @@ { JackCard* obj = (JackCard*) arg; -#ifdef DEBUG - g_message("jack_shutdown"); -#endif - obj->jack_running = FALSE; obj->jack_active = FALSE; - if (obj->read.open) obj->read.init = TRUE; - if (obj->write.open) obj->write.init = TRUE; - // obj->read.can_process = FALSE; - // obj->write.can_process = FALSE; - // obj->can_process = FALSE; - // g_free(obj->read.phys_ports); - // g_free(obj->write.phys_ports); + obj->read.port = NULL; + if (obj->read.open) + obj->read.init = TRUE; + obj->write.port = NULL; + if (obj->write.open) + obj->write.init = TRUE; } int samplerate(jack_nframes_t rate, void *arg) @@ -67,10 +60,6 @@ JackCard* obj = (JackCard*) arg; int error; -#ifdef DEBUG - g_message("samplerate : %d", rate); -#endif - obj->rate = rate; if (obj->read.open) { obj->read.data.src_ratio = (double)obj->read.rate / (double)obj->rate; @@ -105,37 +94,32 @@ JackCard* obj = (JackCard*) arg; sample_t *out; sample_t *in; - size_t bytes; -#ifdef DEBUG - size_t bytes2; -#endif - + + if (obj->clear && !obj->write.can_process) { + out = (sample_t *) jack_port_get_buffer (obj->write.port, nframes); + memset (out, 0, nframes * sizeof(sample_t)); + obj->clear = FALSE; + } + if (!obj->can_process) return 0; if(obj->read.can_process) { in = (sample_t *) jack_port_get_buffer (obj->read.port, nframes); - bytes = jack_ringbuffer_write (obj->read.buffer, (void *) in, sizeof(sample_t) * nframes); - obj->read.real_total_bytes += bytes; -#ifdef DEBUG - if (bytes != sizeof(sample_t) * nframes) - g_warning("not all read in process: %d %d", bytes, sizeof(sample_t) * nframes); -#endif + jack_ringbuffer_write (obj->read.buffer, (void *) in, sizeof(sample_t) * nframes); } if (obj->write.can_process) { out = (sample_t *) jack_port_get_buffer (obj->write.port, nframes); memset (out, 0, nframes * sizeof(sample_t)); -#ifdef DEBUG - if ((bytes2 = jack_ringbuffer_read_space(obj->write.buffer))write.buffer, (void *) out, sizeof(sample_t) * nframes); - obj->write.real_total_bytes += bytes; -#ifdef DEBUG - if (bytes != MIN(bytes2,nframes*sizeof(sample_t))) - g_warning("not all written in process: %d %d", bytes, MIN(bytes2,nframes*sizeof(sample_t))); -#endif + if (obj->clear && jack_ringbuffer_read_space(obj->write.buffer) == 0) { + obj->write.can_process = FALSE; + if (!obj->read.open) + obj->can_process = FALSE; + obj->clear = FALSE; + return 0; + } + jack_ringbuffer_read (obj->write.buffer, (void *) out, sizeof(sample_t) * nframes); } return 0; } @@ -186,63 +170,58 @@ } if (obj->read.init) { - if ((obj->read.port = jack_port_register (obj->client, "input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0))==NULL) { + if (!obj->read.port && (obj->read.port = jack_port_register (obj->client, "input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0))==NULL) { g_warning("error while trying to register input port"); return -1; } - if (obj->read.phys_ports) { - g_free(obj->read.phys_ports); - obj->read.phys_ports = NULL; - } - if ((obj->read.phys_ports = jack_get_ports (obj->client, NULL, NULL, JackPortIsPhysical|JackPortIsOutput)) == NULL) { + if (!obj->read.phys_ports && (obj->read.phys_ports = jack_get_ports (obj->client, NULL, NULL, JackPortIsPhysical|JackPortIsOutput)) == NULL) { g_warning("Cannot find any physical capture ports\n"); jack_port_unregister(obj->client, obj->read.port); + obj->read.port = NULL; return -1; } -#ifdef DEBUG - else g_message("found port %s", obj->read.phys_ports[0]); -#endif - if ((error = jack_connect (obj->client, obj->read.phys_ports[0], jack_port_name (obj->read.port))) != 0) { - g_warning("cannot connect input ports: %s -> %s\n", jack_port_name (obj->read.port), obj->read.phys_ports[0]); - if (error == EEXIST) g_warning("connection already made"); - else { - jack_port_unregister(obj->client, obj->read.port); - return -1; + if (!jack_port_connected(obj->read.port)) + if ((error = jack_connect (obj->client, obj->read.phys_ports[0], jack_port_name (obj->read.port))) != 0) { + g_warning("cannot connect input ports: %s -> %s\n", jack_port_name (obj->read.port), obj->read.phys_ports[0]); + if (error == EEXIST) g_warning("connection already made"); + else { + jack_port_unregister(obj->client, obj->read.port); + obj->read.port = NULL; + return -1; + } } - } obj->read.init = FALSE; } + if (obj->write.init) { - if ((obj->write.port = jack_port_register (obj->client, "output", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0))==NULL) { + if (!obj->write.port && (obj->write.port = jack_port_register (obj->client, "output", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0))==NULL) { g_warning("error while trying to register output port"); return -1; } - if (obj->write.phys_ports) { - g_free(obj->write.phys_ports); - obj->write.phys_ports = NULL; - } - if ((obj->write.phys_ports = jack_get_ports (obj->client, NULL, NULL, JackPortIsPhysical|JackPortIsInput)) == NULL) { + if (!obj->write.phys_ports && (obj->write.phys_ports = jack_get_ports (obj->client, NULL, NULL, JackPortIsPhysical|JackPortIsInput)) == NULL) { g_warning("Cannot find any physical playback ports\n"); jack_port_unregister(obj->client, obj->write.port); + obj->write.port = NULL; return -1; } -#ifdef DEBUG - else g_message("found port %s and %s", obj->write.phys_ports[0], obj->write.phys_ports[1]); -#endif - if ((error = jack_connect (obj->client, jack_port_name (obj->write.port), obj->write.phys_ports[0])) != 0) { - g_warning("cannot connect output ports: %s -> %s\n", jack_port_name (obj->write.port), obj->write.phys_ports[0]); - if (error == EEXIST) g_warning("connection already made"); - else { - jack_port_unregister(obj->client, obj->write.port); - return -1; + if (!jack_port_connected(obj->write.port)) { + if ((error = jack_connect (obj->client, jack_port_name (obj->write.port), obj->write.phys_ports[0])) != 0) { + g_warning("cannot connect output ports: %s -> %s\n", jack_port_name (obj->write.port), obj->write.phys_ports[0]); + if (error == EEXIST) g_warning("connection already made"); + else { + jack_port_unregister(obj->client, obj->write.port); + obj->write.port = NULL; + return -1; + } } - } - if ((error = jack_connect (obj->client, jack_port_name (obj->write.port), obj->write.phys_ports[1])) != 0) { - g_warning("cannot connect output ports: %s -> %s\n", jack_port_name (obj->write.port), obj->write.phys_ports[1]); - if (error == EEXIST) g_warning("connection already made"); - else { - jack_port_unregister(obj->client, obj->write.port); - return -1; + if ((error = jack_connect (obj->client, jack_port_name (obj->write.port), obj->write.phys_ports[1])) != 0) { + g_warning("cannot connect output ports: %s -> %s\n", jack_port_name (obj->write.port), obj->write.phys_ports[1]); + if (error == EEXIST) g_warning("connection already made"); + else { + jack_port_unregister(obj->client, obj->write.port); + obj->write.port = NULL; + return -1; + } } } obj->write.init = FALSE; @@ -253,13 +232,7 @@ int jack_card_open_r(JackCard *obj,int bits,int stereo,int rate) { int channels = stereo + 1, bsize, error; - -#ifdef DEBUG - g_message("jack_card_open_r"); -#endif - obj->read.init = TRUE; - if (jack_init(obj) != 0) return -1; obj->read.rate = rate; @@ -267,8 +240,6 @@ obj->frame_size = channels * obj->sample_size; bsize = BSIZE; obj->read.frames = bsize / 2; - obj->read.total_bytes = 0; - obj->read.real_total_bytes = 0; SND_CARD(obj)->bsize = bsize; SND_CARD(obj)->flags |= SND_CARD_FLAGS_OPENED; obj->read.channels = channels; @@ -279,7 +250,8 @@ obj->read.data.data_in = malloc(obj->read.data.input_frames*sizeof(float)); obj->read.data.data_out = malloc(obj->read.frames*sizeof(float)); obj->read.data.end_of_input = 0; - obj->read.buffer = jack_ringbuffer_create(READBUFFERSIZE); + if (!obj->read.buffer) + obj->read.buffer = jack_ringbuffer_create(READBUFFERSIZE); obj->read.can_process = TRUE; obj->can_process = TRUE; obj->read.open = TRUE; @@ -290,13 +262,7 @@ int jack_card_open_w(JackCard *obj,int bits,int stereo,int rate) { int channels = stereo + 1, bsize, err; - -#ifdef DEBUG - g_message("jack_card_open_w"); -#endif - obj->write.init = TRUE; - if (jack_init(obj) != 0) return -1; obj->write.rate = rate; @@ -304,8 +270,6 @@ obj->frame_size = channels * obj->sample_size; bsize = BSIZE; obj->write.frames = bsize / 2; - obj->write.total_bytes = 0; - obj->write.real_total_bytes = 0; SND_CARD(obj)->bsize = bsize; SND_CARD(obj)->flags |= SND_CARD_FLAGS_OPENED; obj->write.channels = channels; @@ -316,8 +280,8 @@ obj->write.data.end_of_input = 0; obj->write.data.output_frames = (long)((double)obj->write.frames*obj->write.data.src_ratio); obj->write.data.data_out = malloc(obj->write.data.output_frames*sizeof(float)); - obj->write.buffer = jack_ringbuffer_create(WRITEBUFFERSIZE); - obj->write.chan = 0; + if (!obj->write.buffer) + obj->write.buffer = jack_ringbuffer_create(WRITEBUFFERSIZE); obj->write.can_process = TRUE; obj->can_process = TRUE; obj->write.open = TRUE; @@ -331,79 +295,34 @@ void jack_card_close_r(JackCard *obj) { -#ifdef DEBUG - g_message("jack_card_close_r"); -#endif obj->read.open = FALSE; obj->read.init = FALSE; obj->read.can_process = FALSE; - if (obj->jack_running) { - jack_disconnect(obj->client, obj->read.phys_ports[0], jack_port_name(obj->read.port)); - g_free(obj->read.phys_ports); - obj->read.phys_ports = NULL; - jack_port_unregister(obj->client, obj->read.port); - if ((obj->jack_active)&&(!obj->write.open)) { - obj->can_process = FALSE; - if (jack_deactivate(obj->client)) - g_warning("jack_card_close_r: Error while deactivating JACK"); - obj->jack_active = FALSE; - } - } + if (!obj->write.open) + obj->can_process = FALSE; if (obj->read.src_state) obj->read.src_state = src_delete (obj->read.src_state); g_free(obj->read.data.data_in); g_free(obj->read.data.data_out); - jack_ringbuffer_free(obj->read.buffer); - obj->read.buffer=NULL; } void jack_card_close_w(JackCard *obj) { -#ifdef DEBUG - g_message("jack_card_close_w"); -#endif obj->write.open = FALSE; obj->write.init = FALSE; - if (obj->jack_running) { - // hopefully there weren't that much dropped frames otherwise this will - // probably lead to an infinite loop. But if we do not wait until jack - // has played back everything, we won't hear everything. - while (obj->write.real_total_bytes + 1024 < obj->write.total_bytes) g_usleep(1000); + obj->clear = TRUE; + if (!obj->jack_running) { obj->write.can_process = FALSE; - jack_disconnect (obj->client, jack_port_name (obj->write.port), obj->write.phys_ports[0]); - jack_disconnect (obj->client, jack_port_name (obj->write.port), obj->write.phys_ports[1]); - g_free(obj->write.phys_ports); - obj->write.phys_ports = NULL; - jack_port_unregister(obj->client, obj->write.port); - if ((obj->jack_active)&&(!obj->read.open)) { - obj->can_process = FALSE; - if (jack_deactivate(obj->client)) - g_warning("jack_card_close_w: Error while deactivating JACK"); - obj->jack_active = FALSE; - } + obj->can_process = FALSE; } if (obj->write.src_state) obj->write.src_state = src_delete (obj->write.src_state); g_free(obj->write.data.data_in); g_free(obj->write.data.data_out); - jack_ringbuffer_free(obj->write.buffer); - obj->write.buffer=NULL; } int jack_card_probe(JackCard *obj,int bits,int stereo,int rate) { - // int ret; - -#ifdef DEBUG - g_message("jack_card_probe"); -#endif - /* - ret = jack_card_open_w(obj,bits,stereo,rate); - if (ret<0) return -1; - ret = SND_CARD(obj)->bsize; - jack_card_close_w(obj); - return ret; - */ if (obj->jack_running) return BSIZE; else if (jack_init(obj) == 0) return BSIZE; else return -1; @@ -411,22 +330,28 @@ void jack_card_destroy(JackCard *obj) { -#ifdef DEBUG - g_message("jack_card_destroy"); -#endif - if (obj->jack_running) jack_client_close (obj->client); snd_card_uninit(SND_CARD(obj)); - if (obj->read.buffer!=0) jack_ringbuffer_free(obj->read.buffer); - if (obj->write.buffer!=0) jack_ringbuffer_free(obj->write.buffer); + if (obj->read.buffer) { + jack_ringbuffer_free(obj->read.buffer); + obj->read.buffer = NULL; + } + if (obj->write.buffer) { + jack_ringbuffer_free(obj->write.buffer); + obj->write.buffer = NULL; + } + if (obj->read.phys_ports) { + g_free(obj->read.phys_ports); + obj->read.phys_ports = NULL; + } + if (obj->write.phys_ports) { + g_free(obj->write.phys_ports); + obj->write.phys_ports = NULL; + } } gboolean jack_card_can_read(JackCard *obj) { -#ifdef DEBUG - // g_message("jack_card_can_read"); -#endif - g_return_val_if_fail(obj->read.buffer!=NULL,0); if (jack_ringbuffer_read_space(obj->read.buffer)>=(long)((double)obj->read.frames/obj->read.data.src_ratio)*sizeof(sample_t)) return TRUE; else return FALSE; @@ -434,15 +359,11 @@ int jack_card_read(JackCard *obj,char *buf,int size) { -#ifdef DEBUG - // g_message("jack_card_read"); -#endif size_t bytes, can_read, i; int error; float norm, value; g_return_val_if_fail((obj->read.buffer!=NULL)&&(obj->read.src_state!=NULL),-1); - // if (!obj->jack_running) return -1; if (jack_init(obj) != 0) return -1; size /= 2; can_read = MIN(size, obj->read.frames); @@ -451,21 +372,11 @@ obj->read.can_process = FALSE; bytes = jack_ringbuffer_read (obj->read.buffer, (void *)obj->read.data.data_in, can_read); obj->read.can_process = TRUE; -#ifdef DEBUG - if (bytes != can_read) - g_warning("not read everything by ringbuffer, bytes: %d, in: %d", bytes, can_read); -#endif obj->read.data.input_frames = bytes / sizeof(sample_t); can_read = MIN(size, obj->read.frames); obj->read.data.output_frames = can_read; if ((error = src_process(obj->read.src_state, &(obj->read.data))) != 0) g_warning("error while samplerate conversion. error: %s", src_strerror(error)); -#ifdef DEBUG - if (bytes != obj->read.data.input_frames_used* sizeof(sample_t)) - g_warning("not read everything by rate conversion, bytes: %d, used: %d", bytes, obj->read.data.input_frames_used*sizeof(sample_t)); - if (can_read != obj->read.data.output_frames_gen) - g_warning("not read everything by rate conversion, can_read: %d, gen: %d", can_read, obj->read.data.output_frames_gen); -#endif norm = obj->read.level*obj->level*(float)0x8000; for (i=0; i < obj->read.data.output_frames_gen; i++) { value = obj->read.data.data_out[i]*norm; @@ -476,14 +387,8 @@ else ((short*)buf)[i] = (short)value; } -#ifdef DEBUG - if (can_read != size) - g_warning("not read everything by format conversion, can_read: %d, size: %d", can_read, size); -#endif bytes = obj->read.data.output_frames_gen * 2; - obj->read.total_bytes += bytes; return bytes; - // return 0; } int jack_card_write(JackCard *obj,char *buf,int size) @@ -492,49 +397,25 @@ int error; float norm; -#ifdef DEBUG - // g_message("jack_card_write, size: %d, written bytes : %d", size, obj->write.total_bytes); -#endif - g_return_val_if_fail((obj->write.buffer!=NULL)&&(obj->write.src_state!=NULL),-1); - // if (!obj->jack_running) return -1; if (jack_init(obj) != 0) return -1; size /= 2; - // if (obj->write.chan == 0) { - can_write = MIN(size, obj->write.frames); - norm = obj->write.level*obj->level/(float)0x8000; - for (i=0; iwrite.data.data_in[i] = (float)((short*)buf)[i]*norm; - } -#ifdef DEBUG - if (can_write != size) - g_warning("not written everything by format conversion can_write: %d, size: %d", can_write, size); -#endif - obj->write.data.input_frames = can_write; - if ((error = src_process(obj->write.src_state, &(obj->write.data))) != 0) - g_warning("error while samplerate conversion. error: %s", src_strerror(error)); - obj->write.can_process = FALSE; - bytes = jack_ringbuffer_write (obj->write.buffer, (void *) obj->write.data.data_out, sizeof(sample_t)*obj->write.data.output_frames_gen); - obj->write.can_process = TRUE; - obj->write.total_bytes += bytes; -#ifdef DEBUG - if (bytes != obj->write.data.output_frames_gen*sizeof(float)) - g_warning("not written everything by ringbuffer, bytes: %d, gen: %d", bytes, obj->write.data.output_frames_gen*sizeof(float)); - if (can_write != obj->write.data.input_frames_used) - g_warning("not written everything by rate conversion, bytes: %d, gen: %d", can_write, obj->write.data.input_frames_used); -#endif - // } -// obj->write.chan++; -// obj->write.chan %= obj->write.channels; + can_write = MIN(size, obj->write.frames); + norm = obj->write.level*obj->level/(float)0x8000; + for (i=0; iwrite.data.data_in[i] = (float)((short*)buf)[i]*norm; + } + obj->write.data.input_frames = can_write; + if ((error = src_process(obj->write.src_state, &(obj->write.data))) != 0) + g_warning("error while samplerate conversion. error: %s", src_strerror(error)); + obj->write.can_process = FALSE; + bytes = jack_ringbuffer_write (obj->write.buffer, (void *) obj->write.data.data_out, sizeof(sample_t)*obj->write.data.output_frames_gen); + obj->write.can_process = TRUE; return bytes; } void jack_card_set_level(JackCard *obj,gint way,gint a) { -#ifdef DEBUG - // g_message("jack_card_set_level"); -#endif - switch(way){ case SND_CARD_LEVEL_GENERAL: obj->level = (float)a / 100.0; @@ -554,10 +435,6 @@ { gint value = 0; -#ifdef DEBUG - // g_message("jack_card_get_level"); -#endif - switch(way){ case SND_CARD_LEVEL_GENERAL: value = (gint)(obj->level*100.0); @@ -596,10 +473,6 @@ JackCard * obj; SndCard *base; -#ifdef DEBUG - g_message("jack_card_new"); -#endif - obj= g_new0(JackCard,1); if (!client) return NULL; @@ -607,14 +480,19 @@ obj->jack_running = TRUE; obj->jack_active = FALSE; obj->can_process = FALSE; + obj->clear = TRUE; obj->write.can_process = FALSE; obj->write.open = FALSE; - obj->write.init = FALSE; + obj->write.init = TRUE; + obj->write.port = NULL; obj->write.phys_ports = NULL; + obj->write.buffer = NULL; obj->read.can_process = FALSE; obj->read.open = FALSE; - obj->read.init = FALSE; + obj->read.init = TRUE; + obj->read.port = NULL; obj->read.phys_ports = NULL; + obj->read.buffer = NULL; /* tell the JACK server to call `process()' whenever there is work to be done. @@ -631,12 +509,9 @@ obj->rate = jack_get_sample_rate (client); obj->buffer_size = jack_get_buffer_size(obj->client); - /* - if ((obj->write.port = jack_port_register (obj->client, "output", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0))==NULL) - g_warning("error while trying to register output port"); - if ((obj->read.port = jack_port_register (obj->client, "input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0))==NULL) - g_warning("error while trying to register input port"); - */ + + jack_init(obj); + base= SND_CARD(obj); snd_card_init(base); @@ -679,9 +554,6 @@ jack_client_t *client = NULL; char* client_name; -#ifdef DEBUG - g_message("jack_card_manager_init"); -#endif client_name=g_strdup_printf("linphone-%u", g_random_int()); if ((client = jack_client_new (client_name))!= NULL) { --- linphone-1.0.0pre5.orig/mediastreamer/jackcard.h 2004-10-18 12:19:48.000000000 +0200 +++ linphone-1.0.0pre5/mediastreamer/jackcard.h 2004-12-01 06:45:59.000000000 +0100 @@ -42,11 +42,8 @@ const char **phys_ports; float level; jack_ringbuffer_t *buffer; - size_t total_bytes; - size_t real_total_bytes; gint channels; gint rate; - gint chan; SRC_STATE* src_state; SRC_DATA data; size_t frames; @@ -68,6 +65,7 @@ gint frame_size; gint rate; gboolean can_process; + gboolean clear; jackcard_mode_t read, write; };