gst_plugins_base/gst-libs/gst/rtsp/gstrtspmessage.c
branchRCL_3
changeset 30 7e817e7e631c
parent 29 567bb019e3e3
--- a/gst_plugins_base/gst-libs/gst/rtsp/gstrtspmessage.c	Tue Aug 31 15:30:33 2010 +0300
+++ b/gst_plugins_base/gst-libs/gst/rtsp/gstrtspmessage.c	Wed Sep 01 12:16:41 2010 +0100
@@ -57,7 +57,6 @@
 
 #include <string.h>
 
-#include <gst/gstutils.h>
 #include "gstrtspmessage.h"
 
 typedef struct _RTSPKeyValue
@@ -82,9 +81,9 @@
  * gst_rtsp_message_new:
  * @msg: a location for the new #GstRTSPMessage
  *
- * Create a new initialized #GstRTSPMessage. Free with gst_rtsp_message_free().
+ * Create a new initialized #GstRTSPMessage.
  *
- * Returns: a #GstRTSPResult.
+ * Returns: a #GstRTSPResult. Free with gst_rtsp_message_free().
  */
 GstRTSPResult
 gst_rtsp_message_new (GstRTSPMessage ** msg)
@@ -145,9 +144,9 @@
  * @uri: the uri of the request
  *
  * Create a new #GstRTSPMessage with @method and @uri and store the result
- * request message in @msg. Free with gst_rtsp_message_free().
+ * request message in @msg. 
  *
- * Returns: a #GstRTSPResult.
+ * Returns: a #GstRTSPResult. Free with gst_rtsp_message_free().
  */
 GstRTSPResult
 gst_rtsp_message_new_request (GstRTSPMessage ** msg, GstRTSPMethod method,
@@ -214,7 +213,7 @@
     GstRTSPMethod * method, const gchar ** uri, GstRTSPVersion * version)
 {
   g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
-  g_return_val_if_fail (msg->type == GST_RTSP_MESSAGE_REQUEST, GST_RTSP_EINVAL);
+  g_return_val_if_fail (msg->type != GST_RTSP_MESSAGE_REQUEST, GST_RTSP_EINVAL);
 
   if (method)
     *method = msg->type_data.request.method;
@@ -234,14 +233,14 @@
  * @request: the request that triggered the response or #NULL
  *
  * Create a new response #GstRTSPMessage with @code and @reason and store the
- * result message in @msg. Free with gst_rtsp_message_free().
+ * result message in @msg. 
  *
  * When @reason is #NULL, the default reason for @code will be used.
  *
  * When @request is not #NULL, the relevant headers will be copied to the new
  * response message.
  *
- * Returns: a #GstRTSPResult.
+ * Returns: a #GstRTSPResult. Free with gst_rtsp_message_free().
  */
 GstRTSPResult
 gst_rtsp_message_new_response (GstRTSPMessage ** msg, GstRTSPStatusCode code,
@@ -310,7 +309,8 @@
         *pos = '\0';
       }
       g_strchomp (header);
-      gst_rtsp_message_take_header (msg, GST_RTSP_HDR_SESSION, header);
+      gst_rtsp_message_add_header (msg, GST_RTSP_HDR_SESSION, header);
+      g_free (header);
     }
 
     /* FIXME copy more headers? */
