Skip to main content
Version: Dev (main)

Several API Instances in One Process

Source: qar-streaming-c/examples/api_instances_in_one_process.c. Which mode to use: Choosing an Onboarding Mode. API details: Onboarding and Sessions.

Several API Instances in One Process

What You Will Learn​

  • Why a second API instance in the same process needs no invite and no approval of its own
  • That two local onboards with the same app tag return two handles to one session, under one peer id
  • That a different tag is a different peer with a session of its own
  • When the shared session ends

When to Use It​

Your process uses QAROS from more than one place: an application and a plug-in it hosts, native code and a C# or Unity layer, two libraries that each wrap the C API. Each place creates its own QarRuntime and onboards as a local app (Local App Onboarding). The runtime library notices that the process already holds a session with that hub and tag and hands out a new handle to it. One approval, one enrollment, one peer. Windows and Linux, like local enrollment.

A process you start is not covered: it is another executable, so it joins with a co-process invite (Co-Process Onboarding).

Build and Run​

 cmake --build --preset x64-windows-debug --target api_instances_in_one_process
 ./build/x64-windows/Debug/api_instances_in_one_process.exe

The first run asks for the approval once, as any local app does.

Step 1: Each Instance Onboards as a Local App​

Both "instances" run the same function with their own runtime, as two libraries in one process would:

 /* What every API user in the process does on its own: create a runtime and
  * onboard as a local app with its tag. Returns false and logs the reason on
  * failure; on success the caller owns both handles. */
 static bool
  const char* who, const char* app_tag, QarRuntime** out_runtime, QarSession** out_session
 )
 {
  QarResult created = qar_runtime_create(&runtime_init, out_runtime);
  if(qar_result_is_error(created))
  {
  log_result("qar_runtime_create", created);
  return false;
  }
 
  init.presentation.display_name = "API Instances Example";
  tag.app_tag = app_tag;
  init.header.next = &local.header;
  local.header.next = &tag.header;
 
  QarOnboardingId onboarding_id = qar_onboarding_id_default(); /* stays zero */
  *out_runtime, &init, on_progress, (void*)who, NULL, &onboarding_id, out_session
  );
  if(qar_result_is_error(joined))
  {
  log_result(who, joined);
  qar_runtime_handle_destroy(*out_runtime);
  *out_runtime = NULL;
  return false;
  }
  return true;
 }

Step 2: One Session, One Peer​

  QarSessionId session_id_a, session_id_b, session_id_c;
  QarPeerId peer_a, peer_b, peer_c;
  if(read_ids(session_a, &session_id_a, &peer_a)
  && read_ids(session_b, &session_id_b, &peer_b)
  && read_ids(session_c, &session_id_c, &peer_c))
  {
  /* Same tag: two handles, one session, one peer. */
  const bool shared =
  same_id(session_id_a.data, session_id_b.data) && same_id(peer_a.data, peer_b.data);
  /* Another tag: a peer of its own. */
  const bool separate = !same_id(peer_a.data, peer_c.data);
  printf(
  "Same tag shares one peer: %s. Other tag is another peer: %s.\n",
  shared ? "yes" : "NO",
  separate ? "yes" : "NO"
  );
  exit_code = shared && separate ? 0 : 1;
  }

What the runtime does for the second onboard with the same hub and tag:

  • It sends nothing to the hub: no enrollment, no prompt, no new certificate. The progress callback reports one INFO message saying the session is shared.
  • The returned onboarding id is zero, as for every local app.
  • The first onboard's options win: the second one's presentation and QarGraphicsDeviceId are not applied. Agree on them inside your process, or let the instance that starts first decide.
  • Two onboards running at the same time share one enrollment: the second waits for the first and then shares its session (or fails with QAR_STATUS_TIMEOUT when the first does not finish within the approval timeout plus two minutes).

A different tag is a different peer: the hub remembers a peer id per approval and tag, so the third onboard below enrolls on its own and gets a session of its own.

Step 3: Release in Any Order​

  /* Order does not matter. Releasing A (whose runtime enrolled the session)
  * first leaves B's handle working; the shared session is left with B. */
  if(session_a != NULL)
  {
  }
  if(runtime_a != NULL)
  {
  }
  if(session_b != NULL)
  {
  /* Still joined: B shares the session A created. */
  if(!example_print_own_peer_id("Plug-in (native code already left)", session_b))
  {
  exit_code = 1;
  }
  qar_session_handle_destroy(session_b); /* last handle: session left */
  }
  if(runtime_b != NULL)
  {
  }
  if(session_c != NULL)
  {
  }
  if(runtime_c != NULL)
  {
  }
  • Each handle is released on its own with qar_session_handle_destroy. The session is left when the last handle to it is released, whichever instance created it; destroying the runtime that enrolled it first is fine.
  • If the hub refuses to renew the session's certificate, every handle sees the same session end; it stays shared until its last handle goes.
  • The next onboard after the last handle enrolls again and, with no other live process holding it, gets the same remembered peer id.

What Is Stored​

Nothing, as for any local app: the key lives in this process's memory with a 30-minute certificate the runtime renews; the hub keeps the approval and the remembered peer id per tag.


Generated via doxygen2docusaurus 2.2.1 by Doxygen 1.9.8.