summaryrefslogtreecommitdiff
path: root/client/x11/record.cpp
blob: 017a94dbfe6795d8923bd028e320e2709d2824db (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
/*
   Copyright (C) 2009 Red Hat, Inc.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "record.h"
#include "utils.h"
#include "debug.h"


class WaveRecorder::EventTrigger: public EventSources::File {
public:
    EventTrigger(WaveRecorder& recorder, int fd);
    virtual void on_event();
    virtual int get_fd() { return _fd;}

private:
    WaveRecorder& _recorder;
    int _fd;
};

WaveRecorder::EventTrigger::EventTrigger(WaveRecorder& recorder, int fd)
    : _recorder (recorder)
    , _fd (fd)
{
}

void WaveRecorder::EventTrigger::on_event()
{
    _recorder.on_event();
}

WaveRecorder::WaveRecorder(Platform::RecordClient& client,
                           uint32_t sampels_per_sec,
                           uint32_t bits_per_sample,
                           uint32_t channels)
    : _client (client)
    , _pcm (NULL)
    , _hw_params (NULL)
    , _sw_params (NULL)
    , _sample_bytes (bits_per_sample * channels / 8)
    , _frame (new uint8_t[_sample_bytes * WaveRecordAbstract::FRAME_SIZE])
    , _frame_pos (_frame)
    , _frame_end (_frame + _sample_bytes * WaveRecordAbstract::FRAME_SIZE)
    , _event_trigger (NULL)
{
    if (!init(sampels_per_sec, bits_per_sample, channels)) {
        cleanup();
        THROW("failed");
    }
}

WaveRecorder::~WaveRecorder()
{
    cleanup();
    delete[] _frame;
}

void WaveRecorder::cleanup()
{
    if (_event_trigger) {
        _client.remove_event_source(*_event_trigger);
        delete _event_trigger;
    }

    if (_sw_params) {
        snd_pcm_sw_params_free(_sw_params);
    }

    if (_hw_params) {
        snd_pcm_hw_params_free(_hw_params);
    }

    if (_pcm) {
        snd_pcm_close(_pcm);
    }
}

bool WaveRecorder::init(uint32_t sampels_per_sec,
                        uint32_t bits_per_sample,
                        uint32_t channels)
{
    const int frame_size = WaveRecordAbstract::FRAME_SIZE;
    const char* pcm_device = "hw:0,0"; // "default" ???
    snd_pcm_format_t format;
    int err;

    switch (bits_per_sample) {
    case 8:
        format = SND_PCM_FORMAT_S8;
        break;
    case 16:
        format = SND_PCM_FORMAT_S16_LE;
        break;
    default:
        return false;
    }

    if ((err = snd_pcm_open(&_pcm, pcm_device, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK)) < 0) {
        LOG_ERROR("cannot open audio record device %s %s", pcm_device, snd_strerror(err));
        return false;
    }

    if ((err = snd_pcm_hw_params_malloc(&_hw_params)) < 0) {
        LOG_ERROR("cannot allocate hardware parameter structure %s", snd_strerror(err));
        return false;
    }

    if ((err = snd_pcm_sw_params_malloc(&_sw_params)) < 0) {
        LOG_ERROR("cannot allocate software parameter structure %s", snd_strerror(err));
        return false;
    }

    if ((err = snd_pcm_hw_params_any(_pcm, _hw_params)) < 0) {
        LOG_ERROR("cannot initialize hardware parameter structure %s", snd_strerror(err));
        return false;
    }

    if ((err = snd_pcm_hw_params_set_rate_resample(_pcm, _hw_params, 1)) < 0) {
        LOG_ERROR("cannot set rate resample %s", snd_strerror(err));
        return false;
    }

    if ((err = snd_pcm_hw_params_set_access(_pcm, _hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
        LOG_ERROR("cannot set access type %s", snd_strerror(err));
        return false;
    }

    if ((err = snd_pcm_hw_params_set_rate(_pcm, _hw_params, sampels_per_sec, 0)) < 0) {
        LOG_ERROR("cannot set sample rate %s", snd_strerror(err));
        return false;
    }

    if ((err = snd_pcm_hw_params_set_channels(_pcm, _hw_params, channels)) < 0) {
        LOG_ERROR("cannot set channel count %s", snd_strerror(err));
        return false;
    }

    if ((err = snd_pcm_hw_params_set_format(_pcm, _hw_params, format)) < 0) {
        LOG_ERROR("cannot set sample format %s", snd_strerror(err));
        return false;
    }

    int direction = 0;
    snd_pcm_uframes_t buffer_size = (sampels_per_sec * 160 / 1000) / frame_size * frame_size;

    if ((err = snd_pcm_hw_params_set_buffer_size_near(_pcm, _hw_params, &buffer_size)) < 0) {
        LOG_ERROR("cannot set buffer size %s", snd_strerror(err));
        return false;
    }

    snd_pcm_uframes_t period_size = frame_size;
    if ((err = snd_pcm_hw_params_set_period_size_near(_pcm, _hw_params, &period_size,
                                                      &direction)) < 0) {
        LOG_ERROR("cannot set period size near %s", snd_strerror(err));
        return false;
    }

    if ((err = snd_pcm_hw_params(_pcm, _hw_params)) < 0) {
        LOG_ERROR("cannot set parameters %s", snd_strerror(err));
        return false;
    }

    if ((err = snd_pcm_sw_params_current(_pcm, _sw_params)) < 0) {
        LOG_ERROR("cannot get current sw parameters %s", snd_strerror(err));
        return false;
    }

    if ((err = snd_pcm_sw_params_set_start_threshold(_pcm, _sw_params, frame_size)) < 0) {
        LOG_ERROR("cannot set start threshold %s", snd_strerror(err));
        return false;
    }

    if ((err = snd_pcm_sw_params(_pcm, _sw_params)) < 0) {
        LOG_ERROR("cannot set sw parameters %s", snd_strerror(err));
        return false;
    }

    struct pollfd pfd;
    if ((err = snd_pcm_poll_descriptors(_pcm, &pfd, 1)) < 0) {
        LOG_ERROR("cannot get poll ID %s", snd_strerror(err));
        return false;
    }
    _event_trigger = new WaveRecorder::EventTrigger(*this, pfd.fd);
    _client.add_event_source(*_event_trigger);
    return true;
}

void WaveRecorder::start()
{
    _frame_pos = _frame;
    snd_pcm_prepare(_pcm);
    snd_pcm_start(_pcm);
    snd_pcm_nonblock(_pcm, 1);
}

void WaveRecorder::stop()
{
    snd_pcm_drop(_pcm);
    snd_pcm_prepare(_pcm);
}

bool WaveRecorder::abort()
{
    return true;
}

void WaveRecorder::on_event()
{
    for (;;) {
        snd_pcm_sframes_t size = (_frame_end - _frame_pos) / _sample_bytes;
        size = snd_pcm_readi(_pcm, _frame_pos, size);
        if (size < 0) {
            if (snd_pcm_recover(_pcm, size, 1) == 0) {
                continue;
            }
            return;
        }
        _frame_pos += size * _sample_bytes;
        if (_frame_pos == _frame_end) {
            _client.push_frame(_frame);
            _frame_pos = _frame;
        }
    }
}