5221397497251dc91da4d03ab62cf7d09d873c8c
[platform/adaptation/emulator/gst-plugins-emulator.git] / src / gstmarudevice.c
1 /*
2  * GStreamer codec plugin for Tizen 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  * Contributors:
27  * - S-Core Co., Ltd
28  *
29  */
30
31 #include <fcntl.h>
32 #include <stdint.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <sys/ioctl.h>
38 #include <sys/mman.h>
39 #include <sys/stat.h>
40
41 #include "gstmaruinterface.h"
42 #include "gstmarudevice.h"
43
44 static GMutex gst_avcodec_mutex;
45
46 #define CODEC_DEVICE_MEM_SIZE 32 * 1024 * 1024
47
48 gpointer device_mem = MAP_FAILED;
49 int device_fd = -1;
50 int opened_cnt = 0;
51
52 int
53 gst_maru_codec_device_open (CodecDevice *dev, int media_type)
54 {
55   CODEC_LOG (DEBUG, "enter: %s\n", __func__);
56
57   g_mutex_lock (&gst_avcodec_mutex);
58   if (device_fd == -1) {
59     if ((device_fd = open(CODEC_DEV, O_RDWR)) < 0) {
60       GST_ERROR ("failed to open codec device.");
61       g_mutex_unlock (&gst_avcodec_mutex);
62       return -1;
63     }
64     GST_INFO ("succeeded to open %s. %d", CODEC_DEV, device_fd);
65   } else {
66     GST_DEBUG ("codec device is already opened");
67   }
68   dev->fd = device_fd;
69   // g_mutex_unlock (&gst_avcodec_mutex);
70
71   // FIXME
72   dev->buf_size = CODEC_DEVICE_MEM_SIZE;
73   GST_DEBUG ("mmap_size: %d", dev->buf_size);
74
75   // g_mutex_lock (&gst_avcodec_mutex);
76   if (device_mem == MAP_FAILED) {
77     device_mem =
78       mmap (NULL, CODEC_DEVICE_MEM_SIZE, PROT_READ | PROT_WRITE,
79           MAP_SHARED, device_fd, 0);
80     if (device_mem == MAP_FAILED) {
81       GST_ERROR ("failed to map device memory of codec");
82       close (device_fd);
83       dev->fd = device_fd = -1;
84       g_mutex_unlock (&gst_avcodec_mutex);
85       return -1;
86     }
87     GST_INFO ("succeeded to map device memory: %p", device_mem);
88   } else {
89     GST_DEBUG ("mapping device memory is already done");
90   }
91   dev->buf = device_mem;
92
93   opened_cnt++;
94   GST_DEBUG ("open count: %d", opened_cnt);
95   g_mutex_unlock (&gst_avcodec_mutex);
96
97   CODEC_LOG (DEBUG, "leave: %s\n", __func__);
98
99   return 0;
100 }
101
102 int
103 gst_maru_codec_device_close (CodecDevice *dev)
104 {
105   int fd = 0;
106
107   CODEC_LOG (DEBUG, "enter: %s\n", __func__);
108
109   fd = dev->fd;
110   if (fd < 0) {
111     GST_ERROR ("Failed to get %s fd.\n", CODEC_DEV);
112     return -1;
113   }
114
115   g_mutex_lock (&gst_avcodec_mutex);
116   if (opened_cnt > 0) {
117     opened_cnt--;
118   }
119   GST_DEBUG ("open count: %d", opened_cnt);
120
121   if (opened_cnt == 0) {
122     GST_INFO ("release device memory %p", device_mem);
123     if (munmap(device_mem, CODEC_DEVICE_MEM_SIZE) != 0) {
124       GST_ERROR ("failed to release device memory of %s", CODEC_DEV);
125     }
126     device_mem = MAP_FAILED;
127
128     GST_INFO ("close %s", CODEC_DEV);
129     if (close(fd) != 0) {
130       GST_ERROR ("failed to close %s fd: %d\n", CODEC_DEV, fd);
131     }
132     dev->fd = device_fd = -1;
133   }
134   dev->buf = MAP_FAILED;
135   g_mutex_unlock (&gst_avcodec_mutex);
136
137   CODEC_LOG (DEBUG, "leave: %s\n", __func__);
138
139   return 0;
140 }
141
142 int
143 gst_maru_avcodec_open (CodecContext *ctx,
144                       CodecElement *codec,
145                       CodecDevice *dev)
146 {
147   int ret;
148
149   if (gst_maru_codec_device_open (dev, codec->media_type) < 0) {
150     GST_ERROR ("failed to open device");
151     return -1;
152   }
153
154   g_mutex_lock (&gst_avcodec_mutex);
155   ret = codec_init (ctx, codec, dev);
156   g_mutex_unlock (&gst_avcodec_mutex);
157
158   return ret;
159 }
160
161 int
162 gst_maru_avcodec_close (CodecContext *ctx, CodecDevice *dev)
163 {
164   int ret;
165
166   GST_DEBUG ("close %d of context", ctx->index);
167
168   if (ctx && ctx->index == 0) {
169     GST_INFO ("context is not opened yet or context before %d", ctx->index);
170     return -1;
171   }
172
173   if (dev && dev->fd < 0) {
174     GST_INFO ("fd is not opened yet or closed before %d", dev->fd);
175     return -1;
176   }
177
178   g_mutex_lock (&gst_avcodec_mutex);
179   codec_deinit (ctx, dev);
180   g_mutex_unlock (&gst_avcodec_mutex);
181
182   ret = gst_maru_codec_device_close (dev);
183
184   return ret;
185 }