~ubuntu-branches/ubuntu/saucy/dahdi-tools/saucy

« back to all changes in this revision

Viewing changes to dahdi_monitor.c

  • Committer: Stefan Lesicnik
  • Date: 2011-05-08 12:22:46 UTC
  • mfrom: (2.1.4 sid)
  • Revision ID: stefan@lsd.co.za-20110508122246-lh6k2x1uy8pl3vdi
Tags: 1:2.4.1-1ubuntu1
* Merge from Debian. Remaining changes:
  - Bug Fix: If linux-headers are not installed, don't block, and print
    information for the user.
  - added debian/dahdi.postinst
  - added --error-handler=init_failed to debian/rules
  - Changes from Debian:
    - debian/control: Change Maintainer
    - debian/control: Removed Uploaders field.
    - debian/control: Removed Debian Vcs-Svn entry and replaced with
      ubuntu-voip Vcs-Bzr, to reflect divergence in packages.
    - debian/control: Package dahdi Depends on dahdi-dkms | dahdi-source
* debian/control: Added gawk as dependency for dkms build (LP: #493304)
* New upstream release (Closes: #581076, #582094).
* Patches hardware_rescan, perl_fix_noserial, perl_fix_transportdir,
  astribank_allow_ignoreend, init_unload_modules and wcb4xxp_extra_trunk
  dropped: merged upstream.
* dahdi-linux 2.3.0 is required (extra config options for dahdi_cfg).
* Convert to dpkg v.3 format.
* Standards version: 3.9.1.0 (No change needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
#include <fcntl.h>
37
37
#include <errno.h>
38
38
#include <ctype.h>
 
39
#include <signal.h>
39
40
 
40
41
#include <dahdi/user.h>
41
42
#include "dahdi_tools_version.h"
 
43
#include "wavformat.h"
 
44
#include "autoconfig.h"
42
45
 
43
 
#include <linux/soundcard.h>
 
46
#ifdef HAVE_SYS_SOUNDCARD_H
 
47
# include <sys/soundcard.h>
 
48
#else
 
49
# ifdef HAVE_LINUX_SOUNDCARD_H
 
50
#  include <linux/soundcard.h>
 
51
# else
 
52
#  error "Your installation appears to be missing soundcard.h which is needed to continue."
 
53
# endif
 
54
#endif
44
55
 
45
56
/*
46
57
* defines for file handle numbers
58
69
 
59
70
#define FRAG_SIZE 8
60
71
 
 
72
#define MAX_OFH 6
 
73
 
61
74
/* Put the ofh (output file handles) outside the main loop in case we ever add a
62
75
 * signal handler.
63
76
 */
64
 
static FILE *ofh[6];
 
77
static FILE *ofh[MAX_OFH];
 
78
static int run = 1;
65
79
 
66
80
static int stereo;
67
81
static int verbose;
68
82
 
 
83
/* handler to catch ctrl-c */
 
84
void cleanup_and_exit(int signal)
 
85
{
 
86
        fprintf(stderr, "cntrl-c pressed\n");
 
87
        run = 0; /* stop reading */
 
88
}
 
89
 
 
90
int filename_is_wav(char *filename)
 
91
{
 
92
        if (NULL != strstr(filename, ".wav"))
 
93
                return 1;
 
94
        return 0;
 
95
}
 
96
 
 
97
/*
 
98
 * Fill the wav header with default info
 
99
 * num_chans - 0 = mono; 1 = stereo
 
100
 */
 
101
void wavheader_init(struct wavheader *wavheader, int num_chans)
 
102
{
 
103
        memset(wavheader, 0, sizeof(struct wavheader));
 
104
 
 
105
        memcpy(&wavheader->riff_chunk_id, "RIFF", 4);
 
106
        memcpy(&wavheader->riff_type, "WAVE", 4);
 
107
 
 
108
        memcpy(&wavheader->fmt_chunk_id, "fmt ", 4);
 
109
        wavheader->fmt_data_size = 16;
 
110
        wavheader->fmt_compression_code = 1;
 
111
        wavheader->fmt_num_channels = num_chans;
 
112
        wavheader->fmt_sample_rate = 8000;
 
113
        wavheader->fmt_avg_bytes_per_sec = 16000;
 
114
        wavheader->fmt_block_align = 2;
 
115
        wavheader->fmt_significant_bps = 16;
 
116
 
 
117
        memcpy(&wavheader->data_chunk_id, "data", 4);
 
118
}
 
119
 
69
120
int audio_open(void)
70
121
{
71
122
        int fd;
280
331
        struct dahdi_confinfo zc;
281
332
        int opt;
282
333
        extern char *optarg;
 
334
        struct wavheader wavheaders[MAX_OFH]; /* we have one for each potential filehandle */
 
335
        unsigned int bytes_written[MAX_OFH] = {0};
 
336
        int file_is_wav[MAX_OFH] = {0};
 
337
        int i;
283
338
 
284
339
        if ((argc < 2) || (atoi(argv[1]) < 1)) {
285
340
                fprintf(stderr, "Usage: dahdi_monitor <channel num> [-v[v]] [-m] [-o] [-l limit] [-f FILE | -s FILE | -r FILE1 -t FILE2] [-F FILE | -S FILE | -R FILE1 -T FILE2]\n");
289
344
                fprintf(stderr, "        -l LIMIT: Stop after reading LIMIT bytes\n");
290
345
                fprintf(stderr, "        -m: Separate rx/tx streams.\n");
291
346
                fprintf(stderr, "        -o: Output audio via OSS.  Note: Only 'normal' combined rx/tx streams are output via OSS.\n");
292
 
                fprintf(stderr, "        -f FILE: Save combined rx/tx stream to FILE. Cannot be used with -m.\n");
 
347
                fprintf(stderr, "        -f FILE: Save combined rx/tx stream to mono FILE. Cannot be used with -m.\n");
293
348
                fprintf(stderr, "        -r FILE: Save rx stream to FILE. Implies -m.\n");
294
349
                fprintf(stderr, "        -t FILE: Save tx stream to FILE. Implies -m.\n");
295
350
                fprintf(stderr, "        -s FILE: Save stereo rx/tx stream to FILE. Implies -m.\n");
348
403
                                exit(EXIT_FAILURE);
349
404
                        }
350
405
                        fprintf(stderr, "Writing combined stream to %s\n", optarg);
 
406
                        file_is_wav[MON_BRX] = filename_is_wav(optarg);
 
407
                        if (file_is_wav[MON_BRX]) {
 
408
                                wavheader_init(&wavheaders[MON_BRX], 1);
 
409
                                if (fwrite(&wavheaders[MON_BRX], 1, sizeof(struct wavheader), ofh[MON_BRX]) != sizeof(struct wavheader)) {
 
410
                                        fprintf(stderr, "Could not write wav header to %s: %s\n", optarg, strerror(errno));
 
411
                                        exit(EXIT_FAILURE);
 
412
                                }
 
413
                        }
351
414
                        savefile = 1;
352
415
                        break;
353
416
                case 'F':
381
444
                                exit(EXIT_FAILURE);
382
445
                        }
383
446
                        fprintf(stderr, "Writing receive stream to %s\n", optarg);
 
447
                        file_is_wav[MON_BRX] = filename_is_wav(optarg);
 
448
                        if (file_is_wav[MON_BRX]) {
 
449
                                wavheader_init(&wavheaders[MON_BRX], 1);
 
450
                                if (fwrite(&wavheaders[MON_BRX], 1, sizeof(struct wavheader), ofh[MON_BRX]) != sizeof(struct wavheader)) {
 
451
                                        fprintf(stderr, "Could not write wav header to %s: %s\n", optarg, strerror(errno));
 
452
                                        exit(EXIT_FAILURE);
 
453
                                }
 
454
                        }
384
455
                        multichannel = 1;
385
456
                        savefile = 1;
386
457
                        break;
398
469
                                exit(EXIT_FAILURE);
399
470
                        }
400
471
                        fprintf(stderr, "Writing pre-echo receive stream to %s\n", optarg);
 
472
                        file_is_wav[MON_PRE_BRX] = filename_is_wav(optarg);
 
473
                        if (file_is_wav[MON_PRE_BRX]) {
 
474
                                wavheader_init(&wavheaders[MON_PRE_BRX], 1);
 
475
                                if (fwrite(&wavheaders[MON_PRE_BRX], 1, sizeof(struct wavheader), ofh[MON_PRE_BRX]) != sizeof(struct wavheader)) {
 
476
                                        fprintf(stderr, "Could not write wav header to %s: %s\n", optarg, strerror(errno));
 
477
                                        exit(EXIT_FAILURE);
 
478
                                }
 
479
                        }
401
480
                        preecho = 1;
402
481
                        multichannel = 1;
403
482
                        savefile = 1;
416
495
                                exit(EXIT_FAILURE);
417
496
                        }
418
497
                        fprintf(stderr, "Writing transmit stream to %s\n", optarg);
 
498
                        file_is_wav[MON_TX] = filename_is_wav(optarg);
 
499
                        if (file_is_wav[MON_TX]) {
 
500
                                wavheader_init(&wavheaders[MON_TX], 1);
 
501
                                if (fwrite(&wavheaders[MON_TX], 1, sizeof(struct wavheader), ofh[MON_TX]) != sizeof(struct wavheader)) {
 
502
                                        fprintf(stderr, "Could not write wav header to %s: %s\n", optarg, strerror(errno));
 
503
                                        exit(EXIT_FAILURE);
 
504
                                }
 
505
                        }
419
506
                        multichannel = 1;
420
507
                        savefile = 1;
421
508
                        break;
433
520
                                exit(EXIT_FAILURE);
434
521
                        }