@@ -340,7 +340,7 @@
     GstRTSPStatusCode * code, const gchar ** reason, GstRTSPVersion * version)
 {
   g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
-  g_return_val_if_fail (msg->type == GST_RTSP_MESSAGE_RESPONSE,
+  g_return_val_if_fail (msg->type != GST_RTSP_MESSAGE_RESPONSE,
       GST_RTSP_EINVAL);
 
   if (code)
@@ -359,9 +359,9 @@
  * @channel: the channel
  *
  * Create a new data #GstRTSPMessage with @channel and store the
- * result message in @msg. Free with gst_rtsp_message_free().
+ * result message in @msg. 
  *
- * Returns: a #GstRTSPResult.
+ * Returns: a #GstRTSPResult. Free with gst_rtsp_message_free().
  */
 GstRTSPResult
 gst_rtsp_message_new_data (GstRTSPMessage ** msg, guint8 channel)
@@ -412,7 +412,7 @@
 gst_rtsp_message_parse_data (GstRTSPMessage * msg, guint8 * channel)
 {
   g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
-  g_return_val_if_fail (msg->type == GST_RTSP_MESSAGE_DATA, GST_RTSP_EINVAL);
+  g_return_val_if_fail (msg->type != GST_RTSP_MESSAGE_DATA, GST_RTSP_EINVAL);
 
   if (channel)
     *channel = msg->type_data.data.channel;
@@ -424,7 +424,7 @@
  * gst_rtsp_message_unset:
  * @msg: a #GstRTSPMessage
  *
- * Unset the contents of @msg so that it becomes an uninitialized
+ * Unset the concents of @msg so that it becomes an uninitialized
  * #GstRTSPMessage again. This function is mostly used in combination with 
  * gst_rtsp_message_init_request(), gst_rtsp_message_init_response() and
  * gst_rtsp_message_init_data() on stack allocated #GstRTSPMessage structures.
@@ -463,7 +463,7 @@
   }
   g_free (msg->body);
 
-  memset (msg, 0, sizeof (GstRTSPMessage));
+  memset (msg, 0, sizeof *msg);
 
   return GST_RTSP_OK;
 }
@@ -491,21 +491,18 @@
 }
 
 /**
- * gst_rtsp_message_take_header:
+ * gst_rtsp_message_add_header:
  * @msg: a #GstRTSPMessage
  * @field: a #GstRTSPHeaderField
  * @value: the value of the header
  *
- * Add a header with key @field and @value to @msg. This function takes
- * ownership of @value.
+ * Add a header with key @field and @value to @msg.
  *
  * Returns: a #GstRTSPResult.
- *
- * Since: 0.10.23
  */
 GstRTSPResult
-gst_rtsp_message_take_header (GstRTSPMessage * msg, GstRTSPHeaderField field,
-    gchar * value)
+gst_rtsp_message_add_header (GstRTSPMessage * msg, GstRTSPHeaderField field,
+    const gchar * value)
 {
   RTSPKeyValue key_value;
 
@@ -513,7 +510,7 @@
   g_return_val_if_fail (value != NULL, GST_RTSP_EINVAL);
 
   key_value.field = field;
-  key_value.value = value;
+  key_value.value = g_strdup (value);
 
   g_array_append_val (msg->hdr_fields, key_value);
 
@@ -521,24 +518,6 @@
 }
 
 /**
- * gst_rtsp_message_add_header:
- * @msg: a #GstRTSPMessage
- * @field: a #GstRTSPHeaderField
- * @value: the value of the header
- *
- * Add a header with key @field and @value to @msg. This function takes a copy
- * of @value.
- *
- * Returns: a #GstRTSPResult.
- */
-GstRTSPResult
-gst_rtsp_message_add_header (GstRTSPMessage * msg, GstRTSPHeaderField field,
-    const gchar * value)
-{
-  return gst_rtsp_message_take_header (msg, field, g_strdup (value));
-}
-
-/**
  * gst_rtsp_message_remove_header:
  * @msg: a #GstRTSPMessage
  * @field: a #GstRTSPHeaderField
@@ -560,10 +539,9 @@
   g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
 
   while (i < msg->hdr_fields->len) {
-    RTSPKeyValue *key_value = &g_array_index (msg->hdr_fields, RTSPKeyValue, i);
+    RTSPKeyValue key_value = g_array_index (msg->hdr_fields, RTSPKeyValue, i);
 
-    if (key_value->field == field && (indx == -1 || cnt++ == indx)) {
-      g_free (key_value->value);
+    if (key_value.field == field && (indx == -1 || cnt++ == indx)) {
       g_array_remove_index (msg->hdr_fields, i);
       res = GST_RTSP_OK;
       if (indx != -1)
@@ -582,8 +560,7 @@
  * @value: pointer to hold the result
  * @indx: the index of the header
  *
- * Get the @indx header value with key @field from @msg. The result in @value
- * stays valid as long as it remains present in @msg.
+ * Get the @indx header value with key @field from @msg.
  *
  * Returns: #GST_RTSP_OK when @field was found, #GST_RTSP_ENOTIMPL if the key
  * was not found.
@@ -597,16 +574,12 @@
 
   g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
 
-  /* no header initialized, there are no headers */
-  if (msg->hdr_fields == NULL)
-    return GST_RTSP_ENOTIMPL;
+  for (i = 0; i < msg->hdr_fields->len; i++) {
+    RTSPKeyValue key_value = g_array_index (msg->hdr_fields, RTSPKeyValue, i);
 
-  for (i = 0; i < msg->hdr_fields->len; i++) {
-    RTSPKeyValue *key_value = &g_array_index (msg->hdr_fields, RTSPKeyValue, i);
-
-    if (key_value->field == field && cnt++ == indx) {
+    if (key_value.field == field && cnt++ == indx) {
       if (value)
-        *value = key_value->value;
+        *value = key_value.value;
       return GST_RTSP_OK;
     }
   }
@@ -633,13 +606,10 @@
   g_return_val_if_fail (str != NULL, GST_RTSP_EINVAL);
 
   for (i = 0; i < msg->hdr_fields->len; i++) {
-    RTSPKeyValue *key_value;
-    const gchar *keystr;
+    RTSPKeyValue key_value = g_array_index (msg->hdr_fields, RTSPKeyValue, i);
+    const gchar *keystr = gst_rtsp_header_as_text (key_value.field);
 
-    key_value = &g_array_index (msg->hdr_fields, RTSPKeyValue, i);
-    keystr = gst_rtsp_header_as_text (key_value->field);
-
-    g_string_append_printf (str, "%s: %s\r\n", keystr, key_value->value);
+    g_string_append_printf (str, "%s: %s\r\n", keystr, key_value.value);
   }
   return GST_RTSP_OK;
 }
@@ -742,7 +712,38 @@
 }
 
 static void
