/******************************************************************************
* Copyright (c) 2000-2023 Ericsson Telecom AB
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
*
* Contributors:
*   Gabor Szalai - initial implementation and initial documentation
*   Peter Kremer
*   Sandor Palugyai
*   Tibor Harai
******************************************************************************/
//
//  File:               RTP_Types.ttcn
//  Description:        RTP
//  Rev:                R5B
//  Prodnr:             CNL 113 392
//
module RTP_Types{

import from General_Types all;

external function f_RTP_enc ( in RTP_messages_union pdu ) return octetstring;
external function f_RTP_dec ( in octetstring data ) return RTP_messages_union;
external function f_RTP_packet_enc ( in RTP_packet pdu ) return octetstring;
external function f_RTP_packet_dec ( in octetstring data ) return RTP_packet;
external function f_RTCP_PDU_enc ( in PDU_RTCP pdu ) return octetstring
with { extension "prototype(convert) encode(RAW)" };

external function f_RTCP_PDU_dec ( in octetstring data ) return PDU_RTCP
with { extension "prototype(convert) decode(RAW)" }

type set of BIT32_BO_LAST RTP_CSRC_list;

type record RTP_field_ext_header{
  BIT16_BO_LAST      ext_header_id,
  LIN2_BO_LAST      ext_header_length,
  octetstring   ext_header_data optional
} with {
  variant (ext_header_length) "LENGTHTO(ext_header_data), UNIT(dword32)";
};

type record RTP_header {
  INT2b                version,
  BIT1                 padding_ind,
  BIT1                 extension_ind,
  INT4b                CSRC_count,
  BIT1                 marker_bit,
  INT7b                payload_type,
  LIN2_BO_LAST         sequence_number,
  BIT32_BO_LAST        time_stamp,
  BIT32_BO_LAST        SSRC_id,
  RTP_CSRC_list        CSRCs optional,
  RTP_field_ext_header ext_header optional
} with {
  variant (ext_header) "PRESENCE(extension_ind = '1'B)";
  variant (CSRC_count) "LENGTHTO(CSRCs), UNIT(elements)";
  variant "FIELDORDER(msb)"
}

type record RTP_packet {
  RTP_header header,
  octetstring payload
} with { variant "" }

type record PDU_RTP{
  INT2b                  version,
  BIT1                  padding_ind,
  BIT1                  extension_ind,
  INT4b                  CSRC_count,
  BIT1                  marker_bit,
  INT7b                  payload_type,
  LIN2_BO_LAST              sequence_number,
  BIT32_BO_LAST              time_stamp,
  BIT32_BO_LAST              SSRC_id,
  RTP_CSRC_list         CSRCs optional,
  RTP_field_ext_header  ext_header optional,
  octetstring data
} with {
  variant (ext_header) "PRESENCE(extension_ind = '1'B)";
  variant (CSRC_count) "LENGTHTO(CSRCs), UNIT(elements)";
  variant "FIELDORDER(msb)"
};


type record RTCP_field_sender_info{
  BIT32_BO_LAST              NTP_timestamp_MSW,
  BIT32_BO_LAST              NTP_timestamp_LSW,
  BIT32_BO_LAST              RTP_timestamp,
  BIT32_BO_LAST              senders_packet_count,
  BIT32_BO_LAST              senders_octet_count
};

type record RTCP_report{
  BIT32_BO_LAST              SSRC_id,
  LIN1                  farction_lost,
  LIN3_BO_LAST              number_lost_packet,
  BIT32_BO_LAST              highest_seq_num,
  BIT32_BO_LAST              jitter,
  BIT32_BO_LAST              last_SR_time_stamp,
  BIT32_BO_LAST              delay_since_SR
};

type set of RTCP_report RTCP_report_list;

type record RTCP_IE_SR{
  INT2b                  version,
  BIT1                  padding_ind,
  INT5b                  RR_count,
  LIN1                  payload_type,
  LIN2_BO_LAST              packet_length,
  BIT32_BO_LAST              SSRC_id,
  RTCP_field_sender_info sender_info,
  RTCP_report_list      report_list optional,
  octetstring           padding optional
} with {
  variant (padding) "PRESENCE(padding_ind = '1'B)";
  variant (RR_count) "LENGTHTO(report_list), UNIT(elements)";
  variant (packet_length) "LENGTHTO(SSRC_id,sender_info,report_list,padding)";
  variant (packet_length) "UNIT(dword32)";
  variant "FIELDORDER(msb)"
};

type record RTCP_IE_RR{
  INT2b                  version,
  BIT1                  padding_ind,
  INT5b                  RR_count,
  LIN1                  payload_type,
  LIN2_BO_LAST              packet_length,
  BIT32_BO_LAST              SSRC_id,
  RTCP_report_list      report_list optional,
  octetstring           padding optional
} with {
  variant (padding) "PRESENCE(padding_ind = '1'B)";
  variant (RR_count) "LENGTHTO(report_list), UNIT(elements)";
  variant (packet_length) "LENGTHTO(SSRC_id,report_list,padding)";
  variant (packet_length) "UNIT(dword32)";
  variant "FIELDORDER(msb)"
};

type record RTCP_field_SDES{
  LIN1                  id,
  LIN1                  content_length,
  charstring            content
} with {
  variant (content_length) "LENGTHTO(content)"
};

type record RTCP_field_PRIV{
  LIN1                  id,
  LIN1                  content_length,
  LIN1                  prefix_length,
  charstring            prefix,
  charstring            content
} with {
  variant (content_length) "LENGTHTO(prefix_length,prefix,content)";
  variant (prefix_length) "LENGTHTO(prefix)"
};

type union RTCP_SDES_item{
  RTCP_field_SDES       cname,
  RTCP_field_SDES       name,
  RTCP_field_SDES       email,
  RTCP_field_SDES       phone,
  RTCP_field_SDES       loc,
  RTCP_field_SDES       tool,
  RTCP_field_SDES       note,
  RTCP_field_PRIV       priv
} with {
  variant "TAG(cname, id=1;
                 name,  id=2;
                 email, id=3;
                 phone, id=4;
                 loc,   id=5;
                 tool,  id=6;
                 note,  id=7;
                 priv,  id=8)"
};

