Use Suffix Extensions in Makefiles (doc, src/{arm,dht,integration,statistics}) for...
[oweals/gnunet.git] / src / conversation / gnunet_gst.c
index 657bb8e8a65ef2df3d9c97b1c1c604f0d79b2282..52cb2ccbc5dd6c7e474cf1c152f3a20edf47c0b9 100644 (file)
@@ -1,3 +1,27 @@
+/*
+  This file is part of GNUnet.
+  Copyright (C) 2016 GNUnet e.V.
+
+  GNUnet is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published
+  by the Free Software Foundation; either version 3, or (at your
+  option) any later version.
+
+  GNUnet is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with GNUnet; see the file COPYING.  If not, write to the
+  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+  Boston, MA 02110-1301, USA.
+*/
+/**
+ * @file conversation/gnunet_gst.c
+ * @brief FIXME
+ * @author Hark
+ */
 #include "gnunet_gst_def.h"
 
 /**
@@ -5,7 +29,8 @@
  */
 static struct GNUNET_CONFIGURATION_Handle *cfg;
 
-  void
+
+void
 dump_buffer(unsigned n, const unsigned char* buf)
 {
   const unsigned char *p, *end;
@@ -37,15 +62,13 @@ BREAKOUT:
 /***
  * load gnunet configuration
  */
-  void
+void
 gg_load_configuration(GNUNET_gstData * d)
 {
   char *audiobackend_string;
   cfg =  GNUNET_CONFIGURATION_create();
   GNUNET_CONFIGURATION_load(cfg, "mediahelper.conf");
 
-  char *section = "MEDIAHELPER";
-
   GNUNET_CONFIGURATION_get_value_string(cfg, "MEDIAHELPER", "JACK_PP_IN", &d->jack_pp_in);
   GNUNET_CONFIGURATION_get_value_string(cfg, "MEDIAHELPER", "JACK_PP_OUT", &d->jack_pp_out);
 
@@ -53,22 +76,22 @@ gg_load_configuration(GNUNET_gstData * d)
 
  // printf("abstring: %s \n", audiobackend_string);
 
-  if ( audiobackend_string == "AUTO" )
+  if (0 == strcasecmp (audiobackend_string, "AUTO"))
   {
     d->audiobackend = AUTO;
-  } else if ( audiobackend_string = "JACK" )
+  } else if (0 == strcasecmp (audiobackend_string, "JACK"))
   {
     d->audiobackend = JACK;
-  } else if ( audiobackend_string = "ALSA" )
+  } else if (0 == strcasecmp (audiobackend_string, "ALSA"))
   {
     d->audiobackend = ALSA;
-  } else if ( audiobackend_string = "FAKE" )
+  } else if (0 == strcasecmp (audiobackend_string, "FAKE"))
   {
     d->audiobackend = FAKE;
-  } else if ( audiobackend_string = "TEST" )
+  } else if (0 == strcasecmp (audiobackend_string, "TEST"))
   {
     d->audiobackend = TEST;
-  } else 
+  } else
   {
     d->audiobackend = AUTO;
   }
