changeset 28016:3b8e66828e10

use mmap instead of shmat for MPlayerOSX, patch by Adrian Stutz<adrian@sttz.ch> Original thread: date: Sun, Oct 12, 2008 at 10:32 PM subject: [MPlayer-dev-eng] [PATCH] vo_macosx: use mmap instead of shmat for MPlayerOSX
author nplourde
date Sun, 30 Nov 2008 12:55:11 +0000
parents 94cf4a9270cb
children aee575e7aee5
files libvo/vo_macosx.m
diffstat 1 files changed, 32 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/libvo/vo_macosx.m	Sun Nov 30 09:17:21 2008 +0000
+++ b/libvo/vo_macosx.m	Sun Nov 30 12:55:11 2008 +0000
@@ -9,7 +9,7 @@
 #import "vo_macosx.h"
 #include <sys/types.h>
 #include <sys/ipc.h>
-#include <sys/shm.h>
+#include <sys/mman.h>
 #include <CoreServices/CoreServices.h>
 //special workaround for Apple bug #6267445
 //(OSServices Power API disabled in OSServices.h for 64bit systems)
@@ -39,8 +39,7 @@
 OSType pixelFormat;
 
 //shared memory
-int shm_id;
-struct shmid_ds shm_desc;
+int shm_fd;
 BOOL shared_buffer = false;
 
 //Screen
@@ -165,21 +164,36 @@
 	else
 	{
 		movie_aspect = (float)d_width/(float)d_height;
-				
-		shm_id = shmget(9849, image_width*image_height*image_bytes, IPC_CREAT | 0666);
-		if (shm_id == -1)
+		
+		// create shared memory
+		shm_fd = shm_open("mplayerosx", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
+		if (shm_fd == -1)
 		{
-			perror("vo_mplayer shmget: ");
+			mp_msg(MSGT_VO, MSGL_FATAL, 
+				   "vo_macosx: failed to open shared memory. Error: %s\n", strerror(errno));
 			return 1;
 		}
 		
-		image_data = shmat(shm_id, NULL, 0);
-		if (!image_data)
-		{	
-			perror("vo_mplayer shmat: ");
+		
+		if (ftruncate(shm_fd, image_width*image_height*image_bytes) == -1)
+		{
+			mp_msg(MSGT_VO, MSGL_FATAL, 
+				   "vo_macosx: failed to size shared memory, possibly already in use. Error: %s\n", strerror(errno));
+			shm_unlink("mplayerosx");
 			return 1;
 		}
 		
+		image_data = mmap(NULL, image_width*image_height*image_bytes,
+					PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
+		
+		if (image_data == MAP_FAILED)
+		{
+			mp_msg(MSGT_VO, MSGL_FATAL, 
+				   "vo_macosx: failed to map shared memory. Error: %s\n", strerror(errno));
+			shm_unlink("mplayerosx");
+			return 1;
+		}		
+		
 		//connnect to mplayerosx
 		mplayerosxProxy=[NSConnection rootProxyForConnectionWithRegisteredName:@"mplayerosx" host:nil];
 		if ([mplayerosxProxy conformsToProtocol:@protocol(MPlayerOSXVOProto)]) {
@@ -269,12 +283,13 @@
 		mplayerosxProto = nil;
 		[mplayerosxProxy release];
 		mplayerosxProxy = nil;
-
-		if (shmdt(image_data) == -1)
-			mp_msg(MSGT_VO, MSGL_FATAL, "uninit: shmdt failed\n");
-	
-		if (shmctl(shm_id, IPC_RMID, &shm_desc) == -1)
-			mp_msg(MSGT_VO, MSGL_FATAL, "uninit: shmctl failed\n");
+		
+		if (munmap(image_data, image_width*image_height*image_bytes) == -1)
+			mp_msg(MSGT_VO, MSGL_FATAL, "uninit: munmap failed. Error: %s\n", strerror(errno));
+		
+		if (shm_unlink("mplayerosx") == -1)
+			mp_msg(MSGT_VO, MSGL_FATAL, "uninit: shm_unlink failed. Error: %s\n", strerror(errno));
+		
 	}
 
     SetSystemUIMode( kUIModeNormal, 0);