forked from OpenVisualCloud/Media-Transport-Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtx_st20_pipeline_sample.c
295 lines (251 loc) · 7.89 KB
/
tx_st20_pipeline_sample.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright(c) 2022 Intel Corporation
*/
#include "sample_util.h"
struct tx_st20p_sample_ctx {
mtl_handle st;
int idx;
st20p_tx_handle handle;
bool stop;
pthread_t frame_thread;
int fb_send;
int fb_send_done;
pthread_cond_t wake_cond;
pthread_mutex_t wake_mutex;
size_t frame_size;
uint8_t* source_begin;
mtl_iova_t source_begin_iova;
uint8_t* source_end;
uint8_t* frame_cursor;
struct st_frame_user_meta meta;
bool has_user_meta;
};
static int tx_st20p_close_source(struct tx_st20p_sample_ctx* s) {
if (s->source_begin) {
mtl_hp_free(s->st, s->source_begin);
s->source_begin = NULL;
}
return 0;
}
static int tx_st20p_open_source(struct tx_st20p_sample_ctx* s, char* file) {
int fd = -EIO;
struct stat i;
int frame_cnt = 2;
uint8_t* m = NULL;
size_t fbs_size = s->frame_size * frame_cnt;
fd = st_open(file, O_RDONLY);
if (fd < 0) {
err("%s, open %s fail\n", __func__, file);
goto init_fb;
}
if (fstat(fd, &i) < 0) {
err("%s, fstat %s fail\n", __func__, file);
close(fd);
return -EIO;
}
if (i.st_size < s->frame_size) {
err("%s, %s file size small then a frame %" PRIu64 "\n", __func__, file,
s->frame_size);
close(fd);
return -EIO;
}
if (i.st_size % s->frame_size) {
err("%s, %s file size should be multiple of frame size %" PRIu64 "\n", __func__, file,
s->frame_size);
close(fd);
return -EIO;
}
m = mmap(NULL, i.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (MAP_FAILED == m) {
err("%s, mmap %s fail\n", __func__, file);
close(fd);
return -EIO;
}
frame_cnt = i.st_size / s->frame_size;
fbs_size = i.st_size;
init_fb:
s->source_begin = mtl_hp_zmalloc(s->st, fbs_size, MTL_PORT_P);
if (!s->source_begin) {
err("%s, source malloc on hugepage fail\n", __func__);
if (m) munmap(m, i.st_size);
if (fd >= 0) close(fd);
return -EIO;
}
s->frame_cursor = s->source_begin;
if (m) mtl_memcpy(s->source_begin, m, fbs_size);
s->source_end = s->source_begin + fbs_size;
if (m) munmap(m, i.st_size);
if (fd >= 0) close(fd);
return 0;
}
static int tx_st20p_frame_available(void* priv) {
struct tx_st20p_sample_ctx* s = priv;
st_pthread_mutex_lock(&s->wake_mutex);
st_pthread_cond_signal(&s->wake_cond);
st_pthread_mutex_unlock(&s->wake_mutex);
return 0;
}
static int tx_st20p_frame_done(void* priv, struct st_frame* frame) {
struct tx_st20p_sample_ctx* s = priv;
MTL_MAY_UNUSED(frame);
s->fb_send_done++;
dbg("%s(%d), done %d\n", __func__, s->idx, s->fb_send_done);
return 0;
}
static void tx_st20p_build_frame(struct tx_st20p_sample_ctx* s, struct st_frame* frame) {
uint8_t* src = s->frame_cursor;
mtl_memcpy(frame->addr[0], src, s->frame_size);
}
static void* tx_st20p_frame_thread(void* arg) {
struct tx_st20p_sample_ctx* s = arg;
st20p_tx_handle handle = s->handle;
struct st_frame* frame;
info("%s(%d), start\n", __func__, s->idx);
while (!s->stop) {
frame = st20p_tx_get_frame(handle);
if (!frame) { /* no frame */
st_pthread_mutex_lock(&s->wake_mutex);
if (!s->stop) st_pthread_cond_wait(&s->wake_cond, &s->wake_mutex);
st_pthread_mutex_unlock(&s->wake_mutex);
continue;
}
if (s->source_begin) tx_st20p_build_frame(s, frame);
if (s->has_user_meta) {
s->meta.idx = s->fb_send;
frame->user_meta = &s->meta;
frame->user_meta_size = sizeof(s->meta);
}
st20p_tx_put_frame(handle, frame);
/* point to next frame */
s->frame_cursor += s->frame_size;
if (s->frame_cursor + s->frame_size > s->source_end) {
s->frame_cursor = s->source_begin;
}
s->fb_send++;
dbg("%s(%d), fb_send %d\n", __func__, s->idx, s->fb_send);
}
info("%s(%d), stop\n", __func__, s->idx);
return NULL;
}
int main(int argc, char** argv) {
struct st_sample_context ctx;
int ret;
/* init sample(st) dev */
memset(&ctx, 0, sizeof(ctx));
ret = tx_sample_parse_args(&ctx, argc, argv);
if (ret < 0) return ret;
ctx.st = mtl_init(&ctx.param);
if (!ctx.st) {
err("%s: mtl_init fail\n", __func__);
return -EIO;
}
uint32_t session_num = ctx.sessions;
struct tx_st20p_sample_ctx* app[session_num];
// create and register tx session
for (int i = 0; i < session_num; i++) {
app[i] = malloc(sizeof(struct tx_st20p_sample_ctx));
if (!app[i]) {
err("%s(%d), app context malloc fail\n", __func__, i);
ret = -ENOMEM;
goto error;
}
memset(app[i], 0, sizeof(struct tx_st20p_sample_ctx));
app[i]->st = ctx.st;
app[i]->idx = i;
app[i]->stop = false;
if (ctx.has_user_meta) {
snprintf(app[i]->meta.dummy, sizeof(app[i]->meta.dummy), "st20p_tx_%d", i);
app[i]->has_user_meta = true;
}
st_pthread_mutex_init(&app[i]->wake_mutex, NULL);
st_pthread_cond_init(&app[i]->wake_cond, NULL);
struct st20p_tx_ops ops_tx;
memset(&ops_tx, 0, sizeof(ops_tx));
ops_tx.name = "st20p_test";
ops_tx.priv = app[i]; // app handle register to lib
ops_tx.port.num_port = ctx.param.num_ports;
memcpy(ops_tx.port.dip_addr[MTL_SESSION_PORT_P], ctx.tx_dip_addr[MTL_PORT_P],
MTL_IP_ADDR_LEN);
snprintf(ops_tx.port.port[MTL_SESSION_PORT_P], MTL_PORT_MAX_LEN, "%s",
ctx.param.port[MTL_PORT_P]);
ops_tx.port.udp_port[MTL_SESSION_PORT_P] = ctx.udp_port + i * 2;
if (ops_tx.port.num_port > 1) {
memcpy(ops_tx.port.dip_addr[MTL_SESSION_PORT_R], ctx.tx_dip_addr[MTL_PORT_R],
MTL_IP_ADDR_LEN);
snprintf(ops_tx.port.port[MTL_SESSION_PORT_R], MTL_PORT_MAX_LEN, "%s",
ctx.param.port[MTL_PORT_R]);
ops_tx.port.udp_port[MTL_SESSION_PORT_R] = ctx.udp_port + i * 2;
}
ops_tx.port.payload_type = ctx.payload_type;
ops_tx.width = ctx.width;
ops_tx.height = ctx.height;
ops_tx.fps = ctx.fps;
ops_tx.interlaced = ctx.interlaced;
ops_tx.input_fmt = ctx.input_fmt;
ops_tx.transport_fmt = ctx.fmt;
ops_tx.device = ST_PLUGIN_DEVICE_AUTO;
ops_tx.framebuff_cnt = ctx.framebuff_cnt;
ops_tx.notify_frame_available = tx_st20p_frame_available;
ops_tx.notify_frame_done = tx_st20p_frame_done;
st20p_tx_handle tx_handle = st20p_tx_create(ctx.st, &ops_tx);
if (!tx_handle) {
err("%s(%d), st20p_tx_create fail\n", __func__, i);
ret = -EIO;
goto error;
}
app[i]->handle = tx_handle;
app[i]->frame_size = st20p_tx_frame_size(tx_handle);
info("%s(%d), frame_size %" PRId64 "\n", __func__, i, app[i]->frame_size);
ret = tx_st20p_open_source(app[i], ctx.tx_url);
if (ret < 0) {
err("%s(%d), open source fail\n", __func__, i);
goto error;
}
ret = pthread_create(&app[i]->frame_thread, NULL, tx_st20p_frame_thread, app[i]);
if (ret < 0) {
err("%s(%d), thread create fail %d\n", __func__, ret, i);
ret = -EIO;
goto error;
}
}
// start tx
ret = mtl_start(ctx.st);
while (!ctx.exit) {
sleep(1);
}
// stop app thread
for (int i = 0; i < session_num; i++) {
app[i]->stop = true;
st_pthread_mutex_lock(&app[i]->wake_mutex);
st_pthread_cond_signal(&app[i]->wake_cond);
st_pthread_mutex_unlock(&app[i]->wake_mutex);
pthread_join(app[i]->frame_thread, NULL);
info("%s(%d), sent frames %d(done %d)\n", __func__, i, app[i]->fb_send,
app[i]->fb_send_done);
tx_st20p_close_source(app[i]);
}
// stop tx
ret = mtl_stop(ctx.st);
// check result
for (int i = 0; i < session_num; i++) {
if (app[i]->fb_send <= 0) {
err("%s(%d), error, no sent frames %d\n", __func__, i, app[i]->fb_send);
ret = -EIO;
}
}
error:
for (int i = 0; i < session_num; i++) {
if (app[i]) {
st_pthread_mutex_destroy(&app[i]->wake_mutex);
st_pthread_cond_destroy(&app[i]->wake_cond);
if (app[i]->handle) st20p_tx_free(app[i]->handle);
free(app[i]);
}
}
/* release sample(st) dev */
if (ctx.st) {
mtl_uninit(ctx.st);
ctx.st = NULL;
}
return ret;
}