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