Skip to content

Commit 1c5e96f

Browse files
Add peer_connection_create_datachannel
Allows the user to send a DataChannel Open message.
1 parent 3154a96 commit 1c5e96f

6 files changed

Lines changed: 81 additions & 8 deletions

File tree

src/ice.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ int ice_candidate_from_description(IceCandidate* candidate, char* description, c
8787
candidate_start += strlen("a=");
8888
}
8989
candidate_start += strlen("candidate:");
90-
printf("candidate_start: %s\n", candidate_start);
9190

9291
// a=candidate:448736988 1 udp 2122260223 172.17.0.1 49250 typ host generation 0 network-id 1 network-cost 50
9392
// a=candidate:udpcandidate 1 udp 120 192.168.1.102 8000 typ host

src/peer_connection.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,42 @@ int peer_connection_datachannel_send_sid(PeerConnection* pc, char* message, size
268268
#endif
269269
}
270270

271+
int peer_connection_create_datachannel(PeerConnection* pc, DecpChannelType channel_type, uint16_t priority, uint32_t reliability_parameter, char* label, char* protocol) {
272+
return peer_connection_create_datachannel_sid(pc, channel_type, priority, reliability_parameter, label, protocol, 0);
273+
}
274+
275+
int peer_connection_create_datachannel_sid(PeerConnection* pc, DecpChannelType channel_type, uint16_t priority, uint32_t reliability_parameter, char* label, char* protocol, uint16_t sid) {
276+
if (!sctp_is_connected(&pc->sctp)) {
277+
LOGE("sctp not connected");
278+
return -1;
279+
}
280+
281+
// 0 1 2 3
282+
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
283+
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
284+
// | Message Type | Channel Type | Priority |
285+
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
286+
// | Reliability Parameter |
287+
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
288+
// | Label Length | Protocol Length |
289+
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
290+
// | |
291+
// | Label |
292+
// | |
293+
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
294+
// | |
295+
// | Protocol |
296+
// | |
297+
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
298+
299+
int msg_size = 12 + strlen(label) + strlen(protocol);
300+
char *msg = calloc(1, msg_size);
301+
302+
msg[0] = DATA_CHANNEL_OPEN;
303+
304+
return sctp_outgoing_data(&pc->sctp, msg, msg_size, PPID_CONTROL, sid);
305+
}
306+
271307
static char* peer_connection_dtls_role_setup_value(DtlsSrtpRole d) {
272308
return d == DTLS_SRTP_ROLE_SERVER ? "a=setup:passive" : "a=setup:active";
273309
}

src/peer_connection.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ typedef enum DataChannelType {
3232

3333
} DataChannelType;
3434

