Fix close routine at decoder plugin.
[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   g_mutex_lock (&gst_avcodec_mutex);
56   if (device_fd == -1) {
57     if ((device_fd = open(CODEC_DEV, O_RDWR)) < 0) {
58       GST_ERROR ("failed to open codec device.");
59       g_mutex_unlock (&gst_avcodec_mutex);
60       return -1;
61     }
62     GST_INFO ("succeeded to open %s. %d", CODEC_DEV, device_fd);
63   } else {
64     GST_DEBUG ("codec device is already opened");
65   }
66   dev->fd = device_fd;
67   // g_mutex_unlock (&gst_avcodec_mutex);
68
69   // FIXME
70   dev->buf_size = CODEC_DEVICE_MEM_SIZE;
71   GST_DEBUG ("mmap_size: %d", dev->buf_size);
72
73   // g_mutex_lock (&gst_avcodec_mutex);
74   if (device_mem == MAP_FAILED) {
75     device_mem =
76       mmap (NULL, CODEC_DEVICE_MEM_SIZE, PROT_READ | PROT_WRITE,
77           MAP_SHARED, device_fd, 0);
78     if (device_mem == MAP_FAILED) {
79       GST_ERROR ("failed to map device memory of codec");
80       close (device_fd);
81       dev->fd = device_fd = -1;
82       g_mutex_unlock (&gst_avcodec_mutex);
83       return -1;
84     }
85     GST_INFO ("succeeded to map device memory: %p", device_mem);
86   } else {
87     GST_DEBUG ("mapping device memory is already done");
88   }
89   dev->buf = device_mem;
90
91   opened_cnt++;
92   GST_DEBUG ("open count: %d", opened_cnt);
93   g_mutex_unlock (&gst_avcodec_mutex);
94
95   return 0;
96 }
97
98 int
99 gst_maru_codec_device_close (CodecDevice *dev)
100 {
101   int fd = 0;
102
103   fd = dev->fd;
104   if (fd < 0) {
105     GST_ERROR ("Failed to get %s fd %d", CODEC_DEV, fd);
106     return -1;
107   }
108
109   g_mutex_lock (&gst_avcodec_mutex);
110   if (opened_cnt > 0) {
111     opened_cnt--;
112   }
113   GST_DEBUG ("open count: %d", opened_cnt);
114
115   if (opened_cnt == 0) {
116     GST_INFO ("release device memory %p", device_mem);
117     if (munmap(device_mem, CODEC_DEVICE_MEM_SIZE) != 0) {
118       GST_ERROR ("failed to release device memory of %s", CODEC_DEV);
119     }
120     device_mem = MAP_FAILED;
121
122     GST_INFO ("close %s", CODEC_DEV);
123     if (close(fd) != 0) {
124       GST_ERROR ("failed to close %s fd: %d", CODEC_DEV, fd);
125     }
126     dev->fd = device_fd = -1;
127   }
128   dev->buf = MAP_FAILED;
129   g_mutex_unlock (&gst_avcodec_mutex);
130
131   return 0;
132 }
133
134 int
135 gst_maru_avcodec_open (CodecContext *ctx,
136                       CodecElement *codec,
137                       CodecDevice *dev)
138 {
139   int ret;
140
141   if (gst_maru_codec_device_open (dev, codec->media_type) < 0) {
142     GST_ERROR ("failed to open device");
143     return -1;
144   }
145
146   g_mutex_lock (&gst_avcodec_mutex);
147   ret = interface->init (ctx, codec, dev);
148   g_mutex_unlock (&gst_avcodec_mutex);
149
150   return ret;
151 }
152
153 int
154 gst_maru_avcodec_close (CodecContext *ctx, CodecDevice *dev)
155 {
156   int ret;
157
158   GST_DEBUG ("close %d of context", ctx->index);
159
160   if (ctx && ctx->index == 0) {
161     GST_INFO ("context is not opened yet or context before %d", ctx->index);
162     return -1;
163   }
164
165   if (dev && dev->fd < 0) {
166     GST_INFO ("fd is not opened yet or closed before %d", dev->fd);
167     return -1;
168   }
169
170   g_mutex_lock (&gst_avcodec_mutex);
171   interface->deinit (ctx, dev);
172   g_mutex_unlock (&gst_avcodec_mutex);
173
174   ret = gst_maru_codec_device_close (dev);
175
176   return ret;
177 }