C SDK Integration
Integrate the TiRTC C SDK into a device-side project, initialize the runtime, and choose how the SDK outputs logs for your device environment.
When you finish this page, continue with Connection. After the connection succeeds, implement audio and video, voice talkback, command messages, or stream messages.
Get the binary library
Start by getting the matching C SDK from Download C SDK.
After you get the archive, do not copy only include/ and lib/. Read the README.md, manifest/, and any build contract files declared in the archive before you start. For MCU / RTOS / mini prebuilt packages, these files and the headers together form the runtime contract; if you follow only the generic initialization steps on this page, the build may pass but the program can crash at runtime because the RTOS, PSRAM, thread stack, socket, or board network prerequisites do not match.
Library file names differ between platforms, but the public C API entry is the same. For the Linux x86_64 standard package, the extracted layout looks like this:
tirtc-c-sdk/
|-- include/
| `-- tirtc/
| |-- basedef.h
| `-- tiRTC.h
`-- lib/
|-- libTiRTC.a
`-- libTiRTC.soBusiness code only needs to include include/tirtc/tiRTC.h. basedef.h is a public header that tiRTC.h depends on, so keep it together with tiRTC.h in include/tirtc/.
Reference the SDK in your project
Add the SDK include/ directory to the header search path, then link the TiRTC entry library from the package.
Business code should include the header like this:
#include <tirtc/tiRTC.h>The Linux standard package supports two integration modes:
- Static: link
lib/libTiRTC.aat build time. - Dynamic: link
lib/libTiRTC.soat build time, and make sure the program can find that.soat runtime, for example in a system library directory, a library directory your program agrees on, or throughLD_LIBRARY_PATH/ rpath.
Other device platform packages usually provide a static library; link it statically.
Initialize the SDK
TiRtcInit() initializes the SDK runtime. The function performs no network interaction, so you can call it before the device has internet access.
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <tirtc/tiRTC.h>
static volatile sig_atomic_t g_should_exit = 0;
static void on_signal(int signo)
{
(void)signo;
g_should_exit = 1;
}
int main(int argc, char **argv)
{
signal(SIGINT, on_signal);
signal(SIGTERM, on_signal);
if (TiRtcInit() != 0) {
return 1;
}
while (!g_should_exit) {
sleep(1);
}
TiRtcUninit();
return 0;
}Configure SDK log output
Whether to keep SDK logs is up to the application and does not affect SDK startup. Decide during integration how you will obtain logs. During development and integration you can watch console output directly; once a device is in the field it is usually inconvenient to attach a debug interface, so keep logs that have a bounded size and can be retrieved from the device. That way you can restore the scene after a problem instead of attaching a debug interface and reproducing it.
Configure the log level and callback after TiRtcInit() and before TiRtcStart(). For upgrade requirements from older versions, see C SDK Release Notes.
| Need | Configuration |
|---|---|
| Watch logs in real time during development | Use the platform default console output and adjust verbosity with TiRtcLogSetLevel() |
| Save to a file, write to a storage card, or forward logs | Use TiRtcLogSetCallback() to connect the application log system, and let the application save or forward them |
Customize log destination with a callback
Use TiRtcLogSetCallback() to obtain SDK logs and connect them to your application's output and storage flow:
static void on_tirtc_log(const char *log, uint32_t length)
{
app_log_enqueue_copy(log, length); // implemented by the application; copy before returning
}
TiRtcInit();
TiRtcLogSetCallback(on_tirtc_log);
TiRtcLogSetLevel(4);app_log_enqueue_copy() in the example is not a TiRTC API. The content that log points to is valid only during this callback and is not guaranteed to end with NUL; if you need to keep it, copy length bytes into your own buffer before the callback returns. The callback runs synchronously on the thread that produces the log and may arrive from different threads, so the handler must be thread-safe and return quickly. Operations that may block, such as writing an SD card or flash or uploading, should be handed to application tasks.
The application can output queued logs to the platform log system, or write them to an SD card, flash, a ring buffer, or an upload channel. When you keep logs yourself, also set capacity limits, rolling or dropping policies for the queue and log files, and handle unavailable or failing storage so that logs do not grow without bound.
Setting a callback does not turn off the SDK default console output; the SDK passes log content to the application at the same time. Pass TiRtcLogSetCallback(NULL) to stop the callback.
On devices that use printf, stdout, or stderr as the default console, whether logs finally go out through UART depends on the chip SDK and firmware configuration; other devices depend on their platform console adaptation. When you need to control the log destination precisely, connect your own log chain through the callback.
After configuration, confirm on the first SDK start that logs appear on the console or in the callback. If files saved by the application stay empty, first check queue consumption, storage mount, parent directory, and write permissions; if the callback receives nothing, confirm that the application has registered it and that later code does not clear it. To learn how to locate and submit logs after a problem, see Diagnose integration problems.
Configure the RTOS thread priority
C SDK 2.6.0 provides TIRTC_OPT_SDK_TASK_PRIORITY for RTOS packages that support it. To adjust it, set the option before TiRtcInit() creates the internal threads; choose the priority value according to the target platform and the application task layout:
static int configure_sdk_task_priority(int priority) {
return TiRtcSetOption(TIRTC_OPT_SDK_TASK_PRIORITY,
&priority, sizeof(priority));
}Check the return value; packages that do not support this option return TIRTC_E_INVALID_PARAMETER. Typical integrations do not need to set it.
Add network initialization for 4G devices
The SDK cannot detect the current access restrictions of the device network, so you must provide the relevant network information before TiRtcStart(). The SDK adapts to the network based on it.
Some 4G private APN cards restrict reachable addresses with a domain or IP allowlist. Such networks are also called 4G allowlist networks; this documentation calls them restricted networks.
Set TIRTC_OPT_RESTRICTED_NETWORK through TiRtcSetOption to tell the SDK about this restriction. Set it to 1 when the device uses a restricted IoT card directly, or when the device reaches the internet through Wi-Fi connected to a router that uses such a card; set it to 0 for an ordinary 4G network. If you are not sure about the current access restrictions, confirm with the SIM card operator or solution provider.
When the device uses a 4G network directly, also set TIRTC_OPT_NETWORK_TYPE and TIRTC_OPT_ICCID. TIRTC_OPT_NETWORK_TYPE describes only the network that the device itself uses directly. When the device connects through Wi-Fi, set it to TIRTC_NETCONN_WIFI; do not set it to TIRTC_NETCONN_4G even if the upstream router uses 4G.
TIRTC_OPT_ICCID carries only the ICCID of the 4G SIM card that the device uses directly. When the device goes online through Wi-Fi, do not fill in the ICCID of the SIM card inside the upstream router.
| Device network scenario | TIRTC_OPT_NETWORK_TYPE | TIRTC_OPT_ICCID | TIRTC_OPT_RESTRICTED_NETWORK |
|---|---|---|---|
| Ordinary 4G network | TIRTC_NETCONN_4G | ICCID of the current SIM card, required | Set explicitly to 0 |
| Restricted network, device goes online directly through 4G | TIRTC_NETCONN_4G | ICCID of the current SIM card, required | Set explicitly to 1 |
| Restricted network, device connects through Wi-Fi | TIRTC_NETCONN_WIFI | Do not provide the ICCID of the SIM card in the router | Set to 1 |
The example below applies when the device uses a 4G SIM card directly:
static int set_4g_network_options(const char *iccid, int restricted_network)
{
int network_type = TIRTC_NETCONN_4G;
int code;
if (iccid == NULL || iccid[0] == '\0') {
return TIRTC_E_INVALID_PARAMETER;
}
if (restricted_network != 0 && restricted_network != 1) {
return TIRTC_E_INVALID_PARAMETER;
}
code = TiRtcSetOption(TIRTC_OPT_NETWORK_TYPE, &network_type,
(uint32_t)sizeof(network_type));
if (code != 0) {
return code;
}
code = TiRtcSetOption(TIRTC_OPT_ICCID, iccid,
(uint32_t)strlen(iccid));
if (code != 0) {
return code;
}
return TiRtcSetOption(TIRTC_OPT_RESTRICTED_NETWORK,
&restricted_network,
(uint32_t)sizeof(restricted_network));
}The call order is: TiRtcInit() → set the three network options above → TiRtcStart().
Next step
After you reference and initialize the C SDK and decide how logs go out, continue with Connection: set the device secret device_secret_key and the startup identifier client_id, then complete TiRtcStart() startup and client connection handling.