b858dbdd436f8a3a763dc0e0b5758f9f115b6efa
[platform/adaptation/emulator/gst-plugins-emulator.git] / src / gstmaru.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) 2013 Samsung Electronics Co., Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /* First, include the header file for the plugin, to bring in the
22  * object definition and other useful things.
23  */
24
25 /* Modifications by Samsung Electronics Co., Ltd.
26  * 1. Get available Video/Audio Codecs from Qemu
27  */
28
29 #include "gstmaru.h"
30 #include "gstmaruutils.h"
31 #include "gstmaruinterface.h"
32
33 GST_DEBUG_CATEGORY (maru_debug);
34
35 #define GST_TYPE_MARUDEC \
36   (gst_maru_dec_get_type())
37 #define GST_MARUDEC(obj) \
38   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_MARUDEC,GstMaruDec))
39 #define GST_MARUDEC_CLASS(klass) \
40   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_MARUDEC,GstMaruDecClass))
41 #define GST_IS_MARUDEC(obj) \
42   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_MARUDEC))
43 #define GST_IS_MARUDEC_CLASS(klass) \
44   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_MARUDEC))
45
46 gboolean gst_marudec_register (GstPlugin *plugin, GList *element);
47 gboolean gst_maruenc_register (GstPlugin *plugin, GList *element);
48
49 static GList *elements = NULL;
50 static gboolean codec_element_init = FALSE;
51 static GMutex gst_maru_mutex;
52
53 int device_version;
54
55 static gboolean
56 gst_maru_codec_element_init ()
57 {
58   int fd = 0, ret = TRUE;
59   void *buffer = MAP_FAILED;
60
61   CODEC_LOG (DEBUG, "enter: %s\n", __func__);
62
63   codec_element_init = TRUE;
64
65   fd = open (CODEC_DEV, O_RDWR);
66   if (fd < 0) {
67     perror ("[gst-maru] failed to open codec device");
68     GST_ERROR ("failed to open codec device");
69     ret = FALSE;
70     goto out;
71   }
72
73   // get device version
74   // if version 3
75   device_version = interface_version_3->get_device_version(fd);
76   if (device_version == -EINVAL) {
77     // if version 2
78     device_version = interface_version_2->get_device_version(fd);
79   }
80   if (device_version < 0) {
81     perror ("[gst-maru] Incompatible device version");
82     GST_ERROR ("Incompatible device version");
83     ret = FALSE;
84     goto out;
85   }
86
87   // interface mapping
88   if (device_version < 3) {
89     interface = interface_version_2;
90   } else if (device_version >= 3 && device_version < 4) {
91     interface = interface_version_3;
92   } else {
93     perror ("[gst-maru] Incompatible device version");
94     GST_ERROR ("Incompatible device version");
95     ret = FALSE;
96     goto out;
97   }
98
99   // prepare elements
100   if ((elements = interface->prepare_elements(fd)) == NULL) {
101     perror ("[gst-maru] cannot prepare elements");
102     GST_ERROR ("cannot prepare elements");
103     ret = FALSE;
104     goto out;
105   }
106
107   // try to mmap device memory
108   buffer = mmap (NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
109   if (buffer == MAP_FAILED) {
110     perror ("[gst-maru] memory mapping failure");
111     GST_ERROR ("memory mapping failure");
112     ret = FALSE;
113   }
114
115 out:
116   if (buffer != MAP_FAILED) {
117     munmap (buffer, 4096);
118   }
119   if (fd >= 0) {
120     close (fd);
121   }
122
123   CODEC_LOG (DEBUG, "leave: %s\n", __func__);
124
125   return ret;
126 }
127
128 static gboolean
129 plugin_init (GstPlugin *plugin)
130 {
131   GST_DEBUG_CATEGORY_INIT (maru_debug,
132       "tizen-emul", 0, "Tizen Emulator Codec Elements");
133
134   gst_maru_init_pix_fmt_info ();
135
136   g_mutex_lock (&gst_maru_mutex);
137   if (!codec_element_init) {
138     if (!gst_maru_codec_element_init ()) {
139       g_mutex_unlock (&gst_maru_mutex);
140
141       GST_ERROR ("failed to get codec elements from QEMU");
142       return FALSE;
143     }
144   }
145   g_mutex_unlock (&gst_maru_mutex);
146
147   if (!gst_marudec_register (plugin, elements)) {
148     GST_ERROR ("failed to register decoder elements");
149     return FALSE;
150   }
151   if (!gst_maruenc_register (plugin, elements)) {
152     GST_ERROR ("failed to register encoder elements");
153     return FALSE;
154   }
155
156   return TRUE;
157 }
158
159 #ifndef PACKAGE
160 #define PACKAGE "gst-plugins-emulator"
161 #endif
162
163 GST_PLUGIN_DEFINE (
164   GST_VERSION_MAJOR,
165   GST_VERSION_MINOR,
166   "tizen-emul",
167   "Codecs for Tizen Emulator",
168   plugin_init,
169   "0.3.0",
170   "LGPL",
171   "gst-plugins-emulator",
172   "http://www.tizen.org"
173 )