implemented two ways to copy data from guest and host
[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 "gstemulapi.h"
42 #include "gstemuldev.h"
43
44
45 int
46 gst_emul_codec_device_open (CodecDevice *dev)
47 {
48   int fd;
49 //  CodecDevMemInfo mem_info;
50   void *mmapbuf;
51
52   printf("enter: %s\n", __func__);
53
54   if ((fd = open(CODEC_DEV, O_RDWR)) < 0) {
55     perror("Failed to open codec device.");
56     return -1;
57   }
58
59 //  GST_DEBUG("succeeded to open %s. %d.\n", CODEC_DEV, fd);
60   printf("succeeded to open %s. %d.\n", CODEC_DEV, fd);
61 //  memset(&mem_info, 0x00, sizeof(CodecDevMemInfo));
62   dev->mem_info.index = dev->buf_size;
63
64   ioctl(fd, CODEC_CMD_GET_DEVICE_MEM_INFO, &dev->mem_info);
65
66 #if 1
67   printf("mem type: %d, index: %d, offset: %d\n",
68     dev->mem_info.type, dev->mem_info.index, dev->mem_info.offset);
69 #endif
70
71   mmapbuf = mmap (NULL, dev->buf_size, PROT_READ | PROT_WRITE,
72                   MAP_SHARED, fd, dev->mem_info.offset);
73   if (!mmapbuf) {
74     perror("Failed to map device memory of codec.");
75     close(fd);
76     return -1;
77   }
78
79 //  GST_DEBUG("succeeded to map device memory.\n");
80   printf("succeeded to map device memory: %p.\n", mmapbuf);
81   dev->fd = fd;
82   dev->buf = mmapbuf;
83
84   printf("leave: %s\n", __func__);
85
86   return 0;
87 }
88
89 int
90 gst_emul_codec_device_close (CodecDevice *dev)
91 {
92   int fd = 0;
93   void *mmapbuf = NULL;
94
95   printf("enter: %s\n", __func__);
96
97   fd = dev->fd;
98   if (fd < 0) {
99     GST_ERROR("Failed to get %s fd.\n", CODEC_DEV);
100     return -1;
101   }
102
103   mmapbuf = dev->buf;
104   if (!mmapbuf) {
105     GST_ERROR("Failed to get mmaped memory address.\n");
106     return -1;
107   }
108
109 //  GST_DEBUG("Release memory region of %s.\n", CODEC_DEV);
110   CODEC_LOG(LOG, "Release memory region of %s.\n", CODEC_DEV);
111
112   if (munmap(mmapbuf, dev->buf_size) != 0) {
113     GST_ERROR("Failed to release memory region of %s.\n", CODEC_DEV);
114   }
115   dev->buf = NULL; 
116
117   ioctl(fd, CODEC_CMD_RELEASE_DEVICE_MEM, &dev->mem_info);
118
119 //  GST_DEBUG("close %s.\n", CODEC_DEV);
120   CODEC_LOG(LOG, "close %s.\n", CODEC_DEV);
121
122   if (close(fd) != 0) {
123     GST_ERROR("Failed to close %s. fd: %d\n", CODEC_DEV, fd);
124   }
125
126   printf("leave: %s\n", __func__);
127
128   return 0;
129 }
130
131 static GStaticMutex gst_avcodec_mutex = G_STATIC_MUTEX_INIT;
132
133 int
134 gst_emul_avcodec_open (CodecContext *ctx, CodecElement *codec, CodecDevice *dev)
135 {
136   int ret;
137
138   g_static_mutex_lock (&gst_avcodec_mutex);
139
140   if (gst_emul_codec_device_open (dev) < 0) {
141     perror("failed to open device.\n");
142     return -1;
143   }
144   ret = emul_avcodec_init (ctx, codec, dev);
145   g_static_mutex_unlock (&gst_avcodec_mutex);
146
147   return ret;
148 }
149
150 int
151 gst_emul_avcodec_close (CodecContext *ctx, CodecDevice *dev)
152 {
153   int ret;
154
155   g_static_mutex_lock (&gst_avcodec_mutex);
156
157   printf ("gst_emul_avcodec_close\n");
158   emul_avcodec_deinit (ctx, dev);
159
160   ret = gst_emul_codec_device_close (dev);
161   g_static_mutex_unlock (&gst_avcodec_mutex);
162
163   return ret;
164 }