Skip to content

Commit

Permalink
Merge pull request #103 from richlegrand/send-service
Browse files Browse the repository at this point in the history
add service flag to sctp_outgoing data
  • Loading branch information
sepfy authored Aug 28, 2024
2 parents 6a87554 + 5b0a7d7 commit 6b6e3b2
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 14 deletions.
2 changes: 1 addition & 1 deletion examples/esp32/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ $ cd libpeer/examples/esp32
$ idf.py add-dependency "espressif/esp32-camera^2.0.4"
$ idf.py add-dependency "mdns"
$ git clone --recursive https://github.com/sepfy/esp_ports.git components/srtp
$ git clone -b components https://github.com/sepfy/usrsctp-esp32.git components/usrtsctp
$ git clone -b components https://github.com/sepfy/usrsctp-esp32.git components/usrsctp
```

### Configure
Expand Down
12 changes: 6 additions & 6 deletions src/peer_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,20 +255,20 @@ int peer_connection_send_video(PeerConnection *pc, const uint8_t *buf, size_t le
}

int peer_connection_datachannel_send(PeerConnection *pc, char *message, size_t len) {
return peer_connection_datachannel_send_sid(pc, message, len, 0);
return peer_connection_datachannel_send_ext(pc, message, len, 0, SVC_PARTIALLY_RELIABLE);
}

int peer_connection_datachannel_send_sid(PeerConnection *pc, char *message, size_t len, uint16_t sid) {
int peer_connection_datachannel_send_ext(PeerConnection *pc, char *message, size_t len, uint16_t sid, SctpService service) {

if(!sctp_is_connected(&pc->sctp)) {
LOGE("sctp not connected");
return -1;
}

if (pc->config.datachannel == DATA_CHANNEL_STRING)
return sctp_outgoing_data(&pc->sctp, message, len, PPID_STRING, sid);
return sctp_outgoing_data(&pc->sctp, message, len, PPID_STRING, sid, service);
else
return sctp_outgoing_data(&pc->sctp, message, len, PPID_BINARY, sid);
return sctp_outgoing_data(&pc->sctp, message, len, PPID_BINARY, sid, service);
}

static void peer_connection_state_new(PeerConnection *pc) {
Expand Down Expand Up @@ -410,9 +410,9 @@ int peer_connection_loop(PeerConnection *pc) {
if (data) {

if (pc->config.datachannel == DATA_CHANNEL_STRING)
sctp_outgoing_data(&pc->sctp, (char*)data, bytes, PPID_STRING, 0);
sctp_outgoing_data(&pc->sctp, (char*)data, bytes, PPID_STRING, 0, SVC_RELIABLE);
else
sctp_outgoing_data(&pc->sctp, (char*)data, bytes, PPID_BINARY, 0);
sctp_outgoing_data(&pc->sctp, (char*)data, bytes, PPID_BINARY, 0, SVC_RELIABLE);
buffer_pop_head(pc->data_rb);
}

Expand Down
3 changes: 2 additions & 1 deletion src/peer_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ int peer_connection_loop(PeerConnection *pc);
* @param[in] message buffer
* @param[in] length of message
*/

int peer_connection_datachannel_send(PeerConnection *pc, char *message, size_t len);

int peer_connection_datachannel_send_sid(PeerConnection *pc, char *message, size_t len, uint16_t sid);
int peer_connection_datachannel_send_ext(PeerConnection *pc, char *message, size_t len, uint16_t sid, SctpService service);

int peer_connection_send_audio(PeerConnection *pc, const uint8_t *packet, size_t bytes);

Expand Down
25 changes: 20 additions & 5 deletions src/sctp.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,19 @@ static int sctp_outgoing_data_cb(void *userdata, void *buf, size_t len, uint8_t
return 0;
}

int sctp_outgoing_data(Sctp *sctp, char *buf, size_t len, SctpDataPpid ppid, uint16_t sid) {
int sctp_outgoing_data(Sctp *sctp, char *buf, size_t len, SctpDataPpid ppid, uint16_t sid,SctpService service) {

#ifdef HAVE_USRSCTP
int res;
struct sctp_sendv_spa spa = {0};

spa.sendv_flags = SCTP_SEND_SNDINFO_VALID;
if (service==SVC_PARTIALLY_RELIABLE)
{
spa.sendv_flags |= SCTP_SEND_PRINFO_VALID;
spa.sendv_prinfo.pr_policy = SCTP_PR_SCTP_TTL; //SCTP_PR_SCTP_RTX;
spa.sendv_prinfo.pr_value = 10;
}

spa.sendv_sndinfo.snd_sid = sid;
spa.sendv_sndinfo.snd_flags = SCTP_EOR;
Expand Down Expand Up @@ -214,7 +220,17 @@ void sctp_parse_data_channel_open(Sctp *sctp, uint16_t sid, char *data, size_t l
}
}


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");
}

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

Expand Down Expand Up @@ -426,12 +442,11 @@ static int sctp_handle_incoming_data(Sctp *sctp, char *data, size_t len, uint32_
static void sctp_process_notification(Sctp *sctp, union sctp_notification *notification, size_t len) {


if(notification->sn_header.sn_length != (uint32_t)len) {
return;
}
if(notification->sn_header.sn_length != (uint32_t)len) {
return;
}

switch (notification->sn_header.sn_type) {

case SCTP_ASSOC_CHANGE:

switch (notification->sn_assoc_change.sac_state) {
Expand Down
7 changes: 6 additions & 1 deletion src/sctp.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ typedef enum SctpDataPpid {

} SctpDataPpid;

typedef enum SctpService {
SVC_RELIABLE,
SVC_PARTIALLY_RELIABLE
} SctpService;

#define SCTP_MAX_STREAMS 5

typedef struct {
Expand Down Expand Up @@ -177,7 +182,7 @@ int sctp_is_connected(Sctp *sctp);

void sctp_incoming_data(Sctp *sctp, char *buf, size_t len);

int sctp_outgoing_data(Sctp *sctp, char *buf, size_t len, SctpDataPpid ppid, uint16_t sid);
int sctp_outgoing_data(Sctp *sctp, char *buf, size_t len, SctpDataPpid ppid, uint16_t sid, SctpService service);

void sctp_onmessage(Sctp *sctp, void (*onmessage)(char *msg, size_t len, void *userdata, uint16_t sid));

Expand Down

0 comments on commit 6b6e3b2

Please sign in to comment.