@@ -109,7 +132,7 @@ write_data (const char *ptr, size_t msg_size)
   {
     ret = write (1, &ptr[off], msg_size - off);
     if (0 >= ret)
-    { 
+    {
       if (-1 == ret)
         GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "write");
 //      quit (2);
@@ -123,11 +146,9 @@ write_data (const char *ptr, size_t msg_size)
 extern GstFlowReturn
 on_appsink_new_sample (GstElement * element, GNUNET_gstData * d)
 {
-  static unsigned long long toff;
   //size of message including gnunet header
   size_t msg_size;
+
   GstSample *s;
   GstBuffer *b;
   GstMapInfo map;
@@ -137,14 +158,13 @@ on_appsink_new_sample (GstElement * element, GNUNET_gstData * d)
   GstCaps *s_caps;
   char *caps_str;
 */
-  (d->audio_message)->header.size = htons ((uint16_t) msg_size);
 
   if (gst_app_sink_is_eos(GST_APP_SINK(element)))
     return GST_FLOW_OK;
 
   //pull sample from appsink
    s = gst_app_sink_pull_sample (GST_APP_SINK(element));
-   
+
    if (s == NULL)
      return GST_FLOW_OK;
 
@@ -157,29 +177,23 @@ on_appsink_new_sample (GstElement * element, GNUNET_gstData * d)
 
 
 
-   gst_buffer_map (b, &map, GST_MAP_READ);   
+   gst_buffer_map (b, &map, GST_MAP_READ);
 
    size_t len;
     len = map.size;
    if (len > UINT16_MAX - sizeof (struct AudioMessage))
-   { 
+   {
      // this should never happen?
      printf("GSTREAMER sample too big! \n");
      exit(20);
      len = UINT16_MAX - sizeof (struct AudioMessage);
    }
-   
+
    msg_size = sizeof (struct AudioMessage) + len;
+
   // copy the data into audio_message
-  memcpy (((char *) &(d->audio_message)[1]), map.data, len);
-/*
-  toff += msg_size;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Sending %u bytes of audio data (total: %llu)\n",
-              (unsigned int) msg_size,
-              toff);
-*/
+  GNUNET_memcpy (((char *) &(d->audio_message)[1]), map.data, len);
+  (d->audio_message)->header.size = htons ((uint16_t) msg_size);
   if (d->pure_ogg)
     // write the audio_message without the gnunet headers
     write_data ((const char *) &(d->audio_message)[1], len);
@@ -205,7 +219,7 @@ pl_graph(GstElement * pipeline)
   gst_debug_bin_to_dot_file_with_ts(GST_BIN(pipeline), GST_DEBUG_GRAPH_SHOW_ALL, "record_helper.dot");
 
 #endif
+
 
   //  load_configuration();
 }
@@ -215,7 +229,7 @@ pl_graph(GstElement * pipeline)
 extern gboolean
 gnunet_gst_bus_call (GstBus *bus, GstMessage *msg, gpointer data)
 {
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Bus message\n");
   switch (GST_MESSAGE_TYPE (msg))
   {
@@ -229,15 +243,15 @@ gnunet_gst_bus_call (GstBus *bus, GstMessage *msg, gpointer data)
     {
       gchar  *debug;
       GError *error;
-      
+
       gst_message_parse_error (msg, &error, &debug);
       g_free (debug);
-      
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
-                 "Error: %s\n", 
+
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                 "Error: %s\n",
                  error->message);
       g_error_free (error);
-      
+
       exit (10);
       break;
     }
@@ -253,7 +267,7 @@ gnunet_gst_bus_call (GstBus *bus, GstMessage *msg, gpointer data)
 state_changed_cb (GstBus * bus, GstMessage * msg, GNUNET_gstData * d)
 {
   GstState old_state, new_state, pending_state;
+
   gst_message_parse_state_changed (msg, &old_state, &new_state,
       &pending_state);
   switch (new_state)
@@ -349,7 +363,7 @@ feed_buffer_to_gnunet (GNUNET_gstData * d)
     return OK;
   }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "...pulled!\n");
-  
+
     const GstStructure *si;
     char *si_str;
     GstCaps *s_caps;
@@ -378,7 +392,7 @@ feed_buffer_to_gnunet (GNUNET_gstData * d)
     }
     else
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got sample with no caps\n");
-  
+
   b = gst_sample_get_buffer (s);
   if (NULL == b || !gst_buffer_map (b, &m, GST_MAP_READ))
   {
@@ -396,7 +410,7 @@ feed_buffer_to_gnunet (GNUNET_gstData * d)
   msg_size = sizeof (struct AudioMessage) + len;
   audio_message.header.size = htons ((uint16_t) msg_size);
 
-  
+
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
       "Sending %u bytes of audio data\n", (unsigned int) msg_size);
   for (phase = 0; phase < 2; phase++)
@@ -436,10 +450,10 @@ feed_buffer_to_gnunet (GNUNET_gstData * d)
         return FAIL;
       }
     }
