2020-03-13 12:35:07 +00:00
|
|
|
/**
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013-2020 Winlin
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
* this software and associated documentation files (the "Software"), to deal in
|
|
|
|
* the Software without restriction, including without limitation the rights to
|
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2020-05-11 04:07:55 +00:00
|
|
|
#ifndef SRS_KERNEL_RTC_RTP_HPP
|
|
|
|
#define SRS_KERNEL_RTC_RTP_HPP
|
2020-03-13 12:35:07 +00:00
|
|
|
|
|
|
|
#include <srs_core.hpp>
|
|
|
|
|
2020-04-11 15:57:04 +00:00
|
|
|
#include <srs_kernel_buffer.hpp>
|
|
|
|
#include <srs_kernel_codec.hpp>
|
|
|
|
|
2020-03-13 12:35:07 +00:00
|
|
|
#include <string>
|
|
|
|
|
2020-05-02 01:15:49 +00:00
|
|
|
class SrsRtpPacket2;
|
|
|
|
|
2020-05-13 07:27:31 +00:00
|
|
|
// The RTP packet max size, should never exceed this size.
|
|
|
|
const int kRtpPacketSize = 1500;
|
|
|
|
|
|
|
|
const int kRtpHeaderFixedSize = 12;
|
|
|
|
const uint8_t kRtpMarker = 0x80;
|
2020-04-08 06:45:26 +00:00
|
|
|
|
2020-04-11 15:57:04 +00:00
|
|
|
// H.264 nalu header type mask.
|
|
|
|
const uint8_t kNalTypeMask = 0x1F;
|
|
|
|
|
2020-04-23 09:08:21 +00:00
|
|
|
// @see: https://tools.ietf.org/html/rfc6184#section-5.2
|
|
|
|
const uint8_t kStapA = 24;
|
|
|
|
|
|
|
|
// @see: https://tools.ietf.org/html/rfc6184#section-5.2
|
|
|
|
const uint8_t kFuA = 28;
|
|
|
|
|
|
|
|
// @see: https://tools.ietf.org/html/rfc6184#section-5.8
|
|
|
|
const uint8_t kStart = 0x80; // Fu-header start bit
|
|
|
|
const uint8_t kEnd = 0x40; // Fu-header end bit
|
|
|
|
|
|
|
|
|
2020-04-08 06:45:26 +00:00
|
|
|
class SrsBuffer;
|
2020-04-16 06:28:59 +00:00
|
|
|
class SrsRtpRawPayload;
|
2020-04-18 12:37:08 +00:00
|
|
|
class SrsRtpFUAPayload2;
|
2020-05-14 01:33:00 +00:00
|
|
|
class SrsSharedPtrMessage;
|
2020-04-08 06:45:26 +00:00
|
|
|
|
|
|
|
class SrsRtpHeader
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
bool padding;
|
2020-04-23 09:08:21 +00:00
|
|
|
uint8_t padding_length;
|
2020-04-08 06:45:26 +00:00
|
|
|
bool extension;
|
|
|
|
uint8_t cc;
|
|
|
|
bool marker;
|
|
|
|
uint8_t payload_type;
|
|
|
|
uint16_t sequence;
|
2020-05-04 06:47:58 +00:00
|
|
|
uint32_t timestamp;
|
2020-04-08 06:45:26 +00:00
|
|
|
uint32_t ssrc;
|
|
|
|
uint32_t csrc[15];
|
|
|
|
uint16_t extension_length;
|
|
|
|
// TODO:extension field.
|
|
|
|
public:
|
|
|
|
SrsRtpHeader();
|
|
|
|
virtual ~SrsRtpHeader();
|
2020-04-16 05:13:02 +00:00
|
|
|
void reset();
|
2020-04-08 06:45:26 +00:00
|
|
|
public:
|
2020-05-02 01:15:49 +00:00
|
|
|
virtual srs_error_t decode(SrsBuffer* buf);
|
|
|
|
virtual srs_error_t encode(SrsBuffer* buf);
|
|
|
|
virtual int nb_bytes();
|
2020-04-08 06:45:26 +00:00
|
|
|
public:
|
2020-05-02 01:48:04 +00:00
|
|
|
void set_marker(bool v);
|
|
|
|
bool get_marker() const;
|
|
|
|
void set_payload_type(uint8_t v);
|
|
|
|
uint8_t get_payload_type() const;
|
|
|
|
void set_sequence(uint16_t v);
|
|
|
|
uint16_t get_sequence() const;
|
2020-05-04 06:47:58 +00:00
|
|
|
void set_timestamp(uint32_t v);
|
|
|
|
uint32_t get_timestamp() const;
|
2020-05-02 01:48:04 +00:00
|
|
|
void set_ssrc(uint32_t v);
|
|
|
|
uint32_t get_ssrc() const;
|
|
|
|
void set_padding(bool v);
|
|
|
|
void set_padding_length(uint8_t v);
|
|
|
|
uint8_t get_padding_length() const;
|
2020-04-08 06:45:26 +00:00
|
|
|
};
|
2020-03-18 00:45:20 +00:00
|
|
|
|
2020-05-14 06:26:19 +00:00
|
|
|
class ISrsRtpPayloader : public ISrsCodec
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ISrsRtpPayloader();
|
|
|
|
virtual ~ISrsRtpPayloader();
|
|
|
|
public:
|
|
|
|
virtual ISrsRtpPayloader* copy() = 0;
|
|
|
|
};
|
|
|
|
|
2020-05-02 01:15:49 +00:00
|
|
|
class ISrsRtpPacketDecodeHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ISrsRtpPacketDecodeHandler();
|
|
|
|
virtual ~ISrsRtpPacketDecodeHandler();
|
|
|
|
public:
|
|
|
|
// We don't know the actual payload, so we depends on external handler.
|
2020-05-14 06:26:19 +00:00
|
|
|
virtual void on_before_decode_payload(SrsRtpPacket2* pkt, SrsBuffer* buf, ISrsRtpPayloader** ppayload) = 0;
|
2020-05-02 01:15:49 +00:00
|
|
|
};
|
|
|
|
|
2020-04-11 14:54:44 +00:00
|
|
|
class SrsRtpPacket2
|
|
|
|
{
|
2020-05-02 01:15:49 +00:00
|
|
|
// RTP packet fields.
|
2020-04-11 14:54:44 +00:00
|
|
|
public:
|
2020-05-02 01:15:49 +00:00
|
|
|
// TODO: FIXME: Rename to header.
|
2020-04-11 14:54:44 +00:00
|
|
|
SrsRtpHeader rtp_header;
|
2020-05-14 06:26:19 +00:00
|
|
|
ISrsRtpPayloader* payload;
|
2020-05-02 01:15:49 +00:00
|
|
|
// TODO: FIXME: Merge into rtp_header.
|
2020-04-13 05:44:55 +00:00
|
|
|
int padding;
|
2020-05-13 12:13:25 +00:00
|
|
|
// Helper fields.
|
2020-05-02 01:15:49 +00:00
|
|
|
public:
|
|
|
|
// The first byte as nalu type, for video decoder only.
|
|
|
|
SrsAvcNaluType nalu_type;
|
2020-05-14 06:30:32 +00:00
|
|
|
// The original shared message, all RTP packets can refer to its data.
|
|
|
|
SrsSharedPtrMessage* shared_msg;
|
2020-05-13 12:13:25 +00:00
|
|
|
// The frame type, for RTMP bridger or SFU source.
|
|
|
|
SrsFrameType frame_type;
|
2020-05-02 01:15:49 +00:00
|
|
|
// Fast cache for performance.
|
2020-04-16 06:28:59 +00:00
|
|
|
private:
|
2020-05-02 01:15:49 +00:00
|
|
|
// Cache frequently used payload for performance.
|
2020-04-16 06:28:59 +00:00
|
|
|
SrsRtpRawPayload* cache_raw;
|
2020-04-18 12:37:08 +00:00
|
|
|
SrsRtpFUAPayload2* cache_fua;
|
|
|
|
int cache_payload;
|
2020-05-02 01:15:49 +00:00
|
|
|
// The helper handler for decoder, use RAW payload if NULL.
|
|
|
|
ISrsRtpPacketDecodeHandler* decode_handler;
|
2020-04-11 15:57:04 +00:00
|
|
|
public:
|
|
|
|
SrsRtpPacket2();
|
|
|
|
virtual ~SrsRtpPacket2();
|
|
|
|
public:
|
2020-04-17 10:04:52 +00:00
|
|
|
// Set the padding of RTP packet.
|
2020-04-15 14:46:06 +00:00
|
|
|
void set_padding(int size);
|
2020-04-17 10:04:52 +00:00
|
|
|
// Increase the padding of RTP packet.
|
|
|
|
void add_padding(int size);
|
2020-04-15 14:46:06 +00:00
|
|
|
// Reset RTP packet.
|
|
|
|
void reset();
|
2020-04-16 06:28:59 +00:00
|
|
|
// Reuse the cached raw message as payload.
|
|
|
|
SrsRtpRawPayload* reuse_raw();
|
2020-04-16 06:51:36 +00:00
|
|
|
// Reuse the cached fua message as payload.
|
2020-04-18 12:37:08 +00:00
|
|
|
SrsRtpFUAPayload2* reuse_fua();
|
2020-05-02 01:15:49 +00:00
|
|
|
// Set the decode handler.
|
2020-05-02 01:48:04 +00:00
|
|
|
void set_decode_handler(ISrsRtpPacketDecodeHandler* h);
|
2020-05-13 12:13:25 +00:00
|
|
|
// Whether the packet is Audio packet.
|
|
|
|
bool is_audio();
|
2020-05-14 06:26:19 +00:00
|
|
|
// Copy the RTP packet.
|
|
|
|
SrsRtpPacket2* copy();
|
2020-04-13 05:44:55 +00:00
|
|
|
// interface ISrsEncoder
|
|
|
|
public:
|
|
|
|
virtual int nb_bytes();
|
2020-04-11 15:57:04 +00:00
|
|
|
virtual srs_error_t encode(SrsBuffer* buf);
|
2020-05-02 01:15:49 +00:00
|
|
|
virtual srs_error_t decode(SrsBuffer* buf);
|
2020-04-11 15:57:04 +00:00
|
|
|
};
|
|
|
|
|
2020-04-13 11:23:17 +00:00
|
|
|
// Single payload data.
|
2020-05-14 06:26:19 +00:00
|
|
|
class SrsRtpRawPayload : public ISrsRtpPayloader
|
2020-04-11 15:57:04 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-04-18 12:37:08 +00:00
|
|
|
// The RAW payload, directly point to the shared memory.
|
|
|
|
// @remark We only refer to the memory, user must free its bytes.
|
2020-04-11 14:54:44 +00:00
|
|
|
char* payload;
|
|
|
|
int nn_payload;
|
|
|
|
public:
|
2020-04-11 15:57:04 +00:00
|
|
|
SrsRtpRawPayload();
|
|
|
|
virtual ~SrsRtpRawPayload();
|
2020-05-14 06:26:19 +00:00
|
|
|
// interface ISrsRtpPayloader
|
2020-04-11 15:57:04 +00:00
|
|
|
public:
|
|
|
|
virtual int nb_bytes();
|
|
|
|
virtual srs_error_t encode(SrsBuffer* buf);
|
2020-05-02 01:15:49 +00:00
|
|
|
virtual srs_error_t decode(SrsBuffer* buf);
|
2020-05-14 06:26:19 +00:00
|
|
|
virtual ISrsRtpPayloader* copy();
|
2020-04-11 15:57:04 +00:00
|
|
|
};
|
|
|
|
|
2020-04-13 11:23:17 +00:00
|
|
|
// Multiple NALUs, automatically insert 001 between NALUs.
|
2020-05-14 06:26:19 +00:00
|
|
|
class SrsRtpRawNALUs : public ISrsRtpPayloader
|
2020-04-13 11:23:17 +00:00
|
|
|
{
|
|
|
|
private:
|
2020-04-18 12:37:08 +00:00
|
|
|
// We will manage the samples, but the sample itself point to the shared memory.
|
2020-04-13 11:23:17 +00:00
|
|
|
std::vector<SrsSample*> nalus;
|
|
|
|
int nn_bytes;
|
|
|
|
int cursor;
|
|
|
|
public:
|
|
|
|
SrsRtpRawNALUs();
|
|
|
|
virtual ~SrsRtpRawNALUs();
|
|
|
|
public:
|
|
|
|
void push_back(SrsSample* sample);
|
|
|
|
public:
|
|
|
|
uint8_t skip_first_byte();
|
2020-04-18 12:37:08 +00:00
|
|
|
// We will manage the returned samples, if user want to manage it, please copy it.
|
|
|
|
srs_error_t read_samples(std::vector<SrsSample*>& samples, int packet_size);
|
2020-05-14 06:26:19 +00:00
|
|
|
// interface ISrsRtpPayloader
|
2020-04-13 11:23:17 +00:00
|
|
|
public:
|
|
|
|
virtual int nb_bytes();
|
|
|
|
virtual srs_error_t encode(SrsBuffer* buf);
|
2020-05-02 01:15:49 +00:00
|
|
|
virtual srs_error_t decode(SrsBuffer* buf);
|
2020-05-14 06:26:19 +00:00
|
|
|
virtual ISrsRtpPayloader* copy();
|
2020-04-13 11:23:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// STAP-A, for multiple NALUs.
|
2020-05-14 06:26:19 +00:00
|
|
|
class SrsRtpSTAPPayload : public ISrsRtpPayloader
|
2020-04-11 15:57:04 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// The NRI in NALU type.
|
|
|
|
SrsAvcNaluType nri;
|
2020-04-19 01:32:09 +00:00
|
|
|
// The NALU samples, we will manage the samples.
|
2020-04-11 15:57:04 +00:00
|
|
|
// @remark We only refer to the memory, user must free its bytes.
|
2020-04-11 17:05:11 +00:00
|
|
|
std::vector<SrsSample*> nalus;
|
2020-04-11 15:57:04 +00:00
|
|
|
public:
|
|
|
|
SrsRtpSTAPPayload();
|
|
|
|
virtual ~SrsRtpSTAPPayload();
|
2020-05-02 12:57:36 +00:00
|
|
|
public:
|
|
|
|
SrsSample* get_sps();
|
|
|
|
SrsSample* get_pps();
|
2020-05-14 06:26:19 +00:00
|
|
|
// interface ISrsRtpPayloader
|
2020-04-11 17:01:39 +00:00
|
|
|
public:
|
|
|
|
virtual int nb_bytes();
|
|
|
|
virtual srs_error_t encode(SrsBuffer* buf);
|
2020-05-02 01:15:49 +00:00
|
|
|
virtual srs_error_t decode(SrsBuffer* buf);
|
2020-05-14 06:26:19 +00:00
|
|
|
virtual ISrsRtpPayloader* copy();
|
2020-04-11 17:01:39 +00:00
|
|
|
};
|
|
|
|
|
2020-04-13 11:23:17 +00:00
|
|
|
// FU-A, for one NALU with multiple fragments.
|
2020-04-18 12:37:08 +00:00
|
|
|
// With more than one payload.
|
2020-05-14 06:26:19 +00:00
|
|
|
class SrsRtpFUAPayload : public ISrsRtpPayloader
|
2020-04-11 17:01:39 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// The NRI in NALU type.
|
|
|
|
SrsAvcNaluType nri;
|
|
|
|
// The FUA header.
|
|
|
|
bool start;
|
|
|
|
bool end;
|
|
|
|
SrsAvcNaluType nalu_type;
|
2020-04-19 01:32:09 +00:00
|
|
|
// The NALU samples, we manage the samples.
|
2020-04-11 17:01:39 +00:00
|
|
|
// @remark We only refer to the memory, user must free its bytes.
|
|
|
|
std::vector<SrsSample*> nalus;
|
|
|
|
public:
|
|
|
|
SrsRtpFUAPayload();
|
|
|
|
virtual ~SrsRtpFUAPayload();
|
2020-05-14 06:26:19 +00:00
|
|
|
// interface ISrsRtpPayloader
|
2020-04-16 06:51:36 +00:00
|
|
|
public:
|
2020-04-18 12:37:08 +00:00
|
|
|
virtual int nb_bytes();
|
|
|
|
virtual srs_error_t encode(SrsBuffer* buf);
|
2020-05-02 01:15:49 +00:00
|
|
|
virtual srs_error_t decode(SrsBuffer* buf);
|
2020-05-14 06:26:19 +00:00
|
|
|
virtual ISrsRtpPayloader* copy();
|
2020-04-18 12:37:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// FU-A, for one NALU with multiple fragments.
|
|
|
|
// With only one payload.
|
2020-05-14 06:26:19 +00:00
|
|
|
class SrsRtpFUAPayload2 : public ISrsRtpPayloader
|
2020-04-18 12:37:08 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// The NRI in NALU type.
|
|
|
|
SrsAvcNaluType nri;
|
|
|
|
// The FUA header.
|
|
|
|
bool start;
|
|
|
|
bool end;
|
|
|
|
SrsAvcNaluType nalu_type;
|
|
|
|
// The payload and size,
|
|
|
|
char* payload;
|
|
|
|
int size;
|
|
|
|
public:
|
|
|
|
SrsRtpFUAPayload2();
|
|
|
|
virtual ~SrsRtpFUAPayload2();
|
2020-05-14 06:26:19 +00:00
|
|
|
// interface ISrsRtpPayloader
|
2020-04-11 14:54:44 +00:00
|
|
|
public:
|
2020-04-11 15:57:04 +00:00
|
|
|
virtual int nb_bytes();
|
|
|
|
virtual srs_error_t encode(SrsBuffer* buf);
|
2020-05-02 01:15:49 +00:00
|
|
|
virtual srs_error_t decode(SrsBuffer* buf);
|
2020-05-14 06:26:19 +00:00
|
|
|
virtual ISrsRtpPayloader* copy();
|
2020-04-11 14:54:44 +00:00
|
|
|
};
|
|
|
|
|
2020-03-13 12:35:07 +00:00
|
|
|
#endif
|