/
06_PacketQueue.cpp
125 lines (93 loc) · 1.96 KB
/
06_PacketQueue.cpp
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
#include <stdint.h>
#include <inttypes.h>
#include <windows.h>
#include <stdio.h>
#ifdef __cplusplus
extern "C"
{
#endif
#include <libavfilter/avfilter.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavdevice/avdevice.h>
//#include <libavutil/imgutils.h>
#include <libavutil/avstring.h>
#include <libswresample/swresample.h>
//#include <libswscale/swscale.h>
#define SDL_MAIN_HANDLED
#include <sdl2/SDL.h>
#include <sdl2/SDL_thread.h>
#include <sdl2/SDL_syswm.h>
#include <sdl2/SDL_render.h>
#include <sdl2/SDL_audio.h>
#ifdef __cplusplus
}
#endif
typedef struct _PacketQueue
{
AVPacketList *first, *last;
int nb_packets, size;
} PacketQueue;
void PacketQueueInit(PacketQueue * pq)
{
memset(pq, 0, sizeof(PacketQueue));
}
int PacketQueuePut(PacketQueue * pq, const AVPacket * srcPkt)
{
AVPacketList *elt;
int rv;
if (!pq) return -1;
elt = (AVPacketList*)av_malloc(sizeof(AVPacketList));
if (!elt) return -1;
elt->pkt = *srcPkt;
elt->next = NULL;
if (!pq->last) // check queue is empty, use last item
pq->first = elt;
else
pq->last->next = elt;
pq->last = elt;
pq->nb_packets++;
pq->size += elt->pkt.size;
return 0;
}
int PacketQueueGet(PacketQueue *pq, AVPacket *pkt)
{
AVPacketList * elt;
int rv;
if (!pq || !pkt) return -1;
elt = pq->first;
if (elt)
{
pq->first = elt->next;
if (!pq->first)
pq->last = NULL; // queue is empty
pq->nb_packets--;
pq->size -= elt->pkt.size;
*pkt = elt->pkt;
av_free(elt);
rv = 1;
}
return rv;
}
PacketQueue audioq;
int main()
{
PacketQueueInit(&audioq);
int i;
AVPacket* pkt;
for (i = 0; i < 10; i++)
{
pkt = av_packet_alloc();
pkt->pos = i;
PacketQueuePut(&audioq, pkt);
}
for (i = 0; i < 10; i++)
{
PacketQueueGet(&audioq, pkt);
printf("Packet pos : %d\n", pkt->pos);
}
printf("Number Of Packets Left : %d\n", audioq.nb_packets);
printf("Size of Queue : %d\n", audioq.size);
system("PAUSE");
return 0;
}