4d948d0bd7654b2ed54077599f4a6741386197af
[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.")),
64                                            GST_ERROR_SYSTEM);
65                         g_free(buff);
66                         return FALSE;
67                 case 0:
68                         break;
69                 default:
70                         GST_ELEMENT_ERROR (v4l2element, RESOURCE, FAILED,
71                                            (_("Executing v4l-conf failed.")),
72                                            GST_ERROR_SYSTEM);
73                         g_free(buff);
74                         return FALSE;
75         }
76
77         g_free(buff);
78         return TRUE;
79 }
80
81
82 /******************************************************
83  * gst_v4l2_set_window():
84  *   sets the window where to display the video overlay
85  * return value: TRUE on success, FALSE on error
86  ******************************************************/
87
88 gboolean
89 gst_v4l2_set_window (GstElement       *element,
90                      gint              x,
91                      gint              y,
92                      gint              w,
93                      gint              h,
94                      struct v4l2_clip *clips,
95                      gint              num_clips)
96 {
97         struct v4l2_format fmt;
98         GstV4l2Element *v4l2element = GST_V4L2ELEMENT(element);
99
100         DEBUG("trying to set video window to %dx%d,%d,%d", x,y,w,h);
101         GST_V4L2_CHECK_OVERLAY(v4l2element);
102         GST_V4L2_CHECK_OPEN(v4l2element);
103
104         fmt.type = V4L2_CAP_VIDEO_OVERLAY;
105         fmt.fmt.win.clipcount = 0;
106         fmt.fmt.win.w.left = x;
107         fmt.fmt.win.w.top = y;
108         fmt.fmt.win.w.width = w;
109         fmt.fmt.win.w.height = h;
110         fmt.fmt.win.clips = clips;
111         fmt.fmt.win.clipcount = num_clips;
112         fmt.fmt.win.bitmap = NULL;
113
114         if (ioctl(v4l2element->video_fd, VIDIOC_S_FMT, &fmt) < 0) {
115                 GST_ELEMENT_ERROR (v4l2element, RESOURCE, TOO_LAZY, (NULL),
116                         ("Failed to set the video window: %s", g_strerror (errno)));
117                 return FALSE;
118         }
119
120         return TRUE;
121 }
122
123
124 /******************************************************
125  * gst_v4l_set_overlay():
126  *   enables/disables actual video overlay display
127  * return value: TRUE on success, FALSE on error
128  ******************************************************/
129
130 gboolean
131 gst_v4l2_enable_overlay (GstV4l2Element *v4l2element,
132                          gboolean        enable)
133 {
134         gint doit = enable?1:0;
135
136         DEBUG("trying to %s overlay display", enable?"enable":"disable");
137         GST_V4L2_CHECK_OPEN(v4l2element);
138         GST_V4L2_CHECK_OVERLAY(v4l2element);
139
140         if (ioctl(v4l2element->video_fd, VIDIOC_OVERLAY, &doit) < 0) {
141                 GST_ELEMENT_ERROR (v4l2element, RESOURCE, TOO_LAZY, (NULL),
142                         ("Failed to %s overlay display: %s",
143                          enable?"enable":"disable", g_strerror (errno)));
144                 return FALSE;
145         }
146
147         return TRUE;
148 }