-    
+
  //   if (abort_send)
    //   break;
-      
+
   }
   gst_buffer_unmap (b, &m);
   gst_sample_unref (s);
@@ -477,7 +491,7 @@ feed_buffer_to_gst (const char *audio, size_t b_len, GNUNET_gstData * d)
   switch (flow)
   {
   case GST_FLOW_OK:
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Fed %u bytes to the pipeline\n",
                (unsigned int) b_len);
     break;
@@ -488,7 +502,7 @@ feed_buffer_to_gst (const char *audio, size_t b_len, GNUNET_gstData * d)
     break;
   case GST_FLOW_EOS:
     /* end of stream */
-    GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                "EOS\n");
     break;
   default:
@@ -523,7 +537,7 @@ gst_element_factory_make_debug( gchar *factoryname, gchar *name)
 
 /*
  static gboolean
-gst_element_link_many_debug(...) 
+gst_element_link_many_debug(...)
 {
   va_list arguments;
   gst_element_link_many(argptr);
@@ -544,13 +558,13 @@ lf(char * msg)
  */
 static void
 autoaudiosink_child_added (GstChildProxy *child_proxy,
-                 GObject *object, 
+                 GObject *object,
                  gchar *name,
                  gpointer user_data)
 {
   if (GST_IS_AUDIO_BASE_SRC (object))
     g_object_set (object,
-                 "buffer-time", (gint64) BUFFER_TIME, 
+                 "buffer-time", (gint64) BUFFER_TIME,
                  "latency-time", (gint64) LATENCY_TIME,
                  NULL);
 }
@@ -565,18 +579,19 @@ autoaudiosource_child_added (GstChildProxy *child_proxy, GObject *object, gchar
     g_object_set (object, "buffer-time", (gint64) BUFFER_TIME, "latency-time", (gint64) LATENCY_TIME, NULL);
 }
 
+
 GstElement *
 get_pipeline(GstElement *element)
 {
   GstPipeline *p;
-  
-  p = gst_object_get_parent(element);
 
-  return p;
+  p = GST_PIPELINE (gst_object_get_parent(GST_OBJECT (element)));
+
+  return GST_ELEMENT (p);
 }
 
   static void
