gst-indent
[platform/upstream/gst-plugins-good.git] / sys / v4l2 / v4l2-overlay_calls.c
1 /* G-Streamer generic V4L2 element - generic V4L2 overlay handling
2  * Copyright (C) 2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <sys/ioctl.h>
28 #include <sys/mman.h>
29 #include <string.h>
30 #include <errno.h>
31 #include "v4l2_calls.h"
32
33 #define DEBUG(format, args...) \
34         GST_DEBUG_OBJECT (\
35                 GST_ELEMENT(v4l2element), \
36                 "V4L2-overlay: " format, ##args)
37
38
39 /******************************************************
40  * gst_v4l2_set_display():
41  *   calls v4l-conf
42  * return value: TRUE on success, FALSE on error
43  ******************************************************/
44
45 gboolean
46 gst_v4l2_set_display (GstV4l2Element * v4l2element)
47 {
48   gchar *buff;
49
50   if (v4l2element->display)
51     g_free (v4l2element->display);
52   v4l2element->display = g_strdup (g_getenv ("DISPLAY"));
53
54   DEBUG ("trying to set overlay to '%s'", v4l2element->display);
55
56   /* start v4l-conf */
57   buff = g_strdup_printf ("v4l-conf -q -c %s -d %s",
58       v4l2element->device, v4l2element->display);
59
60   switch (system (buff)) {
61     case -1:
62       GST_ELEMENT_ERROR (v4l2element, RESOURCE, FAILED,
63           (_("Could not start v4l-conf.")), GST_ERROR_SYSTEM);
64       g_free (buff);
65       return FALSE;
66     case 0:
67       break;
68     default:
69       GST_ELEMENT_ERROR (v4l2element, RESOURCE, FAILED,
70           (_("Executing v4l-conf failed.")), GST_ERROR_SYSTEM);
71       g_free (buff);
72       return FALSE;
73   }
74
75   g_free (buff);
76   return TRUE;
77 }
78
79
80 /******************************************************
81  * gst_v4l2_set_window():
82  *   sets the window where to display the video overlay
83  * return value: TRUE on success, FALSE on error
84  ******************************************************/
85
86 gboolean
87 gst_v4l2_set_window (GstElement * element,
88     gint x, gint y, gint w, gint h, struct v4l2_clip * clips, gint num_clips)
89 {
90   struct v4l2_format fmt;
91   GstV4l2Element *v4l2element = GST_V4L2ELEMENT (element);
92
93   DEBUG ("trying to set video window to %dx%d,%d,%d", x, y, w, h);
94   GST_V4L2_CHECK_OVERLAY (v4l2element);
95   GST_V4L2_CHECK_OPEN (v4l2element);
96
97   fmt.type = V4L2_CAP_VIDEO_OVERLAY;
98   fmt.fmt.win.clipcount = 0;
99   fmt.fmt.win.w.left = x;
100   fmt.fmt.win.w.top = y;
101   fmt.fmt.win.w.width = w;
102   fmt.fmt.win.w.height = h;
103   fmt.fmt.win.clips = clips;
104   fmt.fmt.win.clipcount = num_clips;
105   fmt.fmt.win.bitmap = NULL;
106
107   if (ioctl (v4l2element->video_fd, VIDIOC_S_FMT, &fmt) < 0) {
108     GST_ELEMENT_ERROR (v4l2element, RESOURCE, TOO_LAZY, (NULL),
109         ("Failed to set the video window: %s", g_strerror (errno)));
110     return FALSE;
111   }
112
113   return TRUE;
114 }
115
116
117 /******************************************************
118  * gst_v4l_set_overlay():
119  *   enables/disables actual video overlay display
120  * return value: TRUE on success, FALSE on error
121  ******************************************************/
122
123 gboolean
124 gst_v4l2_enable_overlay (GstV4l2Element * v4l2element, gboolean enable)
125 {
126   gint doit = enable ? 1 : 0;
127
128   DEBUG ("trying to %s overlay display", enable ? "enable" : "disable");
129   GST_V4L2_CHECK_OPEN (v4l2element);
130   GST_V4L2_CHECK_OVERLAY (v4l2element);
131
132   if (ioctl (v4l2element->video_fd, VIDIOC_OVERLAY, &doit) < 0) {
133     GST_ELEMENT_ERROR (v4l2element, RESOURCE, TOO_LAZY, (NULL),
134         ("Failed to %s overlay display: %s",
135             enable ? "enable" : "disable", g_strerror (errno)));
136     return FALSE;
137   }
138
139   return TRUE;
140 }