forked from gozfree/gear-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransport_session.c
148 lines (134 loc) · 3.81 KB
/
transport_session.c
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "transport_session.h"
#include "media_source.h"
#include "rtp.h"
#include <liblog.h>
#include <libtime.h>
#include <libdict.h>
#include <libmacro.h>
#include <libatomic.h>
#include <libgevent.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h>
#include <unistd.h>
void *transport_session_pool_create()
{
return (void *)dict_new();
}
void transport_session_pool_destroy(void *pool)
{
int rank = 0;
char *key, *val;
while (1) {
rank = dict_enumerate((dict *)pool, rank, &key, &val);
if (rank < 0) {
break;
}
free(val);
}
dict_free((dict *)pool);
}
static uint32_t get_random_number()
{
struct timeval now = {0};
gettimeofday(&now, NULL);
srand(now.tv_usec);
return (rand() % ((uint32_t)-1));
}
struct transport_session *transport_session_create(void *pool, struct transport_header *t)
{
char key[9];
struct transport_session *s = CALLOC(1, struct transport_session);
s->session_id = get_random_number();
snprintf(key, sizeof(key), "%08X", s->session_id);
s->rtp = rtp_create(90000, 0);
s->rtp->sock = rtp_socket_create(t->mode, 0, t->source, t->destination);
s->rtp->sock->rtp_dst_port = t->rtp.u.client_port1;
s->rtp->sock->rtcp_dst_port = t->rtp.u.client_port2;
dict_add((dict *)pool, key, (char *)s);
return s;
}
void transport_session_destroy(void *pool, char *key)
{
struct transport_session *s = transport_session_lookup(pool, key);
if (!s) {
return;
}
rtp_socket_destroy(s->rtp->sock);
dict_del((dict *)pool, key);
}
struct transport_session *transport_session_lookup(void *pool, char *key)
{
return (struct transport_session *)dict_get((dict *)pool, key, NULL);
}
static void *send_thread(struct thread *t, void *ptr)
{
struct transport_session *ts = (struct transport_session *)ptr;
struct media_source *ms = ts->media_source;
void *data = NULL;
size_t len = 0;
if (-1 == ms->open(ms, "sample.264")) {
loge("open failed!\n");
return NULL;
}
unsigned int ssrc = (unsigned int)rtp_ssrc();
uint64_t pts = time_get_msec();
unsigned int seq = ssrc;
while (t->run) {
if (-1 == ms->read(ms, &data, &len)) {
loge("read failed!\n");
sleep(1);
continue;
}
struct rtp_packet *pkt = rtp_packet_create(len, 96, seq, ssrc);
rtp_payload_h264_encode(ts->rtp->sock, pkt, data, len, pts);
pts += 3000;
seq = pkt->header.seq;
rtp_packet_destroy(pkt);
//usleep(500*1000);
}
return NULL;
}
static void on_recv(int fd, void *arg)
{
char buf[2048];
memset(buf, 0, sizeof(buf));
int ret = skt_recv(fd, buf, 2048);
if (ret > 0) {
rtcp_parse(buf, ret);
} else if (ret == 0) {
loge("delete connection fd:%d\n", fd);
} else if (ret < 0) {
loge("recv failed!\n");
}
}
static void on_error(int fd, void *arg)
{
loge("error: %d\n", errno);
}
static void *event_thread(struct thread *t, void *ptr)
{
struct transport_session *ts = (struct transport_session *)ptr;
gevent_base_loop(ts->evbase);
return NULL;
}
int transport_session_start(struct transport_session *ts, struct media_source *ms)
{
ts->evbase = gevent_base_create();
if (!ts->evbase) {
return -1;
}
skt_set_noblk(ts->rtp->sock->rtcp_fd, true);
struct gevent *e = gevent_create(ts->rtp->sock->rtcp_fd, on_recv, NULL, on_error, NULL);
if (-1 == gevent_add(ts->evbase, e)) {
loge("event_add failed!\n");
gevent_destroy(e);
}
loge("rtcp_fd = %d\n", ts->rtp->sock->rtcp_fd);
ts->media_source = ms;
ts->ev_thread = thread_create(event_thread, ts);
ts->thread = thread_create(send_thread, ts);
logi("session_id = %d\n", ts->session_id);
return 0;
}