#!/usr/bin/env python import linphone import logging import signal import time class SecurityCamera: def __init__(self, username='', password='', whitelist=[], camera='', snd_capture='', snd_playback=''): self.quit = False self.whitelist = whitelist callbacks = { 'call_state_changed': self.call_state_changed, } # Configure the linphone core #logging.basicConfig(level=logging.INFO) signal.signal(signal.SIGINT, self.signal_handler) linphone.set_log_handler(self.log_handler) self.core = linphone.Core.new(callbacks, None, None) self.core.max_calls = 1 self.core.echo_cancellation_enabled = False self.core.video_capture_enabled = True self.core.video_display_enabled = False self.core.stun_server = '192.168.2.10' #self.core.firewall_policy = linphone.FirewallPolicy.PolicyUseIce if len(camera): self.core.video_device = camera if len(snd_capture): self.core.capture_device = snd_capture if len(snd_playback): self.core.playback_device = snd_playback # Only enable PCMU and PCMA audio codecs for codec in self.core.audio_codecs: if codec.mime_type == "PCMA" or codec.mime_type == "PCMU": self.core.enable_payload_type(codec, True) else: self.core.enable_payload_type(codec, False) # Only enable VP8 video codec for codec in self.core.video_codecs: if codec.mime_type == "VP8": self.core.enable_payload_type(codec, True) else: self.core.enable_payload_type(codec, False) #logging.error("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\Codec ....................:{mime}".format(mime=codec.mime_type)) self.configure_sip_account(username, password) def signal_handler(self, signal, frame): self.core.terminate_all_calls() self.quit = True def log_handler(self, level, msg): method = getattr(logging, level) method(msg) def call_state_changed(self, core, call, state, message): if state == linphone.CallState.IncomingReceived: #if call.remote_address.as_string_uri_only() in self.whitelist: params = core.create_call_params(call) core.accept_call_with_params(call, params) #else: #core.decline_call(call, linphone.Reason.Declined) #chat_room = core.get_chat_room_from_uri(self.whitelist[0]) #msg = chat_room.create_message(call.remote_address_as_string + ' tried to call') #chat_room.send_chat_message(msg) elif state == linphone.CallState.CallOutgoingInit \ or state == linphone.CallState.CallOutgoingProgress \ or state == linphone.CallState.CallOutgoingRinging: logging.info("\nCall remote: {address}".format(address=call.remote_address.as_string())) elif state == linphone.CallState.End: logging.info("Call ended normally.") self.current_call = None elif state == linphone.CallState.Error: logging.error("Error ... ending call!") # core.end_call(call) self.current_call = None else: logging.debug("call_state_changed() state = %s" % (linphone.CallState.string(state),)) def configure_sip_account(self, username, password): logging.info("\nIn configure sip account") # Configure the SIP account proxy_cfg = self.core.create_proxy_config() proxy_cfg.identity_address = self.core.create_address('sip:address@hidden'.format(username=username)) proxy_cfg.server_addr = 'sip:192.168.2.10'#;transport=tls' proxy_cfg.register_enabled = True self.core.add_proxy_config(proxy_cfg) auth_info = self.core.create_auth_info(username, None, password, None, None, '192.168.2.10') self.core.add_auth_info(auth_info) def run(self): while not self.quit: # self.core.iterate() # time.sleep(0.03) if self.core.current_call is None: params = self.core.create_call_params(None) params.audio_enabled = True params.video_enabled = True params.audio_multicast_enabled = False params.video_multicast_enabled = False address = linphone.Address.new('sip:address@hidden') logging.error('address = {address}, used_video_codec = {codec}'.format(address=address, codec=params.used_video_codec)) self.current_call = self.core.invite_address_with_params(address, params) if None is self.current_call: logging.error("Error creating call and inviting with params... outgoing call aborted.") self.core.iterate() time.sleep(0.03) def main(): cam = SecurityCamera(username='B1S1F102', password='alba123', whitelist=['sip:address@hidden'], camera='V4L2: /dev/video0', snd_capture='ALSA: default device') cam.run() main()