Пример #1
0
static void set_t30_callbacks(t30_state_t *t30, int chan)
{
    t30_set_phase_b_handler(t30, phase_b_handler, (void *) (intptr_t) chan);
    t30_set_phase_d_handler(t30, phase_d_handler, (void *) (intptr_t) chan);
    t30_set_phase_e_handler(t30, phase_e_handler, (void *) (intptr_t) chan);
    t30_set_real_time_frame_handler(t30, real_time_frame_handler, (void *) (intptr_t) chan);
    t30_set_document_handler(t30, document_handler, (void *) (intptr_t) chan);
}
Пример #2
0
int main(int argc, char *argv[])
{
    SNDFILE *wave_handle;
    SNDFILE *input_wave_handle;
    int i;
    int j;
    int k;
    struct machine_s *mc;
    int outframes;
    char buf[128 + 1];
    int16_t silence[SAMPLES_PER_CHUNK];
    int16_t out_amp[2*SAMPLES_PER_CHUNK];
    int alldone;
    const char *input_tiff_file_name;
    const char *input_audio_file_name;
    int log_audio;
    int use_ecm;
    int use_tep;
    int use_transmit_on_idle;
#if defined(WITH_SPANDSP_INTERNALS)
    int use_line_hits;
#endif
    int polled_mode;
    int reverse_flow;
    int use_page_limits;
    int supported_modems;
    int signal_level;
    int noise_level;
    float signal_scaling;
    time_t start_time;
    time_t end_time;
    int scan_line_time;
    char *page_header_info;
    int opt;
    t30_state_t *t30;
    logging_state_t *logging;

    log_audio = false;
    input_tiff_file_name = INPUT_TIFF_FILE_NAME;
    input_audio_file_name = NULL;
    use_ecm = false;
#if defined(WITH_SPANDSP_INTERNALS)
    use_line_hits = false;
#endif
    use_tep = false;
    polled_mode = false;
    page_header_info = NULL;
    reverse_flow = false;
    use_transmit_on_idle = true;
    use_receiver_not_ready = false;
    use_page_limits = false;
    signal_level = 0;
    noise_level = -99;
    scan_line_time = 0;
    supported_modems = T30_SUPPORT_V27TER | T30_SUPPORT_V29 | T30_SUPPORT_V17;
    while ((opt = getopt(argc, argv, "ehH:i:I:lm:n:prRs:S:tTw:")) != -1)
    {
        switch (opt)
        {
        case 'e':
            use_ecm = true;
            break;
#if defined(WITH_SPANDSP_INTERNALS)
        case 'h':
            use_line_hits = true;
            break;
#endif
        case 'H':
            page_header_info = optarg;
            break;
        case 'i':
            input_tiff_file_name = optarg;
            break;
        case 'I':
            input_audio_file_name = optarg;
            break;
        case 'l':
            log_audio = true;
            break;
        case 'm':
            supported_modems = atoi(optarg);
            break;
        case 'n':
            noise_level = atoi(optarg);
            break;
        case 'p':
            polled_mode = true;
            break;
        case 'r':
            reverse_flow = true;
            break;
        case 'R':
            use_receiver_not_ready = true;
            break;
        case 's':
            signal_level = atoi(optarg);
            break;
        case 'S':
            scan_line_time = atoi(optarg);
            break;
        case 't':
            use_tep = true;
            break;
        case 'T':
            use_page_limits = true;
            break;
        case 'w':
            t30_state_to_wreck = atoi(optarg);
            break;
        default:
            //usage();
            exit(2);
            break;
        }
    }

    input_wave_handle = NULL;
    if (input_audio_file_name)
    {
        if ((input_wave_handle = sf_open_telephony_read(input_audio_file_name, 1)) == NULL)
        {
            fprintf(stderr, "    Cannot open audio file '%s'\n", input_audio_file_name);
            exit(2);
        }
    }

    wave_handle = NULL;
    if (log_audio)
    {
        if ((wave_handle = sf_open_telephony_write(OUTPUT_FILE_NAME_WAVE, 2)) == NULL)
        {
            fprintf(stderr, "    Cannot create audio file '%s'\n", OUTPUT_FILE_NAME_WAVE);
            exit(2);
        }
    }

    memset(silence, 0, sizeof(silence));
    for (j = 0;  j < FAX_MACHINES;  j++)
    {
        machines[j].chan = j;
        mc = &machines[j];

        i = mc->chan + 1;
        sprintf(buf, "%d%d%d%d%d%d%d%d", i, i, i, i, i, i, i, i);
        if (reverse_flow)
            mc->fax = fax_init(NULL, (mc->chan & 1)  ?  true  :  false);
        else
            mc->fax = fax_init(NULL, (mc->chan & 1)  ?  false  :  true);
        mc->awgn = NULL;
        signal_scaling = 1.0f;
        if (noise_level > -99)
        {
            mc->awgn = awgn_init_dbm0(NULL, 1234567, noise_level);
            signal_scaling = powf(10.0f, signal_level/20.0f);
            printf("Signal scaling %f\n", signal_scaling);
        }
        fax_set_transmit_on_idle(mc->fax, use_transmit_on_idle);
        fax_set_tep_mode(mc->fax, use_tep);
        t30 = fax_get_t30_state(mc->fax);
        t30_set_tx_ident(t30, buf);
        t30_set_tx_sub_address(t30, "Sub-address");
        t30_set_tx_sender_ident(t30, "Sender ID");
        t30_set_tx_password(t30, "Password");
        t30_set_tx_polled_sub_address(t30, "Polled sub-address");
        t30_set_tx_selective_polling_address(t30, "Selective polling address");
        t30_set_tx_page_header_info(t30, page_header_info);
        t30_set_tx_nsf(t30, (const uint8_t *) "\x50\x00\x00\x00Spandsp\x00", 12);
        t30_set_ecm_capability(t30, use_ecm);
        t30_set_supported_t30_features(t30,
                                       T30_SUPPORT_IDENTIFICATION
                                     | T30_SUPPORT_SELECTIVE_POLLING
                                     | T30_SUPPORT_SUB_ADDRESSING);

        if ((mc->chan & 1))
            t30_set_minimum_scan_line_time(t30, scan_line_time);
        t30_set_supported_image_sizes(t30,
                                      T30_SUPPORT_US_LETTER_LENGTH
                                    | T30_SUPPORT_US_LEGAL_LENGTH
                                    | T30_SUPPORT_UNLIMITED_LENGTH
                                    | T30_SUPPORT_215MM_WIDTH
                                    | T30_SUPPORT_255MM_WIDTH
                                    | T30_SUPPORT_303MM_WIDTH);
        t30_set_supported_resolutions(t30,
                                      T30_SUPPORT_STANDARD_RESOLUTION
                                    | T30_SUPPORT_FINE_RESOLUTION
                                    | T30_SUPPORT_SUPERFINE_RESOLUTION
                                    | T30_SUPPORT_R8_RESOLUTION
                                    | T30_SUPPORT_R16_RESOLUTION
                                    | T30_SUPPORT_300_300_RESOLUTION
                                    | T30_SUPPORT_400_400_RESOLUTION
                                    | T30_SUPPORT_600_600_RESOLUTION
                                    | T30_SUPPORT_1200_1200_RESOLUTION
                                    | T30_SUPPORT_300_600_RESOLUTION
                                    | T30_SUPPORT_400_800_RESOLUTION
                                    | T30_SUPPORT_600_1200_RESOLUTION);
        t30_set_supported_modems(t30, supported_modems);
        if (use_ecm)
#if defined(SPANDSP_SUPPORT_T85)
            t30_set_supported_compressions(t30, T30_SUPPORT_T4_1D_COMPRESSION | T30_SUPPORT_T4_2D_COMPRESSION | T30_SUPPORT_T6_COMPRESSION | T30_SUPPORT_T85_COMPRESSION);
#else
            t30_set_supported_compressions(t30, T30_SUPPORT_T4_1D_COMPRESSION | T30_SUPPORT_T4_2D_COMPRESSION | T30_SUPPORT_T6_COMPRESSION);
#endif
        if ((mc->chan & 1))
        {
            if (polled_mode)
            {
                if (use_page_limits)
                    t30_set_tx_file(t30, input_tiff_file_name, 3, 6);
                else
                    t30_set_tx_file(t30, input_tiff_file_name, -1, -1);
            }
            else
            {
                sprintf(buf, "fax_tests_%d.tif", (mc->chan + 1)/2);
                t30_set_rx_file(t30, buf, -1);
                t30_set_rx_encoding(t30, T4_COMPRESSION_ITU_T6);
            }
        }
        else
        {
            if (polled_mode)
            {
                sprintf(buf, "fax_tests_%d.tif", (mc->chan + 1)/2);
                t30_set_rx_file(t30, buf, -1);
                t30_set_rx_encoding(t30, T4_COMPRESSION_ITU_T6);
            }
            else
            {
                if (use_page_limits)
                    t30_set_tx_file(t30, input_tiff_file_name, 3, 6);
                else
                    t30_set_tx_file(t30, input_tiff_file_name, -1, -1);
            }
        }
        t30_set_phase_b_handler(t30, phase_b_handler, (void *) (intptr_t) mc->chan + 'A');
        t30_set_phase_d_handler(t30, phase_d_handler, (void *) (intptr_t) mc->chan + 'A');
        t30_set_phase_e_handler(t30, phase_e_handler, (void *) (intptr_t) mc->chan + 'A');
        t30_set_real_time_frame_handler(t30, real_time_frame_handler, (void *) (intptr_t) mc->chan + 'A');
        t30_set_document_handler(t30, document_handler, (void *) (intptr_t) mc->chan + 'A');
        sprintf(mc->tag, "FAX-%d", j + 1);

        logging = t30_get_logging_state(t30);
        span_log_set_level(logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME | SPAN_LOG_FLOW);
        span_log_set_tag(logging, mc->tag);
        if ((j & 1))
        {
            span_log_set_level(&t30->t4.rx.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME | SPAN_LOG_FLOW);
            span_log_set_tag(&t30->t4.rx.logging, mc->tag);
        }
        else
        {
            span_log_set_level(&t30->t4.tx.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME | SPAN_LOG_FLOW);
            span_log_set_tag(&t30->t4.tx.logging, mc->tag);
        }
        logging = fax_get_logging_state(mc->fax);
        span_log_set_level(logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME | SPAN_LOG_FLOW);
        span_log_set_tag(logging, mc->tag);

        memset(mc->amp, 0, sizeof(mc->amp));
        mc->total_audio_time = 0;
        mc->done = false;
    }
    time(&start_time);
    for (;;)
    {
        alldone = true;
        for (j = 0;  j < FAX_MACHINES;  j++)
        {
            mc = &machines[j];

            if ((j & 1) == 0  &&  input_audio_file_name)
            {
                mc->len = sf_readf_short(input_wave_handle, mc->amp, SAMPLES_PER_CHUNK);
                if (mc->len == 0)
                    break;
            }
            else
            {
                mc->len = fax_tx(mc->fax, mc->amp, SAMPLES_PER_CHUNK);
                if (mc->awgn)
                {
                    for (k = 0;  k < mc->len;  k++)
                        mc->amp[k] = ((int16_t) (mc->amp[k]*signal_scaling)) + awgn(mc->awgn);
                }
            }
            mc->total_audio_time += SAMPLES_PER_CHUNK;
            if (!use_transmit_on_idle)
            {
                /* The receive side always expects a full block of samples, but the
                   transmit side may not be sending any when it doesn't need to. We
                   may need to pad with some silence. */
                if (mc->len < SAMPLES_PER_CHUNK)
                {
                    memset(mc->amp + mc->len, 0, sizeof(int16_t)*(SAMPLES_PER_CHUNK - mc->len));
                    mc->len = SAMPLES_PER_CHUNK;
                }
            }
            t30 = fax_get_t30_state(mc->fax);
            logging = t30_get_logging_state(t30);
            span_log_bump_samples(logging, mc->len);
            logging = fax_get_logging_state(mc->fax);
            span_log_bump_samples(logging, mc->len);

            if (log_audio)
            {
                for (k = 0;  k < mc->len;  k++)
                    out_amp[2*k + j] = mc->amp[k];
            }
            if (machines[j ^ 1].len < SAMPLES_PER_CHUNK)
                memset(machines[j ^ 1].amp + machines[j ^ 1].len, 0, sizeof(int16_t)*(SAMPLES_PER_CHUNK - machines[j ^ 1].len));
            t30 = fax_get_t30_state(mc->fax);
#if defined(WITH_SPANDSP_INTERNALS)
            if (use_line_hits)
            {
                /* TODO: This applies very crude line hits. improve it */
                if (t30->state == 22)
                {
                    if (++mc->error_delay == 100)
                    {
                        fprintf(stderr, "HIT %d!\n", j);
                        mc->error_delay = 0;
                        for (k = 0;  k < 5;  k++)
                            mc->amp[k] = 0;
                    }
                }
            }
            if (t30->state == t30_state_to_wreck)
                memset(machines[j ^ 1].amp, 0, sizeof(int16_t)*SAMPLES_PER_CHUNK);
#endif
            if (fax_rx(mc->fax, machines[j ^ 1].amp, SAMPLES_PER_CHUNK))
                break;
            if (!mc->done)
                alldone = false;
        }

        if (log_audio)
        {
            outframes = sf_writef_short(wave_handle, out_amp, SAMPLES_PER_CHUNK);
            if (outframes != SAMPLES_PER_CHUNK)
                break;
        }

        if (alldone  ||  j < FAX_MACHINES)
            break;
    }
    time(&end_time);
    for (j = 0;  j < FAX_MACHINES;  j++)
    {
        mc = &machines[j];
        fax_release(mc->fax);
    }
    if (log_audio)
    {
        if (sf_close(wave_handle))
        {
            fprintf(stderr, "    Cannot close audio file '%s'\n", OUTPUT_FILE_NAME_WAVE);
            exit(2);
        }
    }
    if (input_audio_file_name)
    {
        if (sf_close(input_wave_handle))
        {
            fprintf(stderr, "    Cannot close audio file '%s'\n", input_audio_file_name);
            exit(2);
        }
    }
    printf("Total audio time = %ds (wall time %ds)\n", machines[0].total_audio_time/8000, (int) (end_time - start_time));
    return 0;
}
Пример #3
0
void client_run(int client_socket, char *local_ip, int local_port, char *remote_ip, int remote_port)
{
    char sendbuf[RCVBUFSIZE], recvbuf[RCVBUFSIZE], infobuf[RCVBUFSIZE];
	struct sockaddr_in addr = {0}, sendaddr = {0};
    int read_bytes;
	int usock;
	int reuse_addr = 1;
    fax_state_t fax;
	char tmp[512], fn[512], *file_name = "/tmp/test.tiff";
	int send_fax = FALSE;
	int g711 = 0;
	int pcmu = 0;

	snprintf(sendbuf, sizeof(sendbuf), "connect\n\n");
	send(client_socket, sendbuf, strlen(sendbuf), 0);

    if ((read_bytes = recv(client_socket, infobuf, sizeof(infobuf), 0)) < 0) {
        die("recv() failed");
	}

#if SOCKET2ME_DEBUG
	printf("READ [%s]\n", infobuf);
#endif

	if (cheezy_get_var(infobuf, "Channel-Read-Codec-Name", tmp, sizeof(tmp))) {
		if (!strcasecmp(tmp, "pcmu")) {
			g711 = 1;
			pcmu = 1;
		} else if (!strcasecmp(tmp, "pcma")) {
			g711 = 1;
		}
	}


	snprintf(sendbuf, sizeof(sendbuf), "sendmsg\n"
			 "call-command: unicast\n"
			 "local-ip: %s\n"
			 "local-port: %d\n"
			 "remote-ip: %s\n"
			 "remote-port: %d\n"
			 "transport: udp\n"
			 "%s"
			 "\n",
			 local_ip, local_port,
			 remote_ip, remote_port,
			 g711 ? "flags: native\n" : ""
			 );

	
	if (cheezy_get_var(infobuf, "variable_fax_file_name", fn, sizeof(fn))) {
		file_name = fn;
	}

	if (cheezy_get_var(infobuf, "variable_fax_mode", tmp, sizeof(tmp))) {
		if (!strcasecmp(tmp, "send")) {
			send_fax = TRUE;
		}
	}

	if (cheezy_get_var(infobuf, "variable_fax_preexec", tmp, sizeof(tmp))) {
	  set_vars(infobuf);
	  system(tmp);
	}

#if SOCKET2ME_DEBUG
	printf("SEND: [%s]\n", sendbuf);
#endif
	send(client_socket, sendbuf, strlen(sendbuf), 0);

	memset(recvbuf, 0, sizeof(recvbuf));
    if ((read_bytes = recv(client_socket, recvbuf, sizeof(recvbuf), 0)) < 0) {
        die("recv() failed");
	}
#if SOCKET2ME_DEBUG
	printf("READ [%s]\n", recvbuf);
#endif
	
	if ((usock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
        die("socket() failed");
	}

	setsockopt(usock, SOL_SOCKET, SO_REUSEADDR, &reuse_addr, sizeof(reuse_addr));

    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    /*addr.sin_addr.s_addr = inet_addr(remote_ip);*/
    addr.sin_port = htons(remote_port);

    sendaddr.sin_family = AF_INET;
    sendaddr.sin_addr.s_addr = inet_addr(local_ip);
    sendaddr.sin_port = htons(local_port);
	
    if (bind(usock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
        die("bind() failed");
	}

	printf("%s Fax filename: [%s] from %s:%d -> %s:%d\n", send_fax ? "Sending" : "Receiving", file_name, local_ip, local_port, remote_ip, remote_port);
	
    fax_init(&fax, send_fax);
    t30_set_local_ident(&fax.t30_state, "Socket 2 ME");
    t30_set_header_info(&fax.t30_state, "Socket 2 ME");
	if (send_fax) {
		t30_set_tx_file(&fax.t30_state, file_name, -1, -1);
	} else {
		t30_set_rx_file(&fax.t30_state, file_name, -1);
	}
    t30_set_phase_b_handler(&fax.t30_state, phase_b_handler, NULL);
    t30_set_phase_d_handler(&fax.t30_state, phase_d_handler, NULL);
    t30_set_phase_e_handler(&fax.t30_state, phase_e_handler, NULL);
    t30_set_document_handler(&fax.t30_state, document_handler, NULL);

    t30_set_ecm_capability(&fax.t30_state, TRUE);
    t30_set_supported_compressions(&fax.t30_state, T30_SUPPORT_T4_1D_COMPRESSION | T30_SUPPORT_T4_2D_COMPRESSION | T30_SUPPORT_T6_COMPRESSION);

    t30_set_supported_image_sizes(&fax.t30_state, T30_SUPPORT_US_LETTER_LENGTH | T30_SUPPORT_US_LEGAL_LENGTH | T30_SUPPORT_UNLIMITED_LENGTH
                                  | T30_SUPPORT_215MM_WIDTH | T30_SUPPORT_255MM_WIDTH | T30_SUPPORT_303MM_WIDTH);
    t30_set_supported_resolutions(&fax.t30_state, T30_SUPPORT_STANDARD_RESOLUTION | T30_SUPPORT_FINE_RESOLUTION | T30_SUPPORT_SUPERFINE_RESOLUTION
                                  | T30_SUPPORT_R8_RESOLUTION | T30_SUPPORT_R16_RESOLUTION);

	for (;;) {
		struct sockaddr_in local_addr = {0};
        size_t cliAddrLen = sizeof(local_addr);
		unsigned char audiobuf[1024], rawbuf[1024], outbuf[1024];
		short *usebuf = NULL;
		int tx, tx_bytes, bigger, sample_count;
		fd_set ready;

		FD_ZERO(&ready);

		FD_SET(usock, &ready);
		FD_SET(client_socket, &ready);
		
		bigger = usock > client_socket ? usock : client_socket;
		select(++bigger, &ready, NULL, NULL, NULL);
		
		if (FD_ISSET(client_socket, &ready)) {
			memset(recvbuf, 0, sizeof(recvbuf));
			if ((read_bytes = recv(client_socket, recvbuf, sizeof(recvbuf), 0)) < 0) {
				die("recv() failed");
			}

			if (read_bytes == 0) {
				break;
			}
#if SOCKET2ME_DEBUG
			printf("READ [%s]\n", recvbuf);
#endif
		}

		if (!FD_ISSET(usock, &ready)) {
			continue;
		}

        if ((read_bytes = recvfrom(usock, audiobuf, sizeof(audiobuf), 0, (struct sockaddr *) &local_addr, &cliAddrLen)) < 0) {
			die("recvfrom() failed");
		}

		if (g711) {
			int i;
			short *rp = (short *) rawbuf;
			
			for (i = 0; i < read_bytes; i++) {
				if (pcmu) {
					rp[i] = ulaw_to_linear(audiobuf[i]);
				} else {
					rp[i] = alaw_to_linear(audiobuf[i]);
				}
			}
			usebuf = rp;
			sample_count = read_bytes;
		} else {
			usebuf = (short *) audiobuf;
			sample_count = read_bytes / 2;
		}
		
		fax_rx(&fax, usebuf, sample_count);
#if SOCKET2ME_DEBUG
		printf("Handling client %s:%d %d bytes\n", inet_ntoa(local_addr.sin_addr), ntohs(local_addr.sin_port), read_bytes);
#endif

		
		if ((tx = fax_tx(&fax, (short *)outbuf, sample_count)) < 0) {
            printf("Fax Error\n");
			break;
        } else if (!tx) {
			continue;
		}
		

		if (g711) {
			int i;
			short *bp = (short *) outbuf;
			for (i = 0; i < tx; i++) {
				if (pcmu) {
					rawbuf[i] = linear_to_ulaw(bp[i]);
				} else {
					rawbuf[i] = linear_to_alaw(bp[i]);
				}
			}
			usebuf = (short *) rawbuf;
			tx_bytes = tx;
		} else {
			usebuf = (short *)outbuf;
			tx_bytes = tx * 2;
		}	
		

		cliAddrLen = sizeof(sendaddr);
        if (sendto(usock, usebuf, tx_bytes, 0, (struct sockaddr *) &sendaddr, sizeof(sendaddr)) != tx_bytes) {
			die("sendto() sent a different number of bytes than expected");
		}
	}

	close(client_socket);
	close(usock);
		
    t30_terminate(&fax.t30_state);
    fax_release(&fax);

    if (cheezy_get_var(infobuf, "variable_fax_postexec", tmp, sizeof(tmp))) {
      set_vars(infobuf);
      system(tmp);
    }
	printf("Done\n");
	snprintf(sendbuf, sizeof(sendbuf), "hangup\n\n");
	send(client_socket, sendbuf, strlen(sendbuf), 0);
	
}