Revision b04df2a4 audio/wavcapture.c

b/audio/wavcapture.c
3 3
#include "audio.h"
4 4

  
5 5
typedef struct {
6
    QEMUFile *f;
6
    FILE *f;
7 7
    int bytes;
8 8
    char *path;
9 9
    int freq;
......
35 35
    uint8_t dlen[4];
36 36
    uint32_t datalen = wav->bytes;
37 37
    uint32_t rifflen = datalen + 36;
38
    Monitor *mon = cur_mon;
38 39

  
39 40
    if (wav->f) {
40 41
        le_store (rlen, rifflen, 4);
41 42
        le_store (dlen, datalen, 4);
42 43

  
43
        qemu_fseek (wav->f, 4, SEEK_SET);
44
        qemu_put_buffer (wav->f, rlen, 4);
45

  
46
        qemu_fseek (wav->f, 32, SEEK_CUR);
47
        qemu_put_buffer (wav->f, dlen, 4);
48
        qemu_fclose (wav->f);
44
        if (fseek (wav->f, 4, SEEK_SET)) {
45
            monitor_printf (mon, "wav_destroy: rlen fseek failed\nReason: %s\n",
46
                            strerror (errno));
47
            goto doclose;
48
        }
49
        if (fwrite (rlen, 4, 1, wav->f) != 1) {
50
            monitor_printf (mon, "wav_destroy: rlen fwrite failed\nReason %s\n",
51
                            strerror (errno));
52
            goto doclose;
53
        }
54
        if (fseek (wav->f, 32, SEEK_CUR)) {
55
            monitor_printf (mon, "wav_destroy: dlen fseek failed\nReason %s\n",
56
                            strerror (errno));
57
            goto doclose;
58
        }
59
        if (fwrite (dlen, 1, 4, wav->f) != 4) {
60
            monitor_printf (mon, "wav_destroy: dlen fwrite failed\nReason %s\n",
61
                            strerror (errno));
62
            goto doclose;
63
        }
64
    doclose:
65
        if (fclose (wav->f)) {
66
            fprintf (stderr, "wav_destroy: fclose failed: %s",
67
                     strerror (errno));
68
        }
49 69
    }
50 70

  
51 71
    g_free (wav->path);
......
55 75
{
56 76
    WAVState *wav = opaque;
57 77

  
58
    qemu_put_buffer (wav->f, buf, size);
78
    if (fwrite (buf, size, 1, wav->f) != 1) {
79
        monitor_printf (cur_mon, "wav_capture: fwrite error\nReason: %s",
80
                        strerror (errno));
81
    }
59 82
    wav->bytes += size;
60 83
}
61 84

  
......
71 94
    WAVState *wav = opaque;
72 95
    char *path = wav->path;
73 96

  
74
    monitor_printf(cur_mon, "Capturing audio(%d,%d,%d) to %s: %d bytes\n",
75
                   wav->freq, wav->bits, wav->nchannels,
76
                   path ? path : "<not available>", wav->bytes);
97
    monitor_printf (cur_mon, "Capturing audio(%d,%d,%d) to %s: %d bytes\n",
98
                    wav->freq, wav->bits, wav->nchannels,
99
                    path ? path : "<not available>", wav->bytes);
77 100
}
78 101

  
79 102
static struct capture_ops wav_capture_ops = {
......
98 121
    CaptureVoiceOut *cap;
99 122

  
100 123
    if (bits != 8 && bits != 16) {
101
        monitor_printf(mon, "incorrect bit count %d, must be 8 or 16\n", bits);
124
        monitor_printf (mon, "incorrect bit count %d, must be 8 or 16\n", bits);
102 125
        return -1;
103 126
    }
104 127

  
105 128
    if (nchannels != 1 && nchannels != 2) {
106
        monitor_printf(mon, "incorrect channel count %d, must be 1 or 2\n",
107
                       nchannels);
129
        monitor_printf (mon, "incorrect channel count %d, must be 1 or 2\n",
130
                        nchannels);
108 131
        return -1;
109 132
    }
110 133

  
......
130 153
    le_store (hdr + 28, freq << shift, 4);
131 154
    le_store (hdr + 32, 1 << shift, 2);
132 155

  
133
    wav->f = qemu_fopen (path, "wb");
156
    wav->f = fopen (path, "wb");
134 157
    if (!wav->f) {
135
        monitor_printf(mon, "Failed to open wave file `%s'\nReason: %s\n",
136
                       path, strerror (errno));
158
        monitor_printf (mon, "Failed to open wave file `%s'\nReason: %s\n",
159
                        path, strerror (errno));
137 160
        g_free (wav);
138 161
        return -1;
139 162
    }
......
143 166
    wav->nchannels = nchannels;
144 167
    wav->freq = freq;
145 168

  
146
    qemu_put_buffer (wav->f, hdr, sizeof (hdr));
169
    if (fwrite (hdr, sizeof (hdr), 1, wav->f) != 1) {
170
        monitor_printf (mon, "Failed to write header\nReason: %s\n",
171
                        strerror (errno));
172
        goto error_free;
173
    }
147 174

  
148 175
    cap = AUD_add_capture (&as, &ops, wav);
149 176
    if (!cap) {
150
        monitor_printf(mon, "Failed to add audio capture\n");
151
        g_free (wav->path);
152
        qemu_fclose (wav->f);
153
        g_free (wav);
154
        return -1;
177
        monitor_printf (mon, "Failed to add audio capture\n");
178
        goto error_free;
155 179
    }
156 180

  
157 181
    wav->cap = cap;
158 182
    s->opaque = wav;
159 183
    s->ops = wav_capture_ops;
160 184
    return 0;
185

  
186
error_free:
187
    g_free (wav->path);
188
    if (fclose (wav->f)) {
189
        monitor_printf (mon, "Failed to close wave file\nReason: %s\n",
190
                        strerror (errno));
191
    }
192
    g_free (wav);
193
    return -1;
161 194
}

Also available in: Unified diff