Removed compile warnings.
[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 = NULL;
49 int device_fd = 0;
50 int opened_cnt = 0;
51
52 int
53 gst_maru_codec_device_open (CodecDevice *dev, int media_type)
54 {
55   int fd;
56
57   CODEC_LOG (DEBUG, "enter: %s\n", __func__);
58
59   CODEC_LOG (INFO, "before opening a device. %d\n", dev->fd);
60   if ((fd = open(CODEC_DEV, O_RDWR)) < 0) {
61     perror("Failed to open codec device.");
62     return -1;
63   }
64   dev->fd = fd;
65
66   // FIXME
67   dev->buf_size = CODEC_DEVICE_MEM_SIZE;
68   //
69
70   CODEC_LOG (INFO, "succeeded to open %s. %d.\n", CODEC_DEV, fd);
71   dev->mem_info.index = dev->buf_size;
72
73   CODEC_LOG (DEBUG, "before mmap. buf_size: %d\n", dev->buf_size);
74
75   g_mutex_lock (&gst_avcodec_mutex);
76   if (!device_mem) {
77     device_mem = mmap (NULL, CODEC_DEVICE_MEM_SIZE, PROT_READ | PROT_WRITE,
78         MAP_SHARED, fd, 0);
79     if (device_mem == MAP_FAILED) {
80       perror("Failed to map device memory of codec.");
81       dev->buf = NULL;
82       g_mutex_unlock (&gst_avcodec_mutex);
83       return -1;
84     }
85   }
86   opened_cnt++;
87   g_mutex_unlock (&gst_avcodec_mutex);
88
89   dev->buf = device_mem;
90
91   CODEC_LOG (INFO, "succeeded to map device memory: %p.\n", dev->buf);
92   dev->fd = fd;
93   device_fd = fd;
94
95   CODEC_LOG (DEBUG, "leave: %s\n", __func__);
96
97   return 0;
98 }
99
100 int
101 gst_maru_codec_device_close (CodecDevice *dev)
102 {
103   int fd = 0;
104
105   CODEC_LOG (DEBUG, "enter: %s\n", __func__);
106
107   fd = dev->fd;
108   if (fd < 0) {
109     GST_ERROR("Failed to get %s fd.\n", CODEC_DEV);
110     return -1;
111   }
112
113   ioctl(fd, CODEC_CMD_RELEASE_BUFFER, &dev->mem_info.offset);
114
115   g_mutex_lock (&gst_avcodec_mutex);
116   opened_cnt--;
117   if (!opened_cnt) {
118     CODEC_LOG (INFO, "Release memory region of %p.\n", device_mem);
119     if (munmap(device_mem, CODEC_DEVICE_MEM_SIZE) != 0) {
120       CODEC_LOG(ERR, "Failed to release memory region of %s.\n", CODEC_DEV);
121       device_mem = NULL;
122     }
123   }
124   device_mem = NULL;
125   g_mutex_unlock (&gst_avcodec_mutex);
126
127   dev->buf = NULL;
128
129   CODEC_LOG (INFO, "close %s.\n", CODEC_DEV);
130   if (close(fd) != 0) {
131     GST_ERROR("Failed to close %s. fd: %d\n", CODEC_DEV, fd);
132   }
133
134   CODEC_LOG (DEBUG, "leave: %s\n", __func__);
135
136   return 0;
137 }
138
139 int
140 gst_maru_avcodec_open (CodecContext *ctx,
141                       CodecElement *codec,
142                       CodecDevice *dev)
143 {
144   int ret;
145
146
147   if (gst_maru_codec_device_open (dev, codec->media_type) < 0) {
148     perror("failed to open device.\n");
149     return -1;
150   }
151
152   g_mutex_lock (&gst_avcodec_mutex);
153   ret = codec_init (ctx, codec, dev);
154   g_mutex_unlock (&gst_avcodec_mutex);
155
156   return ret;
157 }
158
159 int
160 gst_maru_avcodec_close (CodecContext *ctx, CodecDevice *dev)
161 {
162   int ret;
163
164   CODEC_LOG (DEBUG, "gst_maru_avcodec_close\n");
165
166   g_mutex_lock (&gst_avcodec_mutex);
167   codec_deinit (ctx, dev);
168   g_mutex_unlock (&gst_avcodec_mutex);
169
170   ret = gst_maru_codec_device_close (dev);
171
172   return ret;
173 }