Changed a way to use mmap system call. 54/11854/1
authorKitae Kim <kt920.kim@samsung.com>
Mon, 4 Nov 2013 12:02:28 +0000 (21:02 +0900)
committerSeokYeon Hwang <syeon.hwang@samsung.com>
Wed, 6 Nov 2013 08:28:15 +0000 (17:28 +0900)
It has to be called once per a process. When emulator does multi decoding or encoding task in a process,
a virtual memory from mmap has to be the same for each thread.

Change-Id: I0eb1c8581dbc958b1fa6bfc4fb4a09bd99f7f102
Signed-off-by: Kitae Kim <kt920.kim@samsung.com>
packaging/gst-plugins-emulator.changes
packaging/gst-plugins-emulator.spec
src/gstmarudevice.c
src/gstmaruinterface.c

index 3df6198..37472cf 100644 (file)
@@ -1,3 +1,8 @@
+* Mon Nov  4 11:53:52 UTC 2013 Kitae Kim <kt920.kim@samsung.com>
+- Changed a way to use mmap api. It has to be called once per a process.
+  When emulator does multi decoding or encoding task in a process,
+  a virtual memory from mmap has to be the same for each thread.
+
 * Thu Oct 24 05:50:49 UTC 2013 Kitae Kim <kt920.kim@samsung.com>
 - A routine to query codec element is called once per a process when loading this plugin.
 
index a2b0fa6..ffd4773 100644 (file)
@@ -1,5 +1,5 @@
 Name: gst-plugins-emulator
-Version: 0.1.12
+Version: 0.1.13
 Release: 2 
 Summary: GStreamer Streaming-media framework plug-in for Tizen emulator.
 Group: TO_BE/FILLED_IN
index 600936a..2d2a7d4 100644 (file)
@@ -45,8 +45,9 @@ static GStaticMutex gst_avcodec_mutex = G_STATIC_MUTEX_INIT;
 
 #define CODEC_DEVICE_MEM_SIZE 32 * 1024 * 1024
 
-gpointer device_mem;
-int device_fd;
+gpointer device_mem = NULL;
+int device_fd = 0;
+int opened_cnt = 0;
 
 int
 gst_maru_codec_device_open (CodecDevice *dev, int media_type)
@@ -67,23 +68,24 @@ gst_maru_codec_device_open (CodecDevice *dev, int media_type)
   dev->mem_info.index = dev->buf_size;
 
   CODEC_LOG (DEBUG, "before mmap. buf_size: %d\n", dev->buf_size);
-  mmapbuf = mmap (NULL, CODEC_DEVICE_MEM_SIZE, PROT_READ | PROT_WRITE,
-                  MAP_SHARED, fd, 0);
-  if (mmapbuf == MAP_FAILED) {
-    perror("Failed to map device memory of codec.");
-    dev->buf = NULL;
-    return -1;
+
+  g_static_mutex_lock (&gst_avcodec_mutex);
+  if (!device_mem) {
+    device_mem = mmap (NULL, CODEC_DEVICE_MEM_SIZE, PROT_READ | PROT_WRITE,
+        MAP_SHARED, fd, 0);
+    if (device_mem == MAP_FAILED) {
+      perror("Failed to map device memory of codec.");
+      dev->buf = NULL;
+      return -1;
+    }
   }
+  dev->buf = device_mem;
+  opened_cnt++;
+  g_static_mutex_unlock (&gst_avcodec_mutex);
 
-  CODEC_LOG (INFO, "succeeded to map device memory: %p.\n", mmapbuf);
+  CODEC_LOG (INFO, "succeeded to map device memory: %p.\n", dev->buf);
   dev->fd = fd;
-  dev->buf = mmapbuf;
-
-  if (media_type == AVMEDIA_TYPE_VIDEO) {
-    device_mem = mmapbuf;
-    device_fd = fd;
-    CODEC_LOG (INFO, "video type! mmapbuf: %p fd: %d\n", mmapbuf, fd);
-  }
+  device_fd = fd;
 
   CODEC_LOG (DEBUG, "leave: %s\n", __func__);
 
@@ -104,16 +106,17 @@ gst_maru_codec_device_close (CodecDevice *dev)
     return -1;
   }
 
