///插件的注册//
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, GST_VERSION_MINOR, coreelements, "GStreamer core elements", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
GST_PLUGIN_EXPORT GstPluginDesc gst_plugin_desc = {
GST_VERSION_MAJOR,
GST_VERSION_MINOR,
#coreelements,
(gchar *) "GStreamer core elements",
plugin_init,
VERSION,
GST_LICENSE,
PACKAGE,
GST_PACKAGE_NAME,
GST_PACKAGE_ORIGIN,
__GST_PACKAGE_RELEASE_DATETIME,
GST_PADDING_INIT
};
GstPlugin *gst_plugin_load_file (const gchar * filename, GError ** error) //gstplugin.c 返回一个pligin结构体
ret = g_module_symbol (module, "gst_plugin_desc", &ptr);
desc = (GstPluginDesc *) ptr;
//创建plugin
plugin = g_object_newv (GST_TYPE_PLUGIN, 0, NULL);
plugin->orig_desc = desc;
白名单机制
注册信号机制
gst_plugin_register_func (plugin, plugin->orig_desc, NULL)
gst_plugin_desc_copy (&plugin->desc, desc);
dest->plugin_init = src->plugin_init;
(desc->plugin_init) (plugin); //此处会调用到so插件里的plugin_init()接口
gst_registry_add_plugin (gst_registry_get (), plugin); //好处:存放好防止重复创建plugin
//接下来进行so插件里plugin_init()接口的分析
gboolean plugin_init (GstPlugin * plugin)
if (!gst_element_register (plugin, "filesrc", GST_RANK_PRIMARY, gst_file_src_get_type ())) //此处注册元素
GstRegistry* registry = gst_registry_get ();
GstElementFactory* factory = GST_ELEMENT_FACTORY_CAST (g_object_newv (GST_TYPE_ELEMENT_FACTORY, 0, NULL)); //创建一个factory
klass = GST_ELEMENT_CLASS (g_type_class_ref (type));
factory->metadata = gst_structure_copy ((GstStructure *) klass->metadata);
factory->staticpadtemplates = g_list_append (factory->staticpadtemplates, newt);
gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE_CAST (factory), rank);
gst_registry_add_feature (registry, GST_PLUGIN_FEATURE_CAST (factory));
//接下来进行个别元素的分析. 如filesrc
G_DEFINE_TYPE_WITH_CODE (GstFileSrc, gst_file_src, GST_TYPE_BASE_SRC, _do_init);
//以下2个初始化函数会在 g_object_new()/g_object_newv()时被调用
void gst_file_src_class_init (GstFileSrcClass * klass)
void gst_file_src_init (GstFileSrc * src)
///插件的使用//
gst_element_factory_make ("fakesrc", "source");
GstElement *gst_element_factory_make (const gchar * factoryname, const gchar * name)
//首先查找GstElementFactory* factory
GstElementFactory* factory = gst_element_factory_find (factoryname); //gstelementfactory.c
feature = gst_registry_find_feature (gst_registry_get (), name, GST_TYPE_ELEMENT_FACTORY);
return GST_ELEMENT_FACTORY (feature);
//然后根据factory创建element
GstElement* element = gst_element_factory_create (factory, name);
GstElementFactory* newfactory = GST_ELEMENT_FACTORY (gst_plugin_feature_load (GST_PLUGIN_FEATURE(factory)));
factory = newfactory;
GstElement* element = GST_ELEMENT_CAST (g_object_new (factory->type, "name", name, NULL));
GstElementClass* oclass = GST_ELEMENT_GET_CLASS (element);
return element;
g_object_set(G_OBJECT(source),"device",argv[2],NULL); //设置属性,由glibc机制提供
source_capsfilter = gst_element_factory_make("capsfilter", "source_capsfilter");
source_caps = gst_caps_new_simple ("video/x-raw", "format", G_TYPE_STRING, "YUY2", "width", G_TYPE_INT, 1280, "height", G_TYPE_INT, 720, NULL);
g_object_set(G_OBJECT(source_capsfilter),"caps", source_caps, NULL);
/创建一个pipeline
GstElement* pipeline = gst_pipeline_new("usb-camera");
gst_element_factory_make ("pipeline", name); //其实就是向"pipeline"这个factory去新建一个元素.对应gstpipeline.c (gst_pipeline_class_init)
/将元素添加到管道pipeline上
gst_bin_add_many(GST_BIN(pipeline), source, source_capsfilter, converter, sink, NULL);
void gst_bin_add_many (GstBin * bin, GstElement * element_1, ...)
while (element_1) {
gst_bin_add (bin, element_1);
GstBinClass* bclass = GST_BIN_GET_CLASS (bin);
bclass->add_element (bin, element); //调用gstbin.c里的add_element()接口. gst_bin_class_init()
element_1 = va_arg (args, GstElement *);
}
/将管道pipeline上的元素链接起来,构成一条链路
gst_element_link_many (filesrc, typefind, sink, NULL);
gst_element_link_many (GstElement * element_1, GstElement * element_2, ...)
while (element_2) {
if (!gst_element_link (filesrc, typefind))
gst_element_link_pads (filesrc, NULL, typefind, NULL);
gst_element_link_pads_full (filesrc, NULL, typefind, NULL, GST_PAD_LINK_CHECK_DEFAULT); //此过程非常复杂,稍后分析.............
srcpads = GST_ELEMENT_PADS (src);
GstPad* srcpad = srcpads ? GST_PAD_CAST (srcpads->data) : NULL; //获得源pad
GstPad* destpad = destpads ? GST_PAD_CAST (destpads->data) : NULL; //获得目的pad
element_1 = element_2;
element_2 = va_arg (args, GstElement *);
}
/管道pipeline的bus消息机制相关
// create pipeline message bus
loop = g_main_loop_new(NULL, -1);
GstBus* bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline)); //pipeline是结构体pluginelement,GST_PIPELINE(pipeline)后变为结构体GstPipeline
GST_ELEMENT_BUS (element)
return (GST_ELEMENT_CAST(elem)->bus)
gst_bus_add_watch(bus, bus_call, loop);
gst_bus_add_watch_full (bus, G_PRIORITY_DEFAULT, bus_call, loop, NULL);
gst_bus_add_watch_full_unlocked (bus, G_PRIORITY_DEFAULT, bus_call, loop, NULL);
source = gst_bus_create_watch (bus);
source = (GstBusSource *) g_source_new (&gst_bus_source_funcs, sizeof (GstBusSource));
g_source_add_poll ((GSource *) source, &bus->priv->pollfd);
g_source_set_callback (source, (GSourceFunc) func, user_data, notify);
///有关gstreamer自带的"pipeline","bin"两个元素分析//
gst.c
init_post()
g_type_class_ref (gst_type_find_probability_get_type ());
/* register core plugins */ //静态注册插件,区别于动态插件。动态注册使用的是.so文件,调用so文件里的desc结构体里的plugin_init,从而创建元素
gst_plugin_register_static (GST_VERSION_MAJOR, GST_VERSION_MINOR, "staticelements", "core elements linked into the GStreamer library",
gst_register_core_elements, VERSION, GST_LICENSE, PACKAGE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
plugin = g_object_newv (GST_TYPE_PLUGIN, 0, NULL);
gst_plugin_register_func (plugin, &desc, NULL)
//从而调用到下面的函数注册 "bin", "pipeline"两个元素
gst_register_core_elements()
gst_registry_add_plugin (gst_registry_get (), plugin);
gst_register_core_elements()
gst_element_register (plugin, "bin", GST_RANK_PRIMARY, GST_TYPE_BIN)
gst_element_register (plugin, "pipeline", GST_RANK_PRIMARY, GST_TYPE_PIPELINE)