35+
typedef enum DecpChannelType {
36+
DATA_CHANNEL_RELIABLE = 0x00,
37+
DATA_CHANNEL_RELIABLE_UNORDERED = 0x80,
38+
DATA_CHANNEL_PARTIAL_RELIABLE_REXMIT = 0x01,
39+
DATA_CHANNEL_PARTIAL_RELIABLE_REXMIT_UNORDERED = 0x81,
40+
DATA_CHANNEL_PARTIAL_RELIABLE_TIMED = 0x02,
41+
DATA_CHANNEL_PARTIAL_RELIABLE_TIMED_UNORDERED = 0x82,
42+
} DecpChannelType;
43+
3544
typedef enum MediaCodec {
3645

3746
CODEC_NONE = 0,
@@ -84,6 +93,11 @@ void peer_connection_destroy(PeerConnection* pc);
8493
void peer_connection_close(PeerConnection* pc);
8594

8695
int peer_connection_loop(PeerConnection* pc);
96+
97+
int peer_connection_create_datachannel(PeerConnection* pc, DecpChannelType channel_type, uint16_t priority, uint32_t reliability_parameter, char* label, char* protocol);
98+
99+
int peer_connection_create_datachannel_sid(PeerConnection* pc, DecpChannelType channel_type, uint16_t priority, uint32_t reliability_parameter, char* label, char* protocol, uint16_t sid);
100+
87101
/**
88102
* @brief send message to data channel
89103
* @param[in] peer connection

src/sctp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ int sctp_create_association(Sctp* sctp, DtlsSrtp* dtls_srtp) {
576576
}
577577

578578
ret = 0;
579-
579+
sctp->connected = 1;
580580
} while (0);
581581

582582
if (ret < 0) {

src/ssl_transport.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "ports.h"
1313
#include "ssl_transport.h"
1414
#include "utils.h"
15+
#include <sys/select.h>
1516

1617
#define SSL_RECV_TIMEOUT 1000
1718

tests/test_peer_connection.c

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
typedef struct {
1010
PeerConnection *offer_peer_connection, *answer_peer_connection;
11+
int onmessage_offer_called, onmessage_answer_called;
1112
} TestUserData;
1213

1314
static void onconnectionstatechange_offerer_peer_connection(PeerConnectionState state, void* user_data) {
@@ -28,6 +29,14 @@ static void onicecandidate_answerer_peer_connection(char* description, void* use
2829
peer_connection_set_remote_description(test_user_data->offer_peer_connection, description);
2930
}
3031

32+
static void ondatachannel_onmessage_offerer_peer_connection(char* msg, size_t len, void* userdata, uint16_t sid) {
33+
printf("\n TODO \n");
34+
}
35+
36+
static void ondatachannel_onmessage_answerer_peer_connection(char* msg, size_t len, void* userdata, uint16_t sid) {
37+
printf("\n TODO \n");
38+
}
39+
3140
static void* peer_connection_task(void* user_data) {
3241
PeerConnection* peer_connection = (PeerConnection*)user_data;
3342

@@ -73,16 +82,26 @@ int main(int argc, char* argv[]) {
7382
peer_connection_onicecandidate(test_user_data.offer_peer_connection, onicecandidate_offerer_peer_connection);
7483
peer_connection_onicecandidate(test_user_data.answer_peer_connection, onicecandidate_answerer_peer_connection);
7584

85+
peer_connection_ondatachannel(test_user_data.offer_peer_connection, ondatachannel_onmessage_offerer_peer_connection, NULL, NULL);
86+
peer_connection_ondatachannel(test_user_data.answer_peer_connection, ondatachannel_onmessage_answerer_peer_connection, NULL, NULL);
87+
7688
peer_connection_create_offer(test_user_data.offer_peer_connection);
7789

7890
pthread_create(&offer_thread, NULL, peer_connection_task, test_user_data.offer_peer_connection);
7991
pthread_create(&answer_thread, NULL, peer_connection_task, test_user_data.answer_peer_connection);
8092

81-
int attempts = 0;
82-
while (1) {
83-
if (peer_connection_get_state(test_user_data.offer_peer_connection) == PEER_CONNECTION_COMPLETED && peer_connection_get_state(test_user_data.answer_peer_connection) == PEER_CONNECTION_COMPLETED) {
84-
break;
85-
} else if (attempts == MAX_CONNECTION_ATTEMPTS) {
93+
int attempts = 0, datachannel_created = 0;
94+
while (attempts < MAX_CONNECTION_ATTEMPTS) {
95+
if (!datachannel_created && peer_connection_get_state(test_user_data.offer_peer_connection) == PEER_CONNECTION_COMPLETED) {
96+
if (peer_connection_create_datachannel(test_user_data.offer_peer_connection, DATA_CHANNEL_RELIABLE, 0, 0, "libpeer", "reepbil") == 0) {
97+
datachannel_created = 1;
98+
}
99+
}
100+
101+
if (peer_connection_get_state(test_user_data.offer_peer_connection) == PEER_CONNECTION_COMPLETED &&
102+
peer_connection_get_state(test_user_data.answer_peer_connection) == PEER_CONNECTION_COMPLETED &&
103+
test_user_data.onmessage_offer_called == 1 &&
104+
test_user_data.onmessage_answer_called == 1) {
86105
break;
87106
}
88107

@@ -94,5 +113,9 @@ int main(int argc, char* argv[]) {
94113
peer_connection_destroy(test_user_data.answer_peer_connection);
95114

96115
peer_deinit();
97-
return attempts == MAX_CONNECTION_ATTEMPTS ? 1 : 0;
116+
if (attempts == MAX_CONNECTION_ATTEMPTS) {
117+
return 1;
118+
}
119+
120+
return 0;
98121
}

0 commit comments

Comments
 (0)