-decoder_ogg_pad_added (GstElement *element, 
+decoder_ogg_pad_added (GstElement *element,
               GstPad *pad,
               gpointer data)
 {
@@ -593,8 +608,9 @@ decoder_ogg_pad_added (GstElement *element,
   gst_object_unref (sinkpad);
 }
 
+
 int
-gnunet_read (GNUNET_gstData * d) 
+gnunet_read (GNUNET_gstData * d)
 {
   char readbuf[MAXLINE];
   int ret;
@@ -614,18 +630,21 @@ gnunet_read (GNUNET_gstData * d)
   if (0 == ret)
     return FAIL;
   //#ifdef DEBUG_READ_PURE_OGG
-  
+
      if (d->pure_ogg)
      {
      feed_buffer_to_gst (readbuf, ret, d);
      }
      else
-     { 
+     {
   //#endif
-  GNUNET_SERVER_mst_receive (d->stdin_mst, NULL,
-      readbuf, ret,
-      GNUNET_NO, GNUNET_NO);
+  GNUNET_MST_from_buffer (d->stdin_mst,
+                          readbuf,
+                          ret,
+                          GNUNET_NO,
+                          GNUNET_NO);
      }
+     return 0;
 }
 
 /**
@@ -633,13 +652,14 @@ gnunet_read (GNUNET_gstData * d)
  */
 static int
 stdin_receiver (void *cls,
-               void *client,
                const struct GNUNET_MessageHeader *msg)
 {
   struct AudioMessage *audio;
   size_t b_len;
+
   printf("stdin receiver \n ");
-  dump_buffer(sizeof(msg), msg);
+  dump_buffer (sizeof(msg),
+               (const unsigned char *) msg);
 
   switch (ntohs (msg->type))
   {
@@ -651,7 +671,7 @@ stdin_receiver (void *cls,
     feed_buffer_to_gst ((const char *) &audio[1], b_len, cls);
     break;
   default:
-    printf("No audio message: %u \n ", ntohs(msg->type));  
+    printf("No audio message: %u \n ", ntohs(msg->type));
     break;
   }
   return GNUNET_OK;
@@ -678,19 +698,19 @@ get_app(GNUNET_gstData *d, int type)
           "Audio playback starts\n");
     printf(" creating appsrc \n ");
     //d->audio_message.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_AUDIO);
+
 // d->audio_message = GNUNET_malloc (UINT16_MAX);
  //  d->audio_message = (AudioMessage*)malloc(sizeof(struct AudioMessage));
 //  d->audio_message = GNUNET_malloc(sizeof(struct AudioMessage));
 
 
  //d->audio_message.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_AUDIO);
 
-    d->stdin_mst = GNUNET_SERVER_mst_create (&stdin_receiver, d);
-  
+
+    d->stdin_mst = GNUNET_MST_create (&stdin_receiver, d);
+
     if ( d->stdin_mst == NULL)
-     printf("stdin_mst = NULL"); 
+     printf("stdin_mst = NULL");
 
     d->appsrc     = gst_element_factory_make ("appsrc",       "appsrc");
 
@@ -714,11 +734,11 @@ get_app(GNUNET_gstData *d, int type)
           "Audio source starts\n");
 
     d->appsink     = gst_element_factory_make ("appsink",       "appsink");
-   
+
     // Move this out of here!
     d->audio_message = GNUNET_malloc (UINT16_MAX);
     (d->audio_message)->header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_AUDIO);
-    g_object_set (G_OBJECT (d->appsink), "emit-signals", TRUE, "sync", TRUE, NULL); 
+    g_object_set (G_OBJECT (d->appsink), "emit-signals", TRUE, "sync", TRUE, NULL);
 
     g_signal_connect (d->appsink, "new-sample",
           G_CALLBACK (on_appsink_new_sample), &d);
@@ -744,7 +764,7 @@ get_coder(GNUNET_gstData *d , int type)
 {
   GstBin *bin;
   GstPad *srcpad, *sinkpad, *srcghostpad, *sinkghostpad;
-  GstCaps *caps, *rtpcaps;
+  GstCaps *rtpcaps;
   GstElement *encoder, *muxer, *decoder, *demuxer, *jitterbuffer, *rtpcapsfilter;
 
   if ( d->usertp == TRUE )
@@ -759,7 +779,7 @@ get_coder(GNUNET_gstData *d , int type)
           "payload", G_TYPE_INT, 96,
           "sprop-stereo", G_TYPE_STRING, "0",
           "encoding-params", G_TYPE_STRING, "2",
-          NULL); 
+          NULL);
 */
       rtpcaps = gst_caps_new_simple ("application/x-rtp",
           "media", G_TYPE_STRING, "audio",
@@ -768,7 +788,7 @@ get_coder(GNUNET_gstData *d , int type)
           "payload", G_TYPE_INT, 96,
           "sprop-stereo", G_TYPE_STRING, "0",
           "encoding-params", G_TYPE_STRING, "2",
-          NULL); 
+          NULL);
 
 
       rtpcapsfilter  = gst_element_factory_make ("capsfilter",    "rtpcapsfilter");
@@ -782,7 +802,7 @@ get_coder(GNUNET_gstData *d , int type)
 
 
   if ( type == ENCODER )
