シグナリングの型定義

この章ではシグナリングの型について説明します。 シグナリングの仕様については シグナリング を参照ください。

型の表記は TypeScript で記述します。

基本的なデータ型

JSON 値

type JSONType =
  | null
  | boolean
  | number
  | string
  | JSONType[]
  | { [prop: string]: JSONType | undefined };

JSON 値を表します。 仕様は RFC 8259 に従います。

シグナリング

シグナリングは「クライアントからサーバー (Sora) に送信される」メッセージと「サーバー (Sora) からクライアントに送信される」メッセージに分かれます。

クライアントからサーバーに送信されるメッセージ

  • connect

  • answer

  • candidate

  • re-answer

  • pong

    • WebSocket 経路利用時のみ

  • disconnect

  • stats

    • DataChannel 経路利用時のみ

Server からクライアントに送信されるメッセージ

  • offer

  • re-offer

  • ping

    • WebSocket 経路利用時のみ

  • push

  • notify

  • req-stats

    • DataChannel 経路利用時のみ

完全な型定義

type JSONType =
  | null
  | boolean
  | number
  | string
  | JSONType[]
  | { [prop: string]: JSONType | undefined };

// シグナリング
type Signaling = ClientToServer | ServerToClient;

// クライアントからサーバーに送信されるメッセージ
type ClientToServer =
  | SignalingConnectMessage
  | SignalingAnswerMessage
  | SignalingCandidateMessage
  | SignalingReOfferToServerMessage
  | SignalingPongMessage
  | SignalingDisconnectMessage;

// サーバーからクライアントに送信されるメッセージ
type ServerToClient =
  | SignalingRedirectMessage
  | SignalingOfferMessage
  | SignalingReAnswerToClientMessage
  | SignalingPingMessage
  | SignalingPushMessage
  | SignalingNotifyMessage
  | SignalingSwitchedMessage;

// type: "connect"
type SignalingConnectMessage = {
  type: "connect";
  role: Role;
  channel_id: string;
  client_id?: string;
  bundle_id?: string;
  metadata?: JSONType;
  signaling_notify_metadata?: JSONType;
  multistream?: boolean;
  spotlight?: Spotlight;
  spotlight_number?: number;
  data_channel_signaling?: boolean;
  ignore_disconnect_websocket?: boolean;
  data_channels?: DataChannel[];
  simulcast?: boolean;
  simulcast_rid?: SimulcastRid;
  spotlight_focus_rid?: SimulcastRid | "none";
  spotlight_unfocus_rid?: SimulcastRid | "none";
  audio?: Audio;
  video?: Video;
  // type: redirect で戻されて再度 type: connect で接続するときに有効にする
  redirect?: boolean;
  e2ee?: boolean;
  sdp?: string;
  sora_client?: string;
  environment?: string;
  libwebrtc?: string;
};

// ストリームの種別
type Role =
  | "sendrecv"
  | "sendonly"
  | "recvonly";

// サイマルキャストで配信する映像の種類
type SimulcastRid =
  | "r0"
  | "r1"
  | "r2";

// スポットライト
// spotlight_legacy が有効かどうかによって boolean または number どちらかを使用します。
// 詳しくはスポットライト機能とスポットライトレガシー機能を参照してください。
type Spotlight = boolean | number;

type AudioCodecType = "OPUS";

// 音声
type Audio =
  | boolean
  | {
      codec_type?: AudioCodecType;
      bit_rate?: number;
      opus_params?: {
        channels?: number;
        maxplaybackrate?: number;
        minptime?: number;
        ptime?: number;
        stereo?: boolean;
        sprop_stereo?: boolean;
        useinbandfec?: boolean;
        usedtx?: boolean;
      };
    };

type VideoCodecType =
  | "VP9"
  | "VP8"
  | "AV1"
  | "H264"
  | "H265";

// 映像
type Video =
  | boolean
  | {
      codec_type?: VideoCodecType;
      bit_rate?: number;
    };

type Mid = {
    auido?: string;
    video?: string;
};

// DataChannel の方向
type Direction =
  | "sendrecv"
  | "sendonly"
  | "recvonly";

// DataChannels
type DataChannel = {
    label: string;
    direction: Direction;
    ordered?: boolean;
    max_packet_life_time?: number;
    max_retransmits?: number;
    protocol?: string;
    compress?: boolean;
}


// type: "redirect"
type SignalingRedirectMessage = {
  type: "redirect";
  location: string;
};


// type: "offer"
type SignalingOfferMessage = {
  type: "offer";
  sdp: string;
  multistream: boolean;
  simulcast: boolean;
  spotlight: boolean;
  client_id: string;
  bundle_id: string;
  connection_id: string;
  metadata?: JSONType;
  config?: JSONType;
  encodings?: RTCRtpEncodingParameters[];
  mid?: Mid;
  data_channels?: DataChannel[];
};

// type: "answer"
type SignalingAnswerMessage = {
  type: "answer";
  sdp: string;
};

// type: "candidate"
type SignalingCandidateMessage = {
  type: "candidate";
  candidate: string;
};