type set of RTCP_SDES_item RTCP_SDES_item_list;

type record RTCP_SDES_chunk{
  BIT32_BO_LAST              SSRC_CSRC_id,
  RTCP_SDES_item_list   SDES_items optional,
  BIT8                  end_of_list // always set to zero
} with {
  variant "PADDING(dword32)"
};

type set of RTCP_SDES_chunk RTCP_SDES_chunk_list;

type record RTCP_IE_SDES{
  INT2b                  version,
  BIT1                  padding_ind,
  INT5b                  source_count,
  LIN1                  payload_type,
  LIN2_BO_LAST              packet_length,
  RTCP_SDES_chunk_list  chunks optional,
  octetstring           padding optional
} with {
  variant (padding) "PRESENCE(padding_ind = '1'B)";
  variant (source_count) "LENGTHTO(chunks), UNIT(elements)";
  variant (packet_length) "LENGTHTO(chunks,padding)";
  variant (packet_length) "UNIT(dword32)";
  variant "FIELDORDER(msb)"
};

type record RTCP_field_reason{
  LIN1                  reason_length,
  charstring            reason
} with {
  variant (reason_length) "LENGTHTO(reason)";
  variant "PADDING(dword32)"
};

type record RTCP_IE_BYE{
  INT2b                  version,
  BIT1                  padding_ind,
  INT5b                  source_count,
  LIN1                  payload_type,
  LIN2_BO_LAST              packet_length,
  RTP_CSRC_list         SSRC_CSRC_ids optional,
  RTCP_field_reason     reason optional,
  octetstring           padding optional
} with {
  variant (padding) "PRESENCE(padding_ind = '1'B)";
  variant (source_count) "LENGTHTO(SSRC_CSRC_ids), UNIT(elements)";
  variant (packet_length) "LENGTHTO(SSRC_CSRC_ids,reason,padding)";
  variant (packet_length) "UNIT(dword32)";
  variant "FIELDORDER(msb)"
};

