Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ if(UNDEFINED_BEHAVIOR_SANITIZER)
enableSanitizer("undefined")
endif()

add_definitions("-Wunused-variable -Werror=sequence-point -Werror=pointer-sign -Werror=return-type -Werror=sizeof-pointer-memaccess -Wincompatible-pointer-types -DHTTP_DO_NOT_USE_CUSTOM_CONFIG -DMQTT_DO_NOT_USE_CUSTOM_CONFIG")
add_definitions("-DHTTP_DO_NOT_USE_CUSTOM_CONFIG -DMQTT_DO_NOT_USE_CUSTOM_CONFIG")
if(NOT WIN32)
add_definitions("-Wunused-variable -Werror=sequence-point -Werror=pointer-sign -Werror=return-type -Werror=sizeof-pointer-memaccess -Wincompatible-pointer-types")
else()
add_definitions("-D__BYTE_ORDER=1234")
endif()

add_subdirectory(src)
add_subdirectory(examples)
Expand Down Expand Up @@ -105,6 +110,7 @@ ExternalProject_Add(usrsctp
CMAKE_ARGS
-DCMAKE_C_FLAGS="-fPIC"
-Dsctp_build_programs=off
-Dsctp_werror=off
-DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/dist
-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}
)
5 changes: 4 additions & 1 deletion examples/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ include_directories(${CMAKE_SOURCE_DIR}/src)

add_executable(sample ${SRCS})

target_link_libraries(sample peer pthread)
target_link_libraries(sample peer)
if(NOT WIN32)
target_link_libraries(sample pthread)
endif()

30 changes: 14 additions & 16 deletions examples/generic/main.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>

#include "ports.h"
#include "peer.h"
#include "reader.h"
#ifdef WIN32
#include <windows.h>
#define pthread_t HANDLE
#define pthread_create(th, attr, func, arg) (*(th) = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)(func), (arg), 0, NULL))
#define pthread_join(th, res) WaitForSingleObject((th), INFINITE)
#define pthread_exit(res) ExitThread(0)
#else
#include <pthread.h>
#endif