// type: "re-offer"
// サーバーからクライアントに送信される
type SignalingReOfferToClientMessage = {
  type: "re-offer";
  sdp: string;
};

// type: "re-answer"
// クライアントからサーバーに送信される
type SignalingReAnswerToServerMessage = {
  type: "re-answer";
  sdp: string;
};

// type: "ping"
type SignalingPingMessage = {
  type: "ping";
  stats: boolean?;
};

// type: "pong"
type SignalingPongMessage = {
  type: "pong";
  stats?: RTCStatsReport[];
};

// type: "push"
type SignalingPushMessage = {
  type: "push";
  data: Record<string, unknown>;
};

// type: "notify"
type SignalingNotifyMessage =
  | SignalingNotifyConnectionCreated
  | SignalingNotifyConnectionUpdated
  | SignalingNotifyConnectionDestroyed
  | SignalingNotifySpotlightFocused
  | SignalingNotifySpotlightUnfocused
  | SignalingNotifyRecordingStarted
  | SignalingNotifyRecordingStopped
  | SignalingNotifyNetworkStatus;

// 接続中のクライアントの情報
type SignalingNotifyMetadata = {
  client_id?: string;
  bundle_id?: string;
  connection_id?: string;
  authn_metadata?: JSONType;
  authz_metadata?: JSONType;
  metadata?: JSONType;
};

// "connection.created",
type SignalingNotifyConnectionCreated = {
  type: "notify";
  event_type: "connection.created";
  role: Role;
  session_id?: string;
  client_id?: string;
  bundle_id?: string;
  connection_id?: string;
  audio?: boolean;
  video?: boolean;
  authn_metadata?: JSONType;
  authz_metadata?: JSONType;
  metadata?: JSONType;
  data?: SignalingNotifyMetadata[];
  minutes: number;
  channel_connections: number;
  channel_sendrecv_connections: number;
  channel_sendonly_connections: number;
  channel_recvonly_connections: number;
  turn_transport_type: "udp" | "tcp";
};

// "connection.updated"
type SignalingNotifyConnectionUpdated = {
  type: "notify";
  event_type: "connection.updated";
  role: Role;
  session_id?: string;
  client_id?: string;
  bundle_id?: string;
  connection_id?: string;
  audio?: boolean;
  video?: boolean;
  authn_metadata?: JSONType;
  authz_metadata?: JSONType;
  metadata?: JSONType;
  minutes: number;
  channel_connections: number;
  channel_sendrecv_connections: number;
  channel_sendonly_connections: number;
  channel_recvonly_connections: number;
  turn_transport_type: "udp" | "tcp";
};

// "connection.destroyed"
type SignalingNotifyConnectionDestroyed = {
  type: "notify";
  event_type: "connection.destroyed";
  role: Role;
  session_id?: string;
  client_id?: string;
  bundle_id?: string;
  connection_id?: string;
  audio?: boolean;
  video?: boolean;
  minutes: number;
  authn_metadata?: JSONType;
  authz_metadata?: JSONType;
  metadata?: JSONType;
  channel_connections: number;
  channel_sendrecv_connections: number;
  channel_sendonly_connections: number;
  channel_recvonly_connections: number;
  turn_transport_type: "udp" | "tcp";
};

// "spotlight.focused"
type SignalingNotifySpotlightFocused = {
  type: "notify";
  event_type: "spotlight.focused";
  client_id: string | null;
  bundle_id: string | null;
  connection_id: string;
  audio: boolean;
  video: boolean;
  fixed: boolean;
};

// "spotlight.unfocused"
type SignalingNotifySpotlightUnfocused = {
  type: "notify";
  event_type: "spotlight.unfocused";
  client_id: string | null;
  bundle_id: string | null;
  connection_id: string;
  audio: boolean;
  video: boolean;
  fixed: boolean;
};

// "recording.started"
type SignalingNotifyRecordingStarted = {
  type: "notify";
  event_type: "recording.started";
  client_id: string | null;
  bundle_id: string | null;
  connection_id: string;
};

// "recording.stopped"
type SignalingNotifyRecordingStopped = {
  type: "notify";
  event_type: "recording.stopped";
  client_id: string | null;
  bundle_id: string | null;
  connection_id: string;
};

// "network.status"
type SignalingNotifyNetworkStatus = {
  type: "notify";
  event_type: "network.status";
  unstable_level: 0 | 1 | 2 | 3;
};

// type: "disconnect"
type SignalingDisconnectMessage = {
  type: "disconnect";
  reason: string;
};

// type: "stats"
// サーバーからクライアントに送信される
type DataChannelStatsToClientMessage = {
  type: "stats";
}

// type: "stats"
// クライアントからサーバーに送信される
type DataChannelStatsToServerMessage = {
  type: "stats";
  reports: RTCStatsReport[];
}

// type: "switched"
type SignalingSwitchedMessage = {
  type: "switched";
  ignore_disconnect_websocket: boolean;

};
© Copyright 2022, Shiguredo Inc Created using Sphinx 5.0.2