-
Notifications
You must be signed in to change notification settings - Fork 26
/
live.h
198 lines (158 loc) · 5.08 KB
/
live.h
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
#ifndef _JANUS_LIVE_H
#define _JANUS_LIVE_H
#include <inttypes.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <opus/opus.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include "mutex.h"
#include "refcount.h"
#define LIBAVCODEC_VER_AT_LEAST(major, minor) \
(LIBAVCODEC_VERSION_MAJOR > major || \
(LIBAVCODEC_VERSION_MAJOR == major && \
LIBAVCODEC_VERSION_MINOR >= minor))
#if LIBAVCODEC_VER_AT_LEAST(51, 42)
#define PIX_FMT_YUV420P AV_PIX_FMT_YUV420P
#endif
#if LIBAVCODEC_VER_AT_LEAST(56, 56)
#ifndef CODEC_FLAG_GLOBAL_HEADER
#define CODEC_FLAG_GLOBAL_HEADER AV_CODEC_FLAG_GLOBAL_HEADER
#endif
#ifndef FF_INPUT_BUFFER_PADDING_SIZE
#define FF_INPUT_BUFFER_PADDING_SIZE AV_INPUT_BUFFER_PADDING_SIZE
#endif
#endif
#if LIBAVCODEC_VER_AT_LEAST(57, 14)
#define USE_CODECPAR
#endif
#define AUDIO_MIN_SEQUENTIAL 2
#define AUDIO_MAX_MISORDER 50
/* Mixer settings */
#define AUDIO_DEFAULT_PREBUFFERING 6
/* Opus settings */
#define AUDIO_BUFFER_SAMPLES 8000
#define AUDIO_OPUS_SAMPLES 960
#define AUDIO_DEFAULT_COMPLEXITY 4
#define JANUS_LIVE_BUFFER_MAX 2 * 1024 * 1024
#define htonll(x) ((1==htonl(1)) ? (x) : ((gint64)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32))
#define ntohll(x) ((1==ntohl(1)) ? (x) : ((gint64)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32))
typedef struct janus_rtp_jb janus_rtp_jb;
typedef struct janus_live_pub janus_live_pub;
typedef struct janus_live_el janus_live_el;
typedef struct janus_adecoder_opus {
uint8_t channels;
uint32_t sampling_rate; /* Sampling rate (e.g., 16000 for wideband; can be 8, 12, 16, 24 or 48kHz) */
OpusDecoder *decoder; /* Opus decoder instance */
gboolean fec; /* Opus FEC status */
uint16_t expected_seq; /* Expected sequence number */
uint16_t probation; /* Used to determine new ssrc validity */
uint32_t last_timestamp; /* Last in seq timestamp */
janus_rtp_jb *jb;
} janus_adecoder_opus;
typedef struct janus_aencoder_fdkaac {
int sample_rate;
int channels;
int bitrate;
AVFrame *aframe;
AVPacket *apacket;
AVCodec *acodec;
AVCodecContext *actx;
int nb_samples;
int buflen;
char *buffer;
janus_rtp_jb *jb;
} janus_aencoder_fdkaac;
typedef struct janus_frame_packet {
char *data; /* data */
uint16_t len; /* Length of the data */
uint16_t seq; /* RTP Sequence number */
uint64_t ts; /* RTP Timestamp */
gboolean video;
uint32_t ssrc;
gint64 created;
int keyFrame;
int pt; /* Payload type of the data */
int skip; /* Bytes to skip, besides the RTP header */
int audiolevel; /* Value of audio level in RTP extension, if parsed */
int rotation; /* Value of rotation in RTP extension, if parsed */
uint8_t drop; /* Whether this packet can be dropped (e.g., padding)*/
struct janus_frame_packet *next;
struct janus_frame_packet *prev;
} janus_frame_packet;
typedef struct janus_rtp_jb {
uint32_t last_ts;
uint32_t reset;
uint32_t ssrc;
uint16_t last_seq;
uint16_t last_seq_out;
int times_resetted;
int post_reset_pkts;
uint32_t tb;
uint64_t start_ts;
gint64 start_sys;
gboolean keyframe_found;
int keyFrame;
int frameLen;
int buflen;
uint8_t *received_frame;
uint64_t ts;
janus_live_pub *pub;
janus_adecoder_opus *adecoder;
janus_aencoder_fdkaac *aencoder;
uint32_t lastts;
uint32_t offset;
uint32_t size;
janus_frame_packet *list;
janus_frame_packet *last;
} janus_rtp_jb;
typedef struct janus_live_pub {
char *url;
char *acodec;
char *vcodec;
gint64 created;
volatile gboolean closed;
janus_rtp_jb *audio_jb;
janus_rtp_jb *video_jb;
GSource *jb_src;
GSource *pub_src;
janus_live_el *jb_loop;
janus_live_el *pub_loop;
int audio_level_extmap_id;
int video_orient_extmap_id;
uint32_t size;
janus_frame_packet *list;
janus_frame_packet *last;
uint32_t start_ts;
gint64 start_sys;
int max_width;
int max_height;
gboolean init_flag;
AVFormatContext *fctx;
AVStream *vStream;
AVStream *aStream;
#ifdef USE_CODECPAR
AVCodecContext *vEncoder;
AVCodecContext *aEncoder;
#endif
AVBitStreamFilterContext *aacbsf;
uint32_t lastts;
janus_mutex mutex;
janus_mutex mutex_live;
volatile gint destroyed;
janus_refcount ref;
} janus_live_pub;
typedef struct janus_live_el {
int id;
char *name;
GThread *thread;
GMainLoop *mainloop;
GMainContext *mainctx;
janus_live_pub *pub;
} janus_live_el;
janus_live_pub *janus_live_pub_create(const char *url, const char *acodec, const char *vcodec);
int janus_live_pub_save_frame(janus_live_pub *pub, char *buffer, uint length, gboolean video, int slot);
int janus_live_pub_close(janus_live_pub *pub);
void janus_live_pub_destroy(janus_live_pub *pub);
#endif /* _JANUS_LIVE_H */