Main Page | Alphabetical List | Data Structures | File List | Data Fields | Globals

xdvshow-shm.c

Go to the documentation of this file.
00001 /* 00002 * Copyright (c) 1999, 2000, 2001, 2002 WIDE Project 00003 * All rights reserved. 00004 * 00005 * Author : Akimichi OGAWA (akimichi@sfc.wide.ad.jp) 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 1. Redistributions of source code MUST retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * 2. Redistributions in binary form MUST reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in the 00014 * documentation and/or other materials provided with the distribution. 00015 * 3. All advertising materials mentioning features or use of this software 00016 * MUST display the following acknowledgement: 00017 * This product includes software developed by Akimichi OGAWA. 00018 * 4. The name of the author MAY NOT be used to endorse or promote products 00019 * derived from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00022 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00023 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00024 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 00025 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00026 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00027 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00028 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 00029 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00030 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00031 * POSSIBILITY OF SUCH DAMAGE. 00032 * 00033 */ 00034 00035 #ifdef HAVE_CONFIG_H 00036 #include <config.h> 00037 #endif /* HAVE_CONFIG_H */ 00038 00039 #include <stdio.h> 00040 #include <stdlib.h> 00041 #include <string.h> 00042 #include <unistd.h> 00043 #include <signal.h> 00044 #include <assert.h> 00045 #include <time.h> 00046 #include <sys/shm.h> 00047 #include <assert.h> 00048 #include <errno.h> 00049 #include <pthread.h> 00050 00051 #include "xdvshow-const.h" 00052 00053 #include "xdvshow-shm.h" 00054 #include "xdvshow-flags.h" 00055 #include "xdvshow-defs.h" 00056 00057 extern u_int32_t flags; 00058 extern u_int32_t dv_format_type; 00059 00060 struct __xdvshow_shm _xdvshow_shm; 00061 00062 sem_t video_ready, video_empty; 00063 sem_t audio_ready, audio_empty; 00064 00074 int xdvshow_semaphore_init() 00075 { 00076 if ((sem_init(&video_ready, 0, 0) != 0) || 00077 (sem_init(&video_empty, 0, _xdvshow_shm.framebufs) != 0)) { 00078 00079 perror("sem_init"); 00080 return (-1); 00081 } 00082 00083 if (flags_use_audio) { 00084 if ((sem_init(&audio_ready, 0 , 0) != 0) || 00085 (sem_init(&audio_empty, 0, _xdvshow_shm.framebufs) != 0)) { 00086 00087 perror("audio sem_init"); 00088 return (-1); 00089 } 00090 } 00091 00092 return(0); 00093 } 00094 00097 void 00098 xdvshow_close_shm() 00099 { 00100 if(shmdt(_xdvshow_shm.shm_buf)) { 00101 perror("detaching shared memory _xdvshow_shm.shm_buf"); 00102 assert(0); 00103 } 00104 } 00105 00110 int 00111 xdvshow_read_unlock() 00112 { 00113 _xdvshow_shm.locked_shm_framebuf->lock = DVFRAME_READY; 00114 00115 if (sem_post(&video_empty) < 0) { 00116 perror("video_empty sem_post"); 00117 assert(0); 00118 } 00119 00120 return(1); 00121 } 00122 00128 int 00129 xdvshow_read_audio_unlock() 00130 { 00131 _xdvshow_shm.locked_audio_shm_framebuf->lock = DVFRAME_READY; 00132 00133 if (sem_post(&audio_empty) < 0) { 00134 perror("audio_empty sem_post"); 00135 assert(0); 00136 } 00137 00138 return(1); 00139 } 00140 00150 int 00151 xdvshow_read_shm(u_char **dvframe) 00152 { 00153 #ifdef DEBUG 00154 int iDebugSemVal; 00155 if(sem_getvalue(&video_ready, &iDebugSemVal)) { 00156 perror("Unable to get value of video_ready semaphore!"); 00157 exit(EXIT_FAILURE); 00158 } 00159 DPRINT("semaphore video_ready value: %d\n", iDebugSemVal); 00160 #endif /* DEBUG */ 00161 00162 ts_sem_wait(&video_ready); 00163 00164 00165 _xdvshow_shm.read_frame->frame_buf->lock = DVFRAME_COPYING; 00166 00167 *dvframe = _xdvshow_shm.read_frame->frame_buf->data; 00168 _xdvshow_shm.locked_shm_framebuf = _xdvshow_shm.read_frame->frame_buf; 00169 00170 _xdvshow_shm.read_frame = _xdvshow_shm.read_frame->next; 00171 00172 return(1); 00173 } 00174 00184 int 00185 xdvshow_read_audio_shm(u_char **audioframe) 00186 { 00187 #ifdef DEBUG 00188 int iDebugSemVal; 00189 if(sem_getvalue(&audio_ready, &iDebugSemVal)) { 00190 perror("Unable to get value of audio_ready semaphore!"); 00191 exit(EXIT_FAILURE); 00192 } 00193 DPRINT("semaphore audio_ready value: %d\n", iDebugSemVal); 00194 #endif /* DEBUG */ 00195 00196 ts_sem_wait(&audio_ready); 00197 00198 _xdvshow_shm.audio_shm_frame->next->frame_buf->lock = DVFRAME_COPYING; 00199 00200 *audioframe = _xdvshow_shm.audio_shm_frame->frame_buf->data; 00201 _xdvshow_shm.locked_audio_shm_framebuf = _xdvshow_shm.audio_shm_frame->frame_buf; 00202 00203 _xdvshow_shm.audio_shm_frame = _xdvshow_shm.audio_shm_frame->next; 00204 00205 return(1); 00206 } 00207 00215 int 00216 _xdvshow_alloc_shm() 00217 { 00218 int shmid; 00219 00220 shmid = shmget(IPC_PRIVATE, 00221 SHM_FRAME_BUF_NUM*sizeof(struct shm_frame_buf)*2, /* audio and video */ 00222 0600); 00223 if (shmid < 0) { 00224 perror("shmget"); 00225 return(-1); 00226 } 00227 00228 _xdvshow_shm.shmid = shmid; 00229 _xdvshow_shm.framebufs = SHM_FRAME_BUF_NUM; 00230 00231 return(1); 00232 } 00233 00242 int 00243 _xdvshow_attach_shm() 00244 { 00245 u_long *shm_buf; 00246 00247 int i; 00248 struct shm_frame *shm_frame = NULL; 00249 struct shm_frame *shm_frame_last = NULL; 00250 00251 if ((shm_buf = shmat(_xdvshow_shm.shmid, 0, 0)) == (void *)-1) { 00252 perror("shmat"); 00253 return(-1); 00254 } 00255 00256 _xdvshow_shm.shm_buf = shm_buf; 00257 00258 for (i=0; i<_xdvshow_shm.framebufs; i++) { 00259 shm_frame = (struct shm_frame *)malloc(sizeof(struct shm_frame)); 00260 memset(shm_frame, 0, sizeof(struct shm_frame)); 00261 00262 shm_frame->frame_buf = (struct shm_frame_buf *)(shm_buf + (i * sizeof(struct shm_frame_buf))/4); 00263 shm_frame->frame_buf->lock = DVFRAME_READY; 00264 00265 if (shm_frame_last == NULL) { 00266 _xdvshow_shm.shm_frame = shm_frame; 00267 } 00268 else { 00269 shm_frame_last->next = shm_frame; 00270 } 00271 shm_frame_last = shm_frame; 00272 } 00273 shm_frame->next = _xdvshow_shm.shm_frame; 00274 _xdvshow_shm.read_frame = _xdvshow_shm.shm_frame; 00275 00276 shm_frame = NULL; 00277 shm_frame_last = NULL; 00278 for (i=0; i<_xdvshow_shm.framebufs; i++) { 00279 shm_frame = (struct shm_frame *)malloc(sizeof(struct shm_frame)); 00280 memset(shm_frame, 0, sizeof(struct shm_frame)); 00281 00282 shm_frame->frame_buf = (struct shm_frame_buf *)(shm_buf + ((_xdvshow_shm.framebufs + i) * sizeof(struct shm_frame_buf))/4); 00283 shm_frame->frame_buf->lock = DVFRAME_READY; 00284 00285 if (shm_frame_last == NULL) { 00286 _xdvshow_shm.audio_shm_frame = shm_frame; 00287 } 00288 else { 00289 shm_frame_last->next = shm_frame; 00290 } 00291 shm_frame_last = shm_frame; 00292 } 00293 shm_frame->next = _xdvshow_shm.audio_shm_frame; 00294 00295 return(1); 00296 }

Generated on Wed Nov 3 19:19:02 2004 for xdvshow by doxygen 1.3.7