Skip to main content
This page covers creating and destroying SDK instances, joining and leaving channels, and every callback registration function. All functions are thread-safe.

Logging

rtc_set_log_level

Sets the global log level. It applies to the whole process; we recommend calling it before rtc_create. It only affects local output (printed to stderr) and is independent of log upload below.

rtc_set_log_upload

Sets whether SDK logs are uploaded to the server (since 0.4.0). It applies to the whole process. Upload is on by default and covers RTC_LOG_INFO and above. When enabled, each time the instance joins a channel, the SDK uploads that channel’s logs (tagged with the channel name and uid) to the server-side log service in batches in the background. Technical support can then look them up by channel and user, so you no longer need to collect logs from the on-site server when troubleshooting. Failed uploads are discarded without retry and do not affect audio and video.
  • The switch only applies to channels joined afterwards; the level takes effect immediately, including for instances already in a channel
  • If the deployment has no log service or it is unreachable, the SDK prints one upload-failure warning per minute locally; call rtc_set_log_upload(0, RTC_LOG_INFO) to turn upload off

Language

rtc_set_language

Sets the language (since 0.1.0). It applies to the whole process and can be called at any time; requests sent after the call use the new language. It determines the language of the reason text for server business errors (error code ≥1000), that is, what ends up in msg_buf when rtc_get_last_error returns a server error. Reasons for SDK errors (180xxx) are always in English and are not affected. When it isn’t called, the system language is used: the first non-empty value among the environment variables LC_ALL, LC_MESSAGES, and LANG. When the system language is C / POSIX or none of them is set, the server replies in Chinese.

Instance lifecycle

rtc_create

Creates an SDK instance and returns the instance handle. Every channel-related function afterward takes this handle. One handle corresponds to one channel connection. To connect to multiple channels at the same time, create multiple instances.

rtc_destroy

Destroys the instance and releases its resources. Starting with 0.0.11, rtc_destroy waits for all running callbacks to return before it returns. After it returns, no further callbacks fire for this instance, and you can safely free the context you passed to the callbacks.
  • You may call rtc_destroy from within one of this instance’s callbacks (including the disconnect callback). In that case it only waits for callbacks on other threads—don’t free the current callback’s context until that callback returns
  • If a callback does slow work (decoding, writing files), rtc_destroy waits correspondingly longer
  • Neither rtc_leave_channel nor replacing a callback with rtc_set_*_callback waits for running callbacks, so neither is a safe point to free context
0.0.10 and earlier don’t provide these guarantees: callbacks may still be running when rtc_destroy returns, and calling rtc_destroy from the disconnect callback deadlocks. With these older versions, don’t free context right after rtc_destroy, and don’t destroy the instance in the disconnect callback; we recommend upgrading to 0.0.11.
You must call rtc_destroy; otherwise the instance’s resources are not released. The handle can’t be used after it’s destroyed.

Joining and leaving a channel

rtc_join_channel

Joins a channel asynchronously and returns immediately. The actual connection result is reported through the connection state callback (rtc_set_connection_callback). Returns

rtc_join_channel_sync

Joins a channel synchronously, blocking until the connection succeeds, fails, or times out. Returns

rtc_get_last_error

Gets the error details of this instance’s most recent failed call (since 0.0.9). Call it after join, subscribe, publish, or similar functions return anything other than RTC_OK. Since 0.1.0, returning RTC_INVALID_PARAM / RTC_NOT_CONNECTED also records an error (180031 / 180001). Returns: The error code; 0 means nothing was recorded. If the handle itself is invalid there is no instance to record on, so 0 is returned; functions that take only a track handle, such as rtc_write_sample, don’t record errors.
Each instance keeps only the most recent error. When multiple threads call functions on the same instance concurrently, a later failure overwrites an earlier one.

rtc_leave_channel

Leaves the channel. The instance stays valid after leaving and can join again. When you’re done with it for good, you still need to call rtc_destroy. Returns: RTC_OK / RTC_INVALID_PARAM (invalid handle).