type record RTCP_IE_APP{
  INT2b                  version,
  BIT1                  padding_ind,
  INT5b                  sub_type,
  LIN1                  payload_type,
  LIN2_BO_LAST              packet_length,
  BIT32_BO_LAST              SSRC_CSRC_id,
  CHAR4                 name,
  octetstring           appdata optional,
  octetstring           padding optional
} with {
  variant (padding) "PRESENCE(padding_ind = '1'B)";
  variant (packet_length) "LENGTHTO(SSRC_CSRC_id,name,appdata,padding)";
  variant (packet_length) "UNIT(dword32)";
  variant "FIELDORDER(msb)"
};

type record RTCP_IE_RTPFB {
  INT2b                 version,
  BIT1                  padding_ind,
  INT5b                 fmt,
  LIN1                  payload_type,
  LIN2_BO_LAST          packet_length,
  BIT32_BO_LAST         SSRC_packet_sender,
  BIT32_BO_LAST         SSRC_media_sender,
  RTCP_RTPFB_union      fci optional,
  octetstring           padding optional
} with {
  variant (padding) "PRESENCE(padding_ind = '1'B)";
  variant (packet_length) "LENGTHTO(SSRC_packet_sender,SSRC_media_sender,fci,padding)";
  variant (packet_length) "UNIT(dword32)";
  variant "FIELDORDER(msb)"
  variant (fci) "CROSSTAG( 
                    gen_naks, fmt=1;
                    tmmbr, fmt=3;
                    tmmbn, fmt=4;
                    unknown_fci, OTHERWISE
                 )";
};

type union RTCP_RTPFB_union {
  RTCP_generic_NAKs    gen_naks,
  RTCP_RTPFB_TMMBRs    tmmbr,
  RTCP_RTPFB_TMMBNs    tmmbn,
  octetstring          unknown_fci
}

type record of RTCP_RTPFB_TMMB RTCP_RTPFB_TMMBRs

type record of RTCP_RTPFB_TMMB RTCP_RTPFB_TMMBNs

  type integer INT14b_BO_LAST ( 0 .. 16383 ) with { variant "FIELDLENGTH(14), COMP(nosign), BYTEORDER(last)" };

type record RTCP_RTPFB_TMMB {
  BIT32_BO_LAST         SSRC,
  INT6b                 exp,
  integer               mantissa ( 0 .. 131072 ),
  integer               measured_overhead( 0 .. 511 )
} with {
  variant "FIELDORDER(msb)"
  variant (mantissa)  "FIELDLENGTH(17), COMP(nosign), BYTEORDER(last)"
  variant (measured_overhead)  "FIELDLENGTH(9), COMP(nosign), BYTEORDER(last)"
}

type record of RTCP_generic_NAK RTCP_generic_NAKs

type record RTCP_generic_NAK {
  LIN2_BO_LAST         pid,
  BIT16                blb
}

type record RTCP_IE_PSFB {
  INT2b                 version,
  BIT1                  padding_ind,
  INT5b                 fmt,
  LIN1                  payload_type,
  LIN2_BO_LAST          packet_length,
  BIT32_BO_LAST         SSRC_packet_sender,
  BIT32_BO_LAST         SSRC_media_sender,
  RTCP_PSFB_union       fci,
  octetstring           padding optional
} with {
  variant (padding) "PRESENCE(padding_ind = '1'B)";
  variant (packet_length) "LENGTHTO(SSRC_packet_sender,SSRC_media_sender,fci,padding)";
  variant (packet_length) "UNIT(dword32)";
  variant "FIELDORDER(msb)"
  variant (fci) "CROSSTAG( 
                    pli, fmt=1;
                    sli, fmt=2;
                    rpsi, fmt=3;
                    fir, fmt=4;
                    tstr, fmt=5;
                    tstn, fmt=6;
                    vbcm, fmt=7;
                    afb, fmt=15;
                    unknown_fci, OTHERWISE
                 )";
};

