gst_plugins_base/gst/subparse/samiparse.c
branchRCL_3
changeset 29 567bb019e3e3
parent 0 0e761a78d257
child 30 7e817e7e631c
--- a/gst_plugins_base/gst/subparse/samiparse.c	Wed Mar 31 22:03:18 2010 +0300
+++ b/gst_plugins_base/gst/subparse/samiparse.c	Tue Aug 31 15:30:33 2010 +0300
@@ -47,8 +47,8 @@
                                  * and _context_pop_state(). */
   htmlParserCtxtPtr htmlctxt;   /* html parser context */
   gboolean has_result;          /* set when ready to push out result */
-  gboolean in_title;            /* flag to avoid appending the title content
-                                 * to buf */
+  gboolean in_sync;             /* flag to avoid appending anything except the
+                                 * content of the sync elements to buf */
   guint64 time1;                /* previous start attribute in sync tag */
   guint64 time2;                /* current start attribute in sync tag  */
 };
@@ -62,6 +62,7 @@
 static void
 sami_context_push_state (GstSamiContext * sctx, char state)
 {
+  GST_LOG ("state %c", state);
   g_string_append_c (sctx->state, state);
 }
 
@@ -72,6 +73,7 @@
   GString *context_state = sctx->state;
   int i;
 
+  GST_LOG ("state %c", state);
   for (i = context_state->len - 1; i >= 0; i--) {
     switch (context_state->str[i]) {
       case ITALIC_TAG:         /* <i> */
@@ -131,10 +133,13 @@
       if (!value)
         continue;
       if (!xmlStrncmp ((const xmlChar *) "start", key, 5)) {
-        sctx->time1 = sctx->time2;
+        /* Only set a new start time if we don't have text pending */
+        if (sctx->resultbuf->len == 0)
+          sctx->time1 = sctx->time2;
+
         sctx->time2 = atoi ((const char *) value) * GST_MSECOND;
-        sctx->has_result = TRUE;
         g_string_append (sctx->resultbuf, sctx->buf->str);
+        sctx->has_result = (sctx->resultbuf->len != 0) ? TRUE : FALSE;
         g_string_truncate (sctx->buf, 0);
       }
     }
@@ -210,10 +215,11 @@
 {
   GstSamiContext *sctx = (GstSamiContext *) ctx;
 
-  if (!xmlStrncmp ((const xmlChar *) "title", name, 5)) {
-    sctx->in_title = TRUE;
-  } else if (!xmlStrncmp ((const xmlChar *) "sync", name, 4)) {
+  GST_LOG ("name:%s", name);
+
+  if (!xmlStrncmp ((const xmlChar *) "sync", name, 4)) {
     handle_start_sync (sctx, atts);
+    sctx->in_sync = TRUE;
   } else if (!xmlStrncmp ((const xmlChar *) "font", name, 4)) {
     handle_start_font (sctx, atts);
   } else if (!xmlStrncmp ((const xmlChar *) "ruby", name, 4)) {
@@ -239,8 +245,24 @@
 {
   GstSamiContext *sctx = (GstSamiContext *) ctx;
 
-  if (!xmlStrncmp ((const xmlChar *) "title", name, 5)) {
-    sctx->in_title = FALSE;
+  GST_LOG ("name:%s", name);
+
+  if (!xmlStrncmp ((const xmlChar *) "sync", name, 4)) {
+    sctx->in_sync = FALSE;
+  } else if ((!xmlStrncmp ((const xmlChar *) "body", name, 4)) ||
+      (!xmlStrncmp ((const xmlChar *) "sami", name, 4))) {
+    /* We will usually have one buffer left when the body is closed
+     * as we need the next sync to actually send it */
+    if (sctx->buf->len != 0) {
+      /* Only set a new start time if we don't have text pending */
+      if (sctx->resultbuf->len == 0)
+        sctx->time1 = sctx->time2;
+
+      sctx->time2 = GST_CLOCK_TIME_NONE;
+      g_string_append (sctx->resultbuf, sctx->buf->str);
+      sctx->has_result = (sctx->resultbuf->len != 0) ? TRUE : FALSE;
+      g_string_truncate (sctx->buf, 0);
+    }
   } else if (!xmlStrncmp ((const xmlChar *) "font", name, 4)) {
     sami_context_pop_state (sctx, SPAN_TAG);
   } else if (!xmlStrncmp ((const xmlChar *) "ruby", name, 4)) {
@@ -255,12 +277,29 @@
 {
   GstSamiContext *sctx = (GstSamiContext *) ctx;
   gchar *escaped;
+  gchar *tmp;
+  gint i;
 
-  /* skip title */
-  if (sctx->in_title)
+  /* Skip everything except content of the sync elements */
+  if (!sctx->in_sync)
     return;
 
   escaped = g_markup_escape_text ((const gchar *) ch, len);
+  g_strstrip (escaped);
+
+  /* Remove double spaces forom the string as those are
+   * usually added by newlines and indention */
+  tmp = escaped;
+  for (i = 0; i <= strlen (escaped); i++) {
+    escaped[i] = *tmp;
+    if (*tmp != ' ') {
+      tmp++;
+      continue;
+    }
+    while (*tmp == ' ')
+      tmp++;
+  }
+
   if (has_tag (sctx->state, RT_TAG)) {
     g_string_append_c (sctx->rubybuf, ' ');
     g_string_append (sctx->rubybuf, escaped);
@@ -373,7 +412,7 @@
     g_string_truncate (context->resultbuf, 0);
     g_string_truncate (context->state, 0);
     context->has_result = FALSE;
-    context->in_title = FALSE;
+    context->in_sync = FALSE;
     context->time1 = 0;
     context->time2 = 0;
   }