435
522
                        fprintf(stderr, "Writing pre-echo transmit stream to %s\n", optarg);
 
523
                        file_is_wav[MON_PRE_TX] = filename_is_wav(optarg);
 
524
                        if (file_is_wav[MON_PRE_TX]) {
 
525
                                wavheader_init(&wavheaders[MON_PRE_TX], 1);
 
526
                                if (fwrite(&wavheaders[MON_PRE_TX], 1, sizeof(struct wavheader), ofh[MON_PRE_TX]) != sizeof(struct wavheader)) {
 
527
                                        fprintf(stderr, "Could not write wav header to %s: %s\n", optarg, strerror(errno));
 
528
                                        exit(EXIT_FAILURE);
 
529
                                }
 
530
                        }
436
531
                        preecho = 1;
437
532
                        multichannel = 1;
438
533
                        savefile = 1;
451
546
                                exit(EXIT_FAILURE);
452
547
                        }
453
548
                        fprintf(stderr, "Writing stereo stream to %s\n", optarg);
 
549
                        file_is_wav[MON_STEREO] = filename_is_wav(optarg);
 
550
                        if (file_is_wav[MON_STEREO]) {
 
551
                                wavheader_init(&wavheaders[MON_STEREO], 2);
 
552
                                if (fwrite(&wavheaders[MON_STEREO], 1, sizeof(struct wavheader), ofh[MON_STEREO]) != sizeof(struct wavheader)) {
 
553
                                        fprintf(stderr, "Could not write wav header to %s: %s\n", optarg, strerror(errno));
 
554
                                        exit(EXIT_FAILURE);
 
555
                                }
 
556
                        }
