// Copyright 2023 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

package livekit.proto;
option csharp_namespace = "LiveKit.Proto";


enum DataChannelState {
    DC_CONNECTING = 0;
    DC_OPEN = 1;
    DC_CLOSING = 2;
    DC_CLOSED = 3;
}

enum QualityLimitationReason {
    LIMITATION_NONE = 0;
    LIMITATION_CPU = 1;
    LIMITATION_BANDWIDTH = 2;
    LIMITATION_OTHER = 3;
}

enum IceRole {
    ICE_UNKNOWN = 0;
    ICE_CONTROLLING = 1;
    ICE_CONTROLLED = 2;
}

enum DtlsTransportState {
    DTLS_TRANSPORT_NEW = 0;
    DTLS_TRANSPORT_CONNECTING = 1;
    DTLS_TRANSPORT_CONNECTED = 2;
    DTLS_TRANSPORT_CLOSED = 3;
    DTLS_TRANSPORT_FAILED = 4;
}

enum IceTransportState {
    ICE_TRANSPORT_NEW = 0;
    ICE_TRANSPORT_CHECKING = 1;
    ICE_TRANSPORT_CONNECTED = 2;
    ICE_TRANSPORT_COMPLETED = 3;
    ICE_TRANSPORT_DISCONNECTED = 4;
    ICE_TRANSPORT_FAILED = 5;
    ICE_TRANSPORT_CLOSED = 6;
}

enum DtlsRole {
    DTLS_CLIENT = 0;
    DTLS_SERVER = 1;
    DTLS_UNKNOWN = 2;
}

enum IceCandidatePairState {
    PAIR_FROZEN = 0;
    PAIR_WAITING = 1;
    PAIR_IN_PROGRESS = 2;
    PAIR_FAILED = 3;
    PAIR_SUCCEEDED = 4;
}

enum IceCandidateType {
    HOST = 0;
    SRFLX = 1;
    PRFLX = 2;
    RELAY = 3;
}

enum IceServerTransportProtocol {
    TRANSPORT_UDP = 0;
    TRANSPORT_TCP = 1;
    TRANSPORT_TLS = 2;
}

enum IceTcpCandidateType {
    CANDIDATE_ACTIVE = 0;
    CANDIDATE_PASSIVE = 1;
    CANDIDATE_SO = 2;
}

message RtcStats {
    message Codec {
        RtcStatsData rtc = 1;
    	CodecStats codec = 2;
    }

    message InboundRtp {
	RtcStatsData rtc = 1;
	RtpStreamStats stream = 2;
	ReceivedRtpStreamStats received = 3;
	InboundRtpStreamStats inbound = 4;
    }

    message OutboundRtp {
        RtcStatsData rtc = 1;
	RtpStreamStats stream = 2;
	SentRtpStreamStats sent = 3;
	OutboundRtpStreamStats outbound = 4;
    }

    message RemoteInboundRtp {
        RtcStatsData rtc = 1;
	RtpStreamStats stream = 2;
	ReceivedRtpStreamStats received = 3;
	RemoteInboundRtpStreamStats remote_inbound = 4;
    }

    message RemoteOutboundRtp {
        RtcStatsData rtc = 1;
	RtpStreamStats stream = 2;
	SentRtpStreamStats sent = 3;
	RemoteOutboundRtpStreamStats remote_outbound = 4;
    }

    message MediaSource {
        RtcStatsData rtc = 1;
        MediaSourceStats source = 2;
	AudioSourceStats audio = 3;
	VideoSourceStats video = 4;
    }

    message MediaPlayout {
        RtcStatsData rtc = 1;
        AudioPlayoutStats audio_playout = 2;
    }

    message PeerConnection {
        RtcStatsData rtc = 1;
	PeerConnectionStats pc = 2;
    }

    message DataChannel {
        RtcStatsData rtc = 1;
	DataChannelStats dc = 2;
    }

    message Transport {
        RtcStatsData rtc = 1;
	TransportStats transport  = 2;
    }

    message CandidatePair {
        RtcStatsData rtc = 1;
	CandidatePairStats candidate_pair = 2;
    }

    message LocalCandidate {
        RtcStatsData rtc = 1;
	IceCandidateStats candidate = 2;
    }

    message RemoteCandidate {
        RtcStatsData rtc = 1;
	IceCandidateStats candidate = 2;
    }

    message Certificate {
        RtcStatsData rtc = 1;
	CertificateStats certificate = 2;
    }

    message Track {
	// Deprecated
    }

    oneof stats {
	Codec codec = 3;
	InboundRtp inbound_rtp = 4;
	OutboundRtp outbound_rtp = 5;
	RemoteInboundRtp remote_inbound_rtp = 6;
	RemoteOutboundRtp remote_outbound_rtp = 7;
	MediaSource media_source = 8;
	MediaPlayout media_playout = 9;
	PeerConnection peer_connection = 10;
	DataChannel data_channel = 11;
	Transport transport = 12;
	CandidatePair candidate_pair = 13;
	LocalCandidate local_candidate = 14;
	RemoteCandidate remote_candidate = 15;
	Certificate certificate = 16;
	Track track = 17;
    }
}

