Discussions
Problems to set up the streaming avatar with Python
7 months ago by Kilian Kramer
Dear Heygen-Team,
I still got problems to set up the streaming avatar with Python
When answering the offer (see # START), I always receive {"code":400,"message":"peer start failed,please check the answer"} 400. What I am doing wrong?
Also what do I need to pass (see #PEER CONNECTION) for submitted the ICE candidate from my offer?
payload = {
"session_id": session_id,
"candidate": {
"candidate": "", # <- ?
"sdpMid": "<SDP_MID>", # <- ?
"sdpMLineIndex": "<SDP_MLINE_INDEX>", # <- ?
"usernameFragment": "<USERNAME_FRAGMENT>" # <- ?
}
}
Many thanks for your help in advance!
from aiortc import RTCPeerConnection, RTCSessionDescription
import requests
import json
import time
import asyncio
import cv2
# # <https://docs.heygen.com/reference/overview-copy>
# # <https://aiortc.readthedocs.io/en/latest/>
with open('heygen/offer.json', 'r') as file:
data = json.load(file)
session_id = data['data']['session_id']
headers = {
"accept": "application/json",
"content-type": "application/json",
"x-api-key": "NzU4M2ZhZDUyNTIyNGRmNTg1NmE0OTU1YzZlNWE2MmMtMTcxMzE4MDIxNg=="
}
# # ==============
# # STOP:
print("========================= Stop old stream:")
time.sleep(1)
url = "<https://api.heygen.com/v1/streaming.stop">
payload = {"session_id": session_id}
response = requests.post(url, json=payload, headers=headers)
print(response.text, response.status_code)
# ==============
# # LIST:
print("========================= Show streams:")
time.sleep(1)
url = "<https://api.heygen.com/v1/streaming.list">
response = requests.get(url, headers=headers)
print(response.text, response.status_code)
# ==============
# CREATE:
print("========================= Create stream:")
time.sleep(1)
url = "<https://api.heygen.com/v1/streaming.new">
payload = {"quality": "medium"}
response = requests.post(url, json=payload, headers=headers)
print(response.text, response.status_code)
if response.status_code == 200:
text = response.json()
with open('heygen/offer.json', 'w') as file:
json.dump(response.json(), file, indent=2)
with open('heygen/offer.json', 'r') as file:
data = json.load(file)
session_id = data['data']['session_id']
sdp = data['data']['sdp']['sdp']
# ==============
# START:
print("========================= Answer sdp:")
time.sleep(1)
async def handle_offer(pc, offer_sdp_data):
await pc.setRemoteDescription(RTCSessionDescription(offer_sdp_data, 'offer'))
answer = await pc.createAnswer()
await pc.setLocalDescription(answer)
# answer_sdp = {'type': 'answer', 'sdp': pc.localDescription.sdp}
answer_sdp = pc.localDescription.sdp
return answer_sdp
pc = RTCPeerConnection()
answer_sdp = asyncio.run(handle_offer(pc, sdp))
url = "<https://api.heygen.com/v1/streaming.start">
payload = {
"session_id": session_id,
"sdp": {
"type": "answer",
"sdp": answer_sdp
}
}
response = requests.post(url, json=payload, headers=headers)
print(response.text, response.status_code)
# # ==============
# # PEER CONNECTION:
print("========================= Submit ICE candidate:")
time.sleep(1)
url = "<https://api.heygen.com/v1/streaming.ice">
payload = {
"session_id": session_id,
"candidate": {
"candidate": "<CANDIDATE>",
"sdpMid": "\<SDP_MID>",
"sdpMLineIndex": "\<SDP_MLINE_INDEX>",
"usernameFragment": "\<USERNAME_FRAGMENT>"
}
}
response = requests.post(url, json=payload, headers=headers)
print(response.text, response.status_code)