-  { 
+  {
     bin = GST_BIN(gst_bin_new("Gnunet audioencoder"));
 
     encoder  = gst_element_factory_make ("opusenc",       "opus-encoder");
@@ -819,7 +839,7 @@ get_coder(GNUNET_gstData *d , int type)
     srcghostpad = gst_ghost_pad_new ("src", srcpad);
 
   }
-  if ( type == DECODER ) 
+  if ( type == DECODER )
   {
      bin = GST_BIN(gst_bin_new("Gnunet audiodecoder"));
 
@@ -829,7 +849,7 @@ get_coder(GNUNET_gstData *d , int type)
 
       demuxer  = gst_element_factory_make ("rtpopusdepay",      "ogg-demuxer");
       jitterbuffer = gst_element_factory_make ("rtpjitterbuffer", "rtpjitterbuffer");
-    } else { 
+    } else {
       demuxer  = gst_element_factory_make ("oggdemux",      "ogg-demuxer");
     }
     decoder  = gst_element_factory_make ("opusdec",       "opus-decoder");
@@ -844,9 +864,9 @@ get_coder(GNUNET_gstData *d , int type)
     } else {
       gst_bin_add_many( bin, demuxer, decoder, NULL);
 
-      g_signal_connect (demuxer, 
+      g_signal_connect (demuxer,
           "pad-added",
-          G_CALLBACK (decoder_ogg_pad_added), 
+          G_CALLBACK (decoder_ogg_pad_added),
           decoder);
 
       sinkpad = gst_element_get_static_pad(demuxer, "sink");
@@ -866,9 +886,11 @@ get_coder(GNUNET_gstData *d , int type)
   gst_element_add_pad (GST_ELEMENT(bin), srcghostpad);
 
 
-  return bin; 
+  return bin;
 }
-  extern GstBin *
+
+
+extern GstBin *
 get_audiobin(GNUNET_gstData *d , int type)
 {
   GstBin *bin;
@@ -893,7 +915,7 @@ get_audiobin(GNUNET_gstData *d , int type)
     {
       sink     = gst_element_factory_make ("autoaudiosink", "audiosink");
       g_signal_connect (sink, "child-added", G_CALLBACK (autoaudiosink_child_added), NULL);
-    
+
     }
 
     if ( d->audiobackend == ALSA )
@@ -904,7 +926,7 @@ get_audiobin(GNUNET_gstData *d , int type)
     if ( d->audiobackend == JACK )
     {
       sink     = gst_element_factory_make ("jackaudiosink", "audiosink");
-      
+
       g_object_set (G_OBJECT (sink), "client-name", "gnunet", NULL);
 
       if (g_object_class_find_property
@@ -925,7 +947,7 @@ get_audiobin(GNUNET_gstData *d , int type)
     }
 
     g_object_set (sink,
-        "buffer-time", (gint64) BUFFER_TIME, 
+        "buffer-time", (gint64) BUFFER_TIME,
         "latency-time", (gint64) LATENCY_TIME,
         NULL);
 
@@ -983,7 +1005,7 @@ get_audiobin(GNUNET_gstData *d , int type)
 
     if ( d->dropsilence == TRUE )
     {
-      gst_bin_add_many (bin , queue ,removesilence , NULL); 
+      gst_bin_add_many (bin , queue ,removesilence , NULL);
 
       if ( !gst_element_link_many ( queue, removesilence, conv,  NULL) )
         lf ("queue, removesilence, conv ");
@@ -995,10 +1017,10 @@ get_audiobin(GNUNET_gstData *d , int type)
       pad = gst_element_get_static_pad(conv, "sink");
 
     }
-    
+
     ghostpad = gst_ghost_pad_new ("sink", pad);
 
-  } else { 
+  } else {
     // SOURCE
 
     bin = GST_BIN(gst_bin_new("Gnunet audiosource"));
@@ -1028,7 +1050,7 @@ get_audiobin(GNUNET_gstData *d , int type)
 
     if (d->audiobackend == AUTO ) {
             g_signal_connect (source, "child-added", G_CALLBACK (autoaudiosource_child_added), NULL);
-    
+
     } else {
       if (GST_IS_AUDIO_BASE_SRC (source))
         g_object_set (source, "buffer-time", (gint64) BUFFER_TIME, "latency-time", (gint64) LATENCY_TIME, NULL);
@@ -1039,7 +1061,7 @@ get_audiobin(GNUNET_gstData *d , int type)
         {
 
           char *portpattern = "moc";
-          
+
           g_object_set (G_OBJECT (source), "port-pattern", portpattern,
               NULL);
         }