/*
 * Copyright (C) 2012 Google Inc. All rights reserved.
 * Copyright (C) 2017 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

[
    Conditional=WEB_RTC,
    EnabledAtRuntime=PeerConnection,
    ImplementationLacksVTable,
] interface RTCStatsReport {
    readonly maplike<DOMString, object>;
};

enum RTCStatsType {
    "codec",
    "inbound-rtp",
    "outbound-rtp",
    "peer-connection",
    "data-channel",
    "track",
    "transport",
    "candidate-pair",
    "local-candidate",
    "remote-candidate",
    "certificate"
};

typedef double DOMHighResTimeStamp;

dictionary RTCStats {
    DOMHighResTimeStamp timestamp;
    RTCStatsType type;
    DOMString id;

};

dictionary RTCRTPStreamStats : RTCStats {
    unsigned long ssrc;
    DOMString associateStatsId;
    boolean isRemote = false;
    DOMString mediaType;
    DOMString trackId;
    DOMString transportId;
    DOMString codecId;
    unsigned long firCount;
    unsigned long pliCount;
    unsigned long nackCount;
    unsigned long sliCount;
    unsigned long long qpSum;
};

[ JSGenerateToJSObject ]
dictionary RTCInboundRTPStreamStats : RTCRTPStreamStats {
    unsigned long packetsReceived;
    unsigned long long bytesReceived;
    unsigned long packetsLost;
    double jitter;
    double fractionLost;
    unsigned long packetsDiscarded;
    unsigned long packetsRepaired;
    unsigned long burstPacketsLost;
    unsigned long burstPacketsDiscarded;
    unsigned long burstLossCount;
    unsigned long burstDiscardCount;
    double burstLossRate;
    double burstDiscardRate;
    double gapLossRate;
    double gapDiscardRate;
    unsigned long framesDecoded;
};

[ JSGenerateToJSObject ]
dictionary RTCOutboundRTPStreamStats : RTCRTPStreamStats {
    unsigned long packetsSent;
    unsigned long long bytesSent;
    double targetBitrate;
    // FIXME 169662: missing roundTripTime
    unsigned long framesEncoded;
};

[ JSGenerateToJSObject ]
dictionary RTCMediaStreamTrackStats : RTCStats {
    DOMString trackIdentifier;
    boolean remoteSource;
    boolean ended;
    boolean detached;
    // FIXME: Add sequence<DOMString> ssrcIds;
    unsigned long frameWidth;
    unsigned long frameHeight;
    double framesPerSecond;
    unsigned long framesSent;
    unsigned long framesReceived;
    unsigned long framesDecoded;
    unsigned long framesDropped;
    unsigned long framesCorrupted;
    unsigned long partialFramesLost;
    unsigned long fullFramesLost;
    double audioLevel;
    double echoReturnLoss;
    double echoReturnLossEnhancement;
};

[ JSGenerateToJSObject ]
dictionary RTCDataChannelStats : RTCStats {
    DOMString label;
    DOMString protocol;
    long datachannelid;
    // FIXME: Switch state to RTCDataChannelState
    DOMString state;
    unsigned long messagesSent;
    unsigned long long bytesSent;
    unsigned long messagesReceived;
    unsigned long long bytesReceived;
};

enum RTCStatsIceCandidatePairState {
    "frozen",
    "waiting",
    "inprogress",
    "failed",
    "succeeded",
    "cancelled"
};

[ JSGenerateToJSObject ]
dictionary RTCIceCandidatePairStats : RTCStats {
    DOMString transportId;
    DOMString localCandidateId;
    DOMString remoteCandidateId;
    RTCStatsIceCandidatePairState state;
    unsigned long long priority;
    boolean nominated;
    boolean writable;
    boolean readable;
    unsigned long long bytesSent;
    unsigned long long bytesReceived;
    double totalRoundTripTime;
    double currentRoundTripTime;
    double availableOutgoingBitrate;
    double availableIncomingBitrate;
    unsigned long long requestsReceived;
    unsigned long long requestsSent;
    unsigned long long responsesReceived;
    unsigned long long responsesSent;
    // FIXME: Expose other fields once libwebrtc support them.
};

enum RTCIceCandidateType {
    "host",
    "srflx",
    "prflx",
    "relay"
};

[ JSGenerateToJSObject ]
dictionary RTCIceCandidateStats : RTCStats {
    DOMString transportId;
    // FIXME: decide whether to expose networkType
    // FIXME: Expose relayProtocol
    DOMString address;
    long port;
    DOMString protocol;
    RTCIceCandidateType candidateType;
    long priority;
    DOMString url;
    boolean deleted = false;
};

[ JSGenerateToJSObject ]
dictionary RTCCertificateStats : RTCStats {
    DOMString fingerprint;
    DOMString fingerprintAlgorithm;
    DOMString base64Certificate;
    DOMString issuerCertificateId;
};

enum RTCCodecType {
    "encode",
    "decode"
};

[ JSGenerateToJSObject ]
dictionary RTCCodecStats : RTCStats {
    unsigned long payloadType;
    RTCCodecType codecType;
    DOMString transportId;
    DOMString mimeType;
    unsigned long clockRate;
    unsigned long channels;
    DOMString sdpFmtpLine;
    DOMString implementation;
};

[ JSGenerateToJSObject ]
dictionary RTCTransportStats : RTCStats {
    // FIXME 169662: Support more fields.
    // unsigned long packetsSent;
    // unsigned long packetsReceived;
    unsigned long long bytesSent;
    unsigned long long bytesReceived;
    DOMString rtcpTransportStatsId;
    // RTCIceRole iceRole;
    // RTCDtlsTransportState dtlsState;
    DOMString selectedCandidatePairId;
    DOMString localCertificateId;
    DOMString remoteCertificateId;
    // DOMString tlsVersion;
    // DOMString dtlsCipher;
    // DOMString srtpCipher;
    // DOMString tlsGroup;
};

[ JSGenerateToJSObject ]
dictionary RTCPeerConnectionStats : RTCStats {
    // FIXME 169662: Support more fields.
    unsigned long dataChannelsOpened;
    unsigned long dataChannelsClosed;
    // unsigned long dataChannelsRequested;
    // unsigned long dataChannelsAccepted;
};

// FIXME 169662: missing RTCMediaStreamStats
