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