Auto-subscribe

rtc_set_auto_subscribe

Sets whether to automatically subscribe to remote tracks. When on, every already-published and newly published track of the corresponding type in the channel is subscribed automatically, and all data goes through the track data callback.
Must be called before joining the channel. Setting it after joining has no effect on existing tracks.
If a user wants to “hear everyone,” the right approach is rtc_set_auto_subscribe(rtc, 1, 0) to subscribe to everyone’s audio and then mix it yourself. Don’t subscribe to the audio composite stream—it includes your own voice and causes echo.

Device type

rtc_set_device_type

Sets the device type this instance reports when it joins a channel (since 0.3.0). This is the value other members see in rtc_user_info_t.device_type and the device_type in server callbacks. Defaults to 80 (agent joining on the server side) if not called.
Must be called before rtc_join_channel / rtc_join_channel_sync. The device type is reported only once, at join time; setting it after joining has no effect on the current session (automatic reconnection keeps the same session, so it doesn’t change either). To change the type, leave and join again.
It applies per instance: multiple instances in the same process can each set their own. For example, a device gateway that creates one instance per connected device can report each device’s kind (82 for SIP devices, 83 for H.323 devices, 84 for GB28181 cameras, and so on), so your application can tell where each member comes from.

Callback registration

Every callback carries your context through the context parameter; the SDK passes it back unchanged without interpreting it.
context must be a real pointer (or NULL). Don’t cast small integers like 1 or 2 to pointers to use as IDs: the SDK stores it internally as a pointer, and a value below 4096 is treated as an invalid pointer and terminates the process immediately. If you need an ID, pass the address of memory that holds it.
Callbacks run on internal SDK threads. Keep three things in mind:
  1. User, track, channel, custom message, reconnection (connection state 3 / 1) and SFU signaling (layer switch, network quality, active speakers) callbacks fire one at a time in the order the events happen and never run concurrently with each other (since 0.1.1). Media sample callbacks (one thread per track), keyframe request callbacks and disconnection callbacks (rtc_set_disconnected_callback and connection state 2) can run concurrently with them, so protect shared state yourself
  2. Don’t do slow work in callbacks: event callbacks fire one at a time, so a blocked callback delays every event after it; queue slow processing and hand it to your own threads
  3. Pointers in callback parameters (uid, track_info, data, props, content_json, speakers, etc.) are only valid during the callback; if you need to keep them (including handing them to another thread for async processing), copy them before the callback returns

rtc_set_connection_callback

Connection state changes. state: 0=connecting, 1=connected, 2=disconnected, 3=reconnecting.

rtc_set_disconnected_callback

Fires once when the instance finally leaves the channel (at the same moment as state=2 in the connection state callback, and before it), with the disconnect reason (since 0.0.9).
Use it to decide whether to rejoin automatically: don’t rejoin automatically when removed from the channel (KICKED), replaced by another session with the same uid (REPLACE), or when the channel is destroyed (DESTROY); in cases such as a heartbeat timeout (TIMEOUT), you can rejoin with a newly issued token.

rtc_set_user_event_callback

Users joining and leaving the channel. event_type: 0=joined, 1=left.

rtc_set_track_event_callback

Remote tracks added, updated, or removed. event_type: 0=added, 1=updated, 2=removed. With manual subscription, use this callback to discover tracks you can subscribe to.

rtc_set_track_sample_callback

Media data for subscribed tracks. Every subscribed track (including composite streams) comes out of this single callback; use user_info->uid + track_info->track_id to tell the sources apart. data is a complete encoded frame (one frame for video, one encoded packet for audio). The pointer is only valid during the callback:

rtc_set_custom_msg_callback

In-channel custom messages; see Custom messages.

SeaStart-only callbacks

The following three callbacks only fire when the channel uses the SeaStart engine; with other engines they’re never called. See SeaStart advanced features.

Typical call order