Compile and run the sample project
Updated
Set up, compile, and run the sample Server Gateway project provided by Agora.
Agora provides a runnable sample project on GitHub. This page shows you how to clone and run the project to send and receive media streams.
Execute the following command in the terminal to clone the repository locally:
git clone git@github.com:AgoraIO-Extensions/Agora-Python-Server-SDK.gitTo find the sample projects, navigate to the agora_rtc/examples directory. This article demonstrates running the example_audio_pcm_send and example_audio_pcm_receive projects.
The complete contents of the examples directory are as follows:
.
└── agora_rtc
└── examples
├── common # Common components
│ ├── __init__.py
│ ├── audio_consumer.py
│ ├── example_base.py
│ ├── pacer.py
│ ├── parse_args.py
│ ├── path_utils.py
│ ├── push_audio_encoded_file.py
│ ├── push_audio_pcm_file.py
│ ├── push_video_encoded_file.py
│ └── push_video_yuv_file.py
├── example_agora_parameter.py # Set private parameters
├── example_audio_encoded_receive.py # Receive encoded audio data
├── example_audio_encoded_send.py # Send encoded audio data
├── example_audio_pcm_loopback.py # Loopback test
├── example_audio_pcm_receive.py # Receive raw audio data
├── example_audio_pcm_receive_and_send.py # Send and receive raw audio data
├── example_audio_pcm_send.py # Send raw audio data
├── example_pcm_yuv_receive.py # Receive raw video and audio data
├── example_pcm_yuv_send.py # Send raw video and audio data
├── example_pcm_yuv_send_receive.py # Send and receive raw video and audio data
├── example_stream_message_receive.py # Receive stream messages
├── example_stream_message_send.py # Send stream messages
├── example_video_encoded_receive.py # Receive encoded video data
├── example_video_encoded_send.py # Send encoded video data
├── example_video_yuv_receive.py # Receive YUV video data
├── example_video_yuv_send.py # Send YUV video data
└── observer # Event listeners
├── audio_frame_observer.py
├── connection_observer.py
├── local_user_observer.py
├── video_encoded_frame_observer.py
└── video_frame_observer.pyRun the server streaming sample
To run the example_audio_pcm_send sample project, take the following steps:
-
In the terminal, run the following command to execute the
example_audio_pcm_sendproject to send PCM audio data. Modify the example parameter values as needed:python agora_rtc/examples/example_audio_pcm_send.py \ --appId=0a****************************99 \ --channelId=test_example \ --userId=6 \ --audioFile=./test_data/demo.pcm \ --sampleRate=16000 \ --numOfChannels=1Where:
appIdis the App ID for your Agora project.channelIdis the channel name.userIdis your user ID.audioFilespecifies the storage path of the audio data file. To facilitate testing, Agora provides test data that you can download and decompress to theAgora-Python-Server-SDKdirectory for use.sampleRatedefines the audio sampling rate.numOfChannelsspecifies the number of channels.
-
When the server successfully connects to the channel, you see the following output in the terminal:
INFO:common.parse_args:Parsed arguments:Namespace(appId='0a****************************99', token=None, channelId='test_example', connectionNumber='1', userId='6', audioFile='./test_data/demo.pcm', lowdelay=False, videoFile=None, sampleRate=16000, numOfChannels=1, fps=None, width=None, height=None, bitrate=None, message=None, hours='0', mode=1, value=0) INFO:common.example_base:------channel_id: test_example, uid: 6 INFO:common.example_base:connect_and_release: 0, auto_subscribe_audio: 0 INFO:observer.connection_observer:on_connecting, agora_rtc_conn=<agora.rtc.rtc_connection.RTCConnection object at 0x103c173d0>, local_user_id=0, state=2, internal_uid=0 ,reason=0 INFO:observer.connection_observer:on_connected, agora_rtc_conn=<agora.rtc.rtc_connection.RTCConnection object at 0x103c173d0>, local_user_id=3997884694, state=3, internal_uid=0 ,reason=1 -
After successfully sending PCM audio data, the terminal displays the following output:
INFO:common.push_audio_pcm_file:send pcm: count,ret=1, 0, 3200, 0.1 INFO:common.push_audio_pcm_file:send pcm: count,ret=2, 0, 3200, 0.1 ...
To simulate a client receiving PCM audio data in a browser, use the Agora real-time interactive Web Demo as follows:
-
Open the Web Demo and fill in the initialization settings. The App ID must be the same as the App ID you specified when running the server.
-
Select Basic Voice Calling, fill in the
ChannelIdyou specified earlier in Channel, and click Create Client and Join Channel in that order. -
After you successfully join the channel, when there is a streaming user in the channel, Web Demo automatically fills in the user ID of the streaming user in the input box under Step 4 Subscribe & Play. Click the Subscribe & Play button to subscribe to the streaming user.
When running the Demo, join the channel on the Web first, and then run the sample project on the server to connect to the channel. Otherwise, the input box under Step 4 Subscribe & Play does not automatically show the user ID, and the Web Demo is not able to successfully subscribe to the server.
-
After successfully subscribing, you hear the audio, and see the user ID of the subscribed stream under Remote Stream: in the Web Demo.
Run the server receiving sample
To run the example_audio_pcm_send sample project, take the following steps:
-
In the terminal, run the following command to execute the
example_audio_pcm_receiveproject to receive PCM audio data. Modify the example parameter values as needed:python agora_rtc/examples/example_audio_pcm_receive.py \ --appId=0a****************************99 \ --channelId=test_example \ --userId=8 \ --sampleRate=16000 \ --numOfChannels=1Where:
appIdis the App ID for your Agora project.channelIdis the channel name.userIdis your user ID.sampleRatedefines the audio sampling rate.numOfChannelsspecifies the number of channels.
-
When the server successfully connects to the channel, you see the following output in the terminal:
INFO:common.parse_args:Parsed arguments:Namespace(appId='0a****************************99', audioFile=None, bitrate=None, channelId='test_example', connectionNumber='1', fps=None, height=None, hours='0', lowdelay=False, message=None, numOfChannels=1, sampleRate=16000, token=None, userId='8', videoFile=None, width=None) INFO:common.example_base:------channel_id: test_example, uid: 8 INFO:observer.connection_observer:on_connecting, agora_rtc_conn=<agora.rtc.rtc_connection.RTCConnection object at 0x7f419d81f4a8>, local_user_id=6, state=2, internal_uid=0 ,reason=0 # Successfully connected to the channel INFO:observer.connection_observer:on_connected, agora_rtc_conn=<agora.rtc.rtc_connection.RTCConnection object at 0x7f419d81f4a8>, local_user_id=6, state=3, internal_uid=0 ,reason=1 -
Use the Agora real-time interactive Web Demo to simulate a client sending PCM audio data in the browser. Open the Web Demo and fill in the initialization settings. Use the same App ID you used to run the server example.
-
Select Basic Voice Calling, fill in the
ChannelId, you specified earlier, in Channel, and click Create Client and Join Channel in that order. After successfully joining the channel, you see the following output in the terminal:INFO:observer.connection_observer:on_user_joined, agora_rtc_conn=<agora.rtc.rtc_connection.RTCConnection object at 0x7f419d81f4a8>, user_id=45 INFO:observer.local_user_observer:on_user_info_updated, user_id=45, msg=0,val=1 INFO:observer.local_user_observer:on_user_info_updated, user_id=45, msg=4,val=1 -
In the Web Demo, click Create Track & Publish to publish the stream. After the Web client successfully publishes the stream, the server outputs the following content, indicating that the audio data is successfully received:
INFO:observer.audio_frame_observer:on_playback_audio_frame_before_mixing, channelId=test_dys1, uid=45, type=0, samples_per_sec=16000, samples_per_channel=160, bytes_per_sample=2, channels=1, len=320 INFO:observer.audio_frame_observer:on_playback_audio_frame_before_mixing, file_path=/home/agora/Documents/Agora-Python-Server-SDK-dev-2.0/logs/audio_frame_observer/2024_09_27_17_46_37/test_dys1_45.pcm, len=320When running the Demo, complete the channel connection on the server first, and then run the sample project on the Web to join the channel and stream concurrently. Otherwise, the server cannot successfully receive the audio stream from the Web.