-  mmapbuf = dev->buf;
-  if (!mmapbuf) {
-    GST_ERROR("Failed to get mmaped memory address.\n");
-    return -1;
-  }
-
-  CODEC_LOG (INFO, "Release memory region of %p.\n", mmapbuf);
-  if (munmap(mmapbuf, CODEC_DEVICE_MEM_SIZE) != 0) {
-    CODEC_LOG(ERR, "Failed to release memory region of %s.\n", CODEC_DEV);
+  g_static_mutex_lock (&gst_avcodec_mutex);
+  if (opened_cnt) {
+    if (opened_cnt == 1) {
+      CODEC_LOG (INFO, "Release memory region of %p.\n", device_mem);
+      if (munmap(device_mem, CODEC_DEVICE_MEM_SIZE) != 0) {
+        CODEC_LOG(ERR, "Failed to release memory region of %s.\n", CODEC_DEV);
+      }
+    }
+    opened_cnt--;
   }
+  g_static_mutex_unlock (&gst_avcodec_mutex);
   dev->buf = NULL;
 
   ioctl(fd, CODEC_CMD_RELEASE_BUFFER, &dev->mem_info.offset);
@@ -135,12 +138,13 @@ gst_maru_avcodec_open (CodecContext *ctx,
 {
   int ret;
 
-  g_static_mutex_lock (&gst_avcodec_mutex);
-
+//  g_static_mutex_lock (&gst_avcodec_mutex);
   if (gst_maru_codec_device_open (dev, codec->media_type) < 0) {
     perror("failed to open device.\n");
     return -1;
   }
+
+  g_static_mutex_lock (&gst_avcodec_mutex);
   ret = codec_init (ctx, codec, dev);
   g_static_mutex_unlock (&gst_avcodec_mutex);
 
@@ -152,13 +156,14 @@ gst_maru_avcodec_close (CodecContext *ctx, CodecDevice *dev)
 {
   int ret;
 
-  g_static_mutex_lock (&gst_avcodec_mutex);
-
   CODEC_LOG (DEBUG, "gst_maru_avcodec_close\n");
+
+  g_static_mutex_lock (&gst_avcodec_mutex);
   codec_deinit (ctx, dev);
+  g_static_mutex_unlock (&gst_avcodec_mutex);
 
   ret = gst_maru_codec_device_close (dev);
-  g_static_mutex_unlock (&gst_avcodec_mutex);
+//  g_static_mutex_unlock (&gst_avcodec_mutex);
 
   return ret;
 }
index 171e938..c2d4e97 100644 (file)
@@ -113,14 +113,12 @@ secure_device_mem (guint buf_size)
   ret = ioctl (device_fd, cmd, &mem_offset);
   if (ret < 0) {
     CODEC_LOG (ERR, "failed to get available buffer\n");
-    // FIXME:
   } else {
     if (mem_offset == (LARGE_BUFFER * 8)) {
       CODEC_LOG (ERR, "acquired memory is over!!\n");
     } else {
       info.start = (gpointer)((uint32_t)device_mem + mem_offset);
       info.offset = mem_offset;
-
       CODEC_LOG (DEBUG, "acquire device_memory: 0x%x\n", mem_offset);
     }
   }
@@ -169,8 +167,7 @@ codec_buffer_alloc (GstPad *pad, guint64 offset, guint size,
 
   info = secure_device_mem (size);
 
-  CODEC_LOG (DEBUG, "memory start: 0x%p, offset 0x%x\n",
-            info.start, info.offset);
+  CODEC_LOG (DEBUG, "memory start: 0x%p, offset 0x%x\n", info.start, info.offset);
 
   GST_BUFFER_DATA (*buf) = GST_BUFFER_MALLOCDATA (*buf) = info.start;
   GST_BUFFER_SIZE (*buf) = size;
@@ -367,8 +364,10 @@ codec_picture_copy (CodecContext *ctx, uint8_t *pict,
 
   if (pict_size < (SMALL_BUFFER)) {
     dev->mem_info.offset = (uint32_t)pict - (uint32_t)mmapbuf;
-    CODEC_LOG (DEBUG, "pict: %p , device_mem: %p\n",  pict, mmapbuf);
-    CODEC_LOG (DEBUG, "picture_copy, mem_offset = 0x%x\n",  dev->mem_info.offset);
+    CODEC_LOG (DEBUG, "%d of pict: %p , device_mem: %p\n",
+              ctx->index, pict, mmapbuf);
+    CODEC_LOG (DEBUG, "%d of picture_copy, mem_offset = 0x%x\n",
+              ctx->index, dev->mem_info.offset);
   }
 
   _codec_write_to_qemu (ctx->index, CODEC_PICTURE_COPY,