1.Implementation for rtp session module; 2.Separate signal server from main project

This commit is contained in:
dijunkun
2023-09-06 11:29:34 +08:00
parent 213318bfa3
commit cea0cfdb95
15 changed files with 179 additions and 29 deletions

View File

@@ -79,7 +79,7 @@ int VideoEncoder::Encode(const uint8_t *pData, int nSize) {
return -1;
}
if (0 == seq_++ % (30 * 5)) {
if (0 == seq_++ % (30)) {
ForceIdr();
}

View File

@@ -1,7 +1,8 @@
#ifndef _RTP_PACKET_H_
#define _RTP_PACKET_H_
#include <cstring>
#include <stdint.h>
#include <vector>
// 0 1 2 3
@@ -39,16 +40,18 @@ class RtpPacket {
public:
// Set Header
void SetMarker(bool has_marker) { marker_ = has_marker; }
void SetPayloadType(uint8_t payload_type) { payload_type_ = payload_type; }
void SetVerion(uint32_t version) { version_ = version; }
void SetHasPadding(bool has_padding) { has_padding_ = has_padding; }
void SetHasExtension(bool has_extension) { has_extension_ = has_extension; }
void SetMarker(bool marker) { marker_ = marker; }
void SetPayloadType(uint32_t payload_type) { payload_type_ = payload_type; }
void SetSequenceNumber(uint16_t sequence_number) {
sequence_number_ = sequence_number;
}
void SetTimestamp(uint32_t timestamp) { timestamp_ = timestamp; }
void SetSsrc(uint32_t ssrc) { ssrc_ = ssrc; }
void SetCsrcs(){};
void SetCsrcs(std::vector<uint32_t> &csrcs) { csrcs_ = csrcs; }
void SetHasExtension(bool has_extension) { has_extension_ = has_extension; };
void SetExtensionProfile(uint16_t extension_profile) {
extension_profile_ = extension_profile;
}
@@ -64,19 +67,21 @@ class RtpPacket {
public:
// Get Header
bool Marker() const { return marker_; }
uint8_t PayloadType() const { return payload_type_; }
uint16_t SequenceNumber() const { return sequence_number_; }
uint32_t Timestamp() const { return timestamp_; }
uint32_t Ssrc() const { return ssrc_; }
std::vector<uint32_t> Csrcs() const;
bool HasExtension() const { return has_extension_; };
uint16_t ExtensionProfile() const { return extension_profile_; }
uint8_t *ExtensionData() const { return extension_data_; }
const uint32_t Verion() { return version_; }
const bool HasPadding() { return has_padding_; }
const bool HasExtension() { return has_extension_; }
const bool Marker() { return marker_; }
const uint32_t PayloadType() { return payload_type_; }
const uint16_t SequenceNumber() { return sequence_number_; }
const uint32_t Timestamp() { return timestamp_; }
const uint32_t Ssrc() { return ssrc_; }
const std::vector<uint32_t> Csrcs() { return csrcs_; };
const uint16_t ExtensionProfile() { return extension_profile_; }
const uint8_t *ExtensionData() { return extension_data_; }
// Payload
uint8_t *Payload() const { return payload_; };
size_t PayloadSize() const { return payload_size_; }
const uint8_t *Payload() { return payload_; };
const size_t PayloadSize() { return payload_size_; }
public:
const uint8_t *Buffer() { return buffer_; }
@@ -89,7 +94,7 @@ class RtpPacket {
bool has_extension_ = false;
uint32_t total_csrc_number_ = 0;
bool marker_ = false;
uint8_t payload_type_ = 0;
uint32_t payload_type_ = 0;
uint16_t sequence_number_ = 0;
uint32_t timestamp_ = 0;
uint32_t ssrc_ = 0;

55
src/rtp/rtp_session.cpp Normal file
View File

@@ -0,0 +1,55 @@
#include "rtp_session.h"
#include <chrono>
#define RTP_VERSION 1
RtpSession ::RtpSession(uint32_t payload_type)
: version_(RTP_VERSION),
has_padding_(false),
has_extension_(false),
payload_type_(payload_type),
sequence_number_(0) {}
RtpSession ::~RtpSession() {
if (extension_data_) {
delete extension_data_;
extension_data_ = nullptr;
}
if (rtp_packet_) {
delete rtp_packet_;
rtp_packet_ = nullptr;
}
}
RtpPacket RtpSession::Encode(uint8_t* buffer, size_t size) {
if (!rtp_packet_) {
rtp_packet_ = new RtpPacket();
}
rtp_packet_->SetVerion(version_);
rtp_packet_->SetHasPadding(has_padding_);
rtp_packet_->SetHasExtension(has_extension_);
rtp_packet_->SetMarker(marker_);
rtp_packet_->SetPayloadType(payload_type_);
rtp_packet_->SetSequenceNumber(sequence_number_++);
timestamp_ =
std::chrono::high_resolution_clock::now().time_since_epoch().count();
rtp_packet_->SetTimestamp(timestamp_);
rtp_packet_->SetSsrc(ssrc_);
if (!csrcs_.empty()) {
rtp_packet_->SetCsrcs(csrcs_);
}
if (has_extension_) {
rtp_packet_->SetExtensionProfile(extension_profile_);
rtp_packet_->SetExtensionData(extension_data_, extension_len_);
}
rtp_packet_->Encode(buffer, size);
return *rtp_packet_;
}

40
src/rtp/rtp_session.h Normal file
View File

@@ -0,0 +1,40 @@
#ifndef _RTP_SESSION_H_
#define _RTP_SESSION_H_
#include <stdint.h>
#include <vector>
#include "rtp_packet.h"
class RtpSession
{
public:
RtpSession(uint32_t payload_type);
~RtpSession();
public:
RtpPacket Encode(uint8_t* buffer, size_t size);
private:
uint32_t version_ = 0;
bool has_padding_ = false;
bool has_extension_ = false;
uint32_t total_csrc_number_ = 0;
bool marker_ = false;
uint8_t payload_type_ = 0;
uint16_t sequence_number_ = 0;
uint32_t timestamp_ = 0;
uint32_t ssrc_ = 0;
std::vector<uint32_t> csrcs_;
uint16_t profile_ = 0;
uint16_t extension_profile_ = 0;
uint16_t extension_len_ = 0;
uint8_t* extension_data_ = nullptr;
private:
RtpPacket* rtp_packet_ = nullptr;
};
#endif

View File

@@ -29,6 +29,11 @@ IceTransmission::IceTransmission(
on_receive_ice_msg_cb_(on_receive_ice_msg) {}
IceTransmission::~IceTransmission() {
if (rtp_session_) {
delete rtp_session_;
rtp_session_ = nullptr;
}
if (ice_agent_) {
delete ice_agent_;
ice_agent_ = nullptr;
@@ -107,6 +112,7 @@ int IceTransmission::InitIceTransmission(std::string &ip, int port) {
ikcp_release(kcp);
});
rtp_session_ = new RtpSession(1);
ice_agent_ = new IceAgent(ip, port);
ice_agent_->CreateIceAgent(
@@ -269,12 +275,14 @@ int IceTransmission::SendData(const char *data, size_t size) {
int num = 0;
for (num = 0; num * 1400 < size; num++) {
RtpPacket packet((uint8_t *)(data + num * 1400), 1400);
RtpPacket packet =
rtp_session_->Encode((uint8_t *)(data + num * 1400), 1400);
send_ringbuffer_.push(packet);
}
if (size - num * 1400 > 0) {
RtpPacket packet((uint8_t *)(data + num * 1400), size - num * 1400);
if (size > num * 1400 && size - num * 1400 > 0) {
RtpPacket packet = rtp_session_->Encode((uint8_t *)(data + num * 1400),
size - num * 1400);
send_ringbuffer_.push(packet);
}
}

View File

@@ -7,6 +7,7 @@
#include "ice_agent.h"
#include "ringbuffer.h"
#include "rtp_packet.h"
#include "rtp_session.h"
#include "ws_transmission.h"
class IceTransmission {
public:
@@ -72,6 +73,9 @@ class IceTransmission {
RingBuffer<RtpPacket> recv_ringbuffer_;
bool kcp_stop_ = false;
std::thread *kcp_update_thread_ = nullptr;
private:
RtpSession *rtp_session_ = nullptr;
};
#endif