ea8845da330f7214ee21482b5d9a11cdf721b0f6
[platform/adaptation/emulator/gst-plugins-emulator.git] / src / gstemuldev.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 "gstemulcommon.h"
42 #include "gstemuldev.h"
43
44
45 int
46 gst_emul_codec_device_open (CodecDevice *dev)
47 {
48   int fd;
49   void *mmapbuf;
50
51   printf("enter: %s\n", __func__);
52
53   if ((fd = open(CODEC_DEV, O_RDWR)) < 0) {
54     perror("Failed to open codec device.");
55     return -1;
56   }
57   GST_DEBUG("succeeded to open %s.\n", CODEC_DEV);
58
59   mmapbuf = mmap (NULL, dev->buf_size, PROT_READ | PROT_WRITE,
60                   MAP_SHARED, fd, 0);
61   if (!mmapbuf) {
62     perror("Failed to map device memory of codec.");
63     close(fd);
64     return -1;
65   }
66   GST_DEBUG("succeeded to map device memory.\n");
67
68   dev->fd = fd;
69   dev->buf = mmapbuf;
70
71   return 0;
72 }
73
74 int
75 gst_emul_codec_device_close (CodecDevice *dev)
76 {
77   int fd = 0;
78   void *mmapbuf = NULL;
79
80   printf("enter: %s\n", __func__);
81
82   fd = dev->fd;
83   if (fd < 0) {
84     GST_ERROR("Failed to get %s fd.\n", CODEC_DEV);
85     return -1;
86   }
87
88   mmapbuf = dev->buf;
89   if (!mmapbuf) {
90     GST_ERROR("Failed to get mmaped memory address.\n");
91     return -1;
92   }
93
94   GST_DEBUG("Release memory region of %s.\n", CODEC_DEV);
95   if (munmap(mmapbuf, dev->buf_size) != 0) {
96     GST_ERROR("Failed to release memory region of %s.\n", CODEC_DEV);
97   }
98
99   GST_DEBUG("close %s.\n", CODEC_DEV);
100   if (close(fd) != 0) {
101     GST_ERROR("Failed to close %s. fd: %d\n", CODEC_DEV, fd);
102   }
103
104   printf("leave: %s\n", __func__);
105
106   return 0;
107 }