Cocos2d-x SDK Guide
Cocos2d-x C++ binding, public interface and supported scenarios.
The Cocos2d-x SDK ships as a C++ binding library: the public headers define member names and signatures aligned with the Agora C++ API, the implementation forwards through an IRtcEngineCore seam to the ni-meet C++ core, and media capability is injected by the host application.
#include <nimeet/cocos2dx/rtc_engine.h> // public C++ interface
#include <nimeet/cocos2dx/core_seam.h> // core seam (implemented by the host)Prerequisites
- Cocos2d-x 3.17+ (C++17); the Android NDK / Xcode come from the host project.
- The ni-meet C++ core already present in the host application (the same
nimeet::RtcEngineSessionas the Windows / macOS SDKs),
plus a thin bridge that connects it to nimeet::cocos2dx::IRtcEngineCore (1:1 forwarding, no media logic).
- An App ID and a token service (see Enable Services and Get Credentials).
Installation
g++ -std=c++17 -I sdk/cocos2dx/include -I sdk/cocos2dx/src \
sdk/cocos2dx/tests/adapter_test.cpp sdk/cocos2dx/src/rtc_engine_adapter.cpp -o adapter_testAdd sdk/cocos2dx/include to the header search path and sdk/cocos2dx/src/*.cpp to the source list; no package is published today (no vcpkg or Cocos extension artifact) — it ships as a source directory only.
Get a token
As on every platform: your backend issues the token with POST /v1/tokens. The binding's joinChannel(token, channelId, uid) takes the token string directly; for the semantics see AccessToken2 Semantics and Privileges.
Join a channel
#include "nimeet/cocos2dx/rtc_engine.h"
#include "nimeet/cocos2dx/core_seam.h"
class AppHandler : public nimeet::cocos2dx::IRtcEngineEventHandler
{
public:
void onJoinChannelSuccess(const char* channel, nimeet::cocos2dx::uid_t uid, int elapsed) override { /* update UI */ }
void onUserJoined(nimeet::cocos2dx::uid_t uid, int elapsed) override { /* attach remote view */ }
void onError(int err, const char* msg) override { /* err is the documented error code */ }
};
nimeet::cocos2dx::RtcEngineAdapter engine(core); // core implements nimeet::cocos2dx::IRtcEngineCore
AppHandler handler;
nimeet::cocos2dx::RtcEngineContext context;
context.appId = appId;
context.eventHandler = &handler;
engine.initialize(context);
engine.joinChannel(token, "channel-a", "info", 1234);Only after initialize may you call joinChannel / leaveChannel: otherwise the documented error code -7 (ERR_NOT_INITIALIZED) is returned. An initialize call without an App ID returns -2 (ERR_INVALID_ARGUMENT).
Publishing and subscribing
The binding exposes only the documented switch semantics; it does not manage tracks:
- Audio:
enableAudio/disableAudio/enableLocalAudio/muteLocalAudioStream/muteRemoteAudioStream/muteAllRemoteAudioStreams; - Video:
enableVideo/disableVideo/enableLocalVideo/muteLocalVideoStream/muteRemoteVideoStream/muteAllRemoteVideoStreams; - Preview and rendering:
startPreview()/startPreview(VIDEO_SOURCE_TYPE)/stopPreview(…)/switchCamera/
setupLocalVideo(VideoCanvas) / setupRemoteVideo(VideoCanvas) (uid == 0 in setupRemoteVideo returns -2);
- Encoding:
setVideoEncoderConfiguration(a non-positive width or height returns-2).
Events
Callbacks live on IRtcEngineEventHandler; its 14 members match the frozen C++ reference version signature by signature:
onJoinChannelSuccess, onRejoinChannelSuccess, onLeaveChannel, onUserJoined, onUserOffline, onConnectionStateChanged, onError, onAudioVolumeIndication, onLocalAudioStateChanged, onRemoteAudioStateChanged, onLocalVideoStateChanged, onRemoteVideoStateChanged, onRtcStats, onRequestToken.
initialize hands the eventHandler to the core (IRtcEngineCore::SetEventHandler); the core pushes events and the binding does not interpret their content.
Error handling
Members return the documented Agora C++ error codes (0 success; negative failure), and the binding never rewrites a failure into a success:
| code | Meaning | Trigger |
|---|---|---|
0 | Success | — |
-1 | General error | Returned by the core |
-2 | Invalid argument | Missing App ID, empty channel name, empty token, a remote view with uid == 0, non-positive encoding size, out-of-range volume |
-3 | Not ready yet | — |
-4 | Not supported | Returned by the core seam and passed through unchanged by the binding |
-7 | Not initialized | Calling an engine-backed member before initialize |
-8 | Invalid state | Returned by the core |
The projection has no error-code enum; notSupported is recorded as source: "cli" (see the SDK interface definition)
Troubleshooting
| Symptom | Cause | Handling |
|---|---|---|
The linker reports undefined reference | Only the headers were added and sdk/cocos2dx/src/rtc_engine_adapter.cpp was not compiled | Add src/*.cpp to the build |
Every call returns -7 | initialize never succeeded | Call initialize(context) and check the core seam's return value |
| No picture | Only setVideoEncoderConfiguration was called, without setupLocalVideo / startPreview | Follow the documented order: preview → bind the canvas |
| A review says "aligned with the Agora Cocos2d-x SDK" | Agora has no SDK for this platform | The correct statement is "the binding interface definition aligns with the documented subset of C++ members" |
Next steps
- Full interfaces and overloads: API reference for this platform
- Error codes and troubleshooting: Error code table, FAQ
- Issue tokens on your server: Token authentication
Platform information
| Item | value |
|---|---|
| Platform | Cocos2d-x(C++ binding) |
| Language | C++ |
| SDK package | nimeet_rtc_cocos2dx |
| Interface version | 4.7.0 |
| Compatible Agora API | Agora cocos2dx API |
The public interfaces are aligned with the Agora cocos2dx API; installation and runtime requirements are listed under Prerequisites above.
Availability
This platform's SDK: Generally available. The examples use the public API; runtime capability follows the published SDK package.
join-channel flow
The method names, parameter types and return values match the API reference for the same platform. Where a method has several overloads, the most common one is shown here; see the API reference for all overloads.
| Step | Interface | Method | Overload count |
|---|---|---|---|
| enable video and local preview | NimeetCocos2dxRtcEngine | enableVideo | 1 |
| Create instance / initialize | NimeetCocos2dxRtcEngine | initialize | 1 |
| Join a channel | NimeetCocos2dxRtcEngine | joinChannel | 2 |
| Leave a channel | NimeetCocos2dxRtcEngine | leaveChannel | 2 |
| subscribe to remote media | NimeetCocos2dxRtcEngine | muteRemoteAudioStream | 1 |
| Renew the Token | NimeetCocos2dxRtcEngine | renewToken | 1 |
| Channel scenarios | NimeetCocos2dxRtcEngine | setChannelProfile | 1 |
| switch roles | NimeetCocos2dxRtcEngine | setClientRole | 2 |
| bind the local video | NimeetCocos2dxRtcEngine | setupLocalVideo | 1 |
enableVideo(): int
initialize(context: const RtcEngineContext&): int
joinChannel(token: const char*, channelId: const char*, info: const char*, uid: uid_t): int
leaveChannel(): int
muteRemoteAudioStream(uid: uid_t, mute: bool): int
renewToken(token: const char*): int
setChannelProfile(profile: CHANNEL_PROFILE_TYPE): int
setClientRole(role: CLIENT_ROLE_TYPE): int
setupLocalVideo(canvas: const VideoCanvas&): intEvents and callbacks
Callbacks are registered on IRtcEngineEventHandler; naming differs by platform, and the full list is in the API reference for the same platform.
| Callbacks | Signature | Purpose |
|---|---|---|
onConnectionStateChanged | onConnectionStateChanged(state: CONNECTION_STATE_TYPE, reason: CONNECTION_CHANGED_REASON_TYPE): void | join result / connection-state changes:update the UI accordingly and decide whether to reconnect. |
onUserJoined | onUserJoined(uid: uid_t, elapsed: int): void | Remote user joined:allocate the frame container. |
onRemoteVideoStateChanged | onRemoteVideoStateChanged(uid: uid_t, state: REMOTE_VIDEO_STATE, reason: REMOTE_VIDEO_STATE_REASON, elapsed: int): void | Remote video status:it can render once the first frame is decoded. |
onUserOffline | onUserOffline(uid: uid_t, reason: USER_OFFLINE_REASON_TYPE): void | Remote user left:release the container and subscription. |
onRequestToken | onRequestToken(): void | Token expiring soon:call the server to renew, then `renewToken`. |
onError | onError(err: int, msg: const char*): void | Runtime errors:`err` is an Agora error number, route by the error-code table. |
Error codes
This SDK does not expose error-code constants. For platform errors see Error codes; for cross-platform SDK error identifiers see SDK error identifiers.