You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Pull2PushStreamThread.py 2.4KB

пре 1 година
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # -*- coding: utf-8 -*-
  2. from threading import Thread
  3. import time
  4. from traceback import format_exc
  5. from loguru import logger
  6. from enums.StatusEnum import PushStreamStatus
  7. from exception.CustomerException import ServiceException
  8. from util.PushStreamUtils import PushStreamUtil
  9. class PushSteamThread(Thread):
  10. __slots__ = ("pushStreamUtil", "requestId", "videoId", "status", "ex")
  11. def __init__(self, pullUrl, pushUrl, requestId, videoId):
  12. super().__init__()
  13. self.pushStreamUtil = PushStreamUtil(pullUrl, pushUrl, requestId)
  14. self.requestId = requestId
  15. self.videoId = videoId
  16. self.status = True
  17. self.excute_status = PushStreamStatus.WAITING.value[0]
  18. self.ex = None
  19. def run(self):
  20. logger.info("开始启动推流线程, 视频id: {}, requestId:{}", self.videoId, self.requestId)
  21. while True:
  22. try:
  23. self.pushStreamUtil.start_push_stream()
  24. self.excute_status = PushStreamStatus.RUNNING.value[0]
  25. out, err = self.pushStreamUtil.push_stream_sp.communicate()
  26. # 异常断流
  27. if self.status:
  28. logger.warning("推流异常,请检测拉流地址和推流地址是否正常!")
  29. if self.pushStreamUtil.push_stream_sp.returncode != 0:
  30. logger.error("推流异常:{}, 视频id: {}, requestId:{}", err.decode(), self.videoId,
  31. self.requestId)
  32. self.excute_status = PushStreamStatus.RETRYING.value[0]
  33. self.pushStreamUtil.close_push_stream_sp()
  34. time.sleep(5)
  35. # 手动断流
  36. if not self.status:
  37. self.pushStreamUtil.close_push_stream_sp()
  38. break
  39. except ServiceException as s:
  40. logger.error("异常: {}, 视频id: {}, requestId:{}", s.msg, self.videoId, self.requestId)
  41. self.pushStreamUtil.close_push_stream_sp()
  42. self.ex = s
  43. break
  44. except Exception as e:
  45. logger.error("异常:{}, 视频id: {}, requestId:{}", format_exc(), self.videoId, self.requestId)
  46. self.pushStreamUtil.close_push_stream_sp()
  47. self.ex = e
  48. break
  49. logger.info("结束推流线程, 视频id: {}, requestId:{}", self.videoId, self.requestId)