forked from xufuji456/FFmpegAndroid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVpacket_queue.c
More file actions
63 lines (57 loc) · 1.65 KB
/
AVpacket_queue.c
File metadata and controls
63 lines (57 loc) · 1.65 KB
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
//
// Created by frank on 2018/2/3.
//
#include "AVpacket_queue.h"
#include <stdlib.h>
#include <libavcodec/avcodec.h>
AVPacketQueue* queue_init(int size){
AVPacketQueue* queue = malloc(sizeof(AVPacketQueue));
queue->size = size;
queue->next_to_read = 0;
queue->next_to_write = 0;
int i;
queue->packets = malloc(sizeof(*queue->packets) * size);
for(i = 0; i < size; i++){
queue->packets[i] = malloc(sizeof(AVPacket));
}
return queue;
}
void queue_free(AVPacketQueue *queue){
int i;
for(i=0; i<queue->size; i++){
free(queue->packets[i]);
}
free(queue->packets);
free(queue);
}
int queue_next(AVPacketQueue* queue, int current){
return (current+1) % queue->size;
}
void* queue_push(AVPacketQueue* queue, pthread_mutex_t* mutex, pthread_cond_t* cond){
int current = queue->next_to_write;
int next_to_write;
for(;;){
next_to_write = queue_next(queue, current);
//写的不等于读的,跳出循环
if(next_to_write != queue->next_to_read){
break;
}
pthread_cond_wait(cond, mutex);
}
queue->next_to_write = next_to_write;
pthread_cond_broadcast(cond);
return queue->packets[current];
}
void* queue_pop(AVPacketQueue* queue, pthread_mutex_t* mutex, pthread_cond_t* cond){
int current = queue->next_to_read;
for(;;){
//写的不等于读的,跳出循环
if(queue->next_to_write != queue->next_to_read){
break;
}
pthread_cond_wait(cond, mutex);
}
queue->next_to_read = queue_next(queue, current);
pthread_cond_broadcast(cond);
return queue->packets[current];
}