Skip to content

Commit

Permalink
clean up a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
richlegrand committed Jul 19, 2024
1 parent 9451528 commit f6f8434
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 80 deletions.
20 changes: 0 additions & 20 deletions src/mediacodec.h

This file was deleted.

47 changes: 47 additions & 0 deletions src/peer_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,54 @@
#include <unistd.h>
#include <inttypes.h>

#include "sctp.h"
#include "agent.h"
#include "dtls_srtp.h"
#include "sdp.h"
#include "config.h"
#include "rtp.h"
#include "rtcp.h"
#include "buffer.h"
#include "ports.h"
#include "peer_connection.h"

#define STATE_CHANGED(pc, curr_state) if(pc->oniceconnectionstatechange && pc->state != curr_state) { pc->oniceconnectionstatechange(curr_state, pc->config.user_data); pc->state = curr_state; }

struct PeerConnection {

PeerConfiguration config;
PeerConnectionState state;
Agent agent;
DtlsSrtp dtls_srtp;
Sctp sctp;

Sdp local_sdp;
Sdp remote_sdp;

void (*onicecandidate)(char *sdp, void *user_data);
void (*oniceconnectionstatechange)(PeerConnectionState state, void *user_data);
void (*on_connected)(void *userdata);
void (*on_receiver_packet_loss)(float fraction_loss, uint32_t total_loss, void *user_data);

uint8_t temp_buf[CONFIG_MTU];
uint8_t agent_buf[CONFIG_MTU];
int agent_ret;
int b_offer_created;

Buffer *audio_rb;
Buffer *video_rb;
Buffer *data_rb;

RtpEncoder artp_encoder;
RtpEncoder vrtp_encoder;
RtpDecoder vrtp_decoder;
RtpDecoder artp_decoder;

uint32_t remote_assrc;
uint32_t remote_vssrc;

};

