brianthelion

syncfilter.c

Oct 1st, 2012
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.43 KB | None | 0 0
  1. /*
  2.  * GStreamer
  3.  * Copyright (C) 2005 Thomas Vander Stichele <[email protected]>
  4.  * Copyright (C) 2005 Ronald S. Bultje <[email protected]>
  5.  * Copyright (C) 2012 BrianTheLion <<[email protected]>>
  6.  *
  7.  * Permission is hereby granted, free of charge, to any person obtaining a
  8.  * copy of this software and associated documentation files (the "Software"),
  9.  * to deal in the Software without restriction, including without limitation
  10.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11.  * and/or sell copies of the Software, and to permit persons to whom the
  12.  * Software is furnished to do so, subject to the following conditions:
  13.  *
  14.  * The above copyright notice and this permission notice shall be included in
  15.  * all copies or substantial portions of the Software.
  16.  *
  17.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22.  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23.  * DEALINGS IN THE SOFTWARE.
  24.  *
  25.  * Alternatively, the contents of this file may be used under the
  26.  * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
  27.  * which case the following provisions apply instead of the ones
  28.  * mentioned above:
  29.  *
  30.  * This library is free software; you can redistribute it and/or
  31.  * modify it under the terms of the GNU Library General Public
  32.  * License as published by the Free Software Foundation; either
  33.  * version 2 of the License, or (at your option) any later version.
  34.  *
  35.  * This library is distributed in the hope that it will be useful,
  36.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  38.  * Library General Public License for more details.
  39.  *
  40.  * You should have received a copy of the GNU Library General Public
  41.  * License along with this library; if not, write to the
  42.  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  43.  * Boston, MA 02111-1307, USA.
  44.  */
  45.  
  46. /**
  47.  * SECTION:element-syncfilter
  48.  *
  49.  * FIXME:Describe syncfilter here.
  50.  *
  51.  * <refsect2>
  52.  * <title>Example launch line</title>
  53.  * |[
  54.  * gst-launch -v -m fakesrc ! syncfilter ! fakesink silent=TRUE
  55.  * ]|
  56.  * </refsect2>
  57.  */
  58.  
  59. #ifdef HAVE_CONFIG_H
  60. #  include <config.h>
  61. #endif
  62.  
  63. #include <gst/gst.h>
  64.  
  65. #include "gstsyncfilter.h"
  66.  
  67. GST_DEBUG_CATEGORY_STATIC (gst_sync_filter_debug);
  68. #define GST_CAT_DEFAULT gst_sync_filter_debug
  69.  
  70. /* Filter signals and args */
  71. enum {
  72.     /* FILL ME */
  73.     LAST_SIGNAL
  74. };
  75.  
  76. enum {
  77.     PROP_0, PROP_SILENT
  78. };
  79.  
  80. /* the capabilities of the inputs and outputs.
  81.  *
  82.  * describe the real formats here.
  83.  */
  84.  
  85. //static GstStaticPadTemplate SINK_FACTORY_GLOBAL = GST_STATIC_PAD_TEMPLATE("sink", GST_PAD_SINK, GST_PAD_ALWAYS, GST_STATIC_CAPS("ANY"));
  86. static GstStaticPadTemplate SINK_FACTORY_GLOBAL = GST_STATIC_PAD_TEMPLATE(
  87.         "sink", GST_PAD_SINK, GST_PAD_ALWAYS, GST_STATIC_CAPS_ANY);
  88.  
  89. //static GstStaticPadTemplate SRC_FACTORY_GLOBAL = GST_STATIC_PAD_TEMPLATE("src", GST_PAD_SRC, GST_PAD_ALWAYS, GST_STATIC_CAPS("ANY"));
  90. static GstStaticPadTemplate SRC_FACTORY_GLOBAL = GST_STATIC_PAD_TEMPLATE("src",
  91.         GST_PAD_SRC, GST_PAD_ALWAYS, GST_STATIC_CAPS_ANY);
  92.  
  93. #define gst_sync_filter_parent_class parent_class
  94. G_DEFINE_TYPE(GstSyncFilter, gst_sync_filter, GST_TYPE_ELEMENT);
  95.  
  96. static void gst_sync_filter_set_property(GObject * object, guint prop_id,
  97.         const GValue * value, GParamSpec * pspec);
  98. static void gst_sync_filter_get_property(GObject * object, guint prop_id,
  99.         GValue * value, GParamSpec * pspec);
  100.  
  101. //static gboolean gst_sync_filter_sink_event(GstPad * pad, GstObject * parent, GstEvent * event);
  102. //static GstFlowReturn gst_sync_filter_chain(GstPad * pad, GstObject * parent, GstBuffer * buf);
  103. static GstFlowReturn gst_syncfilter_collected(GstCollectPads * pads,
  104.         GstSyncFilter * filter);
  105.  
  106. /* GObject vmethod implementations */
  107.  
  108. /* initialize the syncfilter's class */
  109. static void gst_sync_filter_class_init(GstSyncFilterClass * klass) {
  110.     GObjectClass *gobject_class;
  111.     GstElementClass *gstelement_class;
  112.  
  113.     gobject_class = (GObjectClass *) klass;
  114.     gstelement_class = (GstElementClass *) klass;
  115.  
  116.     gobject_class->set_property = gst_sync_filter_set_property;
  117.     gobject_class->get_property = gst_sync_filter_get_property;
  118.  
  119.     g_object_class_install_property(gobject_class, PROP_SILENT,
  120.             g_param_spec_boolean("silent", "Silent", "Produce verbose output ?",
  121.                     FALSE, G_PARAM_READWRITE));
  122.  
  123.     gst_element_class_set_details_simple(gstelement_class, "SyncFilter",
  124.             "FIXME:Generic", "FIXME:Generic Template Element",
  125.             "BrianTheLion <<[email protected]>>");
  126.  
  127.     gst_element_class_add_pad_template(gstelement_class,
  128.             gst_static_pad_template_get(&SRC_FACTORY_GLOBAL));
  129.     gst_element_class_add_pad_template(gstelement_class,
  130.             gst_static_pad_template_get(&SINK_FACTORY_GLOBAL));
  131. }
  132.  
  133. /* initialize the new element
  134.  * instantiate pads and add them to element
  135.  * set pad calback functions
  136.  * initialize instance structure
  137.  */
  138. static void gst_sync_filter_init(GstSyncFilter * filter) {
  139.  
  140.     filter->asnkpad = gst_pad_new_from_static_template(&SINK_FACTORY_GLOBAL,
  141.             "asnk");
  142. //  gst_pad_set_event_function(filter->asnkpad, GST_DEBUG_FUNCPTR(gst_sync_filter_sink_event));
  143. //  gst_pad_set_chain_function(filter->asnkpad, GST_DEBUG_FUNCPTR(gst_sync_filter_chain));
  144.     GST_PAD_SET_PROXY_CAPS(filter->asnkpad);
  145.     gst_element_add_pad(GST_ELEMENT(filter), filter->asnkpad);
  146.  
  147.     filter->vsnkpad = gst_pad_new_from_static_template(&SINK_FACTORY_GLOBAL,
  148.             "vsnk");
  149. //  gst_pad_set_event_function(filter->vsnkpad, GST_DEBUG_FUNCPTR(gst_sync_filter_sink_event));
  150. //  gst_pad_set_chain_function(filter->vsnkpad, GST_DEBUG_FUNCPTR(gst_sync_filter_chain));
  151.     GST_PAD_SET_PROXY_CAPS(filter->vsnkpad);
  152.     gst_element_add_pad(GST_ELEMENT(filter), filter->vsnkpad);
  153.  
  154.     filter->asrcpad = gst_pad_new_from_static_template(&SRC_FACTORY_GLOBAL,
  155.             "asrc");
  156.     GST_PAD_SET_PROXY_CAPS(filter->asrcpad);
  157.     gst_element_add_pad(GST_ELEMENT(filter), filter->asrcpad);
  158.  
  159.     filter->vsrcpad = gst_pad_new_from_static_template(&SRC_FACTORY_GLOBAL,
  160.             "vsrc");
  161.     GST_PAD_SET_PROXY_CAPS(filter->vsrcpad);
  162.     gst_element_add_pad(GST_ELEMENT(filter), filter->vsrcpad);
  163.  
  164.     filter->collect = gst_collect_pads_new();
  165.     gst_collect_pads_set_function(filter->collect,
  166.             (GstCollectPadsFunction) gst_syncfilter_collected, filter);
  167.  
  168.     filter->video_collect_data = gst_collect_pads_add_pad(filter->collect,
  169.             filter->asnkpad, sizeof(GstCollectData), NULL, TRUE);
  170.     filter->audio_collect_data = gst_collect_pads_add_pad(filter->collect,
  171.             filter->vsnkpad, sizeof(GstCollectData), NULL, TRUE);
  172.  
  173.     filter->silent = FALSE;
  174. }
  175.  
  176. static void gst_sync_filter_set_property(GObject * object, guint prop_id,
  177.         const GValue * value, GParamSpec * pspec) {
  178.     GstSyncFilter *filter = GST_SYNCFILTER (object);
  179.  
  180.     switch (prop_id) {
  181.     case PROP_SILENT:
  182.         filter->silent = g_value_get_boolean(value);
  183.         break;
  184.     default:
  185.         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
  186.         break;
  187.     }
  188. }
  189.  
  190. static void gst_sync_filter_get_property(GObject * object, guint prop_id,
  191.         GValue * value, GParamSpec * pspec) {
  192.     GstSyncFilter *filter = GST_SYNCFILTER (object);
  193.  
  194.     switch (prop_id) {
  195.     case PROP_SILENT:
  196.         g_value_set_boolean(value, filter->silent);
  197.         break;
  198.     default:
  199.         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
  200.         break;
  201.     }
  202. }
  203.  
  204. /* GstElement vmethod implementations */
  205.  
  206. /* this function handles sink events */
  207. //static gboolean gst_sync_filter_sink_event(GstPad * pad, GstObject * parent, GstEvent * event) {
  208. //  gboolean ret;
  209. //  GstSyncFilter *filter;
  210. //
  211. //  filter = GST_SYNCFILTER (parent);
  212. //
  213. //  switch (GST_EVENT_TYPE(event)) {
  214. //  case GST_EVENT_CAPS: {
  215. //      GstCaps * caps;
  216. //
  217. //      gst_event_parse_caps(event, &caps);
  218. //      /* do something with the caps */
  219. //
  220. //      /* and forward */
  221. //      ret = gst_pad_event_default(pad, parent, event);
  222. //      break;
  223. //  }
  224. //  default:
  225. //      ret = gst_pad_event_default(pad, parent, event);
  226. //      break;
  227. //  }
  228. //  return ret;
  229. //}
  230. /* chain function
  231.  * this function does the actual processing
  232.  */
  233. //static GstFlowReturn gst_sync_filter_chain(GstPad * pad, GstObject * parent, GstBuffer * buf) {
  234. //  GstSyncFilter *filter;
  235. //
  236. //  filter = GST_SYNCFILTER (parent);
  237. //
  238. //  if (filter->silent == FALSE)
  239. //      g_print("I'm plugged, therefore I'm in.\n");
  240. //
  241. //  /* just push out the incoming buffer without touching it */
  242. //  return gst_pad_push(filter->srcpad, buf);
  243. //}
  244. static GstFlowReturn gst_syncfilter_collected(GstCollectPads * pads,
  245.         GstSyncFilter * filter) {
  246.  
  247.     GstFlowReturn ret = GST_FLOW_OK;
  248. //  GstClockTime now, video_end, audio_end;
  249.     GstBuffer *video_frame = NULL;
  250.     GstBuffer *audio_buffer = NULL;
  251. //  gchar *text;
  252. //  gint text_len;
  253.  
  254.     GST_DEBUG("Collecting");
  255.  
  256.     video_frame = gst_collect_pads_peek(filter->collect,
  257.             filter->video_collect_data);
  258.     audio_buffer = gst_collect_pads_peek(filter->collect,
  259.             filter->audio_collect_data);
  260.  
  261.     if (video_frame != NULL) {
  262.         if (GST_BUFFER_TIMESTAMP(video_frame) == GST_CLOCK_TIME_NONE)
  263.             g_warning("%s: video frame has invalid timestamp", G_STRLOC);
  264. //      GST_DEBUG ("Got video frame: %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (frame_end));
  265.         ret = gst_pad_push(filter->vsrcpad, video_frame);
  266.     }
  267.  
  268.     if (audio_buffer != NULL) {
  269.         if (GST_BUFFER_TIMESTAMP(audio_buffer) == GST_CLOCK_TIME_NONE)
  270.             g_warning("%s: audio buffer has invalid timestamp", G_STRLOC);
  271. //      txt_end = GST_BUFFER_TIMESTAMP(audio_buffer) + GST_BUFFER_DURATION(audio_buffer);
  272. //      GST_DEBUG ("Got text buffer: %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (text_buf)), GST_TIME_ARGS (txt_end));
  273.         ret = gst_pad_push(filter->asrcpad, audio_buffer);
  274.     }
  275.  
  276.     if ((video_frame == NULL) & (audio_buffer == NULL)) {
  277.         gst_pad_push_event(filter->asrcpad, gst_event_new_eos());
  278.         gst_pad_push_event(filter->vsrcpad, gst_event_new_eos());
  279.         ret = GST_FLOW_EOS;
  280.         goto done;
  281.     }
  282.  
  283. //  /* send EOS if video stream EOSed regardless of text stream */
  284. //  if (video_frame == NULL) {
  285. //      GST_DEBUG("Video stream at EOS");
  286. //      if (filter->audio_collect_data) {
  287. //          audio_buffer = gst_collect_pads_pop(filter->collect, filter->audio_collect_data);
  288. //      }
  289. //      gst_pad_push_event(filter->vsrcpad, gst_event_new_eos());
  290. //      ret = GST_FLOW_EOS;
  291. //      goto done;
  292. //  }
  293.  
  294. //  if (GST_BUFFER_TIMESTAMP(video_frame) == GST_CLOCK_TIME_NONE) {
  295. //      g_warning("%s: video frame has invalid timestamp", G_STRLOC);
  296. //  }
  297.  
  298. //  now = GST_BUFFER_TIMESTAMP(video_frame);
  299. //
  300. //  if (GST_BUFFER_DURATION(video_frame) != GST_CLOCK_TIME_NONE) {
  301. //      frame_end = now + GST_BUFFER_DURATION(video_frame);
  302. //  } else if (filter->fps_n > 0) {
  303. //      frame_end = now + gst_util_uint64_scale_int(GST_SECOND, filter->fps_d, filter->fps_n);
  304. //  } else {
  305. //      /* magic value, does not really matter since texts
  306. //       * tend to span quite a few frames in practice anyway */
  307. //      frame_end = now + GST_SECOND / 25;
  308. //  }
  309. //
  310. //  GST_DEBUG ("Got video frame: %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (frame_end));
  311. //
  312. //  /* text pad not linked? */
  313. //  if (filter->audio_collect_data == NULL) {
  314. //      GST_DEBUG("Text pad not linked, rendering default text: '%s'", GST_STR_NULL(filter->default_text));
  315. //      if (filter->default_text && *filter->default_text != '\0') {
  316. //          gst_text_overlay_render_text(filter, filter->default_text, -1);
  317. //          ret = gst_text_overlay_push_frame(filter, video_frame);
  318. //      } else {
  319. //          ret = gst_pad_push(filter->srcpad, video_frame);
  320. //      }
  321. //      gst_text_overlay_pop_video(filter);
  322. //      video_frame = NULL;
  323. //      goto done;
  324. //  }
  325.  
  326. //  audio_buffer = gst_collect_pads_peek(filter->collect, filter->audio_collect_data);
  327.  
  328.     /* just push the video frame if the text stream has EOSed */
  329. //  if (audio_buffer == NULL) {
  330. //      GST_DEBUG("Text pad EOSed, just pushing video frame as is");
  331. //      ret = gst_pad_push(filter->srcpad, video_frame);
  332. //      gst_text_overlay_pop_video(filter);
  333. //      video_frame = NULL;
  334. //      goto done;
  335. //  }
  336.     /* if the text buffer isn't stamped right, pop it off the
  337.      *  queue and display it for the current video frame only */
  338. //  if (GST_BUFFER_TIMESTAMP(audio_buffer) == GST_CLOCK_TIME_NONE || GST_BUFFER_DURATION(audio_buffer) == GST_CLOCK_TIME_NONE) {
  339. //      GST_WARNING("Got text buffer with invalid time stamp or duration");
  340. //      gst_text_overlay_pop_text(filter);
  341. //      GST_BUFFER_TIMESTAMP (text_buf) = now;
  342. //      GST_BUFFER_DURATION (text_buf) = frame_end - now;
  343. //  }
  344. //  txt_end = GST_BUFFER_TIMESTAMP(audio_buffer) + GST_BUFFER_DURATION(audio_buffer);
  345. //
  346. //  GST_DEBUG ("Got text buffer: %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (text_buf)), GST_TIME_ARGS (txt_end));
  347.     /* if the text buffer is too old, pop it off the
  348.      * queue and return so we get a new one next time */
  349. //  if (txt_end < now) {
  350. //      GST_DEBUG("Text buffer too old, popping off the queue");
  351. //      gst_text_overlay_pop_text(filter);
  352. //      ret = GST_FLOW_OK;
  353. //      goto done;
  354. //  }
  355.     /* if the video frame ends before the text even starts,
  356.      * just push it out as is and pop it off the queue */
  357. //  if (frame_end < GST_BUFFER_TIMESTAMP(audio_buffer)) {
  358. //      GST_DEBUG("Video buffer before text, pushing out and popping off queue");
  359. //      ret = gst_pad_push(filter->srcpad, video_frame);
  360. //      gst_text_overlay_pop_video(filter);
  361. //      video_frame = NULL;
  362. //      goto done;
  363. //  }
  364.     /* text duration overlaps video frame duration */
  365. //  text = g_strndup((gchar *) GST_BUFFER_DATA(audio_buffer), GST_BUFFER_SIZE(audio_buffer));
  366. //  g_strdelimit(text, "\n\r\t", ' ');
  367. //  text_len = strlen(text);
  368. //  if (text_len > 0) {
  369. //      GST_DEBUG("Rendering text '%*s'", text_len, text);
  370. //      ;
  371. //      gst_text_overlay_render_text(filter, text, text_len);
  372. //  } else {
  373. //      GST_DEBUG("No text to render (empty buffer)");
  374. //      gst_text_overlay_render_text(filter, " ", 1);
  375. //  }
  376. //
  377. //  g_free(text);
  378. //
  379. //  gst_text_overlay_pop_video(filter);
  380. //  ret = gst_text_overlay_push_frame(filter, video_frame);
  381. //  video_frame = NULL;
  382. //  goto done;
  383.     done: {
  384.         if (audio_buffer)
  385.             gst_buffer_unref(audio_buffer);
  386.  
  387.         if (video_frame)
  388.             gst_buffer_unref(video_frame);
  389.  
  390.         return ret;
  391.     }
  392. }
  393.  
  394. /* entry point to initialize the plug-in
  395.  * initialize the plug-in itself
  396.  * register the element factories and other features
  397.  */
  398. static gboolean syncfilter_init(GstPlugin * syncfilter) {
  399.     /* debug category for fltering log messages
  400.      *
  401.      * exchange the string 'Template syncfilter' with your description
  402.      */
  403.     GST_DEBUG_CATEGORY_INIT(gst_sync_filter_debug, "syncfilter", 0,
  404.             "Template syncfilter");
  405.     return gst_element_register(syncfilter, "syncfilter", GST_RANK_NONE,
  406.             GST_TYPE_SYNCFILTER);
  407. }
  408.  
  409. /* PACKAGE: this is usually set by autotools depending on some _INIT macro
  410.  * in configure.ac and then written into and defined in config.h, but we can
  411.  * just set it ourselves here in case someone doesn't use autotools to
  412.  * compile this code. GST_PLUGIN_DEFINE needs PACKAGE to be defined.
  413.  */
  414. #ifndef PACKAGE
  415. #define PACKAGE "myfirstsyncfilter"
  416. #endif
  417.  
  418. /* gstreamer looks for this structure to register syncfilters
  419.  *
  420.  * exchange the string 'Template syncfilter' with your syncfilter description
  421.  */
  422. GST_PLUGIN_DEFINE (
  423.         GST_VERSION_MAJOR,
  424.         GST_VERSION_MINOR,
  425.         syncfilter,
  426.         "Template syncfilter",
  427.         syncfilter_init,
  428.         VERSION,
  429.         "LGPL",
  430.         "GStreamer",
  431.         "http://gstreamer.net/"
  432. )
Advertisement
Add Comment
Please, Sign In to add comment