Channel quality
Updated
Adjust audio and video settings to optimize channel quality
PlatformWrapper } from '../../../../src/mdx-components/PlatformWrapper';
Customer satisfaction for your IoT SDK integrated app depends on the quality of video and audio it provides. Quality of audiovisual communication through your app is affected by the following factors:
-
Bandwidth of network connection: Bandwidth is the volume of information that an Internet connection can handle per unit of time. When the available bandwidth is not sufficient to transmit the amount of data necessary to provide the desired video quality, your users see jerky or frozen video along with audio that cuts in and out.
-
Stability of network connection: Network connections are often unstable with the network quality going up and down. Users get temporarily disconnected and come back online after an interruption. These issues lead to a poor audiovisual experience for your users unless your app is configured to respond to these situations and take remedial actions.
-
Hardware quality: The camera and microphone used to capture video and audio must be of sufficiently good quality. If the user's hardware does not capture the audiovisual information in suitably high definition, it limits the quality of audio and video that is available to the remote user.
-
Video and audio settings: The sharpness, smoothness, and overall quality of the video is directly linked to the frame rate, bitrate and other video settings. Similarly, the audio quality depends on the sample rate, bitrate, number of channels and other audio parameters. If you do not choose proper settings, the audio and video transmitted are of poor quality. On the other hand, if the settings are too demanding, the available bandwidth quickly gets choked, leading to suboptimal experience for your users.
-
Echo: Echo is produced when your audio signal is played by a remote user through a speakerphone or an external device. This audio is captured by the remote user's microphone and sent back to you. Echo negatively affects audio quality, making speech difficult to understand.
-
Multiple users in a channel: When multiple users engage in real-time audio and video communication in a channel, the available bandwidth is quickly used up due to several incoming audio and video streams. The device performance also deteriorates due to the excessive workload required to decode and render multiple video streams.
This page shows you how to use IoT SDK features to account for these factors and ensure optimal audio and video quality in your IoT SDK app.
Understand the tech
To provide the best audio and video quality in your app:
-
Choose an audio codec to optimize bit rate and quality
In audio and video streaming, the choice of an encoding algorithm affects both quality and bit rate. Your goal is to lower the required bit rate while maintaining quality at the desired level. IoT SDK offers the following built-in audio codecs:
- Opus
- G722
- G711A (PCMU)
- G711U (PCMU)
You specify an audio codec when you join a channel. Depending on your audio quality requirements you also set the sampling rate and the number of channels. If your app needs a custom encoding algorithm, you disable use of a built-in codec and implement your own encoder.
-
Adjust sending bit rate in real time
In order to optimize data transmission and avoid network congestion, best practice is to adjust the sending bit rate in real-time according to changes in network conditions.
You configure BandWidth Estimation (BWE) before joining a channel to set the minimum, maximum, and starting bit rate values according to the actual bandwidth and bit rate needs. When the network bandwidth changes, IoT SDK triggers an event to prompt your app to adjust the sending bit rate in real time. The bit rate returned by the callback is the maximum recommended encoding bit rate of the video encoder.
-
Change audio and video streaming status
Network traffic can be reduced by suspending data transmission when audio or video feed is not required by the receiver. After a user successfully connects to a channel and starts audio and video streaming, they can suspend sending streams to a specific connection or to all connections to flexibly manage the transmission status of audio and video streams. Similarly, the user may suspend receiving a specific or all streams as required.
When a user changes the transmission status of the local audio or video stream, the IoT SDK triggers a corresponding callback to prompt the remote user to suspend or resume sending their audio or video stream.
-
Request key frames
In video transmission, a frame containing complete image information is known as a key frame. Subsequent frames, known as delta frames, only include modifications to the previous frame. When a video streaming client experiences network congestion, the data loss in delta frames leads to visual inconsistencies in subsequent frames. IoT SDK enables you to request a key frame from the sender to resolve such issues. A fresh key frame resets the video stream to a known state. This feature allows the client to resynchronize with the video stream and resume playback without visual artifacts or distortion.
-
Log files
IoT SDK provides configuration options that you use to customize the location, content and size of log files containing key data of IoT SDK operation. When you set up logging, IoT SDK writes information messages, warnings, and errors regarding activities such as initialization, configuration, connection and disconnection to log files. Log files are useful in detecting and resolving channel quality issues.
The following figure shows the workflow you need to implement to ensure channel quality in your app:
Prerequisites
In order to follow this procedure you must have:
- Implemented the SDK quickstart project for IoT SDK.
To test the code used in this page you need to have:
-
A computer with Internet access. Ensure that no firewall is blocking your network communication.
-
Implemented the SDK quickstart.
Project setup
To create the environment necessary to implement channel quality best practices into your app, open the SDK quickstart for IoT SDK project you created previously.
In this example, you use terminal input to mute or unmute local audio and video streams. To set up your project to accept terminal input without blocking code execution, take the following steps:
-
To configure the terminal add the following header to
hello_rtsa.cafter#include "app_config.h":#include <termios.h> -
The default terminal behaviour is to block code execution while waiting for user input and to process the input after a carriage return. To modify this blocking behaviour, add the following function to
hello_rtsa.cbeforeapp_signal_handler():void nonblock(bool turn_off_canonical) { // Enable or disable non-block mode and canonical mode to read input struct termios ttystate; // get the terminal state tcgetattr(STDIN_FILENO, &ttystate); if (turn_off_canonical) { // turn off canonical mode ttystate.c_lflag &= ~ICANON; // minimum of number input read. ttystate.c_cc[VMIN] = 1; } else { // turn on canonical mode ttystate.c_lflag |= ICANON; } // set the terminal attributes. tcsetattr(STDIN_FILENO, TCSANOW, &ttystate); } -
To enable non-blocking mode, add the following line to the top of
main():nonblock(true); -
To disable non-blocking mode on exit, add the following line to
app_signal_handlerafterg_app.b_stop_flag = true;:nonblock(false); -
To read non-blocking terminal input, add the following function to
hello_rtsa.cbeforemain():int kbhit() { // Read input from the terminal without blocking code execution struct timeval tv; fd_set fds; tv.tv_sec = 0; tv.tv_usec = 0; FD_ZERO(&fds); FD_SET(STDIN_FILENO, &fds); // STDIN_FILENO is 0 select(STDIN_FILENO + 1, &fds, NULL, NULL, &tv); return FD_ISSET(STDIN_FILENO, &fds); }
Implement best practice to optimize channel quality
This section shows you how to integrate channel quality optimization features of IoT SDK into your app, step-by-step.
To implement channel quality features, take the following steps:
-
Configure the IoT SDK log file
To customize the location, and content of the log file, add the following code to
main()beforeagora_rtc_init():// Configure log file location and logging level service_opt.log_cfg.log_path = config->p_sdk_log_dir; service_opt.log_cfg.log_level = RTC_LOG_DEBUG; -
Specify the audio codec, sampling rate, and the number of channels
You set the audio codec, sampling rate, and the number of channels in
channel_optionsthat you pass toagora_rtc_join_channel. To set these parameters according to your requirements, use theconfigobject or modify the following lines inmain():channel_options.audio_codec_opt.audio_codec_type = config->audio_codec_type; channel_options.audio_codec_opt.pcm_sample_rate = config->pcm_sample_rate; channel_options.audio_codec_opt.pcm_channel_num = config->pcm_channel_num;Choose from the following audio codecs built in to the IoT SDK.
AUDIO_CODEC_TYPE_OPUS,AUDIO_CODEC_TYPE_G722AUDIO_CODEC_TYPE_G711AAUDIO_CODEC_TYPE_G711U
To use your own audio codec, set
channel_options.audio_codec_opt.audio_codec_typetoAUDIO_CODEC_DISABLED. When you callagora_rtc_send_audio_data, set thedata_typeparameter ofaudio_frame_infoto your encoding format. This setting transmits the audio encoding format as is to the receiving end. When you disable use of a built-in audio codec, IoT SDK does not process the audio. The receiving end obtains the encoded audio data and the encoding format through theon_audio_datacallback and decodes it using a custom decoder. -
Configure bandwidth estimation parameters
You configure bandwidth estimation parameters before joining a channel. For example, based on the definition levels of a webcam, the minimum bit rate value can be set to 400 kbps, and the maximum value can be set to 4200 kbps. The starting value is between the minimum and the maximum value. If the initial encoding is SD, the initial bit rate can be set to 500 kbps, based on the table.
To specify these parameters, replace the
agora_rtc_set_bwe_paramcall inmain()with the following:// Configure bandwidth estimation parameters // Min, max, and starting bit rates in bps uint32_t min_bps = 400000; uint32_t max_bps = 4200000; uint32_t start_bps = 500000; rval = agora_rtc_set_bwe_param(min_bps, max_bps, start_bps); -
Respond to target bit rate changes
When network bandwidth changes, IoT SDK triggers the
on_target_bitrate_changedcallback to prompt the app to adjust the sending bit rate. Thetarget_bpsvalue returned by the callback is the maximum recommended encoding bit rate of the video encoder.In this example, you use definition levels of a webcam to switch resolution depending on the reported target bit rate. To do this, replace the
__on_target_bitrate_changedhandler with the following:int curTargetBitrate, diffTargetBitrate, lastTargetBitrate = 500; int lowBitrateL1 = 400, highBitrateL1 = 800; int lowBitrateL2 = 1130 , highBitrateL2 = 2260, highBitrateL3 = 4160; int curResolutionLevel = 1 , lastResolutionLevel = 1; static void __on_target_bitrate_changed(const char *channel, uint32_t target_bps) { // Adjust the sending code rate in real time // The current code rate is graded according to 100 K, rounded down curTargetBitrate = target_bps/100000 * 100; diffTargetBitrate = abs(curTargetBitrate - lastTargetBitrate); // If the difference between the detected bandwidth and the set // encoding rate is more than 100K, adjust the encoding parameters if ( (diffTargetBitrate >= 100 ) && (lowBitrateL1 < curTargetBitrate < highBitrateL3) ) { if (lowBitrateL1 < curTargetBitrate < highBitrateL1) { curResolutionLevel = 1; } else if (lowBitrateL2 < curTargetBitrate < highBitrateL2) { curResolutionLevel = 2; } else { curResolutionLevel = 3; } if (curResolutionLevel != lastResolutionLevel) { // Adjust encoding resolution // setEncodeResolution(curResolutionLevel); } // Set the encoding rate // setEncodeBitrate(curTargetBitrate); lastResolutionLevel = curResolutionLevel; lastTargetBitrate = curTargetBitrate; } } -
Manage audio and video streaming status
To pause or resume sending local audio and video streams when a key is pressed, add the following code to
main()afterwhile (!g_app.b_stop_flag) {:char c; // Input character int i = kbhit(); // Check if a key was pressed if (i != 0) // A key was pressed { c = fgetc(stdin); // Get the input character if (c == 'a') { // mute/unmute audio is_audio_muted = !is_audio_muted; agora_rtc_mute_local_audio(g_app.conn_id, is_audio_muted); if (is_audio_muted) printf("\nAudio muted\n"); else printf("\nAudio unmuted\n"); } else if (c == 'v') { // mute/unmute audio is_video_muted = !is_video_muted; agora_rtc_mute_local_video(g_app.conn_id, is_video_muted); if (is_video_muted) printf("\nVideo muted\n"); else printf("\nVideo unmuted\n"); } else { printf("\nPress <a> to mute/unmute audio \nPress <v> to mute/unmute video\n"); } i = 0; }To pause and resume playing remote audio or video streams use
agora_rtc_mute_local_audioandagora_rtc_mute_local_video. -
Handle muting and unmuting notifications for remote streams
When a remote user mutes or unmutes their audio or video stream, you receive notification of these changes. In this example, you write log messages when you receive the
on_user_mute_audioandon_user_mute_videocallbacks. Add the following code tomain():static void __on_user_mute_audio(connection_id_t conn_id, uint32_t uid, bool muted) { LOGI("[conn-%u] audio: uid=%u muted=%d", conn_id, uid, muted); } static void __on_user_mute_video(connection_id_t conn_id, uint32_t uid, bool muted) { LOGI("[conn-%u] video: uid=%u muted=%d", conn_id, uid, muted); } -
Request and send key frames
When you call
agora_rtc_send_video_datato send a video frame, you specify if this frame is a key frame by setting theis_key_frameparameter.When a sender does not send a key frame for a long time, or the key frame is lost or damaged during transmission, IoT SDK triggers the
on_key_frame_gen_reqcallback to advise the sender to generate a key frame. In this example, you log a message when you receive theon_key_frame_gen_reqcallback. Add the following code tomain():static void __on_key_frame_gen_req(connection_id_t conn_id, uint32_t uid, video_stream_type_e stream_type) { LOGW("[conn-%u] Frame loss detected. Set a flag for the encoder to generate a key frame", conn_id); }If the receiver encounters an error when decoding frames, it calls the
agora_rtc_request_video_key_framemethod to request a remote user to generate a fresh key frame.
Test your implementation
To ensure that you have implemented channel quality features into your app:
-
Generate a temporary token in Agora Console .
-
In your browser, navigate to the Agora Muting web demo and update App ID, Channel, and Token with the values for your temporary token, then click Join.
-
Compile the sample project
In the terminal, execute the following commands:
cd <project-root>/agora_rtsa_sdk/example ./build-x86_64.sh -
Launch the sample app:
-
In the terminal, navigate to the output folder:
cd out/x86_64 -
Launch the compiled app
./hello_rtsa --app-id <your-app-id> --channel-id <your-channel-name> --token <authentication-token>Remember to insert your app ID, channel name and token in the command.
-
-
When the app starts, it does the following:
- Sets the log file location and logging level according to your preference
- Sets the audio codec, sampling rate and the number of channels
- Sets bandwidth estimation parameters
- Starts streaming audio and video
- Listens for notification of changes in network bandwidth to adjust video resolution and bit rate accordingly
- Listens for key frame request to notify the encoder to send a key frame
-
Mute and unmute local audio and video
-
Press
aon the keyboard; the audio is muted in the web demo app. Pressaagain; the audio is unmuted. -
Press
von the keyboard; the video stops playing in the web demo app. Pressvagain; the video resumes playing.
-
-
Press Mute Audio and Mute Video buttons in the web demo app. You see terminal messages informing you of these events.
Reference
This section contains additional information that completes the content on this page, or points you to documentation that explains other aspects to this product.
Recommended sampling rates and data sizes for audio
The table below shows the recommended sampling rates and corresponding data sizes for Opus, G722, and G711 audio data.
| Audio format | Sampling rate (Hz) | Audio data size (Byte) |
|---|---|---|
| G711 | 8000 | 320 |
| G722 | 16000 | 640 |
| OPUS | 16000 | 640 |
| OPUS | 48000 | 1920 |
Definition levels of a webcam
| Gear | Resolution | Frame rate | Bit rate range (kbps) |
|---|---|---|---|
| SD | L1: 640*360 | 15 | 400 - 800 |
| HD | L2: 1280*720 | 15 | 1130 - 2260 |
| Ultra HD | L3: 1920*1080 | 15 | 1130 - 4160 |
