• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer Raspberry Pi Camera Source Device Provider
2  * Copyright (C) 2014 Tim-Philipp Müller <tim@centricular.com>
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 "gstrpicamsrcdeviceprovider.h"
25 
26 #include <string.h>
27 
28 #include "RaspiCapture.h"
29 
30 #include <gst/gst-i18n-plugin.h>
31 
32 static GstRpiCamSrcDevice *gst_rpi_cam_src_device_new (void);
33 
34 G_DEFINE_TYPE (GstRpiCamSrcDeviceProvider, gst_rpi_cam_src_device_provider,
35     GST_TYPE_DEVICE_PROVIDER);
36 
37 
38 static GList *gst_rpi_cam_src_device_provider_probe (GstDeviceProvider *
39     provider);
40 
41 static void
gst_rpi_cam_src_device_provider_class_init(GstRpiCamSrcDeviceProviderClass * klass)42 gst_rpi_cam_src_device_provider_class_init (GstRpiCamSrcDeviceProviderClass *
43     klass)
44 {
45   GstDeviceProviderClass *dprovider_class = GST_DEVICE_PROVIDER_CLASS (klass);
46 
47   dprovider_class->probe = gst_rpi_cam_src_device_provider_probe;
48 
49   gst_device_provider_class_set_static_metadata (dprovider_class,
50       "Raspberry Pi Camera Source Device Provider", "Source/Video",
51       "Lists Raspberry Pi camera devices",
52       "Tim-Philipp Müller <tim@centricular.com>");
53 }
54 
55 static void
gst_rpi_cam_src_device_provider_init(GstRpiCamSrcDeviceProvider * provider)56 gst_rpi_cam_src_device_provider_init (GstRpiCamSrcDeviceProvider * provider)
57 {
58   raspicapture_init ();
59 }
60 
61 static GList *
gst_rpi_cam_src_device_provider_probe(GstDeviceProvider * provider)62 gst_rpi_cam_src_device_provider_probe (GstDeviceProvider * provider)
63 {
64   GstRpiCamSrcDevice *device;
65   int supported = 0, detected = 0;
66 
67   raspicamcontrol_get_camera (&supported, &detected);
68 
69   if (!detected) {
70     GST_INFO ("No Raspberry Pi camera module detected.");
71     return NULL;
72   } else if (!supported) {
73     GST_WARNING
74         ("Raspberry Pi camera module not supported, make sure to enable it.");
75     return NULL;
76   }
77 
78   GST_INFO ("Raspberry Pi camera module detected and supported.");
79 
80   device = gst_rpi_cam_src_device_new ();
81 
82   return g_list_append (NULL, device);
83 }
84 
85 G_DEFINE_TYPE (GstRpiCamSrcDevice, gst_rpi_cam_src_device, GST_TYPE_DEVICE);
86 
87 static GstElement *gst_rpi_cam_src_device_create_element (GstDevice * device,
88     const gchar * name);
89 
90 static void
gst_rpi_cam_src_device_class_init(GstRpiCamSrcDeviceClass * klass)91 gst_rpi_cam_src_device_class_init (GstRpiCamSrcDeviceClass * klass)
92 {
93   GstDeviceClass *device_class = GST_DEVICE_CLASS (klass);
94 
95   device_class->create_element = gst_rpi_cam_src_device_create_element;
96 }
97 
98 static void
gst_rpi_cam_src_device_init(GstRpiCamSrcDevice * device)99 gst_rpi_cam_src_device_init (GstRpiCamSrcDevice * device)
100 {
101   /* nothing to do here */
102 }
103 
104 static GstElement *
gst_rpi_cam_src_device_create_element(GstDevice * device,const gchar * name)105 gst_rpi_cam_src_device_create_element (GstDevice * device, const gchar * name)
106 {
107   return gst_element_factory_make ("rpicamsrc", name);
108 }
109 
110 static GstRpiCamSrcDevice *
gst_rpi_cam_src_device_new(void)111 gst_rpi_cam_src_device_new (void)
112 {
113   GstRpiCamSrcDevice *device;
114   GValue profiles = G_VALUE_INIT;
115   GValue val = G_VALUE_INIT;
116   GstStructure *s, *jpeg_s;
117   GstCaps *caps;
118 
119   /* FIXME: retrieve limits from the camera module, max width/height/fps etc. */
120   s = gst_structure_new ("video/x-h264",
121       "width", GST_TYPE_INT_RANGE, 1, 1920,
122       "height", GST_TYPE_INT_RANGE, 1, 1080,
123       "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, RPICAMSRC_MAX_FPS, 1,
124       "stream-format", G_TYPE_STRING, "byte-stream",
125       "alignment", G_TYPE_STRING, "au", NULL);
126 
127   g_value_init (&profiles, GST_TYPE_LIST);
128   g_value_init (&val, G_TYPE_STRING);
129   g_value_set_static_string (&val, "high");
130   gst_value_list_append_value (&profiles, &val);
131   g_value_set_static_string (&val, "main");
132   gst_value_list_append_value (&profiles, &val);
133   g_value_set_static_string (&val, "constrained-baseline");
134   gst_value_list_append_value (&profiles, &val);
135   g_value_set_static_string (&val, "baseline");
136   gst_value_list_append_and_take_value (&profiles, &val);
137   gst_structure_take_value (s, "profiles", &profiles);
138 
139   jpeg_s = gst_structure_new ("image/jpeg",
140       "width", GST_TYPE_INT_RANGE, 1, 1920,
141       "height", GST_TYPE_INT_RANGE, 1, 1080,
142       "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, RPICAMSRC_MAX_FPS, 1,
143       "parsed", G_TYPE_BOOLEAN, "true", NULL);
144 
145   caps = gst_caps_new_full (s, jpeg_s, NULL);
146 
147   device = g_object_new (GST_TYPE_RPICAMSRC_DEVICE,
148       "display-name", _("Raspberry Pi Camera Module"),
149       "device-class", "Video/Source", "caps", caps, NULL);
150 
151   gst_caps_unref (caps);
152 
153   return device;
154 }
155