int g_interrupted = 0;
PeerConnection* g_pc = NULL;
Expand Down Expand Up @@ -39,27 +45,19 @@ static void signal_handler(int signal) {
static void* peer_singaling_task(void* data) {
while (!g_interrupted) {
peer_signaling_loop();
usleep(1000);
ports_sleep_ms(1);
}

pthread_exit(NULL);
}

static void* peer_connection_task(void* data) {
while (!g_interrupted) {
peer_connection_loop(g_pc);
usleep(1000);
ports_sleep_ms(1);
}

pthread_exit(NULL);
}

static uint64_t get_timestamp() {
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}

void print_usage(const char* prog_name) {
printf("Usage: %s -u <url> [-t <token>]\n", prog_name);
}
Expand Down Expand Up @@ -126,7 +124,7 @@ int main(int argc, char* argv[]) {

while (!g_interrupted) {
if (g_state == PEER_CONNECTION_COMPLETED) {
curr_time = get_timestamp();
curr_time = ports_get_epoch_time();

// FPS 25
if (curr_time - video_time > 40) {
Expand All @@ -147,7 +145,7 @@ int main(int argc, char* argv[]) {
audio_time = curr_time;
}
}
usleep(1000);
ports_sleep_ms(1);
}

pthread_join(peer_singaling_thread, NULL);
Expand Down
1 change: 0 additions & 1 deletion examples/generic/reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>

static int g_video_size = 0;
static int g_audio_size = 0;
Expand Down
9 changes: 9 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@ file(GLOB SRCS "*.c")

file(GLOB HEADERS "peer.h" "peer_connection.h" "peer_signaling.h")

if(WIN32)
add_library(peer STATIC
${SRCS}
${HTTP_SOURCES}
${MQTT_SOURCES}
${MQTT_SERIALIZER_SOURCES}
)
else()
add_library(peer
${SRCS}
${HTTP_SOURCES}
${MQTT_SOURCES}
${MQTT_SERIALIZER_SOURCES}
)
endif()

include_directories(peer PUBLIC
${HTTP_INCLUDE_PUBLIC_DIRS}
Expand Down
3 changes: 3 additions & 0 deletions src/address.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include "config.h"
#if CONFIG_USE_LWIP
#include <lwip/sockets.h>
#elif defined(WIN32)
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <arpa/inet.h>
#include <sys/socket.h>
Expand Down
3 changes: 2 additions & 1 deletion src/agent.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef WIN32
#include <sys/select.h>
#include <unistd.h>
#endif

#include "agent.h"
#include "base64.h"
Expand Down
2 changes: 0 additions & 2 deletions src/agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "base64.h"
#include "ice.h"
#include "socket.h"
#include "stun.h"
Expand Down
1 change: 0 additions & 1 deletion src/dtls_srtp.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "address.h"
#include "config.h"
Expand Down
3 changes: 3 additions & 0 deletions src/mdns.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef WIN32
#include <sys/select.h>
#include <unistd.h>
#endif
#include "address.h"
#include "socket.h"
#include "utils.h"
Expand Down Expand Up @@ -53,6 +55,7 @@ static int mdns_add_hostname(const char* hostname, uint8_t* buf, int size) {
buf[offset++] = 0x00;
return offset;
}

static int mdns_parse_answer(uint8_t* buf, int size, Address* addr, const char* hostname) {
int flags_qr, offset;
DnsHeader* header;
Expand Down
1 change: 0 additions & 1 deletion src/peer.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <srtp2/srtp.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "peer.h"
#include "sctp.h"
Expand Down
1 change: 0 additions & 1 deletion src/peer_connection.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "agent.h"
#include "config.h"
Expand Down
1 change: 0 additions & 1 deletion src/peer_signaling.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <cJSON.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>

#include <core_http_client.h>
#include <core_mqtt.h>
Expand Down
92 changes: 91 additions & 1 deletion src/ports.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
#include <errno.h>
#include <string.h>

#ifndef WIN32
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

#endif
#include "config.h"

#if CONFIG_USE_LWIP
#include "lwip/ip_addr.h"
#include "lwip/netdb.h"
#include "lwip/netif.h"
#include "lwip/sys.h"
#elif WIN32
#include <winsock2.h>
// Windows implementation using GetAdaptersAddresses
#include <iphlpapi.h>
#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")
#else
#include <ifaddrs.h>
#include <net/if.h>
Expand Down Expand Up @@ -51,6 +59,73 @@ int ports_get_host_addr(Address* addr, const char* iface_prefix) {
break;
}
}
#elif WIN32
ULONG out_buf_len = 15000;
IP_ADAPTER_ADDRESSES* addresses = (IP_ADAPTER_ADDRESSES *)malloc(out_buf_len);
if (addresses == NULL) {
LOGE("Memory allocation failed for IP_ADAPTER_ADDRESSES struct");
return -1;
}

DWORD dwRetVal = GetAdaptersAddresses(addr->family,
GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER,
NULL, addresses, &out_buf_len);

if (dwRetVal == ERROR_BUFFER_OVERFLOW) {
free(addresses);
addresses = (IP_ADAPTER_ADDRESSES *)malloc(out_buf_len);
if (addresses == NULL) {
LOGE("Memory allocation failed for IP_ADAPTER_ADDRESSES struct");
return -1;
}
dwRetVal = GetAdaptersAddresses(addr->family,
GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER,
NULL, addresses, &out_buf_len);
}

if (dwRetVal != NO_ERROR) {
LOGE("GetAdaptersAddresses() failed: %ld", dwRetVal);
free(addresses);
return ret;
}

for (IP_ADAPTER_ADDRESSES* addr_ptr = addresses; addr_ptr != NULL; addr_ptr = addr_ptr->Next) {
if (iface_prefix && strlen(iface_prefix) > 0) {
if (strncmp(addr_ptr->AdapterName, iface_prefix, strlen(iface_prefix)) != 0) {
continue;
}
} else {
if (addr_ptr->OperStatus != IfOperStatusUp) {
continue;
}
if (addr_ptr->IfType == IF_TYPE_SOFTWARE_LOOPBACK) {
continue;
}
}

IP_ADAPTER_UNICAST_ADDRESS *unicast = addr_ptr->FirstUnicastAddress;
for (; unicast != NULL; unicast = unicast->Next) {
if (unicast->Address.lpSockaddr->sa_family != addr->family)
continue;

switch (addr->family) {
case AF_INET6:
memcpy(&addr->sin6, unicast->Address.lpSockaddr, sizeof(struct sockaddr_in6));
break;
case AF_INET:
default:
memcpy(&addr->sin, unicast->Address.lpSockaddr, sizeof(struct sockaddr_in));
break;
}
ret = 1;
break;
}
if (ret)
break;
}

free(addresses);

#else

struct ifaddrs *ifaddr, *ifa;
Expand Down Expand Up @@ -144,13 +219,28 @@ int ports_resolve_addr(const char* host, Address* addr) {

uint32_t ports_get_epoch_time() {
struct timeval tv;
#if WIN32
FILETIME ft;
ULARGE_INTEGER uli;
GetSystemTimeAsFileTime(&ft);
uli.LowPart = ft.dwLowDateTime;
uli.HighPart = ft.dwHighDateTime;
// FILETIME is in 100-nanosecond intervals since Jan 1, 1601 (UTC)
// Convert to milliseconds since Unix epoch (Jan 1, 1970)
uint64_t epoch = (uli.QuadPart - 116444736000000000ULL) / 10000ULL;
tv.tv_sec = (long)(epoch / 1000);
tv.tv_usec = (long)((epoch % 1000) * 1000);
#else
gettimeofday(&tv, NULL);
#endif
return (uint32_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
}

void ports_sleep_ms(int ms) {
#if CONFIG_USE_LWIP
sys_msleep(ms);
#elif WIN32
Sleep(ms);
#else
usleep(ms * 1000);
#endif
Expand Down
4 changes: 2 additions & 2 deletions src/rtp.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ typedef struct RtpHeader {
uint16_t seq_number;
uint32_t timestamp;
uint32_t ssrc;
uint32_t csrc[0];
//uint32_t csrc[0];

} RtpHeader;

typedef struct RtpPacket {
RtpHeader header;
uint8_t payload[0];
uint8_t payload[1];

} RtpPacket;

Expand Down
3 changes: 2 additions & 1 deletion src/sctp.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ void sctp_parse_data_channel_open(Sctp* sctp, uint16_t sid, char* data, size_t l
char* label = (char*)(data + 12);

// copy and null-terminate
char label_str[label_length + 1];
char* label_str = malloc(label_length + 1);
memcpy(label_str, label, label_length);
label_str[label_length] = '\0';

Expand All @@ -195,6 +195,7 @@ void sctp_parse_data_channel_open(Sctp* sctp, uint16_t sid, char* data, size_t l
sctp_add_stream_mapping(sctp, label_str, sid);
char ack = DATA_CHANNEL_ACK;
sctp_outgoing_data(sctp, &ack, 1, DATA_CHANNEL_PPID_CONTROL, sid);
free(label_str);
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/socket.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include <errno.h>
#include <string.h>
#include <unistd.h>

#include "socket.h"
#include "utils.h"

Expand Down
3 changes: 2 additions & 1 deletion src/ssl_transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
#include "mbedtls/debug.h"
#include "mbedtls/entropy.h"
#include "mbedtls/ssl.h"

#ifndef WIN32
#include <sys/select.h>
#endif
#include "config.h"
#include "ports.h"
#include "ssl_transport.h"
Expand Down
Loading