2017-01-14 23:46:31 +08:00
|
|
|
/*
|
2018-01-22 06:38:08 +08:00
|
|
|
* Copyright © 2016-2018 The TokTok team.
|
2017-01-14 23:46:31 +08:00
|
|
|
* Copyright © 2013-2015 Tox project.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2017-01-14 23:46:31 +08:00
|
|
|
* This file is part of Tox, the free peer to peer instant messenger.
|
2014-01-25 08:32:33 +08:00
|
|
|
*
|
2017-01-14 23:46:31 +08:00
|
|
|
* 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.
|
2014-01-25 08:32:33 +08:00
|
|
|
*
|
2017-01-14 23:46:31 +08:00
|
|
|
* 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.
|
2014-01-25 08:32:33 +08:00
|
|
|
*
|
2017-01-14 23:46:31 +08:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
2014-01-25 08:32:33 +08:00
|
|
|
*/
|
2015-02-17 06:30:20 +08:00
|
|
|
#ifndef RTP_H
|
|
|
|
#define RTP_H
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2016-09-01 07:33:20 +08:00
|
|
|
#include "bwcontroller.h"
|
2016-09-11 22:47:51 +08:00
|
|
|
|
|
|
|
#include "../toxcore/Messenger.h"
|
2016-11-15 15:53:47 +08:00
|
|
|
#include "../toxcore/logger.h"
|
2016-09-11 22:47:51 +08:00
|
|
|
|
|
|
|
#include <stdbool.h>
|
2014-01-27 04:02:11 +08:00
|
|
|
|
2018-01-22 06:38:08 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RTPHeader serialised size in bytes.
|
|
|
|
*/
|
|
|
|
#define RTP_HEADER_SIZE 80
|
|
|
|
|
2018-02-02 23:37:19 +08:00
|
|
|
/**
|
|
|
|
* Number of 32 bit padding fields between \ref RTPHeader::offset_lower and
|
|
|
|
* everything before it.
|
|
|
|
*/
|
|
|
|
#define RTP_PADDING_FIELDS 11
|
|
|
|
|
2015-02-18 06:34:40 +08:00
|
|
|
/**
|
2015-10-11 05:54:23 +08:00
|
|
|
* Payload type identifier. Also used as rtp callback prefix.
|
2015-02-18 06:34:40 +08:00
|
|
|
*/
|
2018-08-13 05:53:59 +08:00
|
|
|
typedef enum RTP_Type {
|
2018-08-12 20:53:12 +08:00
|
|
|
RTP_TYPE_AUDIO = 192,
|
|
|
|
RTP_TYPE_VIDEO = 193,
|
2018-08-13 05:53:59 +08:00
|
|
|
} RTP_Type;
|
2018-01-30 00:05:12 +08:00
|
|
|
|
|
|
|
/**
|
2018-01-31 20:13:48 +08:00
|
|
|
* A bit mask (up to 64 bits) specifying features of the current frame affecting
|
2018-01-30 00:05:12 +08:00
|
|
|
* the behaviour of the decoder.
|
|
|
|
*/
|
2018-08-13 05:53:59 +08:00
|
|
|
typedef enum RTPFlags {
|
2018-01-30 00:05:12 +08:00
|
|
|
/**
|
|
|
|
* Support frames larger than 64KiB. The full 32 bit length and offset are
|
|
|
|
* set in \ref RTPHeader::data_length_full and \ref RTPHeader::offset_full.
|
|
|
|
*/
|
|
|
|
RTP_LARGE_FRAME = 1 << 0,
|
2018-01-31 20:13:48 +08:00
|
|
|
/**
|
|
|
|
* Whether the packet is part of a key frame.
|
|
|
|
*/
|
|
|
|
RTP_KEY_FRAME = 1 << 1,
|
2018-08-13 05:53:59 +08:00
|
|
|
} RTPFlags;
|
2015-04-16 08:00:34 +08:00
|
|
|
|
2018-01-20 05:59:42 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
struct RTPHeader {
|
|
|
|
/* Standard RTP header */
|
2018-01-20 05:59:42 +08:00
|
|
|
unsigned ve: 2; /* Version has only 2 bits! */
|
2018-01-22 06:38:08 +08:00
|
|
|
unsigned pe: 1; /* Padding */
|
|
|
|
unsigned xe: 1; /* Extra header */
|
|
|
|
unsigned cc: 4; /* Contributing sources count */
|
|
|
|
|
|
|
|
unsigned ma: 1; /* Marker */
|
|
|
|
unsigned pt: 7; /* Payload type */
|
2015-10-11 05:54:23 +08:00
|
|
|
|
|
|
|
uint16_t sequnum;
|
|
|
|
uint32_t timestamp;
|
|
|
|
uint32_t ssrc;
|
2015-02-18 06:34:40 +08:00
|
|
|
|
2018-01-30 00:05:12 +08:00
|
|
|
/* Non-standard Tox-specific fields */
|
|
|
|
|
|
|
|
/**
|
2018-01-31 20:13:48 +08:00
|
|
|
* Bit mask of \ref RTPFlags setting features of the current frame.
|
2018-01-30 00:05:12 +08:00
|
|
|
*/
|
2018-01-31 20:13:48 +08:00
|
|
|
uint64_t flags;
|
2018-01-30 00:05:12 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The full 32 bit data offset of the current data chunk. The \ref
|
|
|
|
* offset_lower data member contains the lower 16 bits of this value. For
|
|
|
|
* frames smaller than 64KiB, \ref offset_full and \ref offset_lower are
|
|
|
|
* equal.
|
|
|
|
*/
|
|
|
|
uint32_t offset_full;
|
|
|
|
/**
|
|
|
|
* The full 32 bit payload length without header and packet id.
|
|
|
|
*/
|
|
|
|
uint32_t data_length_full;
|
|
|
|
/**
|
|
|
|
* Only the receiver uses this field (why do we have this?).
|
|
|
|
*/
|
|
|
|
uint32_t received_length_full;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data offset of the current part (lower bits).
|
|
|
|
*/
|
|
|
|
uint16_t offset_lower;
|
|
|
|
/**
|
|
|
|
* Total message length (lower bits).
|
|
|
|
*/
|
|
|
|
uint16_t data_length_lower;
|
2018-01-22 06:38:08 +08:00
|
|
|
};
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-20 05:59:42 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
struct RTPMessage {
|
2018-01-20 05:59:42 +08:00
|
|
|
/**
|
|
|
|
* This is used in the old code that doesn't deal with large frames, i.e.
|
|
|
|
* the audio code or receiving code for old 16 bit messages. We use it to
|
|
|
|
* record the number of bytes received so far in a multi-part message. The
|
|
|
|
* multi-part message in the old code is stored in \ref RTPSession::mp.
|
|
|
|
*/
|
2015-10-11 05:54:23 +08:00
|
|
|
uint16_t len;
|
|
|
|
|
|
|
|
struct RTPHeader header;
|
|
|
|
uint8_t data[];
|
2018-01-22 06:38:08 +08:00
|
|
|
};
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2018-01-20 05:59:42 +08:00
|
|
|
#define USED_RTP_WORKBUFFER_COUNT 3
|
|
|
|
|
|
|
|
/**
|
|
|
|
* One slot in the work buffer list. Represents one frame that is currently
|
|
|
|
* being assembled.
|
|
|
|
*/
|
|
|
|
struct RTPWorkBuffer {
|
|
|
|
/**
|
|
|
|
* Whether this slot contains a key frame. This is true iff
|
|
|
|
* buf->header.flags & RTP_KEY_FRAME.
|
|
|
|
*/
|
|
|
|
bool is_keyframe;
|
|
|
|
/**
|
|
|
|
* The number of bytes received so far, regardless of which pieces. I.e. we
|
|
|
|
* could have received the first 1000 bytes and the last 1000 bytes with
|
|
|
|
* 4000 bytes in the middle still to come, and this number would be 2000.
|
|
|
|
*/
|
|
|
|
uint32_t received_len;
|
|
|
|
/**
|
|
|
|
* The message currently being assembled.
|
|
|
|
*/
|
|
|
|
struct RTPMessage *buf;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct RTPWorkBufferList {
|
|
|
|
int8_t next_free_entry;
|
|
|
|
struct RTPWorkBuffer work_buffer[USED_RTP_WORKBUFFER_COUNT];
|
|
|
|
};
|
|
|
|
|
|
|
|
#define DISMISS_FIRST_LOST_VIDEO_PACKET_COUNT 10
|
|
|
|
|
2018-08-18 01:22:18 +08:00
|
|
|
typedef int rtp_m_cb(const Mono_Time *mono_time, void *cs, struct RTPMessage *msg);
|
2018-08-13 05:53:59 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
/**
|
2014-11-29 20:42:19 +08:00
|
|
|
* RTP control session.
|
2014-01-25 08:32:33 +08:00
|
|
|
*/
|
2018-01-20 05:59:42 +08:00
|
|
|
typedef struct RTPSession {
|
2015-04-22 08:09:37 +08:00
|
|
|
uint8_t payload_type;
|
2015-10-11 05:54:23 +08:00
|
|
|
uint16_t sequnum; /* Sending sequence number */
|
|
|
|
uint16_t rsequnum; /* Receiving sequence number */
|
2015-04-22 08:09:37 +08:00
|
|
|
uint32_t rtimestamp;
|
2018-01-20 05:59:42 +08:00
|
|
|
uint32_t ssrc; // this seems to be unused!?
|
2015-10-11 05:54:23 +08:00
|
|
|
struct RTPMessage *mp; /* Expected parted message */
|
2018-01-20 05:59:42 +08:00
|
|
|
struct RTPWorkBufferList *work_buffer_list;
|
|
|
|
uint8_t first_packets_counter; /* dismiss first few lost video packets */
|
2015-04-22 08:09:37 +08:00
|
|
|
Messenger *m;
|
2015-10-11 05:54:23 +08:00
|
|
|
uint32_t friend_number;
|
2015-11-11 17:33:03 +08:00
|
|
|
BWController *bwc;
|
2015-04-22 08:09:37 +08:00
|
|
|
void *cs;
|
2018-08-13 05:53:59 +08:00
|
|
|
rtp_m_cb *mcb;
|
2014-01-25 08:32:33 +08:00
|
|
|
} RTPSession;
|
|
|
|
|
2018-01-20 05:59:42 +08:00
|
|
|
|
2018-01-22 06:38:08 +08:00
|
|
|
/**
|
|
|
|
* Serialise an RTPHeader to bytes to be sent over the network.
|
|
|
|
*
|
|
|
|
* @param rdata A byte array of length RTP_HEADER_SIZE. Does not need to be
|
|
|
|
* initialised. All RTP_HEADER_SIZE bytes will be initialised after a call
|
|
|
|
* to this function.
|
|
|
|
* @param header The RTPHeader to serialise.
|
|
|
|
*/
|
|
|
|
size_t rtp_header_pack(uint8_t *rdata, const struct RTPHeader *header);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deserialise an RTPHeader from bytes received over the network.
|
|
|
|
*
|
|
|
|
* @param data A byte array of length RTP_HEADER_SIZE.
|
|
|
|
* @param header The RTPHeader to write the unpacked values to.
|
|
|
|
*/
|
|
|
|
size_t rtp_header_unpack(const uint8_t *data, struct RTPHeader *header);
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2016-09-13 04:37:58 +08:00
|
|
|
RTPSession *rtp_new(int payload_type, Messenger *m, uint32_t friendnumber,
|
2018-08-13 05:53:59 +08:00
|
|
|
BWController *bwc, void *cs, rtp_m_cb *mcb);
|
2016-09-13 04:37:58 +08:00
|
|
|
void rtp_kill(RTPSession *session);
|
|
|
|
int rtp_allow_receiving(RTPSession *session);
|
|
|
|
int rtp_stop_receiving(RTPSession *session);
|
2018-01-20 05:59:42 +08:00
|
|
|
/**
|
|
|
|
* Send a frame of audio or video data, chunked in \ref RTPMessage instances.
|
|
|
|
*
|
|
|
|
* @param session The A/V session to send the data for.
|
|
|
|
* @param data A byte array of length \p length.
|
|
|
|
* @param length The number of bytes to send from @p data.
|
|
|
|
* @param is_keyframe Whether this video frame is a key frame. If it is an
|
|
|
|
* audio frame, this parameter is ignored.
|
|
|
|
*/
|
|
|
|
int rtp_send_data(RTPSession *session, const uint8_t *data, uint32_t length,
|
2018-06-24 01:16:28 +08:00
|
|
|
bool is_keyframe, const Logger *log);
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2018-01-22 06:38:08 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
} // extern "C"
|
|
|
|
#endif
|
|
|
|
|
2015-02-17 06:30:20 +08:00
|
|
|
#endif /* RTP_H */
|