Python

Updated at:

Quickly run the ARTC Linux SDK Python sample project to experience ApsaraVideo Real-time Communication features.

Get the sample source code

Download the latest version of the ARTC Linux SDK from the SDK download page. Extract the archive and open the `Python` folder. The package contains:

├── Release 
│   └── lib ## This directory contains the SDK dynamic libraries to link and the packaged ELF files.
│       ├── AliRtcCoreService
│       ├── libAliRtcLinuxEngine.so
│       └── libonnxruntime.so.1.16.3
└── Demo ## This directory contains Python wrappers for the corresponding C++ interfaces, so you can use Python code to implement RTC stream ingest and stream pulling with the same functionality as the C++ interfaces.
    ├── demo.py ## A simple example. Replace the appid and appkey before running.
    ├── AliRTCEngine.py
    ├── AliRTCEngineImpl.py
    └── AliRTCLinuxSdkDefine.py
  • The `Release` directory contains the SDK dynamic libraries for linking and the packaged ELF files. The `Demo` directory contains Python wrappers for the C++ interfaces, enabling RTC stream ingest and pulling through Python code.

  • The `Demo/demo.py` file provides a simple example. Before running, replace the `appid` and `appkey`.

Prerequisites

Before running the sample project, ensure the following requirements are met.

  • Prepare your application: Obtain the `AppID` and `AppKey` for your ApsaraVideo Real-time Communication application. See Get development parameters.

  • Set up your development environment: Ensure your Linux kernel version is 2.6 or later and your Python runtime is 3.6 or later. If you plan to extend the solution using the C++ interfaces, also ensure your GCC version is 4.8 or later.

Steps

Configure the project

In `demo.py`, replace the `appid` and specify the correct path to `AliRtcCoreService`.

image.png

image.png

Run the program

In your terminal, run the following command:

python demo.py

`demo.py` uses `input()` to wait for you to type `exit` and leave the channel. `input()` blocks the main thread, which suspends the asyncio event loop. While the event loop is suspended, `EngineEventListener` callbacks (such as `OnSubscribeAudioFrame`) cannot run, so they are only emitted after the program exits, not in real time.

In production code, do not use `input()`. Keep the main thread active with an asyncio event loop instead:

import asyncio

async def main():
    engine = AliRTCEngine(...)
    engine.joinChannel(...)
    while running:
        await asyncio.sleep(1)

asyncio.run(main())

Python output buffering also affects how quickly callback logs appear. Set the environment variable `PYTHONUNBUFFERED=1`, or add `flush=True` to `print`:

print("callback received", flush=True)

To deliver audio data in real time, pass each frame received in the `OnSubscribeAudioFrame` callback to downstream processing through an `asyncio.Queue`:

audio_queue = asyncio.Queue()

def OnSubscribeAudioFrame(self, uid, frame):
    asyncio.create_task(audio_queue.put(frame))

If your terminal displays the following output, the client has successfully joined the channel and started streaming.

# Joined the channel successfully
[Python] on join channel result. Channel: 12301, user: linux, result: 0

# Started audio and video streaming successfully
[Python] on audio publish state changed, oldState: 0, newState: 2
[Python] on video publish state changed, oldState: 0, newState: 2
[Python] on audio publish state changed, oldState: 2, newState: 3
[Python] on video publish state changed, oldState: 2, newState: 3

# A remote user named web has gone online
[Python] on remote user online: web

# Received audio and video streams from remote user abcd
[Python] on audio subscribe state of web, oldState: 0, newState: 2
[Python] on video subscribe state of web, oldState: 0, newState: 2
[Python] on audio subscribe state of web, oldState: 2, newState: 3
[Python] on video subscribe state of web, oldState: 2, newState: 3
  • If you select the `AudioFormatMixedPcm` mode, the audio callback triggers when you join the channel, even if no remote users are present.

  • If you select the `AudioFormatPcmBeforMixing` mode, the audio callback triggers only when other users are in the channel and publishing audio streams.