454
557
                        multichannel = 1;
455
558
                        savefile = 1;
 
559
                        stereo_output = 1;
456
560
                        break;
457
561
                case 'S':
458
562
                        if (!multichannel && ofh[MON_PRE_BRX]) {
468
572
                                exit(EXIT_FAILURE);
469
573
                        }
470
574
                        fprintf(stderr, "Writing pre-echo stereo stream to %s\n", optarg);
 
575
                        file_is_wav[MON_PRE_STEREO] = filename_is_wav(optarg);
 
576
                        if (file_is_wav[MON_PRE_STEREO]) {
 
577
                                wavheader_init(&wavheaders[MON_PRE_STEREO], 2);
 
578
                                if (fwrite(&wavheaders[MON_PRE_STEREO], 1, sizeof(struct wavheader), ofh[MON_PRE_STEREO]) != sizeof(struct wavheader)) {
 
579
                                        fprintf(stderr, "Could not write wav header to %s: %s\n", optarg, strerror(errno));
 
580
                                        exit(EXIT_FAILURE);
 
581
                                }
 
582
                        }
471
583
                        preecho = 1;
472
584
                        multichannel = 1;
473
585
                        savefile = 1;
 
586
                        stereo_output = 1;
474
587
                        break;
475
588
                }
476
589
        }
561
674
                        }
562
675
                }
563
676
        }
 
677
        if (signal(SIGINT, cleanup_and_exit) == SIG_ERR) {
 
678
                fprintf(stderr, "Error registering signal handler: %s\n", strerror(errno));
 
679
        }
564
680
        if (visual) {
565
681
                printf("\nVisual Audio Levels.\n");
566
682
                printf("--------------------\n");
569
685
                draw_barheader();
570
686
        }
571
687
        /* Now, copy from pseudo to audio */
