peerstreamer-src / src / msg_buffer.h @ a8a91b7e
History | View | Annotate | Download (5.01 KB)
1 |
#ifndef MSG_BUFFER
|
---|---|
2 |
#define MSG_BUFFER
|
3 |
|
4 |
#include <stdint.h> |
5 |
#include <sys/time.h> |
6 |
|
7 |
/*
|
8 |
* Thresholds sizes and timeouts should be carefully selected depending on the
|
9 |
* type of content stored in the buffer.
|
10 |
*
|
11 |
* In case of audio/video content the values should depend on the stream
|
12 |
* parameters:
|
13 |
*
|
14 |
* AVStream *st = ic->streams[i]; where ic is an AVFormatContext
|
15 |
*
|
16 |
*/
|
17 |
|
18 |
#define MSG_BUFFER_START_BUF_SIZE_TH (0x40000) /* 256KiB */ |
19 |
#define MSG_BUFFER_TH1_SIZE (0x20000) /* 128KiB */ |
20 |
#define MSG_BUFFER_TH1_TO_US (33000) /* 33ms (e.g., 30fps video) */ |
21 |
#define MSG_BUFFER_TH2_SIZE (0x10000) /* 64KiB */ |
22 |
#define MSG_BUFFER_TH2_TO_US (66000) /* 66ms */ |
23 |
|
24 |
#define MSG_BUFFER_FLUSH_TO_US (300000) /* 300ms */ |
25 |
|
26 |
#define MSG_BUFFER_MIN_TO_US (0) /* 0ms */ |
27 |
|
28 |
#define MSG_BUFFER_N_START_SLOT (4096) /* Default # starting slots */ |
29 |
|
30 |
#define MSG_BUFFER_INVALID_SLOT_IDX (-1) |
31 |
|
32 |
#define MSG_BUFFER_DATA_START_BUF (0x4) |
33 |
#define MSG_BUFFER_DATA_FLUSHED (0x2) |
34 |
#define MSG_BUFFER_DATA_READY (0x1) |
35 |
#define MSG_BUFFER_DATA_NOT_READY (0x0) |
36 |
|
37 |
#define MSG_BUFFER_SLOW_MODE_DISABLED (0x0) |
38 |
#define MSG_BUFFER_SLOW_MODE_TH1 (0x1) |
39 |
#define MSG_BUFFER_SLOW_MODE_TH2 (0x2) |
40 |
|
41 |
/* msg_buffer_init must be called for initializing the msg_buffer.
|
42 |
*
|
43 |
* - debug: 1 for printing verbose debuggin messages, 0 otherwise
|
44 |
* - id: identifier string for this buffer
|
45 |
*
|
46 |
* Return a pointer to a struct msg_buffer that must be passed as parameter to
|
47 |
* all the other functions listed in this file.
|
48 |
*/
|
49 |
struct msg_buffer *msg_buffer_init(int debug, char *id); |
50 |
|
51 |
/* msg_buffer_destroy frees the resources used by the msg_buffer.
|
52 |
*
|
53 |
* - msgb: pointer to a struct msg_buffer previously initialized with
|
54 |
* msg_buffer_init
|
55 |
*/
|
56 |
void msg_buffer_destroy(struct msg_buffer **msgb); |
57 |
|
58 |
/* Push a new message in the msg_buffer */
|
59 |
int msg_buffer_push(struct msg_buffer *msgb, uint8_t *buf, uint32_t size); |
60 |
/* Check the status of the buffer.
|
61 |
*
|
62 |
* Return:
|
63 |
* - MSG_BUFFER_DATA_READY: a new message can be popped from the buffer
|
64 |
* - MSG_BUFFER_DATA_FLUSHED: buffer is now empty after the flush
|
65 |
* - MSG_BUFFER_DATA_START_BUF: buffer is waiting to reach the initial buffering
|
66 |
* threshold
|
67 |
* - MSG_BUFFER_DATA_NOT_READY: there are no messages to pop from the buffer
|
68 |
* */
|
69 |
int msg_buffer_get_status(struct msg_buffer *msgb, struct timeval *tv); |
70 |
/* Pop a message from the buffer.
|
71 |
* The caller has the responsibility to free buf.
|
72 |
*/
|
73 |
int msg_buffer_pop(struct msg_buffer *msgb, uint8_t **buf); |
74 |
|
75 |
/* The following three functions are used to parse the content of msg_buffer
|
76 |
* (starting from next_slot_pop) without actually removing the slots from the
|
77 |
* buffer
|
78 |
*
|
79 |
* This is useful for parsing the content of the buffer without actually
|
80 |
* removing the content in order to consume it later.
|
81 |
*
|
82 |
* The usage patter is the following:
|
83 |
* - call msg_buffer_parse_start(), if it returns 0 then
|
84 |
* - call (also multiple times) msg_buffer_parse_next() for parsing the content
|
85 |
* of the buffer
|
86 |
* - When the parsing is complete, close the transaction with
|
87 |
* msg_buffer_parse_stop
|
88 |
*/
|
89 |
|
90 |
/* Return -1 if the msg_buffer is empty, 0 otherwise */
|
91 |
int msg_buffer_parse_start(struct msg_buffer *msgb); |
92 |
int msg_buffer_parse_next(struct msg_buffer *msgb, uint8_t **buf); |
93 |
void msg_buffer_parse_stop(struct msg_buffer *msgb); |
94 |
|
95 |
/* Reinitialize the bootstrap of the buffer.
|
96 |
* The next data will be ready when the buffer reaches a size greater or equal
|
97 |
* to the start buffering threshold (configured using
|
98 |
* msg_buffer_set_start_buf_size_th)
|
99 |
*/
|
100 |
void msg_buffer_start_buf_reinit(struct msg_buffer *msgb, uint32_t to_us); |
101 |
|
102 |
/* Get/Set functions */
|
103 |
|
104 |
/* return the initial buffering threshold */
|
105 |
uint32_t msg_buffer_get_start_buf_size(struct msg_buffer *msgb);
|
106 |
/* return the current buffer size in byte */
|
107 |
uint32_t msg_buffer_get_current_size(struct msg_buffer *msgb);
|
108 |
/* set the timeouts (us) for threshold1 (TH1) and threshold2 (TH2) */
|
109 |
void msg_buffer_set_ths_to(struct msg_buffer *msgb, |
110 |
uint32_t th1_to_us, |
111 |
uint32_t th2_to_us); |
112 |
/* set the sizes (bytes) for threshold1 (TH1) and threshold2 (TH2) */
|
113 |
void msg_buffer_set_ths_size(struct msg_buffer *msgb, |
114 |
uint32_t th1_size, |
115 |
uint32_t th2_size); |
116 |
/* set the timeout (us) for the initial buffering */
|
117 |
void msg_buffer_set_initial_buffering_to_us(struct msg_buffer *msgb, |
118 |
uint32_t to_us); |
119 |
/* set the size (bytes) of the initial buffering
|
120 |
*
|
121 |
* msg_buffer_get_status() will return MSG_BUFFER_DATA_NOT_READY until the start
|
122 |
* buffer size is reached or until the initail buffer timeout expires (whatever
|
123 |
* comes first)
|
124 |
*/
|
125 |
void msg_buffer_set_start_buf_size_th(struct msg_buffer *msgb, |
126 |
uint32_t size_th); |
127 |
/* Set the flush timeout for the buffer.
|
128 |
* The buffer start flushing if the timeout expires.
|
129 |
* The timeout is reset every time a new message is pushed into the buffer
|
130 |
*/
|
131 |
void msg_buffer_set_flush_to_us(struct msg_buffer *msgb, uint32_t to_us); |
132 |
/* Return 1 if the msg_buffer is currently flushing, 0 otherwise */
|
133 |
int msg_buffer_is_flushing(struct msg_buffer *msgb); |
134 |
/* Return the number of slots in the buffer */
|
135 |
uint32_t msg_buffer_get_nslots(struct msg_buffer *msgb);
|
136 |
|
137 |
#endif // MSG_BUFFER |