type union RTCP_PSFB_union {

  RTCP_PSFB_PLI        pli,
  RTCP_PSFB_SLIs       sli,
  RTCP_PSFB_RPSI       rpsi,
  RTCP_PSFB_FIRs       fir,
  RTCP_PSFB_TSTRs      tstr,
  RTCP_PSFB_TSTNs      tstn,
  RTCP_PSFB_VBCMs      vbcm,
  
  octetstring          afb,
  
  octetstring          unknown_fci
}

type record of RTCP_PSFB_FIR RTCP_PSFB_FIRs

type record RTCP_PSFB_FIR{
  BIT32_BO_LAST         SSRC,
  INT1                  seq_no,
  OCT3                  reserved
}

type record of RTCP_PSFB_TST RTCP_PSFB_TSTRs
type record of RTCP_PSFB_TST RTCP_PSFB_TSTNs

type record RTCP_PSFB_TST{
  BIT32_BO_LAST         SSRC,
  INT1                  seq_no,
  OCT2                  reserved,
  integer               index ( 0 .. 31)  // the encoding includes the 3 reserved
                                           // bit encoded as zero
  
} with {
  variant (seq_no) "FIELDLENGTH(8)"
}

type record of RTCP_PSFB_VBCM RTCP_PSFB_VBCMs

type record RTCP_PSFB_VBCM {
  BIT32_BO_LAST        SSRC,
  INT1                 seq_no,
  BIT1                 marker_bit,
  INT7b                payload_type,
  LIN2_BO_LAST         vbcm_length,
  octetstring          vbcm
} with {
  variant "FIELDORDER(msb)"
  variant "PADDING(dword32)"
  variant (vbcm_length) "LENGTHTO(vbcm)"
}


type record RTCP_PSFB_PLI {
  // empty
}

type record of RTCP_PSFB_SLI RTCP_PSFB_SLIs

type record RTCP_PSFB_SLI{
  INT13b_BO_LAST     first,
  INT13b_BO_LAST     number,
  INT6b              picture_id
} with {
  variant "FIELDORDER(msb)"
}


type record RTCP_PSFB_RPSI {
  INT1          pb,  // must be filled correctly, not handled by the encoder
  INT1          payload_type, // includes the first bit
  octetstring   rpsi_string // includes the padding bits
}

type union RTCP_message{
  RTCP_IE_SR            sr,
  RTCP_IE_RR            rr,
  RTCP_IE_SDES          sdes,
  RTCP_IE_BYE           bye,
  RTCP_IE_APP           app,
  RTCP_IE_RTPFB         rtpfb,
  RTCP_IE_PSFB          psfb
} with {
  variant "TAG(sr,    payload_type=200;
                 rr,    payload_type=201;
                 sdes,  payload_type=202;
                 bye,   payload_type=203;
                 app,   payload_type=204;
                 rtpfb, payload_type=205;
                 psfb,  payload_type=206
                 )"
};

type record of RTCP_message PDU_RTCP with {variant ""};

type record RTP_session_par{
  integer               id optional,
  charstring            local_address optional,
  integer               local_port optional,
  charstring            dest_address optional,
  integer               dest_port optional
}

type record ASP_RTP_Open_session{
  RTP_session_par       session_id
}

type record ASP_RTP_Open_session_result{
  RTP_session_par       session_id
}

type record ASP_RTP_Close_session{
  RTP_session_par       session_id
}

type union RTP_messages_union{
  PDU_RTCP              rtcp,
  PDU_RTP               rtp,
  octetstring           unknown_pdu
}

type record ASP_RTP_message{
  RTP_session_par      session_id,
  RTP_messages_union   data
}

type ASP_RTP_message ASP_RTP_messageDone with { extension "done" };

type port RTPasp_PT message{
  inout ASP_RTP_message;
  out ASP_RTP_Open_session;
  out ASP_RTP_Close_session;
  in ASP_RTP_Open_session_result
}  with {extension "internal"}

type port RTPasp_SP_PT message{
  inout ASP_RTP_message;
  in ASP_RTP_Open_session;
  in ASP_RTP_Close_session;
  out ASP_RTP_Open_session_result
}  with {extension "internal"}

} with { encode "RAW"}

