manual: Fix examples to check for gst_buffer_map return values
authorEdward Hervey <edward@collabora.com>
Wed, 16 Apr 2014 09:42:18 +0000 (11:42 +0200)
committerEdward Hervey <bilboed@bilboed.com>
Tue, 23 Feb 2016 16:30:48 +0000 (17:30 +0100)
Otherwise people reading the manual will expect it to always
succeed :)

https://bugzilla.gnome.org/show_bug.cgi?id=728326

docs/manual/advanced-dataaccess.xml

index 69f7b96..4853f08 100644 (file)
@@ -164,20 +164,27 @@ cb_have_data (GstPad          *pad,
   buffer = GST_PAD_PROBE_INFO_BUFFER (info);
 
   buffer = gst_buffer_make_writable (buffer);
-  
-  gst_buffer_map (buffer, &map, GST_MAP_WRITE);
-
-  ptr = (guint16 *) map.data;
-  /* invert data */
-  for (y = 0; y < 288; y++) {
-    for (x = 0; x < 384 / 2; x++) {
-      t = ptr[384 - 1 - x];
-      ptr[384 - 1 - x] = ptr[x];
-      ptr[x] = t;
+
+  /* Making a buffer writable can fail (for example if it
+   * cannot be copied and is used more than once)
+   */
+  if (buffer == NULL)
+    return GST_PAD_PROBE_OK;
+
+  /* Mapping a buffer can fail (non-writable) */
+  if (gst_buffer_map (buffer, &map, GST_MAP_WRITE)) {
+    ptr = (guint16 *) map.data;
+    /* invert data */
+    for (y = 0; y < 288; y++) {
+      for (x = 0; x < 384 / 2; x++) {
+        t = ptr[384 - 1 - x];
+        ptr[384 - 1 - x] = ptr[x];
+        ptr[x] = t;
+      }
+      ptr += 384;
     }
-    ptr += 384;
+    gst_buffer_unmap (buffer, &map);
   }
-  gst_buffer_unmap (buffer, &map);
 
   GST_PAD_PROBE_INFO_DATA (info) = buffer;
 
@@ -995,16 +1002,18 @@ main (int argc, char *argv[])
     /* create pixmap from buffer and save, gstreamer video buffers have a stride
      * that is rounded up to the nearest multiple of 4 */
     buffer = gst_sample_get_buffer (sample);
-    gst_buffer_map (buffer, &map, GST_MAP_READ);
+    /* Mapping a buffer can fail (non-readable) */
+    if (gst_buffer_map (buffer, &map, GST_MAP_READ)) {
 #ifdef HAVE_GTK
-    pixbuf = gdk_pixbuf_new_from_data (map.data,
-        GDK_COLORSPACE_RGB, FALSE, 8, width, height,
-        GST_ROUND_UP_4 (width * 3), NULL, NULL);
+      pixbuf = gdk_pixbuf_new_from_data (map.data,
+          GDK_COLORSPACE_RGB, FALSE, 8, width, height,
+          GST_ROUND_UP_4 (width * 3), NULL, NULL);
 
-    /* save the pixbuf */
-    gdk_pixbuf_save (pixbuf, "snapshot.png", "png", &error, NULL);
+      /* save the pixbuf */
+      gdk_pixbuf_save (pixbuf, "snapshot.png", "png", &error, NULL);
 #endif
-    gst_buffer_unmap (buffer, &map);
+      gst_buffer_unmap (buffer, &map);
+    }
     gst_sample_unref (sample);
   } else {
     g_print ("could not make snapshot\n");