Skip to content

HarmonyOS API Reference

This page describes the public ArkTS API of the HarmonyOS native SDK tirtc-av for OpenHarmony / HarmonyOS NEXT apps.

If you have not added the SDK or prepared the connection flow, start with Client Integration. Terms such as AppId, remote_id, token, and stream_id follow the glossary.

Object Overview

ObjectPurposeCleanup
TiRtcInitOptionsHolds SDK initialization optionsNo explicit cleanup
TiRtcInitializes and shuts down the shared runtime; formats error codesTiRtc.shutdown()
TiRtcConnConnects, disconnects, sends commands and stream messages, subscribes to media, and requests key framesdispose()
TiRtcAudioOutputPlays one remote audio stream and reads metrics/debug snapshotsdispose()
TiRtcVideoOutputPlays one remote video stream with TiRtcVideoOutputView renderingdispose()
TiRtcAudioInputCaptures local microphone audio and publishes it to the current connection for uplink use cases such as intercomdispose()
TiRtcVideoInputCaptures local camera video, provides preview, and publishes it to the current connection for uplink use casesdispose()
TiRtcLoggingWrites and uploads SDK logsNone

TiRtcInitOptions

Options passed to TiRtc.initialize(...).

FieldDescription
contextcommon.UIAbilityContext required by SDK initialization.
appIdYour TiRTC AppId.
endpointCustom service endpoint. Leave empty to use the runtime default.
consoleLogEnabledMirrors SDK logs to the console when enabled. Default is false.

TiRtc

Typical order: initialize() -> create connection, input, or output objects -> release those objects -> shutdown().

APIDescription
initialize(options: TiRtcInitOptions): Promise<number>Initializes the shared runtime. Returns 0 on success. Repeated initialization is idempotent and does not replace the active configuration.
shutdown(): numberShuts down the shared runtime after every connection, input, and output object has been released. Calling it while already shut down also returns 0.
isInitialized(): booleanReturns whether this process has initialized the runtime.
errorToString(code: number): stringConverts an error code to a stable error token.
formatError(code: number): stringFormats an error code for logs or UI.

TiRtcConn

TiRtcConn is a downlink connection to a remote device or client. Connect first, then send commands, send stream messages, subscribe to media, or attach outputs.

APIDescription
connect(options: TiRtcConnConnectOptions): numberStarts connecting with remoteId and token. A 0 return means the request was accepted; use onStateChanged for lifecycle updates.
disconnect(): numberDisconnects the connection. Calling it while already disconnected is OK.
dispose(): voidPermanently releases the connection and local stream bookkeeping.
sendCommand(options: TiRtcConnCommandOptions): numberSends an application command. HarmonyOS uses the public parameter name commandId.
sendStreamMessage(options: TiRtcConnStreamMessageOptions): numberSends a timestamped stream message.
subscribeAudio(options: TiRtcStreamControlOptions): numberSubscribes to remote audio packets without creating local playback.
unsubscribeAudio(options: TiRtcStreamControlOptions): numberCancels an audio subscription. Attached audio outputs must detach first.
subscribeVideo(options: TiRtcStreamControlOptions): numberSubscribes to remote video packets without creating local playback.
unsubscribeVideo(options: TiRtcStreamControlOptions): numberCancels a video subscription. Attached video outputs must detach first.
requestKeyFrame(options: TiRtcStreamControlOptions): numberRequests a key frame for a remote video stream.
getMetricsSnapshot(): TiRtcConnMetricsResultReturns best-effort connection metrics.

Callbacks:

MemberDescription
onStateChanged(state: TiRtcConnState, errorCode: number) => void
onCommand(command: number, payload: Uint8Array) => void
onStreamMessage(streamId: number, timestampMs: number, payload: Uint8Array) => void

Remote Outputs

Within one connection, a streamId cannot be claimed by audio output, video output, and manual subscription at the same time.

Remote playback does not require camera or microphone permission.

ObjectMain APIs
TiRtcAudioOutputconfigure(...), attach(...), detach(), dispose(), getMetricsSnapshot(), getDebugSnapshot()
TiRtcVideoOutputsetOptions(...), attach(...), detach(), dispose(), getMetricsSnapshot(), getDebugSnapshot()

Render remote video with the SDK ArkUI component:

ts
import { TiRtcVideoFit, TiRtcVideoOutput, TiRtcVideoOutputView } from 'tirtc-av/Index';

@Builder
function RemoteVideo(output: TiRtcVideoOutput) {
  TiRtcVideoOutputView({
    output,
    fit: TiRtcVideoFit.contain,
    width: '100%',
    height: '100%',
  });
}

Local inputs publish microphone or camera capture to an established connection. Use them for client uplink scenarios such as intercom, voice reply, or sending local camera video.

ObjectMain APIs
TiRtcAudioInputsetOptions(...), start(), attach(...), detach(...), stop(), dispose(). start() checks ohos.permission.MICROPHONE; request permission in the app before calling it.
TiRtcVideoInputsetOptions(...), start(), attach(...), detach(...), stop(), dispose(). start() checks ohos.permission.CAMERA; request permission in the app before calling it.

If the required permission is missing, the SDK returns a platform permission error. It does not show permission dialogs.

Render local camera preview with TiRtcVideoInputPreview:

ts
import { TiRtcVideoFit, TiRtcVideoInput, TiRtcVideoInputPreview } from 'tirtc-av/Index';

@Builder
function LocalPreview(input: TiRtcVideoInput) {
  TiRtcVideoInputPreview({
    input,
    fit: TiRtcVideoFit.cover,
    mirror: true,
    width: '100%',
    height: '100%',
  });
}

Common Enums

TypeValues
TiRtcVideoDecoderPreferenceauto, software, hardware
TiRtcVideoEncoderPreferenceauto, software, hardware
TiRtcOutputBufferStrategyautomatic, noBuffer
TiRtcVideoFitcontain, cover
TiRtcAudioCodecg711a, aac
TiRtcVideoCodech264, h265, mjpeg
TiRtcCameraFacingfront, back

TiRtcLogging

APIDescription
d(tag: string, message: string): voidWrites a debug log entry.
i(tag: string, message: string): voidWrites an info log entry.
w(tag: string, message: string): voidWrites a warning log entry.
e(tag: string, message: string): voidWrites an error log entry.
upload(): Promise<TiRtcLoggingUploadResult>Uploads collected SDK logs and returns logId when available.

Common Misuse

  • Creating or using runtime-backed objects before await TiRtc.initialize(...).
  • Treating connect(...) == 0 as already connected.
  • Updating output options after attach().
  • Reusing one streamId for audio and video in the same connection.
  • Starting local input before requesting camera or microphone permission.
  • Calling only disconnect() or detach() and forgetting to dispose() objects.
  • Calling TiRtc.shutdown() while connection, input, or output objects still exist.

TiRTC