SDK Version 2.0.1
AnsuR Technologies ASMIRA SDK
Loading...
Searching...
No Matches
example_basic.cpp

Basic usage of ASMIRA SDK.

Basic usage of ASMIRA SDK

/****************************************************************
* Name: example_basic.cpp
* Purpose: Smallest useful ASMIRA SDK client: connect, join a
* room, and receive decoded video frames.
* Copyright: AnsuR Technologies
****************************************************************/
#include <asmirasdk.h>
#include <chrono>
#include <thread>
struct context_t {
asdk::sdk_t* sdk = nullptr;
bool roomListReceived = false;
bool roomJoined = false;
uint32_t framesReceived = 0;
};
int main()
{
context_t context;
// Loads asmirasdk.dll / libasmirasdk.so and checks the version.
context.sdk = asdk::Initialize();
if (!context.sdk) {
puts("Failed to initialize the ASMIRA SDK");
return 1;
}
// Install the callbacks before connecting. Each one runs on an SDK thread,
// so keep the work short and never block.
context.sdk->SetServerStateCallback([](const asdk::ServerState serverState,
void* opaque) {
reinterpret_cast<context_t*>(opaque)->serverState = serverState;
}, &context);
// The response is a bit set, so test the bit rather than comparing.
context.sdk->SetAuthenticateResponseCallback([](const asdk::AuthenticationResponse response,
void*) {
puts("Authenticated");
else
printf("Login refused (0x%x)\n", response);
});
// Announced once per room after RequestRoomListSubscribe.
context.sdk->SetRoomAnnounceCallback([](const char* roomName, const uint32_t roomID,
const asdk::RoomStateValue state,
const asdk::RoomFlagValue flags, void* opaque) {
printf("Room \"%s\" (id %u)%s%s\n", roomName, roomID,
(state & asdk::RoomStatePassword) ? " [password]" : "",
(flags & asdk::RoomFlagChatViewers) ? " [chat]" : "");
reinterpret_cast<context_t*>(opaque)->roomListReceived = true;
}, &context);
// Answers SendRequest and SendRequestStr. optionalData carries any payload
// the request produced and is valid for the duration of this call only.
context.sdk->SetRoomResponseCallback([](const asdk::Request request,
const asdk::RoomResponse response,
const void*, const uint32_t, void* opaque) {
if (request == asdk::RequestRoomJoin)
reinterpret_cast<context_t*>(opaque)->roomJoined =
}, &context);
// Decoded RGB video. frame and frame->data are only valid for the duration
// of this call, so copy anything that has to outlive it.
context.sdk->SetFrameCallback([](const asdk::frame_t* frame, void* opaque) {
context_t* context = reinterpret_cast<context_t*>(opaque);
if (++context->framesReceived == 1)
printf("First frame: %dx%d\n", frame->width, frame->height);
}, &context);
// Use ConnectSecured with a certificate from RequestCertificate for TLS.
if (context.sdk->Connect("127.0.0.1", 30100, 5000, true, "test", "password",
"Basic Example") != asdk::StatusOK) {
printf("Connect failed: %s\n", context.sdk->errorStr);
context.sdk->Unload();
return 1;
}
// Connecting and joining are asynchronous: ask, then wait for the callback
// to report the result.
bool subscribed = false;
bool joinRequested = false;
while (asdk::TimeSpentSteady(start) < 30000) {
if (context.serverState == asdk::ServerStateAuthenticateError) {
puts("Authentication failed");
break;
}
if (context.serverState == asdk::ServerStateConnectedAndAuthenticated) {
if (!subscribed)
subscribed = context.sdk->SendRequest(asdk::RequestRoomListSubscribe) ==
else if (context.roomListReceived && !joinRequested)
joinRequested = context.sdk->SendRequestStr(asdk::RequestRoomJoin, "test") ==
}
if (context.roomJoined && context.framesReceived > 0)
break;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
printf("Received %u frames\n", context.framesReceived);
context.sdk->Disconnect();
context.sdk->Unload();
return 0;
}
AuthenticationResponse
Outcome of a login attempt, reported by AuthenticateResponseFunc.
Definition asmirasdk.h:194
ServerState
State of the connection to the server, reported by ServerStateFunc.
Definition asmirasdk.h:179
@ UserCapabilityViewer
Definition asmirasdk.h:344
@ AuthenticationResponseSuccess
Definition asmirasdk.h:196
@ ServerStateAuthenticateError
Definition asmirasdk.h:184
@ ServerStateConnectedAndAuthenticated
Definition asmirasdk.h:183
@ ServerStateUndef
Definition asmirasdk.h:180
@ DeviceCapabilityBasic
Common baseline: H.265 video, Speex audio, microphone, WebP photo mode, ROI, chat and a display.
Definition asmirasdk.h:386
sdk_t * Initialize()
Loads the SDK shared library and creates an SDK handle.
Definition asmirasdk.h:1745
@ StatusOK
Definition asmirasdk.h:161
Request
Request sent with sdk_t::SendRequest or sdk_t::SendRequestStr.
Definition asmirasdk.h:213
uint32_t RoomStateValue
Definition asmirasdk.h:267
uint32_t RoomFlagValue
Definition asmirasdk.h:252
RoomResponse
Server answer to a Request, reported by RoomResponseFunc.
Definition asmirasdk.h:225
@ RequestRoomJoin
Definition asmirasdk.h:217
@ RequestRoomListSubscribe
Definition asmirasdk.h:215
@ RoomStatePassword
Definition asmirasdk.h:262
@ RoomFlagChatViewers
Definition asmirasdk.h:244
@ RoomResponseSuccess
Definition asmirasdk.h:227
TimePointSteady TimeNowSteady()
Current steady clock reading, unaffected by clock adjustments.
Definition asmirasdk.h:769
long long TimeSpentSteady(const TimePointSteady &time)
Milliseconds elapsed since a TimeNowSteady() reading.
Definition asmirasdk.h:776
std::chrono::steady_clock::time_point TimePointSteady
Definition asmirasdk.h:765
A decoded video or photo frame, delivered by FrameFunc.
Definition asmirasdk.h:459
int32_t height
Frame resolution height in pixels.
Definition asmirasdk.h:461
int32_t width
Frame resolution width in pixels.
Definition asmirasdk.h:460
SDK Core.
Definition asmirasdk.h:943