forked from rainfly123/flvmuxer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
142 lines (129 loc) · 3.46 KB
/
test.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
#include "soooner_rtmp.h"
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#define AAC_ADTS_HEADER_SIZE 7
static uint32_t find_start_code(uint8_t *buf, uint32_t zeros_in_startcode)
{
uint32_t info;
uint32_t i;
info = 1;
if ((info = (buf[zeros_in_startcode] != 1)? 0: 1) == 0)
return 0;
for (i = 0; i < zeros_in_startcode; i++)
if (buf[i] != 0)
{
info = 0;
break;
};
return info;
}
uint8_t * get_nal(uint32_t *len, uint8_t **offset, uint8_t *start, uint32_t total)
{
uint32_t info;
uint8_t *q ;
uint8_t *p = *offset;
*len = 0;
while(1) {
info = find_start_code(p, 3);
if (info == 1)
break;
p++;
if ((p - start) >= total)
return NULL;
}
q = p + 4;
p = q;
while(1) {
info = find_start_code(p, 3);
if (info == 1)
break;
p++;
if ((p - start) >= total)
return NULL;
}
*len = (p - q);
*offset = p;
return q;
}
uint8_t *get_adts(uint32_t *len, uint8_t **offset, uint8_t *start, uint32_t total)
{
uint8_t *p = *offset;
uint32_t frame_len_1;
uint32_t frame_len_2;
uint32_t frame_len_3;
uint32_t frame_length;
if (total < AAC_ADTS_HEADER_SIZE) {
return NULL;
}
if ((p - start) >= total) {
return NULL;
}
if (p[0] != 0xff) {
return NULL;
}
if ((p[1] & 0xf0) != 0xf0) {
return NULL;
}
frame_len_1 = p[3] & 0x03;
frame_len_2 = p[4];
frame_len_3 = (p[5] & 0xe0) >> 5;
frame_length = (frame_len_1 << 11) | (frame_len_2 << 3) | frame_len_3;
*offset = p + frame_length;
*len = frame_length;
return p;
}
int main()
{
void*p = rtmp_sender_alloc("rtmp://127.0.0.1:1935/live/ddd"); //return handle
rtmp_sender_start_publish(p, 0, 0);
int fd = open("cms.264", O_RDONLY);
uint8_t * buf = malloc(3 *1024 * 1024);
uint32_t total;
total = read(fd, buf, (1024*1024 *3));
close(fd);
int aacfd = open("audiotest.aac", O_RDONLY);
uint8_t * audio_buf = malloc(1 *1024 * 1024);
uint32_t audio_total;
audio_total = read(aacfd, audio_buf, (1024*1024 *3));
close(aacfd);
uint8_t *buf_offset = buf;
uint8_t *audio_buf_offset = audio_buf;
uint32_t len;
uint32_t audio_len;
uint8_t *p_video ;
uint8_t *pp;
uint8_t *p_audio;
uint32_t audio_ts = 0;
uint32_t ts = 0;
uint32_t len_1;
uint32_t len_2;
while (1) {
p_audio = get_adts(&audio_len, &audio_buf_offset, audio_buf, audio_total);
if (p_audio == NULL){
audio_buf_offset = audio_buf;
continue;
}
rtmp_sender_write_audio_frame(p, p_audio, audio_len, audio_ts);
p_video = get_nal(&len, &buf_offset, buf, total);
if (p_video == NULL) {
buf_offset = buf;
continue;
}
printf("%x %d\n", p_video[0], len);
if (p_video[0] == 0x67) {
pp = get_nal(&len_1, &buf_offset, buf, total);
printf("%x %d\n", pp[0], len_1);
pp = get_nal(&len_2, &buf_offset, buf, total);
printf("%x %d\n", pp[0], len_2);
uint8_t temp = len + len_1 + len_2 + 12;
printf("temp %d\n", temp);
rtmp_sender_write_video_frame(p, p_video - 4, temp, ts, 0);
}
else
rtmp_sender_write_video_frame(p, p_video - 4, len + 4, ts, 0);
ts += 50;
audio_ts += 50;
usleep(50 * 1000);
}
}