summaryrefslogtreecommitdiff
path: root/src/home/sound/record.py
blob: 1ad882789dce95d7de265162e79f37a45451f706 (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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import threading
import time
import subprocess
import signal
import os
import logging

from enum import Enum, auto
from typing import Optional
from ..config import config
from ..util import find_child_processes
from .storage import RecordFile, RecordStorage


_history_item_timeout = 7200
_history_cleanup_freq = 3600


class RecordStatus(Enum):
    WAITING = auto()
    RECORDING = auto()
    FINISHED = auto()
    ERROR = auto()


class RecordHistoryItem:
    id: int
    request_time: float
    start_time: float
    stop_time: float
    relations: list[int]
    status: RecordStatus
    error: Optional[Exception]
    file: Optional[RecordFile]
    creation_time: float

    def __init__(self, id):
        self.id = id
        self.request_time = 0
        self.start_time = 0
        self.stop_time = 0
        self.relations = []
        self.status = RecordStatus.WAITING
        self.file = None
        self.error = None
        self.creation_time = time.time()

    def add_relation(self, related_id: int):
        self.relations.append(related_id)

    def mark_started(self, start_time: float):
        self.start_time = start_time
        self.status = RecordStatus.RECORDING

    def mark_finished(self, end_time: float, file: RecordFile):
        self.stop_time = end_time
        self.file = file
        self.status = RecordStatus.FINISHED

    def mark_failed(self, error: Exception):
        self.status = RecordStatus.ERROR
        self.error = error

    def as_dict(self) -> dict:
        data = {
            'id': self.id,
            'request_time': self.request_time,
            'status': self.status.value,
            'relations': self.relations,
            'start_time': self.start_time,
            'stop_time': self.stop_time,
        }
        if self.error:
            data['error'] = str(self.error)
        if self.file:
            data['file'] = self.file.__dict__()
        return data


class RecordingNotFoundError(Exception):
    pass


class RecordHistory:
    history: dict[int, RecordHistoryItem]

    def __init__(self):
        self.history = {}
        self.logger = logging.getLogger(self.__class__.__name__)

    def add(self, record_id: int):
        self.logger.debug(f'add: record_id={record_id}')

        r = RecordHistoryItem(record_id)
        r.request_time = time.time()

        self.history[record_id] = r

    def delete(self, record_id: int):
        self.logger.debug(f'delete: record_id={record_id}')
        del self.history[record_id]

    def cleanup(self):
        del_ids = []
        for rid, item in self.history.items():
            if item.creation_time < time.time()-_history_item_timeout:
                del_ids.append(rid)
        for rid in del_ids:
            self.delete(rid)

    def __getitem__(self, key):
        if key not in self.history:
            raise RecordingNotFoundError()

        return self.history[key]

    def __setitem__(self, key, value):
        raise NotImplementedError('setting history item this way is prohibited')

    def __contains__(self, key):
        return key in self.history


class Recording:
    start_time: float
    stop_time: float
    duration: int
    record_id: int
    arecord_pid: Optional[int]
    process: Optional[subprocess.Popen]

    g_record_id = 1

    def __init__(self):
        self.start_time = 0
        self.stop_time = 0
        self.duration = 0
        self.process = None
        self.arecord_pid = None
        self.record_id = Recording.next_id()
        self.logger = logging.getLogger(self.__class__.__name__)

    def is_started(self) -> bool:
        return self.start_time > 0 and self.stop_time > 0

    def is_waiting(self):
        return self.duration > 0

    def ask_for(self, duration) -> int:
        overtime = 0
        orig_duration = duration

        if self.is_started():
            already_passed = time.time() - self.start_time
            max_duration = Recorder.get_max_record_time() - already_passed
            self.logger.debug(f'ask_for({orig_duration}): recording is in progress, already passed {already_passed}s, max_duration set to {max_duration}')
        else:
            max_duration = Recorder.get_max_record_time()

        if duration > max_duration:
            overtime = duration - max_duration
            duration = max_duration

            self.logger.debug(f'ask_for({orig_duration}): requested duration ({orig_duration}) is greater than max ({max_duration}), overtime is {overtime}')

        self.duration += duration
        if self.is_started():
            til_end = self.stop_time - time.time()
            if til_end < 0:
                til_end = 0

            _prev_stop_time = self.stop_time
            _to_add = duration - til_end
            if _to_add < 0:
                _to_add = 0

            self.stop_time += _to_add
            self.logger.debug(f'ask_for({orig_duration}): adding {_to_add} to stop_time (before: {_prev_stop_time}, after: {self.stop_time})')

        return overtime

    def start(self, output: str):
        assert self.start_time == 0 and self.stop_time == 0, "already started?!"
        assert self.process is None, "self.process is not None, what the hell?"

        cur = time.time()
        self.start_time = cur
        self.stop_time = cur + self.duration

        arecord = config['arecord']['bin']
        lame = config['lame']['bin']
        b = config['lame']['bitrate']

        cmd = f'{arecord} -f S16 -r 44100 -t raw 2>/dev/null | {lame} -r -s 44.1 -b {b} -m m - {output} >/dev/null 2>/dev/null'
        self.logger.debug(f'start: running `{cmd}`')
        self.process = subprocess.Popen(cmd, shell=True, stdin=None, stdout=None, stderr=None, close_fds=True)

        sh_pid = self.process.pid
        self.logger.debug(f'start: started, pid of shell is {sh_pid}')

        arecord_pid = self.find_arecord_pid(sh_pid)
        if arecord_pid is not None:
            self.arecord_pid = arecord_pid
            self.logger.debug(f'start: pid of arecord is {arecord_pid}')

    def stop(self):
        if self.process:
            if self.arecord_pid is None:
                self.arecord_pid = self.find_arecord_pid(self.process.pid)

            if self.arecord_pid is not None:
                os.kill(self.arecord_pid, signal.SIGINT)
                timeout = config['node']['process_wait_timeout']

                self.logger.debug(f'stop: sent SIGINT to {self.arecord_pid}. now waiting up to {timeout} seconds...')
                try:
                    self.process.wait(timeout=timeout)
                except subprocess.TimeoutExpired:
                    self.logger.warning(f'stop: wait({timeout}): timeout expired, calling terminate()')
                    self.process.terminate()
            else:
                self.logger.warning('stop: pid of arecord is unknown, calling terminate()')
                self.process.terminate()

            rc = self.process.returncode
            self.logger.debug(f'stop: rc={rc}')

            self.process = None
            self.arecord_pid = 0

        self.duration = 0
        self.start_time = 0
        self.stop_time = 0

    def find_arecord_pid(self, sh_pid: int):
        try:
            children = find_child_processes(sh_pid)
        except OSError as exc:
            self.logger.warning(f'failed to find child process of {sh_pid}: ' + str(exc))
            return None

        for child in children:
            if 'arecord' in child.cmd:
                return child.pid

        return None

    @staticmethod
    def next_id() -> int:
        cur_id = Recording.g_record_id
        Recording.g_record_id += 1
        return cur_id

    def increment_id(self):
        self.record_id = Recording.next_id()


class Recorder:
    interrupted: bool
    lock: threading.Lock
    history_lock: threading.Lock
    recording: Optional[Recording]
    overtime: int
    history: RecordHistory
    next_history_cleanup_time: float
    storage: RecordStorage

    def __init__(self, storage: RecordStorage):
        self.storage = storage
        self.recording = Recording()
        self.interrupted = False
        self.lock = threading.Lock()
        self.history_lock = threading.Lock()
        self.overtime = 0
        self.history = RecordHistory()
        self.next_history_cleanup_time = 0
        self.logger = logging.getLogger(self.__class__.__name__)

    def start_thread(self):
        t = threading.Thread(target=self.loop)
        t.daemon = True
        t.start()

    def loop(self) -> None:
        tempname = os.path.join(self.storage.root, 'temp.mp3')

        while not self.interrupted:
            cur = time.time()
            stopped = False
            cur_record_id = None

            if self.next_history_cleanup_time == 0:
                self.next_history_cleanup_time = time.time() + _history_cleanup_freq
            elif self.next_history_cleanup_time <= time.time():
                self.logger.debug('loop: calling history.cleanup()')
                try:
                    self.history.cleanup()
                except Exception as e:
                    self.logger.error('loop: error while history.cleanup(): ' + str(e))
                self.next_history_cleanup_time = time.time() + _history_cleanup_freq

            with self.lock:
                cur_record_id = self.recording.record_id
                # self.logger.debug(f'cur_record_id={cur_record_id}')

                if not self.recording.is_started():
                    if self.recording.is_waiting():
                        try:
                            if os.path.exists(tempname):
                                self.logger.warning(f'loop: going to start new recording, but {tempname} still exists, unlinking..')
                                try:
                                    os.unlink(tempname)
                                except OSError as e:
                                    self.logger.exception(e)
                            self.recording.start(tempname)
                            with self.history_lock:
                                self.history[cur_record_id].mark_started(self.recording.start_time)
                        except Exception as exc:
                            self.logger.exception(exc)

                            # there should not be any errors, but still..
                            try:
                                self.recording.stop()
                            except Exception as exc:
                                self.logger.exception(exc)

                            with self.history_lock:
                                self.history[cur_record_id].mark_failed(exc)

                            self.logger.debug(f'loop: start exc path: calling increment_id()')
                            self.recording.increment_id()
                else:
                    if cur >= self.recording.stop_time:
                        try:
                            start_time = self.recording.start_time
                            stop_time = self.recording.stop_time
                            self.recording.stop()

                            saved_name = self.storage.save(tempname,
                                                           record_id=cur_record_id,
                                                           start_time=int(start_time),
                                                           stop_time=int(stop_time))

                            with self.history_lock:
                                self.history[cur_record_id].mark_finished(stop_time, saved_name)
                        except Exception as exc:
                            self.logger.exception(exc)
                            with self.history_lock:
                                self.history[cur_record_id].mark_failed(exc)
                        finally:
                            self.logger.debug(f'loop: stop exc final path: calling increment_id()')
                            self.recording.increment_id()

                        stopped = True

            if stopped and self.overtime > 0:
                self.logger.info(f'recording {cur_record_id} is stopped, but we\'ve got overtime ({self.overtime})')
                _overtime = self.overtime
                self.overtime = 0

                related_id = self.record(_overtime)
                self.logger.info(f'enqueued another record with id {related_id}')

                if cur_record_id is not None:
                    with self.history_lock:
                        self.history[cur_record_id].add_relation(related_id)

            time.sleep(0.2)

    def record(self, duration: int) -> int:
        self.logger.debug(f'record: duration={duration}')
        with self.lock:
            overtime = self.recording.ask_for(duration)
            self.logger.debug(f'overtime={overtime}')

            if overtime > self.overtime:
                self.overtime = overtime

            if not self.recording.is_started():
                with self.history_lock:
                    self.history.add(self.recording.record_id)

            return self.recording.record_id

    def stop(self):
        self.interrupted = True

    def get_info(self, record_id: int) -> RecordHistoryItem:
        with self.history_lock:
            return self.history[record_id]

    def forget(self, record_id: int):
        with self.history_lock:
            self.logger.info(f'forget: removing record {record_id} from history')
            self.history.delete(record_id)

    @staticmethod
    def get_max_record_time() -> int:
        return config['node']['record_max_time']