def add_binding(self, code, username, password, expired=0): wechat_user_info = self._get_wechat_info_from_code(code) binding_id = gen_base64_uuid() wechat_binding = None if expired == 0: expired = int(time.time()) + 2592000 # default expire in a month later elif expired <= time.time(): raise IVRError("expired invalid", 400) with self._dao_context_mngr.context(): user = self._user_dao.get_by_username(username) if user is None: raise IVRError("username/password incorrect", self.HTTP_STATUS_CODE_USER_NOT_EXIST) if user.password != hashlib.sha256(password).hexdigest(): raise IVRError("username/password incorrect", self.HTTP_STATUS_CODE_INCORRECT_PASSWORD) wechat_binding = self._wechat_binding_dao.get_by_open_id(wechat_user_info["openid"]) if wechat_binding is not None: if wechat_binding.expired < time.time(): # the original binding expired, remove it self._wechat_binding_dao.delete_by_binding_id(wechat_binding.binding_id) else: raise IVRError("Wechat Binding Already exists", self.HTTP_STATUS_CODE_BINDING_EXISTS) wechat_binding = WechatBinding( binding_id=binding_id, open_id=wechat_user_info["openid"], username=username, wechat_nickname=wechat_user_info["nickname"], expired=expired, ) self._wechat_binding_dao.add(wechat_binding) wechat_binding = self._wechat_binding_dao.get_by_binding_id(binding_id) return wechat_binding
def request_session(self, project_name, camera_id, stream_format, stream_quality, create=True, ip='', user_agent='', username='', subuser=''): project = self._project_mngr.get_project(project_name) count = self._dao.get_count(project_name=project_name) if project.max_media_sessions != 0 and count >= project.max_media_sessions: raise IVRError('Failed to start session, max concurrent media session count {0} reached for {1}'.format(project.max_media_sessions, project)) camera = self._camera_mngr.get_camera(project_name, camera_id) stream = self._camera_mngr.get_stream(project_name, camera_id, stream_format, stream_quality, auto_delete=True, create=create) session_id = gen_base64_uuid() if stream_format == 'hls': url = stream.hls_url else: url = stream.rtmp_url now = int(time.time()) user_session = UserSession( project_name=project_name, uuid=session_id, camera_uuid=camera_id, camera_name=camera.name, stream_format=stream_format, stream_quality=stream_quality, stream_uuid=stream.uuid, start=now, last_keepalive=now, url=url, ip=ip, user_agent=user_agent, username=username, subuser=subuser ) self._dao.add(user_session) log.info('Created {0} for {1}'.format(user_session, stream)) return user_session.url, session_id
def add_firmware(self, *args, **kwargs): firmware_id = gen_base64_uuid() firmware = Firmware( firmware_id, *args, **kwargs ) self._dao.add(firmware) return firmware_id
def new_live_show(self, project_name, **kwargs): camera_id = kwargs.get('camera_id') camera = self._camera_mngr.get_camera(project_name, camera_id) kwargs['camera_name'] = camera.name live_show_id = gen_base64_uuid() live_show = LiveShow(project_name, live_show_id, **kwargs) self._dao.add(live_show) log.info('Created new {0}'.format(live_show)) return live_show_id
def add_camera(self, project_name, **kwargs): camera = self._dao.get_by_device_channel(kwargs['device_uuid'], kwargs['channel_index']) if camera is not None: raise IVRError('Failed to create camera, channel {0} of device {1} already exists'.format(camera.channel_index, camera.device_uuid)) camera_id = gen_base64_uuid() camera = Camera(project_name, camera_id, **kwargs) device = self._device_mngr.get_device(project_name, camera.device_uuid) if device.media_channel_num <= camera.channel_index: raise IVRError('Unable to add camera to device, max channel number {0} reached for {1}'.format(device.media_channel_num, device)) self._dao.add(camera) return camera_id
def new_stream(self, project_name, camera_id, stream_quality=VideoQuality.LD): # TODO concurrent request for the same stream maybe harmful, need to handle this situation project = self._project_mngr.get_project(project_name) stream_id = gen_base64_uuid() stream = Stream( project_name=project_name, stream_id=stream_id, camera_id=camera_id, stream_quality=stream_quality, publish_to=self._rtmp_publish_url(project, stream_id), hls_url=self._calc_url(project, 'hls', stream_id), rtmp_url=self._calc_url(project, 'rtmp', stream_id), ) self._dao.add(stream) return stream
def new_stream(self, project_name, camera_id, stream_quality=VideoQuality.LD): # TODO concurrent request for the same stream maybe harmful, need to handle this situation # for those dynamically named stream project = self._project_mngr.get_project(project_name) uuid = gen_base64_uuid() stream = self._stream_factory( project_media_server=project.media_server, project_name=project_name, uuid=uuid, camera_id=camera_id, stream_quality=stream_quality, rtmp_url_prefix=project.media_server, ) self._dao.add(stream) log.info('Created {0}'.format(stream)) return stream
def _start_recording(self, config, prerecord=False): # record is not started or stopped exceptionally # assign to another record agent if it requires record agent # otherwise send request to camera to start recording # either way, we need to change record session ID log.info('Try to start record camera {0}'.format(config.camera_uuid)) record_session_id = gen_base64_uuid() agent_id = None stream_id = None if not self._camera_mngr.need_record_agent(config.project_name, config.camera_uuid): self._camera_mngr.start_cloud_record( config.project_name, config.camera_uuid, record_session_id, config.stream_quality, 'http://{0}/api/ivc/v1/record_session_upload_file_cbk/{1}'.format(self._ivc_host, record_session_id) ) log.info('Record session {0} for camera {1} started'.format( record_session_id, config.camera_uuid) ) else: # record agent required, need stream URL stream = self._camera_mngr.get_stream( config.project_name, config.camera_uuid, 'rtmp', config.stream_quality, auto_delete=True, create=True) # pick an available record agent and start record while True: # try pick one available record agent and try to start recording on that agent # if failed, then pick another, until we run out of available record agent try: agent = self._record_agent_mngr.pick() except Exception: log.exception('Failed to start recording camera {0}'.format(config.camera_uuid)) if config.state != config.RecordStateError: config.state = config.RecordStateError self._dao.update(config) raise try: self._record_agent_mngr.start_record( agent, config.project_name, config.camera_uuid, record_session_id, stream.rtmp_url, 'http://{0}/api/ivc/v1/record_session_upload_file_cbk/{1}'.format(self._ivc_host, record_session_id)) log.info('Record session {0} for camera {1} started on agent {2}'.format( record_session_id, config.camera_uuid, agent.agent_id)) agent_id = agent.agent_id stream_id = stream.uuid break except Exception: log.exception('Failed to start recording camera {0} on agent {1}, retry'.format(config.camera_uuid, agent.agent_id)) config.start_recording(record_session_id, agent_id, stream_id, prerecord) self._dao.update(config)
def add_device(self, project_name, *args, **kwargs): device_id = gen_base64_uuid() device = Device(project_name, device_id, *args, **kwargs) self._dao.add(device) return device_id