Discussions

Ask a Question
Back to All

Error ‘IP address rejected by STUN server’ when running Talks Streams.

Talks Streamsを実行するとSTUN サーバーで IP アドレスを拒否されました。

エラーメッセージ

aioice.stun.TransactionFailed: STUN transaction failed (403 - Forbidden IP)

The following is the programme.

Step 1: Create a new stream

    url = "https://api.d-id.com/talks/streams"

    stream_warmup = False
    payload = {
        "stream_warmup": stream_warmup,
        "source_url": "https://cdn.pixabay.com/photo/2023/11/16/22/17/woman-8393229_960_720.jpg",
        "compatibility_mode": "on",
    }
    headers = {
        "accept": "application/json",
        "content-type": "application/json",
        "Authorization": f"Basic {base64_key}",
    }

    response = requests.post(url, json=payload, headers=headers)

Convert ice_servers to type from aiortc import RTCIceServer.

[RTCIceServer(urls=['stun:stun.kinesisvideo.us-west-2.amazonaws.com:443'],
              username=None,
              credential=None,
              credentialType='password'),
 RTCIceServer(urls=['turn:35-87-219-233.t-490d1050.kinesisvideo.us-west-2.amazonaws.com:443?transport=udp',
                    'turns:35-87-219-233.t-490d1050.kinesisvideo.us-west-2.amazonaws.com:443?transport=udp',
                    'turns:35-87-219-233.t-490d1050.kinesisvideo.us-west-2.amazonaws.com:443?transport=tcp'],
              username='1725891018:djE6YXJuOmF3czpraW5lc2lzdmlkZW86dXMtd2VzdC0yOjg5OTAxNjUwOTUyMDpjaGFubmVsL3RhbGtzLXN0cmVhbWVyLXByb2QtMzcvMTY3NzE2OTkyNDk2Mw==',
              credential='uTaWPgYlYvSv3U7rbI2fLVO5dYckZuxf5vww2f+wlBw=',
              credentialType='password')]

Step 2: Starting the stream

Configure ICE servers.

    if not peer_connection:
        # ICEサーバーを設定
        config = RTCConfiguration(iceServers=ice_servers)
        print("config:")
        pprint.pprint(config, width=40, compact=True)
        peer_connection = RTCPeerConnection(config)
        # print("peer_connection:", peer_connection)
        pc_data_channel = peer_connection.createDataChannel("JanusDataChannel")

Set event handlers.

        @peer_connection.on("icecandidate")
        def on_ice_candidate(event):
            print("###event[icecandidate]###")
            # @title Submit network information
            print("###Submit network information###")
            if event.candidate:
                print("New ICE candidate:", event.candidate)
                print("API Submit network information")
                response = asyncio.create_task(submit_ice_candidate(event.candidate))

            else:
                print("All ICE candidates have been gathered")
                response = asyncio.create_task(submit_ice_candidate(None))

            session_id = response.json()["session_id"]
            print("session_id:", session_id)
            print("=" * 50)

Set SDP.

    await peer_connection.setRemoteDescription(
        RTCSessionDescription(sdp=offer["sdp"], type=offer["type"])
    )
    print("set remote sdp OK")
    session_client_answer = await peer_connection.createAnswer()
    print("create local sdp OK")

    await peer_connection.setLocalDescription(session_client_answer)
    print("set local sdp OK")

Start a WebRTC connection

    url = f"https://api.d-id.com/talks/streams/{streams_id}/sdp"

    payload = {
        "answer": {"type": "answer", "sdp": session_client_answer.sdp},
        "session_id": session_id,
    }
    headers = {
        "accept": "application/json",
        "content-type": "application/json",
        "Authorization": f"Basic {base64_key}",
    }

    response = requests.post(url, json=payload, headers=headers)

    session_id = response.json()["session_id"]

Wait until connection is established

    while peer_connection.iceConnectionState != "connected":
        print("iceConnectionState:", peer_connection.iceConnectionState)
        await asyncio.sleep(1)

It does not become peer_connection.iceConnectionState == ‘connected’.

The icecandidate event is not fired.

Error rejecting IP address on STUN server

aioice.stun.TransactionFailed: STUN transaction failed (403 - Forbidden IP)