summaryrefslogtreecommitdiffstats
path: root/debian/transcode/transcode-1.1.7/import/import_sunau.c
blob: 2a3b37f0d1a4a68734fbad705630234a3e70de7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
 *  import_sunau.c
 *
 *  Copyright (C) Jacob Meuser - September 2004
 *
 *  This file is part of transcode, a video stream processing tool
 *
 *  transcode is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  transcode is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#define MOD_NAME	"import_sunau.so"
#define MOD_VERSION	"v0.0.2 (2004-10-02)"
#define MOD_CODEC	"(audio) pcm"

#include "transcode.h"

static int verbose_flag = TC_QUIET;
static int capability_flag = TC_CAP_PCM;

#define MOD_PRE sunau
#include "import_def.h"

#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/audioio.h>

#include "libtc/optstr.h"


static int sunau_fd = -1;

int sunau_init(const char *, int, int, int);
int sunau_grab(size_t, char *);
int sunau_stop(void);


int sunau_init(const char *audio_device,
                    int sample_rate, int precision, int channels)
{
    audio_info_t audio_if;
    int encoding;

    if(!strcmp(audio_device, "/dev/null") || !strcmp(audio_device, "/dev/zero"))
        return(0);

    if(precision != 8 && precision != 16) {
        tc_log_warn(MOD_NAME,
            "bits/sample must be 8 or 16");
        return(1);
    }

    encoding = (precision == 8) ?
        AUDIO_ENCODING_ULINEAR : AUDIO_ENCODING_SLINEAR_LE;

    AUDIO_INITINFO(&audio_if);

    audio_if.record.precision = precision;
    audio_if.record.channels = channels;
    audio_if.record.sample_rate = sample_rate;
    audio_if.record.encoding = encoding;

    audio_if.mode = AUMODE_RECORD;

    if ((sunau_fd = open(audio_device, O_RDONLY)) < 0) {
        tc_log_perror(MOD_NAME, MOD_NAME "open audio device");
        return(1);
    }

    if (ioctl(sunau_fd, AUDIO_SETINFO, &audio_if) < 0) {
        tc_log_perror(MOD_NAME, "AUDIO_SETINFO");
        return(1);
    }

    if (ioctl(sunau_fd, AUDIO_GETINFO, &audio_if) < 0) {
        tc_log_perror(MOD_NAME, "AUDIO_GETINFO");
        return(1);
    }

    if (audio_if.record.precision != precision) {
        tc_log_warn(MOD_NAME,
            "unable to initialize sample size for %s; "
            "tried %d, got %d",
            audio_device, precision, audio_if.record.precision);
        return(1);
    }
    if (audio_if.record.channels != channels) {
        tc_log_warn(MOD_NAME,
            "unable to initialize number of channels for %s; "
            "tried %d, got %d",
            audio_device, channels, audio_if.record.channels);
        return(1);
    }
    if (audio_if.record.sample_rate != sample_rate) {
        tc_log_warn(MOD_NAME,
            "unable to initialize rate for %s; "
            "tried %d, got %d\n",
            audio_device, sample_rate, audio_if.record.sample_rate);
        return(1);
    }
    if (audio_if.record.encoding != encoding) {
        tc_log_warn(MOD_NAME,
            "unable to initialize encoding for %s; "
            "tried %d, got %d",
            audio_device, encoding, audio_if.record.encoding);
        return(1);
    }

    if (ioctl(sunau_fd, AUDIO_FLUSH) < 0) {
        tc_log_perror(MOD_NAME, "AUDIO_FLUSH");
        return(1);
    }

    return(0);
}

int sunau_grab(size_t size, char *buffer)
{
    int left;
    int offset;
    int received;

    for (left = size, offset = 0; left > 0;) {
        received = read(sunau_fd, buffer + offset, left);
        if (received == 0) {
            tc_log_warn(MOD_NAME,
                "audio grab: received == 0");
        }
        if (received < 0) {
            if(errno == EINTR) {
                received = 0;
            } else {
                tc_log_perror(MOD_NAME, MOD_NAME "audio grab");
                return(1);
            }
        }
        if (received > left) {
            tc_log_warn(MOD_NAME,
                "read returns more bytes than requested; "
                "requested: %d, returned: %d",
                left, received);
            return(1);
        }
        offset += received;
        left -= received;
    }
    return(0);
}

int sunau_stop(void)
{
    close(sunau_fd);
    sunau_fd = -1;

    if (verbose_flag & TC_STATS) {
        tc_log_warn(MOD_NAME,
            "totals: (not implemented)");
    }

    return(0);
}


/* ------------------------------------------------------------
 *
 * open stream
 *
 * ------------------------------------------------------------*/

MOD_open
{
    int ret = TC_IMPORT_OK;

    switch (param->flag) {
      case TC_VIDEO:
        tc_log_warn(MOD_NAME,
            "unsupported request (init video)\n");
        ret = TC_IMPORT_ERROR;
        break;
      case TC_AUDIO:
        if (verbose_flag & TC_DEBUG) {
            tc_log_info(MOD_NAME,
                "sunau audio grabbing\n");
        }
        if (sunau_init(vob->audio_in_file,
                      vob->a_rate, vob->a_bits, vob->a_chan)) {
            ret = TC_IMPORT_ERROR;
        }
        break;
      default:
        tc_log_warn(MOD_NAME,
            "unsupported request (init)");
        ret = TC_IMPORT_ERROR;
        break;
    }

    return(ret);
}


/* ------------------------------------------------------------
 *
 * decode  stream
 *
 * ------------------------------------------------------------*/

MOD_decode
{
    int ret = TC_IMPORT_OK;

    switch (param->flag) {
      case TC_VIDEO:
        tc_log_warn(MOD_NAME,
            "unsupported request (decode video)");
        ret = TC_IMPORT_ERROR;
        break;
      case TC_AUDIO:
        if (sunau_grab(param->size, param->buffer)) {
            tc_log_warn(MOD_NAME,
                "error in grabbing audio");
            ret = TC_IMPORT_ERROR;
        }
        break;
      default:
        tc_log_warn(MOD_NAME,
            "unsupported request (decode)");
        ret = TC_IMPORT_ERROR;
        break;
    }

    return(ret);
}

/* ------------------------------------------------------------
 *
 * close stream
 *
 * ------------------------------------------------------------*/

MOD_close
{
    int ret = TC_IMPORT_OK;

    switch (param->flag) {
      case TC_VIDEO:
        tc_log_warn(MOD_NAME,
            "unsupported request (close video)");
        ret = TC_IMPORT_ERROR;
        break;
      case TC_AUDIO:
        sunau_stop();
        break;
      default:
        tc_log_warn(MOD_NAME,
            "unsupported request (close)");
        ret = TC_IMPORT_ERROR;
        break;
    }

    return(ret);
}