2 * GStreamer codec plugin for Tizen Emulator.
4 * Copyright (C) 2013 Samsung Electronics Co., Ltd. All rights reserved.
7 * KiTae Kim <kt920.kim@samsung.com>
8 * SeokYeon Hwang <syeon.hwang@samsung.com>
9 * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
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.
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.
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.
37 #include <sys/ioctl.h>
41 #include "gstmaruinterface.h"
42 #include "gstmarudevice.h"
44 static GMutex gst_avcodec_mutex;
46 #define CODEC_DEVICE_MEM_SIZE 32 * 1024 * 1024
48 gpointer device_mem = MAP_FAILED;
53 gst_maru_codec_device_open (CodecDevice *dev, int media_type)
55 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
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);
64 GST_INFO ("succeeded to open %s. %d", CODEC_DEV, device_fd);
66 GST_DEBUG ("codec device is already opened");
69 // g_mutex_unlock (&gst_avcodec_mutex);
72 dev->buf_size = CODEC_DEVICE_MEM_SIZE;
73 GST_DEBUG ("mmap_size: %d", dev->buf_size);
75 // g_mutex_lock (&gst_avcodec_mutex);
76 if (device_mem == MAP_FAILED) {
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");
83 dev->fd = device_fd = -1;
84 g_mutex_unlock (&gst_avcodec_mutex);
87 GST_INFO ("succeeded to map device memory: %p", device_mem);
89 GST_DEBUG ("mapping device memory is already done");
91 dev->buf = device_mem;
94 GST_DEBUG ("open count: %d", opened_cnt);
95 g_mutex_unlock (&gst_avcodec_mutex);
97 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
103 gst_maru_codec_device_close (CodecDevice *dev)
107 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
111 GST_ERROR ("Failed to get %s fd.\n", CODEC_DEV);
115 g_mutex_lock (&gst_avcodec_mutex);
116 if (opened_cnt > 0) {
119 GST_DEBUG ("open count: %d", opened_cnt);
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);
126 device_mem = MAP_FAILED;
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);
132 dev->fd = device_fd = -1;
134 dev->buf = MAP_FAILED;
135 g_mutex_unlock (&gst_avcodec_mutex);
137 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
143 gst_maru_avcodec_open (CodecContext *ctx,
149 if (gst_maru_codec_device_open (dev, codec->media_type) < 0) {
150 GST_ERROR ("failed to open device");
154 g_mutex_lock (&gst_avcodec_mutex);
155 ret = codec_init (ctx, codec, dev);
156 g_mutex_unlock (&gst_avcodec_mutex);
162 gst_maru_avcodec_close (CodecContext *ctx, CodecDevice *dev)
166 GST_DEBUG ("close %d of context", ctx->index);
168 if (ctx && ctx->index == 0) {
169 GST_INFO ("context is not opened yet or context before %d", ctx->index);
173 if (dev && dev->fd < 0) {
174 GST_INFO ("fd is not opened yet or closed before %d", dev->fd);
178 g_mutex_lock (&gst_avcodec_mutex);
179 codec_deinit (ctx, dev);
180 g_mutex_unlock (&gst_avcodec_mutex);
182 ret = gst_maru_codec_device_close (dev);