-dump_key_value (gpointer data, gpointer user_data G_GNUC_UNUSED)
+dump_mem (guint8 * mem, guint size)
+{
+  guint i, j;
+  GString *string = g_string_sized_new (50);
+  GString *chars = g_string_sized_new (18);
+
+  i = j = 0;
+  while (i < size) {
+    if (g_ascii_isprint (mem[i]))
+      g_string_append_printf (chars, "%c", mem[i]);
+    else
+      g_string_append_printf (chars, ".");
+
+    g_string_append_printf (string, "%02x ", mem[i]);
+
+    j++;
+    i++;
+
+    if (j == 16 || i == size) {
+      g_print ("%08x (%p): %-48.48s %-16.16s\n", i - j, mem + i - j,
+          string->str, chars->str);
+      g_string_set_size (string, 0);
+      g_string_set_size (chars, 0);
+      j = 0;
+    }
+  }
+  g_string_free (string, TRUE);
+  g_string_free (chars, TRUE);
+}
+
+static void
+dump_key_value (gpointer data, gpointer user_data)
 {
   RTSPKeyValue *key_value = (RTSPKeyValue *) data;
 
@@ -779,7 +780,7 @@
       key_value_foreach (msg->hdr_fields, dump_key_value, NULL);
       g_print (" body:\n");
       gst_rtsp_message_get_body (msg, &data, &size);
-      gst_util_dump_mem (data, size);
+      dump_mem (data, size);
       break;
     case GST_RTSP_MESSAGE_RESPONSE:
       g_print ("RTSP response message %p\n", msg);
@@ -792,14 +793,14 @@
       key_value_foreach (msg->hdr_fields, dump_key_value, NULL);
       gst_rtsp_message_get_body (msg, &data, &size);
       g_print (" body: length %d\n", size);
-      gst_util_dump_mem (data, size);
+      dump_mem (data, size);
       break;
     case GST_RTSP_MESSAGE_DATA:
       g_print ("RTSP data message %p\n", msg);
       g_print (" channel: '%d'\n", msg->type_data.data.channel);
       g_print (" size:    '%d'\n", msg->body_size);
       gst_rtsp_message_get_body (msg, &data, &size);
-      gst_util_dump_mem (data, size);
+      dump_mem (data, size);
       break;
     default:
       g_print ("unsupported message type %d\n", msg->type);