syntax = "proto3";

package livekit;
option go_package = "github.com/livekit/protocol/livekit";
option csharp_namespace = "LiveKit.Proto";
option ruby_package = "LiveKit::Proto";

import "google/protobuf/empty.proto";
import "google/protobuf/timestamp.proto";
import "livekit_models.proto";
import "livekit_egress.proto";
import "livekit_ingress.proto";

service AnalyticsRecorderService {
  rpc IngestStats(stream AnalyticsStats) returns (google.protobuf.Empty){};
  rpc IngestEvents(stream AnalyticsEvents) returns (google.protobuf.Empty){};
}

enum StreamType {
  UPSTREAM = 0;
  DOWNSTREAM = 1;
}

message AnalyticsVideoLayer {
  int32 layer = 1;
  uint32 packets = 2;
  uint64 bytes = 3;
  uint32 frames = 4;
}

message AnalyticsStream {
  uint32 ssrc = 1;
  uint32 primary_packets = 2;
  uint64 primary_bytes = 3;
  uint32 retransmit_packets = 4;
  uint64 retransmit_bytes = 5;
  uint32 padding_packets = 6;
  uint64 padding_bytes = 7;
  uint32 packets_lost = 8;
  uint32 frames = 9;
  uint32 rtt = 10;
  uint32 jitter = 11;
  uint32 nacks = 12;
  uint32 plis = 13;
  uint32 firs = 14;
  repeated AnalyticsVideoLayer video_layers = 15;
}

message AnalyticsStat {
  string analytics_key = 1;
  StreamType kind = 2;
  google.protobuf.Timestamp time_stamp = 3;
  string node = 4;
  string room_id = 5;
  string room_name = 6;
  string participant_id = 7;
  string track_id = 8;
  float score = 9;
  repeated AnalyticsStream streams = 10;
  string mime = 11;
}

message AnalyticsStats {
  repeated AnalyticsStat stats = 1;
}

enum AnalyticsEventType {
  ROOM_CREATED = 0;
  ROOM_ENDED = 1;
  PARTICIPANT_JOINED = 2;
  PARTICIPANT_LEFT = 3;
  TRACK_PUBLISHED = 4;
  TRACK_UNPUBLISHED = 5;
  TRACK_SUBSCRIBED = 6;
  TRACK_UNSUBSCRIBED = 7;
  TRACK_PUBLISHED_UPDATE = 10;
  PARTICIPANT_ACTIVE = 11;
  EGRESS_STARTED = 12;
  EGRESS_ENDED = 13;
  TRACK_MAX_SUBSCRIBED_VIDEO_QUALITY = 14;
  RECONNECTED = 15;
  INGRESS_STARTED = 16;
  INGRESS_ENDED = 17;
}

message AnalyticsClientMeta {
  string region = 1;
  string node = 2;
  string client_addr = 3;
  uint32 client_connect_time = 4;
  // udp, tcp, turn
  string connection_type = 5;
}

message AnalyticsEvent {
  AnalyticsEventType type = 1;
  google.protobuf.Timestamp timestamp = 2;
  string room_id = 3;
  Room room = 4;
  string participant_id = 5;
  ParticipantInfo participant = 6;
  string track_id = 7;
  TrackInfo track = 8;
  string analytics_key = 10;
  ClientInfo  client_info = 11;
  AnalyticsClientMeta client_meta = 12;
  string egress_id = 13;
  VideoQuality max_subscribed_video_quality = 14;
  ParticipantInfo publisher = 15;
  string mime = 16;
  EgressInfo egress = 17;
  IngressInfo ingress = 18;

}

message AnalyticsEvents {
  repeated AnalyticsEvent events = 1;
}
