123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- from app import log
- from app import register
- from app import device
- from app.app_config import *
- from time import sleep
- import ctypes
- import vlc
- import threading
- # from app.register import runFunctionThread
- # import traceback
- """
- desc:
- 播放指令执行函数
-
- Parameters:
- param1 - 播放相关的数据
-
- Returns:
- 执行状态
- """
- @register.action(PLAY)
- def action_play(data):
- if "data" in data:
- if not device.play(data.get(DATA)):
- log.logger.error("Play uri:%s on id:%s error" % (data.get(DATA).get("url"), data.get(ID)))
- return False
- log.logger.info("Play uri:%s on id:%s successfully" % (data.get(DATA).get("url"), data.get(ID)))
- return True
- else:
- log.logger.info("Data not found")
- return False
- """
- desc:
- 暂停指令执行函数
-
- Parameters:
- param1 - 暂停相关的数据
-
- Returns:
- 执行状态
- """
- @register.action(PAUSE)
- def action_pause(data):
- if "value" in data.get(DATA):
- value = data.get(DATA).get("value")
- else:
- log.logger.info("Value not found")
- return False
- if not device.pause(value):
- log.logger.error("Player pause on id:%s error" % (data.get(ID)))
- return False
- else:
- log.logger.info("Player pause on id:%s successfully" % (data.get(ID)))
- return True
- """
- desc:
- 恢复指令执行函数
-
- Parameters:
- param1 - 恢复相关的数据
-
- Returns:
- 执行状态
- """
- @register.action(RESUME)
- def action_resume(data):
- if "value" in data.get(DATA):
- value = data.get(DATA).get("value")
- else:
- log.logger.info("Value not found")
- return False
- if not device.resume(value):
- log.logger.error("Player resume on id:%s error" % (data.get(ID)))
- return False
- else:
- log.logger.info("Player resume on id:%s successfully" % (data.get(ID)))
- return True
- """
- desc:
- 停止播放指令执行函数
-
- Parameters:
- param1 - 停止播放相关的数据
-
- Returns:
- 执行状态
- """
- @register.action(STOP)
- def action_stop(data):
- if not device.stop():
- log.logger.error("Player stop on id:%s stop" % (data.get(ID)))
- log.logger.info("Player stop on id:%s successfully" % (data.get(ID)))
- return True
- """
- desc:
- 设置分机号指令执行函数
-
- Parameters:
- param1 - 设置分机号相关的数据
-
- Returns:
- 执行状态
- """
- @register.action(SET_EXTEN)
- def action_set_exten(data):
- if "data" in data:
- if HOST in data.get(DATA):
- host = data.get(DATA).get(HOST)
- if EXTEN in data.get(DATA):
- exten = data.get(DATA).get(EXTEN)
-
- if PASSWORD in data.get(DATA):
- password = data.get(DATA).get(PASSWORD)
-
- if not (host and exten and password):
- log.logger.warning("data not compelete")
- return False
- else:
- log.logger.info("data not found")
- return False
- if not device.set_exten(host, exten, password):
- log.logger.error("Set exten:%s on id:%s error" % (exten, data.get(ID)))
- log.logger.info("Set exten:%s on id:%s successfully" % (exten, data.get(ID)))
- return True
- """
- desc:
- 设置播放器音量指令执行函数
-
- Parameters:
- param1 - 音量相关的数据
-
- Returns:
- 执行状态
- """
- @register.action(SET_SOLF_VOLUME)
- def action_set_soft_volume(data):
- if "volume" in data.get(DATA):
- volume = int(data.get(DATA).get(VOLUME))
- else:
- log.logger.warning("volume not found!")
- return False
- if not device.set_soft_volume(volume):
- log.logger.error("Set volume:%d on id:%s error" % (volume, data.get(ID)))
- log.logger.info("Set soft volume:%d on id:%s successfully" % (volume, data.get(ID)))
- return True
- """
- desc:
- 设置硬件音量指令执行函数
-
- Parameters:
- param1 - 硬件音量相关的数据
-
- Returns:
- 执行状态
- """
- @register.action(SET_HARD_VOLUME)
- def action_set_hard_volume(data):
- if "volume" in data.get(DATA):
- volume = int(data.get(DATA).get(VOLUME))
- else:
- log.logger.warning("volume not found!")
- return False
- if not device.set_hard_volume(volume):
- log.logger.error("Set hard volume:%d on id:%s error" % (volume, data.get(ID)))
- log.logger.info("Set hard volume:%d on id:%s successfully" % (volume, data.get(ID)))
- return True
- """
- desc:
- 设置强控指令执行函数
-
- Parameters:
- param1 - 设置强控相关的数据
-
- Returns:
- 执行状态
- """
- @register.action(SET_HARD_VOLUME_CONTROL)
- def action_set_hard_volume_control(data):
- if "value" in data.get(DATA):
- value = data.get(DATA).get(VALUE)
- else:
- log.logger.warning("value not found!")
- return False
- if not device.set_hard_volume_control(value):
- log.logger.error("Set hard volume control:%s on id:%s error" % (value, data.get(ID)))
- log.logger.info("Set hard volume control:%s on id:%s successfully" % (value, data.get(ID)))
- return True
- """
- desc:
- 设置相关数据通用指令执行函数
-
- Parameters:
- param1 - 相关的数据
-
- Returns:
- 执行状态
- """
- @register.action(SET_SERVICE_SETTINGS)
- def action_set_service_settings(data):
- if "data" in data:
- data_obj = data.get(DATA)
- if not device.set_service_settings(data_obj):
- log.logger.error("Set service settings:%s on id:%s error" % (data_obj, data.get(ID)))
- return False
- log.logger.info("Set service settings:%s on id:%s successfully" % (data_obj, data.get(ID)))
- return True
- """
- desc:
- 开始推送rtsp指令执行函数
-
- Parameters:
- param1 - rtsp相关的数据
-
- Returns:
- 执行状态
- """
- @register.action("start-pull-rtsp")
- def action_start_pull_rtsp(data):
- if "src" in data.get(DATA):
- src = data.get(DATA).get("src")
- else:
- log.logger.warning("src not found!")
- return False
- if "dst" in data.get(DATA):
- dst = data.get(DATA).get("dst")
- else:
- log.logger.warning("dst not found!")
- return False
- if not device.start_pull_rtsp(src, dst):
- log.logger.error("Start pull rtsp src:%s on id:%s error" % (src, data.get(ID)))
- else:
- log.logger.info("Start pull rtsp src:%s on id:%s successfully" % (src, data.get(ID)))
- return True
- """
- desc:
- 停止推送rtsp指令执行函数
-
- Parameters:
- param1 - rtsp相关的数据
-
- Returns:
- 执行状态
- """
- @register.action("stop-pull-rtsp")
- def action_stop_pull_rtsp(data):
- if not device.stop_pull_rtsp():
- log.logger.error("Stop pull rtsp on id:%s error" % (data.get(ID)))
- else:
- log.logger.info("Stop pull rtsp on id:%s successfully" % (data.get(ID)))
- return True
- vlc_debug = {
- 'DEBUG': 0,
- 'NOTICE': 2,
- 'WARNING': 3,
- 'ERROR': 4
- }
- """
- desc:
- 捕获libvlc库的日志
- """
- vsnprintf = ctypes.cdll.LoadLibrary(ctypes.util.find_library('c')).vsnprintf
- vsnprintf.restype = ctypes.c_int
- vsnprintf.argtypes = (
- ctypes.c_char_p,
- ctypes.c_size_t,
- ctypes.c_char_p,
- ctypes.c_void_p,
- )
- @vlc.CallbackDecorators.LogCb
- def log_callback(data, level, ctx, fmt, args):
- if level < vlc_debug[VLC_DEBUG_LEVEL]:
- return
- outBuf = ctypes.create_string_buffer(VLC_BUF_LEN)
- vsnprintf(outBuf, VLC_BUF_LEN, fmt, args)
- try:
- log_msg = str(outBuf.value, 'utf-8')
- except Exception as e:
- log.logger.error(e)
- if level == vlc_debug['DEBUG']:
- log.logger.debug(log_msg)
- elif level == vlc_debug['NOTICE']:
- log.logger.info(log_msg)
- elif level == vlc_debug['WARNING']:
- log.logger.warning(log_msg)
- if "buffer too late" in log_msg:
- device.set_vlc_warning_count(device.get_vlc_warning_count() + 1)
- if device.get_vlc_warning_count() > VLC_WARNING_COUNT:
- log.logger.info("Replay uri:%s" % (device.current_uri))
- t = threading.Thread(target = device.player.play, args = (device.current_uri,))
- t.setDaemon(True)
- t.start()
- log.logger.info("Replay finished!")
- device.vlc_error_count = 0
- else:
- pass
- else:
- log.logger.error(log_msg)
- if "Timestamp conversion failed" in log_msg:
- device.set_vlc_error_count(device.get_vlc_error_count() + 1)
- if device.get_vlc_error_count() > VLC_ERROR_COUNT:
- log.logger.info("Replay uri:%s" % (device.current_uri))
- t = threading.Thread(target = device.player.play, args = (device.current_uri,))
- t.setDaemon(True)
- t.start()
- log.logger.info("Replay finished!")
- device.vlc_error_count = 0
- else:
- pass
- def vlc_log_start():
- log.logger.info("Start vlc log")
- device.player.instance.log_set(log_callback, None)
- return True
|