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 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);
62 GST_INFO ("succeeded to open %s. %d", CODEC_DEV, device_fd);
64 GST_DEBUG ("codec device is already opened");
67 // g_mutex_unlock (&gst_avcodec_mutex);
70 dev->buf_size = CODEC_DEVICE_MEM_SIZE;
71 GST_DEBUG ("mmap_size: %d", dev->buf_size);
73 // g_mutex_lock (&gst_avcodec_mutex);
74 if (device_mem == MAP_FAILED) {
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");
81 dev->fd = device_fd = -1;
82 g_mutex_unlock (&gst_avcodec_mutex);
85 GST_INFO ("succeeded to map device memory: %p", device_mem);
87 GST_DEBUG ("mapping device memory is already done");
89 dev->buf = device_mem;
92 GST_DEBUG ("open count: %d", opened_cnt);
93 g_mutex_unlock (&gst_avcodec_mutex);
99 gst_maru_codec_device_close (CodecDevice *dev)
105 GST_ERROR ("Failed to get %s fd %d", CODEC_DEV, fd);
109 g_mutex_lock (&gst_avcodec_mutex);
110 if (opened_cnt > 0) {
113 GST_DEBUG ("open count: %d", opened_cnt);
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);
120 device_mem = MAP_FAILED;
122 GST_INFO ("close %s", CODEC_DEV);
123 if (close(fd) != 0) {
124 GST_ERROR ("failed to close %s fd: %d", CODEC_DEV, fd);
126 dev->fd = device_fd = -1;
128 dev->buf = MAP_FAILED;
129 g_mutex_unlock (&gst_avcodec_mutex);
135 gst_maru_avcodec_open (CodecContext *ctx,
141 if (gst_maru_codec_device_open (dev, codec->media_type) < 0) {
142 GST_ERROR ("failed to open device");
146 g_mutex_lock (&gst_avcodec_mutex);
147 ret = interface->init (ctx, codec, dev);
148 g_mutex_unlock (&gst_avcodec_mutex);
154 gst_maru_avcodec_close (CodecContext *ctx, CodecDevice *dev)
158 GST_DEBUG ("close %d of context", ctx->index);
160 if (ctx && ctx->index == 0) {
161 GST_INFO ("context is not opened yet or context before %d", ctx->index);
165 if (dev && dev->fd < 0) {
166 GST_INFO ("fd is not opened yet or closed before %d", dev->fd);
170 g_mutex_lock (&gst_avcodec_mutex);
171 interface->deinit (ctx, dev);
172 g_mutex_unlock (&gst_avcodec_mutex);
174 ret = gst_maru_codec_device_close (dev);