summaryrefslogtreecommitdiff
path: root/toxrtp/toxrtp.h
blob: 0aa89993273b48f69358ff9b3704eda9650808a9 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*   rtp_impl.h
 *
 *   Rtp implementation includes rtp_session_s struct which is a session identifier.
 *   It contains session information and it's a must for every session.
 *   It's best if you don't touch any variable directly but use callbacks to do so. !Red!
 *
 *
 *   Copyright (C) 2013 Tox project All Rights Reserved.
 *
 *   This file is part of Tox.
 *
 *   Tox is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Tox 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 General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with Tox.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


#ifndef _RTP__IMPL_H_
#define _RTP__IMPL_H_

#define RTP_VERSION 2
#include <inttypes.h>
#include "tox.h"
#include <pthread.h>
/* Extension header flags */
#define RTP_EXT_TYPE_RESOLUTION 0x01
#define RTP_EXT_TYPE_FRAMERATE  0x02

/* Some defines */

#define RTP_PACKET 70

/* Payload identifiers */

/* Audio */
#define _PAYLOAD_OPUS 96

/* Video */
#define _PAYLOAD_VP8 106

/* End of Payload identifiers */

/* End of defines */


/* Our main session descriptor.
 * It measures the session variables and controls
 * the entire session. There are functions for manipulating
 * the session so tend to use those instead of directly accessing
 * session parameters.
 */
typedef struct rtp_session_s {
    uint8_t                 _version;
    uint8_t                 _padding;
    uint8_t                 _extension;
    uint8_t                 _cc;
    uint8_t                 _marker;
    uint8_t                 _payload_type;
    uint16_t                _sequence_number;      /* Set when sending */
    uint16_t                _last_sequence_number; /* Check when recving msg */
    uint32_t                _initial_time;
    uint32_t                _time_elapsed;
    uint32_t                _current_timestamp;
    uint32_t                _ssrc;
    uint32_t               *_csrc;


    /* If some additional data must be sent via message
     * apply it here. Only by allocating this member you will be
     * automatically placing it within a message.
     */

    struct rtp_ext_header_s    *_ext_header;
    /* External header identifiers */
    int                         _exthdr_resolution;
    int                         _exthdr_framerate;

    int                         _max_users;    /* -1 undefined */

    uint64_t                    _packets_sent; /* measure packets */
    uint64_t                    _packets_recv;

    uint64_t                    _bytes_sent;
    uint64_t                    _bytes_recv;

    uint64_t                    _packet_loss;

    const char                 *_last_error;

    struct rtp_dest_list_s     *_dest_list;
    struct rtp_dest_list_s     *_last_user; /* a tail for faster appending */

    struct rtp_msg_s           *_oldest_msg;
    struct rtp_msg_s           *_last_msg; /* tail */

    uint16_t                    _prefix_length;
    uint8_t                    *_prefix;

    /* Specifies multiple session use.
     * When using one session it uses default value ( -1 )
     * Otherwise it's set to 1 and rtp_register_msg () is required
     */
    int                         _multi_session;

    uint32_t                    _current_framerate;

    pthread_mutex_t             _mutex;

} rtp_session_t;


/*
 * Now i don't believe we need to store this _from thing every time
 * since we have csrc table but will leave it like this for a while
 */


void                    rtp_free_msg ( rtp_session_t *_session, struct rtp_msg_s *_msg );
int                     rtp_release_session_recv ( rtp_session_t *_session );

/* Functions handling receiving */
struct rtp_msg_s       *rtp_recv_msg ( rtp_session_t *_session );
void                    rtp_store_msg ( rtp_session_t *_session, struct rtp_msg_s *_msg );

/*
 * rtp_msg_parse() stores headers separately from the payload data
 * and so the _length variable is set accordingly
 */
struct rtp_msg_s       *rtp_msg_parse ( rtp_session_t *_session, const uint8_t *_data, uint32_t _length );

int                     rtp_check_late_message (rtp_session_t *_session, struct rtp_msg_s *_msg);
void                    rtp_register_msg ( rtp_session_t *_session, struct rtp_msg_s * );

/* Functions handling sending */
int                     rtp_send_msg ( rtp_session_t *_session, struct rtp_msg_s *_msg, void *_core_handler );

/*
 * rtp_msg_new() stores headers and payload data in one container ( _data )
 * and the _length is set accordingly. Returned message is used for sending only
 * so there is not much use of the headers there
 */
struct rtp_msg_s       *rtp_msg_new ( rtp_session_t *_session, const uint8_t *_data, uint32_t _length );


/* Convenient functions for creating a header */
struct rtp_header_s    *rtp_build_header ( rtp_session_t *_session );

/* Functions handling session control */

/* Handling an rtp packet */
/* int                  rtp_handlepacket(uint8_t * packet, uint32_t length, IP_Port source); */

/* Session initiation and termination.
 * Set _multi_session to -1 if not using multiple sessions
 */
rtp_session_t          *rtp_init_session ( int _max_users, int _multi_session );
int                     rtp_terminate_session ( rtp_session_t *_session );

/* Adding receiver */
int                     rtp_add_receiver ( rtp_session_t *_session, tox_IP_Port *_dest );

/* Convenient functions for marking the resolution */
int                     rtp_add_resolution_marking ( rtp_session_t *_session, uint16_t _width, uint16_t _height );
int                     rtp_remove_resolution_marking ( rtp_session_t *_session );
uint16_t                rtp_get_resolution_marking_height ( struct rtp_ext_header_s *_header, uint32_t _position );
uint16_t                rtp_get_resolution_marking_width ( struct rtp_ext_header_s *_header, uint32_t _position );

int                     rtp_add_framerate_marking ( rtp_session_t *_session, uint32_t _value );
int                     rtp_remove_framerate_marking ( rtp_session_t *_session );
uint32_t                rtp_get_framerate_marking ( struct rtp_ext_header_s *_header );
/* Convenient functions for marking the payload */
void                    rtp_set_payload_type ( rtp_session_t *_session, uint8_t _payload_value );
uint32_t                rtp_get_payload_type ( rtp_session_t *_session );

/* When using RTP in core be sure to set prefix when sending via rtp_send_msg */
int                     rtp_set_prefix ( rtp_session_t *_session, uint8_t *_prefix, uint16_t _prefix_length );

#endif /* _RTP__IMPL_H_ */