diff --git a/src/mediacodec.h b/src/mediacodec.h deleted file mode 100644 index eba95d1..0000000 --- a/src/mediacodec.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef MEDIA_CODEC -#define MEDIA_CODEC - -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; - -#endif \ No newline at end of file diff --git a/src/peer_connection.c b/src/peer_connection.c index 5b3b75d..91b7066 100644 --- a/src/peer_connection.c +++ b/src/peer_connection.c @@ -3,13 +3,54 @@ #include #include +#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; @@ -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)); @@ -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; diff --git a/src/peer_connection.h b/src/peer_connection.h index 9c202d7..1b620e1 100644 --- a/src/peer_connection.h +++ b/src/peer_connection.h @@ -8,12 +8,6 @@ #include #include #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" { @@ -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; @@ -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); diff --git a/src/rtp.h b/src/rtp.h index d74cba4..acdf6da 100644 --- a/src/rtp.h +++ b/src/rtp.h @@ -4,7 +4,7 @@ #include #include -#include "mediacodec.h" +#include "peer_connection.h" #include "config.h" #ifdef ESP32 diff --git a/src/sctp.c b/src/sctp.c index 323c35d..1a8a472 100644 --- a/src/sctp.c +++ b/src/sctp.c @@ -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] = { @@ -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_countstream_table[sctp->stream_count].label, label, sizeof(sctp->stream_table[sctp->stream_count].label)); sctp->stream_table[sctp->stream_count].sid = sid; @@ -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 @@ -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; @@ -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) { @@ -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; diff --git a/src/sctp.h b/src/sctp.h index 979da09..ddcabca 100644 --- a/src/sctp.h +++ b/src/sctp.h @@ -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 { @@ -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);