572
 
        for (;;) {
 
688
        while (run) {
573
689
                res_brx = read(pfd[MON_BRX], buf_brx, sizeof(buf_brx));
574
690
                if (res_brx < 1)
575
691
                        break;
576
692
                readcount += res_brx;
577
693
                if (ofh[MON_BRX])
578
 
                        x = fwrite(buf_brx, 1, res_brx, ofh[MON_BRX]);
 
694
                        bytes_written[MON_BRX] += fwrite(buf_brx, 1, res_brx, ofh[MON_BRX]);
579
695
 
580
696
                if (multichannel) {
581
697
                        res_tx = read(pfd[MON_TX], buf_tx, res_brx);
582
698
                        if (res_tx < 1)
583
699
                                break;
584
700
                        if (ofh[MON_TX])
585
 
                                x = fwrite(buf_tx, 1, res_tx, ofh[MON_TX]);
 
701
                                bytes_written[MON_TX] += fwrite(buf_tx, 1, res_tx, ofh[MON_TX]);
586
702
 
587
703
                        if (stereo_output && ofh[MON_STEREO]) {
588
704
                                for (x = 0; x < res_tx; x++) {
589
705
                                        stereobuf[x*2] = buf_brx[x];
590
706
                                        stereobuf[x*2+1] = buf_tx[x];
591
707
                                }
592
 
                                x = fwrite(stereobuf, 1, res_tx*2, ofh[MON_STEREO]);
 
708
                                bytes_written[MON_STEREO] += fwrite(stereobuf, 1, res_tx*2, ofh[MON_STEREO]);
593
709
                        }
594
710
 
595
711
                        if (visual) {
605
721
                        if (res_brx < 1)
606
722
                                break;
607
723
                        if (ofh[MON_PRE_BRX])
608
 
                                x = fwrite(buf_brx, 1, res_brx, ofh[MON_PRE_BRX]);
 
724
                                bytes_written[MON_PRE_BRX] += fwrite(buf_brx, 1, res_brx, ofh[MON_PRE_BRX]);
609
725
 
610
726
                        if (multichannel) {
611
727
                                res_tx = read(pfd[MON_PRE_TX], buf_tx, res_brx);
612
728
                                if (res_tx < 1)
613
729
                                        break;
614
730
                                if (ofh[MON_PRE_TX])
615
 
                                        x = fwrite(buf_tx, 1, res_tx, ofh[MON_PRE_TX]);
 
731
                                        bytes_written[MON_PRE_TX] += fwrite(buf_tx, 1, res_tx, ofh[MON_PRE_TX]);
616
732
 
617
733
                                if (stereo_output && ofh[MON_PRE_STEREO]) {
618
734
                                        for (x = 0; x < res_brx; x++) {
619
735
                                                stereobuf[x*2] = buf_brx[x];
620
736
                                                stereobuf[x*2+1] = buf_tx[x];
621
737
                                        }
622
 
                                        x = fwrite(stereobuf, 1, res_brx * 2, ofh[MON_PRE_STEREO]);
 
738
                                        bytes_written[MON_PRE_STEREO] += fwrite(stereobuf, 1, res_brx * 2, ofh[MON_PRE_STEREO]);
623
739
                                }
624
740
                        }
625
741
                }
640
756
                        break;
641
757
                }
642
758
        }
643
 
        if (ofh[MON_BRX]) fclose(ofh[MON_BRX]);
644
 
        if (ofh[MON_TX]) fclose(ofh[MON_TX]);
645
 
        if (ofh[MON_PRE_BRX]) fclose(ofh[MON_PRE_BRX]);
646
 
        if (ofh[MON_PRE_TX]) fclose(ofh[MON_PRE_TX]);
647
 
        if (ofh[MON_STEREO]) fclose(ofh[MON_STEREO]);
648
 
        if (ofh[MON_PRE_STEREO]) fclose(ofh[MON_PRE_STEREO]);
649
 
        exit(0);
 
759
        /* write filesize info */
 
760
        for (i = 0; i < MAX_OFH; i++) {
 
761
                if (NULL == ofh[i])
 
762
                        continue;
 
763
                if (!(file_is_wav[i]))
 
764
                        continue;
 
765
 
 
766
                rewind(ofh[i]);
 
767
 
 
768
                if (fread(&wavheaders[i], 1, sizeof(struct wavheader), ofh[i]) != sizeof(struct wavheader)) {
 
769
                        fprintf(stderr, "Failed to read in a full wav header.  Expect bad things.\n");
 
770
                }
 
771
 
 
772
                wavheaders[i].riff_chunk_size = (bytes_written[i]) + sizeof(struct wavheader) - 8; /* filesize - 8 */
 
773
                wavheaders[i].data_data_size = bytes_written[i];
 
774
 
 
775
                rewind(ofh[i]);
 
776
                if (fwrite(&wavheaders[i], 1, sizeof(struct wavheader), ofh[i]) != sizeof(struct wavheader)) {
 
777
                        fprintf(stderr, "Failed to write out a full wav header.\n");
 
778
                }
 
779
                fclose(ofh[i]);
 
780
        }
 
781
        printf("done cleaning up ... exiting.\n");
 
782
        return 0;
650
783
}