Bladeren bron

rtc接口

locky 3 maanden geleden
bovenliggende
commit
cc1e5ee76b
3 gewijzigde bestanden met toevoegingen van 95 en 0 verwijderingen
  1. 3 0
      azoauth/config.py
  2. 1 0
      azoauth/urls.py
  3. 91 0
      controller/index.py

+ 3 - 0
azoauth/config.py

@@ -38,3 +38,6 @@ CLIENT_CONFIG = {
         'client_secret': 'amzn1.oa2-cs.v1.b4fc22afdbdab405df0ca29554eee4d002cd531d85e16142bfd5b33f2082af47'
     }
 }
+
+GO2RTC_API = 'http://144.24.9.228:1984/{}'
+GO2RTC_RTSP = 'rtsp://144.24.9.228:8554/{}'

+ 1 - 0
azoauth/urls.py

@@ -34,6 +34,7 @@ urlpatterns = [
 
     path('oa2/rtspStart', index.oa2RtspStartView.as_view()),            # 通知摄像头设备推流
     path('oa2/powerController', index.powerController.as_view()),       # 控制智能插座开关
+    path('oa2/rtc', index.RtcController.as_view()),                     # rtc
 
     url(r'^deviceStatus/(?P<operation>.*)$', deviceStatus.deviceStatus.as_view()),  # 更新设备信息等接口
     url(r'^vseesTest/(?P<operation>.*)', index.VesseTest.as_view()),  # test

+ 91 - 0
controller/index.py

@@ -707,6 +707,97 @@ class powerController(TemplateView):
             return JsonResponse({'result_code': '0'})
 
 
+class RtcController(TemplateView):
+    def post(self, request, *args, **kwargs):
+        request.encoding = 'utf-8'
+        request_dict = request.POST
+        return self.validate(request_dict)
+
+    def get(self, request, *args, **kwargs):
+        request.encoding = 'utf-8'
+        request_dict = request.GET
+        return self.validate(request_dict)
+
+    def validate(self, request_dict):
+        uid = request_dict.get("uid")
+        access_token = request_dict.get("access_token")
+        skill_name = request_dict.get("skill_name")
+
+        if not all([uid, access_token, skill_name]):
+            return JsonResponse({'错误': '缺少参数'})
+
+        user_qs = UserModel.objects.filter(access_token=access_token)
+        if not user_qs.exists():
+            return JsonResponse({'错误': '用户数据不存在'})
+
+        ur_qs = UidRtspModel.objects.filter(uid=uid).values('user_id', 'uid', 'nick', 'rtsp_url', 'password')
+        if not ur_qs.exists():
+            return JsonResponse({'错误': 'uid数据不存在'})
+
+        logger = logging.getLogger('django')
+
+        try:
+            # 请求go2rtc创建流
+            rtsp = GO2RTC_RTSP.format(uid)
+            rtc_url = GO2RTC_API.format('api/streams')
+            data = {
+                'src': rtsp,
+                'name': uid,
+            }
+            r = requests.put(url=rtc_url, data=data, timeout=30)
+            assert r.status_code == 200
+
+            # mqtt下发推流指令
+            logger.info('------rtc开始向设备下发推流指令:{}------'.format(uid))
+            
+            # 确认设备的用户地区
+            region = 'US'
+            user_id = ur_qs[0]['user_id']
+            user_qs = UserModel.objects.filter(userID=user_id).values('region_code')
+            if user_qs.exists():
+                region = user_qs[0]['region_code']
+
+            # 根据用户地区确认域名
+            domain_name = SERVER_PREFIX_EU if region == 'EU' else SERVER_PREFIX
+            # 请求MQTT发布消息
+            url = '{}/iot/requestPublishMessage'.format(domain_name)
+            requests_data = {'UID': uid, 'rtsp': rtsp, 'enable': '1'}  # 1: 开始推流,0: 停止推流; channel: 推流通道
+            r = requests.post(url, requests_data)
+            assert r.status_code == 200
+            res = r.json()
+            logger.info('rtc请求MQTT发布消息参数:{},result_code: {}'.format(requests_data, res['result_code']))
+            if res['result_code'] == 0:
+                logger.info('rtc请求MQTT下发指令成功,地区:{}'.format(region))
+            elif res['result_code'] == 10044:
+                url = '{}/iot/requestPublishMessage'.format(SERVER_PREFIX_TEST)  # 测试服务器
+                r = requests.post(url, requests_data)
+                assert r.status_code == 200
+                res = r.json()
+                if res['result_code'] == 0:
+                    logger.info('请求MQTT下发指令成功---测试服')
+
+            # 获取SDP
+            params = {
+                'src': uid,
+            }
+            r = requests.get(url=rtc_url, params=params, timeout=30)
+            assert r.status_code == 200
+            res = r.json()
+            # 遍历producers数组,查找包含sdp字段的对象
+            sdp = ''
+            for producer in data['producers']:
+                if 'sdp' in producer:
+                    sdp = producer['sdp']
+            res_json = {
+                'SDP': sdp
+            }
+            logger.info('------------rtc响应数据---------------: {}'.format(res_json))
+            return JsonResponse(res_json, safe=False)
+        except Exception as e:
+            logger.info('rtc异常,error_line:{},error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
+            return JsonResponse({'错误': 'rtc异常'})
+
+
 class VesseTest(TemplateView):
     def get(self, request, *args, **kwargs):
         request.encoding = 'utf-8'