PjsuaRecorder.__init__()   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 19

Duplication

Lines 19
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
dl 19
loc 19
rs 9.4285
c 1
b 0
f 0
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
import logging
5
logger = logging.getLogger(__name__)
6
logger.debug("%s loaded", __name__)
7
8
import os
9
10
from doorpi import DoorPi
11
from doorpi.sipphone.AbstractBaseClass import RecorderAbstractBaseClass
12
13
class PjsuaRecorder(RecorderAbstractBaseClass):
14
15
    __rec_id = None
16
    __slot_id = None
17
18
    __record_filename = ''
19
    __last_record_filename = ''
20
21
    @property
22
    def record_filename(self): return self.__record_filename
23
24
    @property
25
    def parsed_record_filename(self): return DoorPi().parse_string(self.__record_filename)
26
27
    @property
28
    def last_record_filename(self): return self.__last_record_filename
29
30 View Code Duplication
    def __init__(self):
0 ignored issues
show
Duplication introduced by Thomas Meissner
This code seems to be duplicated in your project.
Loading history...
31
        self.__record_filename = DoorPi().config.get('DoorPi', 'records',
32
                                                     '!BASEPATH!/records/%Y-%m-%d_%H-%M-%S.wav')
33
        if self.__record_filename is '':
34
            logger.debug('no recorder found in config at section DoorPi and key records')
35
            return
36
37
        DoorPi().event_handler.register_event('OnRecorderStarted', __name__)
38
        DoorPi().event_handler.register_event('OnRecorderStopped', __name__)
39
        DoorPi().event_handler.register_event('OnRecorderCreated', __name__)
40
41
        if DoorPi().config.get_bool('DoorPi', 'record_while_dialing', 'False') is True:
42
            DoorPi().event_handler.register_action('OnSipPhoneMakeCall', self.start)
43
        else:
44
            DoorPi().event_handler.register_action('OnCallStateConnect', self.start)
45
46
        DoorPi().event_handler.register_action('OnCallStateDisconnect', self.stop)
47
48
        DoorPi().event_handler('OnRecorderCreated', __name__)
49
50
    def start(self):
51
        if self.__record_filename is '':
52
            return
53
54
        if self.__rec_id is not None:
55
            logger.trace('recorder already created as rec_id %s and record to %s', self.__rec_id, self.last_record_filename)
56
            return
57
58
        DoorPi().sipphone.lib.thread_register('PjsuaPlayer_start_thread')
59
60
        if self.__record_filename is not '':
61
            self.__last_record_filename = DoorPi().parse_string(self.__record_filename)
62
            if not os.path.exists(os.path.dirname(self.__last_record_filename)):
63
                logger.info('Path %s not exists - create it now', os.path.dirname(self.__last_record_filename))
64
                os.makedirs(os.path.dirname(self.__last_record_filename))
65
66
            logger.debug('starting recording to %s', self.__last_record_filename)
67
            self.__rec_id = DoorPi().sipphone.lib.create_recorder(self.__last_record_filename)
68
            self.__slot_id = DoorPi().sipphone.lib.recorder_get_slot(self.__rec_id)
69
            DoorPi().sipphone.lib.conf_connect(0, self.__slot_id)
70
            DoorPi().event_handler('OnRecorderStarted', __name__)
71
72
    def stop(self):
73
        if self.__rec_id is not None:
74
            DoorPi().sipphone.lib.thread_register('PjsuaPlayer_start_thread')
75
            logger.debug('stopping recording to %s', self.__last_record_filename)
76
            DoorPi().sipphone.lib.conf_disconnect(0, self.__slot_id)
77
            DoorPi().sipphone.lib.recorder_destroy(self.__rec_id)
78
            self.__rec_id = None
79
            self.__slot_id = None
80
            DoorPi().event_handler('OnRecorderStopped', __name__)