message RtcStatsData {
    string id = 1;
    int64 timestamp = 2;
}

message CodecStats {
    uint32 payload_type = 1;
    string transport_id = 2;
    string mime_type = 3;
    uint32 clock_rate = 4;
    uint32 channels = 5;
    string sdp_fmtp_line = 6;
}

message RtpStreamStats {
    uint32 ssrc = 1;
    string kind = 2;
    string transport_id = 3;
    string codec_id = 4;
}

message ReceivedRtpStreamStats {
    uint64 packets_received = 1;
    int64 packets_lost = 2;
    double jitter = 3;
}

message InboundRtpStreamStats {
    string track_identifier = 1;
    string mid = 2;
    string remote_id = 3;
    uint32 frames_decoded = 4;
    uint32 key_frames_decoded = 5;
    uint32 frames_rendered = 6;
    uint32 frames_dropped = 7;
    uint32 frame_width = 8;
    uint32 frame_height = 9;
    double frames_per_second = 10;
    uint64 qp_sum = 11;
    double total_decode_time = 12;
    double total_inter_frame_delay = 13;
    double total_squared_inter_frame_delay = 14;
    uint32 pause_count = 15;
    double total_pause_duration = 16;
    uint32 freeze_count = 17;
    double total_freeze_duration = 18;
    double last_packet_received_timestamp = 19;
    uint64 header_bytes_received = 20;
    uint64 packets_discarded = 21;
    uint64 fec_bytes_received = 22;
    uint64 fec_packets_received = 23;
    uint64 fec_packets_discarded = 24;
    uint64 bytes_received = 25;
    uint32 nack_count = 26;
    uint32 fir_count = 27;
    uint32 pli_count = 28;
    double total_processing_delay = 29;
    double estimated_playout_timestamp = 30;
    double jitter_buffer_delay = 31;
    double jitter_buffer_target_delay = 32;
    uint64 jitter_buffer_emitted_count = 33;
    double jitter_buffer_minimum_delay = 34;
    uint64 total_samples_received = 35;
    uint64 concealed_samples = 36;
    uint64 silent_concealed_samples = 37;
    uint64 concealment_events = 38;
    uint64 inserted_samples_for_deceleration = 39;
    uint64 removed_samples_for_acceleration = 40;
    double audio_level = 41;
    double total_audio_energy = 42;
    double total_samples_duration = 43;
    uint64 frames_received = 44;
    string decoder_implementation = 45;
    string playout_id = 46;
    bool power_efficient_decoder = 47;
    uint64 frames_assembled_from_multiple_packets = 48;
    double total_assembly_time = 49;
    uint64 retransmitted_packets_received = 50;
    uint64 retransmitted_bytes_received = 51;
    uint32 rtx_ssrc = 52;
    uint32 fec_ssrc = 53;
}

message SentRtpStreamStats {
    uint64 packets_sent = 1;
    uint64 bytes_sent = 2;
}

message OutboundRtpStreamStats {
    string mid = 1;
    string media_source_id = 2;
    string remote_id = 3;
    string rid = 4;
    uint64 header_bytes_sent = 5;
    uint64 retransmitted_packets_sent = 6;
    uint64 retransmitted_bytes_sent = 7;
    uint32 rtx_ssrc = 8;
    double target_bitrate = 9;
    uint64 total_encoded_bytes_target = 10;
    uint32 frame_width = 11;
    uint32 frame_height = 12;
    double frames_per_second = 13;
    uint32 frames_sent = 14;
    uint32 huge_frames_sent = 15;
    uint32 frames_encoded = 16;
    uint32 key_frames_encoded = 17;
    uint64 qp_sum = 18;
    double total_encode_time = 19;
    double total_packet_send_delay = 20;
    QualityLimitationReason quality_limitation_reason = 21;
    map<string, double> quality_limitation_durations = 22;
    uint32 quality_limitation_resolution_changes = 23;
    uint32 nack_count = 24;
    uint32 fir_count = 25;
    uint32 pli_count = 26;
    string encoder_implementation = 27;
    bool power_efficient_encoder = 28;
    bool active = 29;
    string scalibility_mode = 30;
}