static void peer_connection_outgoing_rtp_packet(uint8_t *data, size_t size, void *user_data) {

PeerConnection *pc = (PeerConnection *) user_data;
Expand Down Expand Up @@ -123,6 +164,11 @@ PeerConnectionState peer_connection_get_state(PeerConnection *pc) {
return pc->state;
}

Sctp *peer_connection_get_sctp(PeerConnection *pc) {

return &pc->sctp;
}

PeerConnection* peer_connection_create(PeerConfiguration *config) {

PeerConnection *pc = calloc(1, sizeof(PeerConnection));
Expand Down Expand Up @@ -307,6 +353,7 @@ static void peer_connection_state_new(PeerConnection *pc) {
}

int peer_connection_loop(PeerConnection *pc) {

int bytes;
uint8_t *data = NULL;
uint32_t ssrc = 0;
Expand Down
59 changes: 19 additions & 40 deletions src/peer_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@
#include <stdlib.h>
#include <stdint.h>
#include "sctp.h"
#include "agent.h"
#include "dtls_srtp.h"
#include "sdp.h"
#include "mediacodec.h"
#include "rtp.h"
#include "buffer.h"

#ifdef __cplusplus
extern "C" {
Expand All @@ -39,6 +33,22 @@ typedef enum DataChannelType {

} DataChannelType;

typedef enum MediaCodec {

CODEC_NONE = 0,

/* Video */
CODEC_H264,
CODEC_VP8, // not implemented yet
CODEC_MJPEG, // not implemented yet

/* Audio */
CODEC_OPUS, // not implemented yet
CODEC_PCMA,
CODEC_PCMU,

} MediaCodec;

typedef struct IceServer {

const char *urls;
Expand All @@ -62,45 +72,14 @@ typedef struct PeerConfiguration {

} PeerConfiguration;

typedef struct PeerConnection {

PeerConfiguration config;
PeerConnectionState state;
Agent agent;
DtlsSrtp dtls_srtp;
Sctp sctp;

Sdp local_sdp;
Sdp remote_sdp;

void (*onicecandidate)(char *sdp, void *user_data);
void (*oniceconnectionstatechange)(PeerConnectionState state, void *user_data);
void (*on_connected)(void *userdata);
void (*on_receiver_packet_loss)(float fraction_loss, uint32_t total_loss, void *user_data);

uint8_t temp_buf[CONFIG_MTU];
uint8_t agent_buf[CONFIG_MTU];
int agent_ret;
int b_offer_created;

Buffer *audio_rb;
Buffer *video_rb;
Buffer *data_rb;

RtpEncoder artp_encoder;
RtpEncoder vrtp_encoder;
RtpDecoder vrtp_decoder;
RtpDecoder artp_decoder;

uint32_t remote_assrc;
uint32_t remote_vssrc;

} PeerConnection;
typedef struct PeerConnection PeerConnection;

const char* peer_connection_state_to_string(PeerConnectionState state);

PeerConnectionState peer_connection_get_state(PeerConnection *pc);

Sctp *peer_connection_get_sctp(PeerConnection *pc);

PeerConnection* peer_connection_create(PeerConfiguration *config);

void peer_connection_destroy(PeerConnection *pc);
Expand Down
2 changes: 1 addition & 1 deletion src/rtp.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <stdint.h>
#include <endian.h>

#include "mediacodec.h"
#include "peer_connection.h"
#include "config.h"

#ifdef ESP32
Expand Down
24 changes: 7 additions & 17 deletions src/sctp.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#define DATA_CHANNEL_PPID_BINARY_PARTIAL 52
#define DATA_CHANNEL_PPID_BINARY 53
#define DATA_CHANNEL_PPID_DOMSTRING_PARTIAL 54
#define DCEP_PPID 0x32
#define DATA_CHANNEL_OPEN 0x03

static const uint32_t crc32c_table[256] = {
Expand Down Expand Up @@ -179,7 +178,7 @@ int sctp_outgoing_data(Sctp *sctp, char *buf, size_t len, SctpDataPpid ppid, uin
return len;
}

void add_stream_mapping(Sctp *sctp, const char *label, uint16_t sid) {
void sctp_add_stream_mapping(Sctp *sctp, const char *label, uint16_t sid) {
if (sctp->stream_count<SCTP_MAX_STREAMS) {
strncpy(sctp->stream_table[sctp->stream_count].label, label, sizeof(sctp->stream_table[sctp->stream_count].label));
sctp->stream_table[sctp->stream_count].sid = sid;
Expand All @@ -188,7 +187,7 @@ void add_stream_mapping(Sctp *sctp, const char *label, uint16_t sid) {
LOGE("Stream table full. Cannot add more streams.");
}

void parse_data_channel_open(Sctp *sctp, uint16_t sid, char *data, size_t length) {
void sctp_parse_data_channel_open(Sctp *sctp, uint16_t sid, char *data, size_t length) {
if (length < 12)
return; // Not enough data for a DATA_CHANNEL_OPEN message

Expand All @@ -211,11 +210,11 @@ void parse_data_channel_open(Sctp *sctp, uint16_t sid, char *data, size_t length
printf("DATA_CHANNEL_OPEN: Label=%s, sid=%d\n", label_str, sid);

// Add stream mapping
add_stream_mapping(sctp, label_str, sid);
sctp_add_stream_mapping(sctp, label_str, sid);
}
}

void handle_sctp_packet(Sctp *sctp, char *buf, size_t len) {
void sctp_handle_sctp_packet(Sctp *sctp, char *buf, size_t len) {
if (len<=29)
return;

Expand All @@ -225,16 +224,8 @@ void handle_sctp_packet(Sctp *sctp, char *buf, size_t len) {
uint16_t sid = ntohs(*(uint16_t *)(buf + 20));
uint32_t ppid = ntohl(*(uint32_t *)(buf + 24));

if (ppid==DCEP_PPID)
parse_data_channel_open(sctp, sid, buf + 28, len - 28);
}

void print_hex_buffer(uint8_t *buf, int len) {
printf("data (%d): ", len);
for (int i = 0; i < len; i++) {
printf("%02X ", buf[i]);
}
printf("\n");
if (ppid==DATA_CHANNEL_PPID_CONTROL)
sctp_parse_data_channel_open(sctp, sid, buf + 28, len - 28);
}

void sctp_incoming_data(Sctp *sctp, char *buf, size_t len) {
Expand All @@ -243,8 +234,7 @@ void sctp_incoming_data(Sctp *sctp, char *buf, size_t len) {
return;

#ifdef HAVE_USRSCTP
//print_hex_buffer((uint8_t *)buf, len);
handle_sctp_packet(sctp, buf, len);
sctp_handle_sctp_packet(sctp, buf, len);
usrsctp_conninput(sctp, buf, len, 0);
#else
size_t length = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/sctp.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ typedef enum SctpDataPpid {
typedef struct {
char label[32]; // Stream label
uint16_t sid; // Stream ID
} Stream_entry;
} SctpStreamEntry;

typedef struct Sctp {

Expand All @@ -155,7 +155,7 @@ typedef struct Sctp {
DtlsSrtp *dtls_srtp;
Buffer **data_rb;
int stream_count;
Stream_entry stream_table[SCTP_MAX_STREAMS];
SctpStreamEntry stream_table[SCTP_MAX_STREAMS];

/* datachannel */
void (*onmessage)(char *msg, size_t len, void *userdata, uint16_t sid);
Expand Down

0 comments on commit f6f8434

Please sign in to comment.