|
@@ -7,6 +7,7 @@
|
|
import base64
|
|
import base64
|
|
import hashlib
|
|
import hashlib
|
|
import json
|
|
import json
|
|
|
|
+import pickle
|
|
import time
|
|
import time
|
|
import uuid
|
|
import uuid
|
|
import datetime
|
|
import datetime
|
|
@@ -544,8 +545,8 @@ class KVSView(View):
|
|
print(e)
|
|
print(e)
|
|
return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
|
|
- @staticmethod
|
|
|
|
- def send_alexa_offer_to_master(request_dict, response):
|
|
|
|
|
|
+ @classmethod
|
|
|
|
+ def send_alexa_offer_to_master(cls, request_dict, response):
|
|
"""
|
|
"""
|
|
发送Alexa offer
|
|
发送Alexa offer
|
|
@param request_dict: 请求参数
|
|
@param request_dict: 请求参数
|
|
@@ -560,8 +561,6 @@ class KVSView(View):
|
|
if uid == '517J385BNUGP3CPP111A':
|
|
if uid == '517J385BNUGP3CPP111A':
|
|
uid = 'NUWGTV5TUK8G8VSS111A'
|
|
uid = 'NUWGTV5TUK8G8VSS111A'
|
|
try:
|
|
try:
|
|
- sdp_offer = base64.b64encode(sdp_offer.encode('utf-8')).decode('utf-8')
|
|
|
|
- print(sdp_offer)
|
|
|
|
serial = CommonService.get_serial_number_by_uid(uid)
|
|
serial = CommonService.get_serial_number_by_uid(uid)
|
|
kvs_qs = KVS.objects.filter(stream_name=serial).values('channel_arn')
|
|
kvs_qs = KVS.objects.filter(stream_name=serial).values('channel_arn')
|
|
if not kvs_qs.exists():
|
|
if not kvs_qs.exists():
|
|
@@ -575,8 +574,6 @@ class KVSView(View):
|
|
config=botocore.client.Config(signature_version='s3v4'),
|
|
config=botocore.client.Config(signature_version='s3v4'),
|
|
region_name='cn-north-1'
|
|
region_name='cn-north-1'
|
|
)
|
|
)
|
|
- # res_1 = client.describe_signaling_channel(ChannelARN=channel_arn)
|
|
|
|
- # print(res_1)
|
|
|
|
res = client.get_signaling_channel_endpoint(
|
|
res = client.get_signaling_channel_endpoint(
|
|
ChannelARN=channel_arn,
|
|
ChannelARN=channel_arn,
|
|
SingleMasterChannelEndpointConfiguration={
|
|
SingleMasterChannelEndpointConfiguration={
|
|
@@ -589,14 +586,20 @@ class KVSView(View):
|
|
url = '{}/v1/send-alexa-offer-to-master'.format(endpoint)
|
|
url = '{}/v1/send-alexa-offer-to-master'.format(endpoint)
|
|
# 构造请求 body
|
|
# 构造请求 body
|
|
client_id = hashlib.md5((str(uuid.uuid1()) + str(int(time.time()))).encode('utf-8')).hexdigest()
|
|
client_id = hashlib.md5((str(uuid.uuid1()) + str(int(time.time()))).encode('utf-8')).hexdigest()
|
|
|
|
+ # offer转base64
|
|
|
|
+ offer = {
|
|
|
|
+ 'type': 'offer',
|
|
|
|
+ 'sdp': sdp_offer
|
|
|
|
+ }
|
|
|
|
+ offer = cls.dict_to_base64(offer)
|
|
|
|
+ LOGGER.info('offer:{}'.format(offer))
|
|
payload = {
|
|
payload = {
|
|
'ChannelARN': channel_arn,
|
|
'ChannelARN': channel_arn,
|
|
'SenderClientId': client_id,
|
|
'SenderClientId': client_id,
|
|
- 'MessagePayload': sdp_offer
|
|
|
|
|
|
+ 'MessagePayload': offer
|
|
}
|
|
}
|
|
|
|
|
|
# 构造 AWSRequest 并签名
|
|
# 构造 AWSRequest 并签名
|
|
- url = f'{endpoint}/v1/send-alexa-offer-to-master'
|
|
|
|
req = AWSRequest(method='POST', url=url, data=json.dumps(payload))
|
|
req = AWSRequest(method='POST', url=url, data=json.dumps(payload))
|
|
credentials = Credentials(ACCESS_KEY_ID, SECRET_ACCESS_KEY)
|
|
credentials = Credentials(ACCESS_KEY_ID, SECRET_ACCESS_KEY)
|
|
SigV4Auth(credentials, 'kinesisvideo', 'cn-north-1').add_auth(req)
|
|
SigV4Auth(credentials, 'kinesisvideo', 'cn-north-1').add_auth(req)
|
|
@@ -610,10 +613,29 @@ class KVSView(View):
|
|
LOGGER.info('SendAlexaOfferToMaster响应: {}'.format(r.json()))
|
|
LOGGER.info('SendAlexaOfferToMaster响应: {}'.format(r.json()))
|
|
sdp_answer = r.json()['Answer']
|
|
sdp_answer = r.json()['Answer']
|
|
assert sdp_answer
|
|
assert sdp_answer
|
|
- # print(base64.b64decode(sdp_answer).decode('utf-8'))
|
|
|
|
|
|
+ # answer转字典
|
|
|
|
+ sdp_answer = cls.base64_to_dict(sdp_answer)
|
|
|
|
+ sdp_answer = sdp_answer['sdp']
|
|
res = {
|
|
res = {
|
|
'sdp_answer': sdp_answer
|
|
'sdp_answer': sdp_answer
|
|
}
|
|
}
|
|
return response.json(0, res)
|
|
return response.json(0, res)
|
|
except Exception as e:
|
|
except Exception as e:
|
|
return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
+
|
|
|
|
+ @staticmethod
|
|
|
|
+ def dict_to_base64(data_dict: dict) -> str:
|
|
|
|
+ # 手动构建JSON字符串,避免自动转义
|
|
|
|
+ json_str = '{\n "type": "%s",\n "sdp": "%s"\n}' % (
|
|
|
|
+ data_dict["type"],
|
|
|
|
+ data_dict["sdp"].replace('"', '\\"').replace('\r', '\\r').replace('\n', '\\n')
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+ # 转换为bytes并进行base64编码
|
|
|
|
+ base64_data = base64.b64encode(json_str.encode('utf-8'))
|
|
|
|
+ return base64_data.decode('utf-8')
|
|
|
|
+
|
|
|
|
+ @staticmethod
|
|
|
|
+ def base64_to_dict(encoded_str: str) -> dict:
|
|
|
|
+ decoded_bytes = base64.b64decode(encoded_str)
|
|
|
|
+ return json.loads(decoded_bytes.decode('utf-8'))
|