summaryrefslogtreecommitdiff
path: root/tinyrtc.c
blob: 209222403bb37d4b39b5410915aeafa295791fd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
 * WebRTC implementation for tiny devices
 *
 * Copyright (c) 2022 Chris Hiszpanski. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 */

#include <stdbool.h>
#include <stdio.h>						// snprintf
#include <string.h>						// memset

#include "tinyrtc.h"


///  OPAQUE TYPE DEFINITIONS  ////////////////////////////////////////////////

struct trtc_peerconn_t {
	bool active;

	char offer[TRTC_MAX_SDP_SIZE];
	char answer[TRTC_MAX_SDP_SIZE];

	char l_ice_pwd[TRTC_MAX_ICE_PWD_SIZE];
	char l_ice_ufrag[TRTC_MAX_ICE_UFRAG_SIZE];
};


///  STATIC ALLOCATIONS  /////////////////////////////////////////////////////

static struct trtc_peerconn_t peer_connection_pool[TRTC_MAX_PEER_CONNECTIONS];


///  RTC PEER CONNECTION API  ////////////////////////////////////////////////

void rtc_init() {
	memset(peer_connection_pool, 0, sizeof(peer_connection_pool));
}

struct trtc_peerconn_t* trtc_peer_connection(struct trtc_config_t cfg) {
	for (int i = 0; i < TRTC_MAX_PEER_CONNECTIONS; i++) {
		if (!peer_connection_pool[i].active) {
			memset(&peer_connection_pool[i], 0, sizeof(struct trtc_peerconn_t));
			peer_connection_pool[i].active = true;
			return &peer_connection_pool[i];
		}
	}

	return NULL;
}

int trtc_add_ice_candidate(struct trtc_peerconn_t *pc, const struct trtc_ice_candidate_t c) {
	return -1;
};

const char * trtc_create_answer(struct trtc_peerconn_t *pc) {
	unsigned char fp[32];

	snprintf(pc->l_ice_ufrag, TRTC_MAX_ICE_UFRAG_SIZE, "xxxx"); // XXX
	snprintf(pc->l_ice_pwd, TRTC_MAX_ICE_PWD_SIZE, "xxxxxxxxxxxxxxxxxxxxxx"); // XXX

	snprintf(pc->answer, TRTC_MAX_SDP_SIZE,
		"v=0\\r\\n"
		"o=- 2210401696197537454 2 IN IP4 127.0.0.1\\r\\n" // XXX
		"s=-\\r\\n"
		"t=0 0\\r\\n"
		"a=group:BUNDLE 0\\r\\n"
		"a=msid-semantic: WMS\\r\\n"
		"m=video 9 UDP/TLS/RTP/SAVPF 98\\r\\n" // XXX
		"c=IN IP4 0.0.0.0\\r\\n"
		"a=rtcp:9 IN IP4 0.0.0.0\\r\\n"
		"a=ice-ufrag:%s\\r\\n"
		"a=ice-pwd:%s\\r\\n"
		"a=ice-options:trickle\\r\\n"
		"a=fingerprint:sha-256 "
			"%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:"
			"%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:"
			"%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:"
			"%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX\\r\\n"
		"a=setup:passive\\r\\n"
		"a=mid:0\\r\\n"
		"a=sendonly\\r\\n"
		"a=rtcp-mux\\r\\n"
		"a=rtcp-rsize\\r\\n"
		"a=rtpmap:98 H264/90000\\r\\n"
		"a=rtcp-fb:98 nack\\r\\n"
		"a=rtcp-fb:98 nack pli\\r\\n"
		"a=fmtp:98 level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f\\r\\n",
		pc->l_ice_ufrag,
		pc->l_ice_pwd,
		fp[ 0], fp[ 1], fp[ 2], fp[ 3], fp[ 4], fp[ 5], fp[ 6], fp[ 7],
		fp[ 8], fp[ 9], fp[10], fp[11], fp[12], fp[13], fp[14], fp[15],
		fp[16], fp[17], fp[18], fp[19], fp[20], fp[21], fp[22], fp[23],
		fp[24], fp[25], fp[26], fp[27], fp[28], fp[29], fp[30], fp[31]
	);

	return pc->answer;
};

int trtc_set_local_description(struct trtc_peerconn_t *pc, const char *sdp) {
	return -1;
};

int trtc_set_remote_description(struct trtc_peerconn_t *pc, const char *sdp) {
	return -1;
};