summaryrefslogtreecommitdiff
path: root/src/rtp.h
blob: 350eddc375d00f88d93d3d0400c0b47338ebd5a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set et sw=2 ts=2: */
/***************************************************************************
 *            rtp.h
 *
 *  Mon Sep  2 15:24:09 CEST 2013
 *  Copyright 2013 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of lrtp.
 *
 * lrtp is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * lrtp is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with lrtp; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */
#ifndef __LRTP_RTP_H__
#define __LRTP_RTP_H__

#include <stdlib.h>
#include <stdint.h>
#include <list>

typedef struct {
  uint16_t cc:4;	    // CSRC count
  uint16_t x:1;	      // Header extension flag
  uint16_t p:1;	      // Padding flag
  uint16_t version:2; // Protocol version

  uint16_t m:1;	      // Marker bit
  uint16_t pt:7;	    // Payload type

  uint16_t seq;		    // Sequence number

  uint32_t ts;		    // Timestamp
  uint32_t ssrc;	    // Synchronization source
} rtp_header_t;

#define MAX_RTP_PACKET_SIZE (16 * 1024)
#define MAX_RTP_PAYLOAD_SIZE ( MAX_RTP_PACKET_SIZE - sizeof(rtp_header_t) )

typedef struct {
  rtp_header_t header;
  char body[MAX_RTP_PAYLOAD_SIZE];
} rtp_t;

typedef unsigned int csrc_t;

class RTP {
public:
  RTP();

  /* // Not implemented
  void setHeaderExtension();
  int headerExtension();
  */

  void setPadding(unsigned char padding);
  unsigned char padding() const;

  void setPayloadType(unsigned char pt);
  unsigned char payloadType() const;

  void setMarker(bool bit);
  bool marker() const;

  void setSeq(uint16_t seq);
  uint16_t seq() const;

  void setTimestamp(uint32_t seq);
  uint32_t timestamp() const;

  void setSSrc(uint32_t ssrc);
  uint32_t SSrc() const;

  void addCSrc(csrc_t csrc);
  std::list<csrc_t> getCSrcs() const;
  void removeCSrc(csrc_t csrc);
  void clearCSrcs();

  /**
   * @return Number of bytes put in payload, or -1 on error.
   * (data too big to fit inside rtp payload)
   */
  int setPayload(const char *payload, size_t size);
  size_t payload(char *payload, size_t maxsize) const;
  size_t payloadSize() const;
  const char *payloadData() const;

  size_t packet(char *packet, size_t maxsize) const;
  int fromPacket(const char *packet, size_t size);

  bool isValid();
  void setValid(bool valid);

private:  
  std::list<csrc_t> csrcs;

  rtp_header_t header;

  unsigned char pads;

  char pload[MAX_RTP_PAYLOAD_SIZE];
  size_t pload_size;

  bool is_valid;
};

#endif/*__LRTP_RTP_H__*/