Metadata-Version: 2.5
Name: dtelecom-rtc
Version: 0.1.1
Summary: Server-side asyncio audio and data participant for dTelecom rooms
Project-URL: Documentation, https://github.com/dTelecom/server-sdk-python/tree/main/rtc#readme
Project-URL: Source, https://github.com/dTelecom/server-sdk-python
Project-URL: Issues, https://github.com/dTelecom/server-sdk-python/issues
License-Expression: Apache-2.0
License-File: LICENSE
License-File: NOTICE
Requires-Python: >=3.11
Requires-Dist: aioice==0.15.1
Requires-Dist: aiortc<1.18,>=1.17
Requires-Dist: av<20,>=14
Requires-Dist: dtelecom-server-sdk==0.1.0
Requires-Dist: pyee<17,>=13
Requires-Dist: websockets<17,>=14
Description-Content-Type: text/markdown

# dTelecom RTC for Python

Join a dTelecom room from a Python process, receive remote audio as PCM16, and publish audio or room data. Supply a participant grant from your backend. Its response includes the signaling URL, token and ranked fallback URLs. [Room connections](https://docs.dtelecom.org/rooms/connect/)

Install on Python 3.11 or later.

```sh
python -m pip install dtelecom-rtc
```

```python
import asyncio
from contextlib import aclosing, closing

from dtelecom_rtc import AudioSource, Room

async def echo(grant, finished):
    with closing(AudioSource(sample_rate=16000)) as source:
        async with asyncio.TaskGroup() as writers, Room() as room:
            forwarding = {}

            async def forward(track):
                async with aclosing(track.stream(sample_rate=16000)) as frames:
                    async for frame in frames:
                        await source.capture_frame(frame)

            @room.on("track_subscribed")
            def subscribed(track, publication, participant):
                forwarding[track] = writers.create_task(forward(track))

            @room.on("track_unsubscribed")
            def unsubscribed(track, publication, participant):
                if task := forwarding.pop(track, None):
                    task.cancel()

            await room.connect(grant.ws_url, grant.token, fallback_urls=grant.fallback_urls)
            await room.publish_audio("echo", source)
            await finished.wait()
```

Use a distinct identity for each participant. Set `auto_subscribe=False` to choose remote tracks explicitly with `set_subscribed`. Register event handlers before joining to include publications already present in the room. A `data_received` handler receives payload bytes, the sender when known, the delivery kind and topic. `publish_data` accepts a string or bytes, optional topic and destination participant SIDs, and reliable or lossy delivery.

The SDK tries the issuer's ranked endpoints with the same grant after a transport failure. When extending a grant through Cloud, call `room.update_connection_token(token, fallback_urls)` so the room uses the refreshed credentials after a reconnect. Use `disconnect()` or exit the async context to leave.

Configure SDK log levels using Python's standard `logging.getLogger("dtelecom_rtc").setLevel(...)`. Use `AudioFrame.duration`, `samples_per_channel`, and the `resample`, `downsample` and `upsample` helpers for standalone PCM. `create_text_message`, `encode_data_packet` and `decode_data_packet` expose the fork's room-data format. The protocol message models and enums are exported from `dtelecom_rtc.models`.

For a live interoperability check, install Playwright and use the ESM bundle of `@dtelecom/livekit-client@1.13.0`. Set `DTELECOM_API_KEY`, `DTELECOM_API_SECRET`, `DTELECOM_CLOUD_URL` and `DTELECOM_BROWSER_MODULE`, then run `python tests/integration/browser_call.py`. The script creates a disposable room and uses synthetic audio. Add `--recovery` to test a grant with three ranked nodes and reconnect the Python publisher. `CHROME_EXECUTABLE` selects a local browser when needed.

On transport loss, `room.state` becomes `"reconnecting"` and the room emits `reconnecting` before releasing remote tracks and participants. A service can keep per-speaker state through that teardown, then reconcile the identities present at `reconnected`. A terminal `disconnected` ends the session.
