Userful gstreamer-vaapi
[vaapi:cpu-gstreamer-vaapi.git] / gst / vaapi / gstvaapi.c
1 /*
2  *  gstvaapi.c - VA-API element registration
3  *
4  *  Copyright (C) 2011-2013 Intel Corporation
5  *    Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
6  *  Copyright (C) 2011 Collabora Ltd.
7  *    Author: Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
8  *
9  *  This library is free software; you can redistribute it and/or
10  *  modify it under the terms of the GNU Lesser General Public License
11  *  as published by the Free Software Foundation; either version 2.1
12  *  of the License, or (at your option) any later version.
13  *
14  *  This library is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  Lesser General Public License for more details.
18  *
19  *  You should have received a copy of the GNU Lesser General Public
20  *  License along with this library; if not, write to the Free
21  *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22  *  Boston, MA 02110-1301 USA
23  */
24
25 #include <gst/gst.h>
26
27 #include "gst/vaapi/gstvaapidisplay_x11.h"
28 #include "gst/vaapi/sysdeps.h"
29 #include "gstvaapidecode.h"
30 #include "gstvaapidownload.h"
31 #include "gstvaapipostproc.h"
32 #include "gstvaapisink.h"
33 #include "gstvaapiupload.h"
34
35 #if USE_ENCODERS
36 #include "gstvaapiencode_h264.h"
37 #include "gstvaapiencode_mpeg2.h"
38 #endif
39
40 #define PLUGIN_NAME     "vaapi"
41 #define PLUGIN_DESC     "VA-API based elements"
42 #define PLUGIN_LICENSE  "LGPL"
43
44 typedef struct
45 {
46   gchar *VaapiDisplay;
47 } Settings;
48
49 static gboolean readkeyfile(gchar * FilePath, Settings *conf)
50 {
51         GKeyFile *keyfile;
52         GKeyFileFlags flags;
53         GError *error = NULL;
54         /* Create a new GKeyFile object and a bitwise list of flags. */
55         keyfile = g_key_file_new ();
56         flags = G_KEY_FILE_NONE;
57         if (!g_key_file_load_from_file (keyfile, FilePath, flags, &error))
58                 return FALSE;
59         conf->VaapiDisplay=g_key_file_get_string(keyfile, "VAAPI", "VaapiDisplay", NULL);
60         if(conf->VaapiDisplay==NULL)
61                 return FALSE;
62         return TRUE;
63 }
64
65 static gboolean writevaapconf(gchar * FilePath, Settings *conf)
66 {
67         GKeyFile *keyfile;
68         GKeyFileFlags flags;
69         gchar *result;
70         gsize resultsize;
71         keyfile = g_key_file_new ();
72         flags = G_KEY_FILE_NONE;
73         g_key_file_set_string(keyfile,"VAAPI","VaapiDisplay",conf->VaapiDisplay);
74         result = g_key_file_to_data(keyfile,&resultsize,NULL);
75         return(g_file_set_contents(FilePath,result,resultsize,NULL));
76 }
77
78 static gboolean QueryGPU(Settings *conf, GstVaapiDisplay *display)
79 {
80         g_print("Query to find vaapi compatible gpu \n");
81     GDir* d = g_dir_open("/tmp/.X11-unix",0,NULL);
82     if (d != NULL) {
83         gchar const * dr;
84         display = gst_vaapi_display_x11_new(":80");
85         if (display!=NULL)
86         {
87                         g_print("Display :80 have compatible gpu \n");
88                         conf->VaapiDisplay=":80";
89                         return TRUE;
90                 }
91         while ((dr = g_dir_read_name(d)) != NULL) {             
92             if (dr[0] != 'X')
93                 continue;
94             gchar *display_name = g_strjoin("",":",dr + 1,NULL);
95             g_print("testing display %s \n",display_name);
96             display = gst_vaapi_display_x11_new(display_name);
97             if (display!=NULL)
98                         {
99                                 g_print("display %s have compatible gpu\n",display_name);
100                                 conf->VaapiDisplay=display_name;
101                                 g_dir_close(d);
102                                 return TRUE;
103                         }
104         }
105         g_dir_close(d);
106     }
107         return FALSE;
108 }
109
110 static gboolean plugin_init (GstPlugin *plugin)
111 {
112         GstVaapiDisplay *display=NULL;
113         gchar * FilePath = g_strjoin("",g_get_user_config_dir (),"/vaapi.conf",NULL);
114         Settings *conf =g_new(Settings,1);
115         if (display==NULL)
116                 if ( readkeyfile(FilePath,conf) )
117                         display = gst_vaapi_display_x11_new(conf->VaapiDisplay);
118                 
119         if (display==NULL)
120         {
121                 if(QueryGPU(conf,display))
122                 {
123                         writevaapconf(FilePath,conf);
124                 }
125                 else
126                 {
127                         g_print("Hardware decoding is not available switching to software decoding \n");                
128                 }
129         }
130         
131 #if !GST_CHECK_VERSION(1,0,0)
132     gst_element_register(plugin, "vaapiupload",
133                          GST_RANK_PRIMARY,
134                          GST_TYPE_VAAPIUPLOAD);
135 #endif
136     gst_element_register(plugin, "vaapidownload",
137                          GST_RANK_SECONDARY,
138                          GST_TYPE_VAAPIDOWNLOAD);
139     gst_element_register(plugin, "vaapidecode",
140                          GST_RANK_PRIMARY+64,
141                          GST_TYPE_VAAPIDECODE);
142     gst_element_register(plugin, "vaapipostproc",
143                          GST_RANK_PRIMARY,
144                          GST_TYPE_VAAPIPOSTPROC);
145     gst_element_register(plugin, "vaapisink",
146                          GST_RANK_PRIMARY,
147                          GST_TYPE_VAAPISINK);
148 #if USE_ENCODERS
149     gst_element_register(plugin, "vaapiencode_h264",
150                          GST_RANK_PRIMARY,
151                          GST_TYPE_VAAPIENCODE_H264);
152     gst_element_register(plugin, "vaapiencode_mpeg2",
153                          GST_RANK_PRIMARY,
154                          GST_TYPE_VAAPIENCODE_MPEG2);
155 #endif
156     return TRUE;
157 }
158
159 #if GST_CHECK_VERSION(1,0,0)
160 /* XXX: use PLUGIN_NAME when GST_PLUGIN_DEFINE is fixed to use
161    G_STRINGIFY() for name argument, instead of plain #name */
162 GST_PLUGIN_DEFINE(GST_VERSION_MAJOR, GST_VERSION_MINOR,
163                   vaapi, PLUGIN_DESC, plugin_init,
164                   PACKAGE_VERSION, PLUGIN_LICENSE, PACKAGE, PACKAGE_BUGREPORT)
165 #else
166 GST_PLUGIN_DEFINE(GST_VERSION_MAJOR, GST_VERSION_MINOR,
167                   PLUGIN_NAME, PLUGIN_DESC, plugin_init,
168                   PACKAGE_VERSION, PLUGIN_LICENSE, PACKAGE, PACKAGE_BUGREPORT)
169 #endif