message RemoteInboundRtpStreamStats {
    string local_id = 1;
    double round_trip_time = 2;
    double total_round_trip_time = 3;
    double fraction_lost = 4;
    uint64 round_trip_time_measurements = 5;
}

message RemoteOutboundRtpStreamStats {
    string local_id = 1;
    double remote_timestamp = 2;
    uint64 reports_sent = 3;
    double round_trip_time = 4;
    double total_round_trip_time = 5;
    uint64 round_trip_time_measurements = 6;
}

message MediaSourceStats {
    string track_identifier = 1;
    string kind = 2;
}

message AudioSourceStats {
    double audio_level = 1;
    double total_audio_energy = 2;
    double total_samples_duration = 3;
    double echo_return_loss = 4;
    double echo_return_loss_enhancement = 5;
    double dropped_samples_duration = 6;
    uint32 dropped_samples_events = 7;
    double total_capture_delay = 8;
    uint64 total_samples_captured = 9;
}

message VideoSourceStats {
    uint32 width = 1;
    uint32 height = 2;
    uint32 frames = 3;
    double frames_per_second = 4;
}

message AudioPlayoutStats {
    string kind = 1;
    double synthesized_samples_duration = 2;
    uint32 synthesized_samples_events = 3;
    double total_samples_duration = 4;
    double total_playout_delay = 5;
    uint64 total_samples_count = 6;
}

message PeerConnectionStats {
    uint32 data_channels_opened = 1;
    uint32 data_channels_closed = 2;
}

message DataChannelStats {
    string label = 1;
    string protocol = 2;
    int32 data_channel_identifier = 3;
    optional DataChannelState state = 4;
    uint32 messages_sent = 5;
    uint64 bytes_sent = 6;
    uint32 messages_received = 7;
    uint64 bytes_received = 8;
}

message TransportStats {
    uint64 packets_sent = 1;
    uint64 packets_received = 2;
    uint64 bytes_sent = 3;
    uint64 bytes_received = 4;
    IceRole ice_role = 5;
    string ice_local_username_fragment = 6;
    optional DtlsTransportState dtls_state = 7;
    optional IceTransportState ice_state = 8;
    string selected_candidate_pair_id = 9;
    string local_certificate_id = 10;
    string remote_certificate_id = 11;
    string tls_version = 12;
    string dtls_cipher = 13;
    DtlsRole dtls_role = 14;
    string srtp_cipher = 15;
    uint32 selected_candidate_pair_changes = 16;
}

message CandidatePairStats {
    string transport_id = 1;
    string local_candidate_id = 2;
    string remote_candidate_id = 3;
    optional IceCandidatePairState state = 4;
    bool nominated = 5;
    uint64 packets_sent = 6;
    uint64 packets_received = 7;
    uint64 bytes_sent = 8;
    uint64 bytes_received = 9;
    double last_packet_sent_timestamp = 10;
    double last_packet_received_timestamp = 11;
    double total_round_trip_time = 12;
    double current_round_trip_time = 13;
    double available_outgoing_bitrate = 14;
    double available_incoming_bitrate = 15;
    uint64 requests_received = 16;
    uint64 requests_sent = 17;
    uint64 responses_received = 18;
    uint64 responses_sent = 19;
    uint64 consent_requests_sent = 20;
    uint32 packets_discarded_on_send = 21;
    uint64 bytes_discarded_on_send = 22;
}

message IceCandidateStats {
    string transport_id = 1;
    string address = 2;
    int32 port = 3;
    string protocol = 4;
    optional IceCandidateType candidate_type = 5;
    int32 priority = 6;
    string url = 7;
    optional IceServerTransportProtocol relay_protocol = 8;
    string foundation = 9;
    string related_address = 10;
    int32 related_port = 11;
    string username_fragment = 12;
    optional IceTcpCandidateType tcp_type = 13;
}

message CertificateStats {
    string fingerprint = 1;
    string fingerprint_algorithm = 2;
    string base64_certificate = 3;
    string issuer_certificate_id = 4;
}

