Packet queue architecture location
Corresponding structure source code
MyAVPacketList
typedef struct MyAVPacketList {
AVPacket pkt; //解封装后的数据
struct MyAVPacketList *next; //下一个节点
int serial; //播放序列
} MyAVPacketList;
PacketQueue
typedef struct PacketQueue {
MyAVPacketList *first_pkt, *last_pkt; // 队首,队尾指针
int nb_packets; // 包数量,也就是队列元素数量
int size; // 队列所有元素的数据大小总和
int64_t duration; // 队列所有元素的数据播放持续时间
int abort_request; // 用户退出请求标志
int serial; // 播放序列号,和MyAVPacketList的serial作用相同,但改变的时序稍微有点不同
SDL_mutex *mutex; // 用于维持PacketQueue的多线程安全(SDL_mutex可以按pthread_mutex_t理解)
SDL_cond *cond; // 用于读、写线程相互通知(SDL_cond可以按pthread_cond_t理解)
} PacketQueue;
Memory management
- Inside is a linked list queue MyAVPacketList, which stores data packets.
- Linked list queue is suitable for situations where the specific size of the buffer area is uncertain
- Completely maintained and managed by PacketQueue
- malloc when putting node put
- Free when getting node get
- Locking mechanism - thread safety
- Before and after put, lock and unlock
- Before and after get, lock and unlock
- The writing end is located in the demultiplexing thread, and the reading end is located in the decoding thread.
serial field
-
In order to distinguish the consecutive frames before and after seek
-
Method: After seek, put node serial+1 in the next section
-
Scenes
用户拖动进度条,seek操作,之前缓存的帧不能播放了,要播放seek操作之后新缓存的帧, 为了区别2段帧,加入serial字段
Queue size settings
PacketQueue is a linked list queue
- When there is sufficient memory: there is no limit to placing data packets in the queue, and there is no need to consider the size of the queue.
- Control queue size: If we need to control the size of the queue, we can use the following three variables to limit the number of queue nodes:
- size: Controls the total size of packets in the queue.
- duration: Controls the total playing time of packets in the queue.
- nb_packets: Controls the number of packets in the queue.