1. Vlc基础数据结构
[email protected]
1.1 基础数据结构 struct vlc_object_t,相关文件为src\misc\objects.c。
定义为:
struct vlc_object_t
{
VLC_COMMON_MEMBERS
};
其中的VLC_COMMON_MEMBERS为micro,扩展而下:
struct vlc_object_t
{
const char *psz_object_type;
char *psz_header;
int i_flags;
volatile bool b_die;
bool b_force;
libvlc_int_t *p_libvlc;
vlc_object_t * p_parent;
};
1.2 VLC_OBJECT宏把包含vlc_object_t且为第一个字段的数据类型cast成vlc_object_t类型
# define VLC_OBJECT( x ) ((vlc_object_t *)(x))
结构vlc_object_internals_t ,在vlc里malloc一个新的object时,vlc_object_internals_t分配在vlc_object_t的前面,vlc_object_internals_t里面的互斥信号量保护object的访问,特别是里面variable的访问, 宏vlc_internals从object指针cast到vlc_object_internals_t指针, 具体的分配函数为object.c: vlc_custom_create()。相关文件为src\libvlc.h
struct vlc_object_internals
{
int i_object_type;
char *psz_name;
void *var_root;
vlc_mutex_t var_lock;
vlc_cond_t var_wait;
vlc_thread_t thread_id;
bool b_thread;
int pipes[2];
vlc_spinlock_t ref_spin;
unsigned i_refcount;
vlc_destructor_t pf_destructor;
vlc_object_internals_t *next;
vlc_object_internals_t *prev;
vlc_object_internals_t *first;
#ifndef NDEBUG
vlc_object_t *old_parent;
#endif
};
1.3 object的释放函数typedef void (*vlc_destructor_t)(struct vlc_object_t *);
其他模块分配了一个object后,可能也分配有资源,就可以自己定义一个destructor函数,并赋值给vlc_object_t,在释放object时会调用这个函数来释放自己分配的资源。
1.4 struct variable_ops_t和struct variable_t结构,vlc的很多参数都是用variable_t结构来表示并动态创建加入某个object的列表。这个列表的root为vlc_object_internals_t.var_root.
每个variable都有name,还有一个回调列表,当改变了这个变量的值时触发这个回调,当然外界也可以调用var_TriggerCallback来触发回调。Vlc里的很多事件就是基于这个原理,如video的长度,time,position等。相关文件为src\misc\variables.c。
typedef struct variable_ops_t
{
int (*pf_cmp) ( vlc_value_t, vlc_value_t );
void (*pf_dup) ( vlc_value_t * );
void (*pf_free) ( vlc_value_t * );
} variable_ops_t;
struct variable_t
{
char * psz_name;
vlc_value_t val;
char * psz_text;
const variable_ops_t *ops;
int i_type;
unsigned i_usage;
vlc_value_t min, max, step;
int i_default;
vlc_list_t choices;
vlc_list_t choices_text;
bool b_incallback;
int i_entries;
callback_entry_t * p_entries;
};
1.5 struct vlc_value_t结构,这是一个union,包含了各种通用的数据类型,一般在struct variable_t里用的很多,定义如下:
typedef union
{
int i_int;
bool b_bool;
float f_float;
char * psz_string;
void * p_address;
vlc_object_t * p_object;
vlc_list_t * p_list;
mtime_t i_time;
struct { int32_t x; int32_t y; } coords;
uint8_t padding[8];
} vlc_value_t;
1.6 struct block_t和 struct block_sys_t,vlc里在vout之前的数据都是以block来保存的。从定义里可以看到vlc内部实际是以block_sys_t来分配的,原因是为了适应各种cpu,vlc为block分配的内存有各种align和pading。struct block_fifo_t为保存block的一个先进先出fifo,提供了多个线程访问struct block_fifo_t之间的同步。相关文件为src\misc\block.c。
struct block_t
{
block_t *p_next;
uint32_t i_flags;
mtime_t i_pts;
mtime_t i_dts;
mtime_t i_length;
int64_t i64_original_pts;
int64_t i64_original_dts;
unsigned i_nb_samples;
int i_rate;
size_t i_buffer;
uint8_t *p_buffer;
block_free_t pf_release;
};
struct block_sys_t
{
block_t self;
size_t i_allocated_buffer;
uint8_t p_allocated_buffer[];
};
struct block_fifo_t
{
vlc_mutex_t lock;
vlc_cond_t wait;
vlc_cond_t wait_room;
block_t *p_first;
block_t **pp_last;
size_t i_depth;
size_t i_size;
bool b_force_wake;
};