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.

129 lines
7.4KB

  1. # -*-coding:utf-8 -*-
  2. from json import loads
  3. from traceback import format_exc
  4. from fastapi import APIRouter
  5. from loguru import logger
  6. from bean.Result import JsonResult
  7. from common.Constant import CHANNEL_LIST_URL, UPDATE_CHANNEL_URL
  8. from enums.HttpExceptionEnum import EasyExceptionType
  9. from util.RequestUtil import HttpRequests
  10. router = APIRouter()
  11. @router.post("/rtmp/start", tags=['EasyRtmpLive启动接口'], response_model=JsonResult)
  12. async def startPushStream():
  13. logger.info("EasyRtmpLive启动接口!!")
  14. request = HttpRequests()
  15. headers = {'content-type': "application/json"}
  16. try:
  17. c_resp = request.send_request('GET', CHANNEL_LIST_URL, headers=headers, timeout=30)
  18. if c_resp.status_code != 200:
  19. logger.error("获取任务列表失败! 状态码: {}, {}", c_resp.status_code, c_resp.__dict__)
  20. return JsonResult.error(code=EasyExceptionType.EASY_TASK_GET_FAIL.value[0],
  21. msg=EasyExceptionType.EASY_TASK_GET_FAIL.value[1])
  22. else:
  23. content = c_resp.content.decode('utf-8')
  24. content = loads(content)
  25. channelList = content.get("ChannelList")
  26. if channelList is None or len(channelList) == 0:
  27. logger.error("获取EasyRtmpLive任务失败!")
  28. return JsonResult.error(code=EasyExceptionType.EASY_TASK_GET_FAIL.value[0],
  29. msg=EasyExceptionType.EASY_TASK_GET_FAIL.value[1])
  30. if channelList is not None and len(channelList) > 1:
  31. logger.error("EasyRtmpLive任务限制, 限制1个任务!")
  32. return JsonResult.error(code=EasyExceptionType.EASY_TASK_LIMIT.value[0],
  33. msg=EasyExceptionType.EASY_TASK_LIMIT.value[1])
  34. channel = channelList[0]
  35. if channel["enable"] == "true":
  36. logger.error("任务正在执行, 请稍后再试!")
  37. return JsonResult.error(code=EasyExceptionType.TASK_IS_EXECUTING.value[0],
  38. msg=EasyExceptionType.TASK_IS_EXECUTING.value[1])
  39. channel["enable"] = "true"
  40. channel["srcURL"] = '"%s"' % channel["srcURL"]
  41. req_param = 'indexCode=%s&name=%s&srcURL=%s&connectType=%s&timeout=5&mediaType=%s&dstURL=%s&dstFormat=%s&enable=%s' % \
  42. (channel["indexcode"], channel["name"], channel["srcURL"], channel["connectType"],
  43. channel["mediaType"], channel["dstURL"], channel["dstFormat"], channel["enable"])
  44. url = UPDATE_CHANNEL_URL % req_param
  45. u_resp = request.send_request('GET', url, headers=headers, timeout=30)
  46. if u_resp.status_code != 200:
  47. channel["enable"] = "false"
  48. fail_req_param = 'indexCode=%s&name=%s&srcURL=%s&connectType=%s&timeout=5&mediaType=%s&dstURL=%s&dstFormat=%s&enable=%s' % \
  49. (channel["indexcode"], channel["name"], channel["srcURL"], channel["connectType"],
  50. channel["mediaType"], channel["dstURL"], channel["dstFormat"], channel["enable"])
  51. fail_url = UPDATE_CHANNEL_URL % fail_req_param
  52. request.send_request('GET', fail_url, headers=headers, timeout=30)
  53. logger.error("EasyRtmpLive启动失败! 状态码: {}, {}", u_resp.status_code, u_resp.__dict__)
  54. return JsonResult.error(code=EasyExceptionType.EASY_TASK_START_FAIL.value[0],
  55. msg=EasyExceptionType.EASY_TASK_START_FAIL.value[1])
  56. else:
  57. u_content = u_resp.content.decode('utf-8')
  58. if u_content == "OK":
  59. return JsonResult.success()
  60. else:
  61. return JsonResult.error(code=EasyExceptionType.EASY_TASK_START_FAIL.value[0],
  62. msg=EasyExceptionType.EASY_TASK_START_FAIL.value[1])
  63. except Exception:
  64. logger.error("EasyRtmpLive启动失败异常: {}", format_exc())
  65. return JsonResult.error(code=EasyExceptionType.EASY_TASK_START_FAIL.value[0],
  66. msg=EasyExceptionType.EASY_TASK_START_FAIL.value[1])
  67. finally:
  68. request.close_session()
  69. @router.put("/rtmp/stop", tags=['EasyRtmpLive停止接口'], response_model=JsonResult)
  70. async def stopPushStream():
  71. logger.info("EasyRtmpLive停止接口!!")
  72. request = HttpRequests()
  73. headers = {'content-type': "application/json"}
  74. try:
  75. c_resp = request.send_request('GET', CHANNEL_LIST_URL, headers=headers, timeout=30)
  76. if c_resp.status_code != 200:
  77. logger.error("获取任务列表失败! 状态码: {}, {}", c_resp.status_code, c_resp.__dict__)
  78. return JsonResult.error(code=EasyExceptionType.EASY_TASK_GET_FAIL.value[0],
  79. msg=EasyExceptionType.EASY_TASK_GET_FAIL.value[1])
  80. else:
  81. content = c_resp.content.decode('utf-8')
  82. content = loads(content)
  83. channelList = content.get("ChannelList")
  84. if channelList is None or len(channelList) == 0:
  85. logger.error("获取EasyRtmpLive任务失败!")
  86. return JsonResult.error(code=EasyExceptionType.EASY_TASK_GET_FAIL.value[0],
  87. msg=EasyExceptionType.EASY_TASK_GET_FAIL.value[1])
  88. if channelList is not None and len(channelList) > 1:
  89. logger.error("EasyRtmpLive任务限制, 限制1个任务")
  90. return JsonResult.error(code=EasyExceptionType.EASY_TASK_LIMIT.value[0],
  91. msg=EasyExceptionType.EASY_TASK_LIMIT.value[1])
  92. channel = channelList[0]
  93. if channel["enable"] == "false":
  94. logger.info("EasyRtmpLive任务已停止")
  95. return JsonResult.success()
  96. channel["enable"] = "false"
  97. channel["srcURL"] = '"%s"' % channel["srcURL"]
  98. req_param = 'indexCode=%s&name=%s&srcURL=%s&connectType=%s&timeout=5&mediaType=%s&dstURL=%s&dstFormat=%s&enable=%s' % \
  99. (channel["indexcode"], channel["name"], channel["srcURL"], channel["connectType"],
  100. channel["mediaType"], channel["dstURL"], channel["dstFormat"], channel["enable"])
  101. url = UPDATE_CHANNEL_URL % req_param
  102. u_resp = request.send_request('GET', url, headers=headers, timeout=30)
  103. if u_resp.status_code != 200:
  104. logger.error("停止EasyRtmpLive失败! 状态码: {}, {}", u_resp.status_code, u_resp.__dict__)
  105. return JsonResult.error(code=EasyExceptionType.EASY_TASK_STOP_FAIL.value[0],
  106. msg=EasyExceptionType.EASY_TASK_STOP_FAIL.value[1])
  107. else:
  108. u_content = u_resp.content.decode('utf-8')
  109. if u_content == "OK":
  110. return JsonResult.success()
  111. else:
  112. return JsonResult.error(code=EasyExceptionType.EASY_TASK_STOP_FAIL.value[0],
  113. msg=EasyExceptionType.EASY_TASK_STOP_FAIL.value[1])
  114. except Exception:
  115. logger.error("EasyRtmpLive启动失败异常: {}", format_exc())
  116. return JsonResult.error(code=EasyExceptionType.EASY_TASK_STOP_FAIL.value[0],
  117. msg=EasyExceptionType.EASY_TASK_STOP_FAIL.value[1])
  118. finally:
  119. request.close_session()