Commits

Anonymous committed f75b7bb

Changes made to try to locate the source of the looping issue. Looks like calling a thread swap causes a deadlock...

Comments (0)

Files changed (3)

     int filled =0;
     len1=0;
     int co = 0;
+    int state = 0;
     for(;co<2;co++)
     {
+    	state=0;
         if      (!movie->paused && movie->audio_paused)
         {
             pauseBuffer(movie->channel);
             movie->audio_paused = 1;
             goto closing;
         }
+        state|=1;
+       
         //check if the movie has ended
         if(movie->stop)
         {
             stopBuffer(movie->channel);
             goto closing;
         }
+        state |=2;
+       
         //fill up the buffer
         while(movie->audio_pkt_size > 0)
         {
             filled=1;
 
         }
+        state |= 4;
+       
 		if(filled)
         {
             //GRABGIL
             //PySys_WriteStdout("movie->audio_pts: %i\n", (int)movie->audio_pts);
             //RELEASEGIL
             /* Buffer is filled up with a new frame, we spin lock/wait for a signal, where we then call playBuffer */
+           
             SDL_LockMutex(movie->audio_mutex);
+            /*if(movie->replay)
+	    	{
+	        	GRABGIL
+	        	PySys_WriteStdout("Preprint\n");
+	        	PySys_WriteStdout("Data_Size: %i\tChannel %i\tPTS: %i\n", data_size, movie->channel, (int)movie->audio_pts);
+	    		RELEASEGIL
+	    	}*/
             //SDL_CondWait(movie->audio_sig, movie->audio_mutex);
             int chan = playBuffer(movie->audio_buf1, data_size, movie->channel, movie->audio_pts);
+            if(movie->replay)
+	    	{
+	        	GRABGIL
+	        	PySys_WriteStdout("State-K: %i\n", state);
+	    		RELEASEGIL
+	    	}
             movie->channel = chan;
             filled=0;
             len1=0;
             SDL_UnlockMutex(movie->audio_mutex);
+            
             goto closing;
         }
-
+		state|=8;
+		
         //either buffer filled or no packets yet
         /* free the current packet */
         if (pkt->data)
             av_free_packet(pkt);
-
-
+		state|=16;
+		
         /* read next packet */
         if (packet_queue_get(&movie->audioq, pkt, 1) <= 0)
         {
             goto closing;
         }
+        state|=32;
+        
         if(pkt->data == flush_pkt.data)
         {
             avcodec_flush_buffers(dec);
             goto closing;
         }
+        state|=64;
+      
         movie->audio_pts      = pkt->pts;
         movie->audio_pkt_data = pkt->data;
         movie->audio_pkt_size = pkt->size;
+		state |=128;
 
-		
 
     }
 closing:
     GRABGIL
     Py_DECREF(movie);
     RELEASEGIL
+    if(movie->replay)
+    	{
+        	GRABGIL
+        	PySys_WriteStdout("StateF: %i\n", state);
+    		RELEASEGIL
+    	}
     return 0;
 }
 
 
         freq = enc->sample_rate;
         channels = enc->channels;
-        if (soundInit  (freq, -16, channels, 1024, av_q2d(enc->time_base)) < 0)
+        if(!movie->replay)
         {
-            RAISE(PyExc_SDLError, SDL_GetError ());
+	        if (soundInit  (freq, -16, channels, 1024, movie->_tstate) < 0)
+	        {
+	            RAISE(PyExc_SDLError, SDL_GetError ());
+	        }
         }
         //movie->audio_hw_buf_size = 1024;
         movie->audio_src_fmt= AUDIO_S16SYS;
     ic=movie->ic;
     int co=0;
     movie->last_showtime = av_gettime()/1000.0;
+    int state = 0;
     //RELEASEGIL
     for(;;)
     {
         co++;
-		GRABGIL
-		PySys_WriteStdout("Counter: %i\n", co);
-		RELEASEGIL
         if (movie->abort_request)
         {
             break;
         }
+        state |= 1;
         if(movie->stop)
         {
             break;
         }
-
+		state |= 2;
         if (movie->paused != movie->last_paused)
         {
             movie->last_paused = movie->paused;
                 av_read_play(ic);
             }
         }
+        state |=4;
         if(movie->paused)
         {
             SDL_Delay(10);
             continue;
         }
+        state|=8;
         if (movie->seek_req)
         {
             int stream_index= -1;
             }
             movie->seek_req = 0;
         }
+        state|=16;
         /* if the queue are full, no need to read more */
         if ((movie->audioq.size > MAX_AUDIOQ_SIZE) || //yay for short circuit logic testing
                 (movie->videoq.size > MAX_VIDEOQ_SIZE ))
             }
             continue;
         }
+        
+        state|=32;
         if(url_feof(ic->pb))
         {
             av_init_packet(pkt);
             packet_queue_put(&movie->videoq, pkt);
             continue;
         }
+        state|=64;
         if(movie->pictq_size<VIDEO_PICTURE_QUEUE_SIZE)
         {
             ret = av_read_frame(ic, pkt);
                 av_free_packet(pkt);
             }
         }
-
+		state|=128;
         if(movie->video_st)
             video_render(movie);
+        state |= 256;
         if(movie->audio_st)
             audio_thread(movie);
+        state |= 512;
+        if(movie->replay)
+    	{
+        	GRABGIL
+        	PySys_WriteStdout("State: %i\n", state);
+    		RELEASEGIL
+    	}
         if(co<2)
             video_refresh_timer(movie);
         if(movie->timing>0)
                 }
             }
         }
+        state |= 1024;
+        if(movie->replay)
+    	{
+        	GRABGIL
+        	PySys_WriteStdout("State: %i\n", state);
+    		RELEASEGIL
+    	}
+    	
     }
 
     ret = 0;
 }
 
 //initialize the mixer audio subsystem, code cribbed from mixer.c
-int soundInit  (int freq, int size, int channels, int chunksize, double time_base)
+int soundInit  (int freq, int size, int channels, int chunksize, PyThreadState *_tstate)
 {
     Uint16 fmt = 0;
     int i;
     ainfo.queue.size=0;
     ainfo.queue.first=ainfo.queue.last=NULL;
     ainfo.sample_rate=freq;
-    ainfo.time_base = time_base;
     ainfo.mutex = SDL_CreateMutex();
     ainfo.queue.mutex = SDL_CreateMutex();
     ainfo.ended=1;
+    ainfo._tstate = _tstate;
     return 0;
 }
 
 int soundEnd   (void)
 {
     ainfo.ended = 1;
+    //temp
+    ainfo.restart=1;
     queue_flush(&ainfo.queue);
     return 0;
 }
-
+//	#define DECLAREGIL PyThreadState *_oldtstate;
+//	#define GRABGIL    PyEval_AcquireLock();_oldtstate = PyThreadState_Swap(movie->_tstate);
+//	#define RELEASEGIL PyThreadState_Swap(_oldtstate); PyEval_ReleaseLock();
 /* Play a sound buffer, with a given length */
 int playBuffer (uint8_t *buf, uint32_t len, int channel, int64_t pts)
 {
+	//temp
+	PyThreadState *_oldtstate;
     Mix_Chunk *mix;
+    //temp
+    if(ainfo.restart)
+    {
+    	PyEval_AcquireLock();_oldtstate = PyThreadState_Swap(ainfo._tstate);
+    	//PySys_WriteStdout("Inside playBuffer\n");
+    	PyThreadState_Swap(_oldtstate); PyEval_ReleaseLock();
+    }
     SDL_mutexP(ainfo.mutex);
     int allocated=0;
     if(!ainfo.ended && (ainfo.queue.size>0||ainfo.playing))
     {
         if(buf)
         {
+        	if(ainfo.restart)
+		    {
+		    	PyEval_AcquireLock();_oldtstate = PyThreadState_Swap(ainfo._tstate);
+		    	PySys_WriteStdout("Inside queue>0 and a buffer exists\n");
+		    	PyThreadState_Swap(_oldtstate); PyEval_ReleaseLock();
+		    }
             //not a callback call, so we copy the buffer into a buffernode and add it to the queue.
             BufferNode *node;
             node = (BufferNode *)PyMem_Malloc(sizeof(BufferNode));
             node->pts = pts;
             queue_put(&ainfo.queue, node);
             SDL_mutexV(ainfo.mutex);
+            if(ainfo.restart)
+		    {
+		    	PyEval_AcquireLock();_oldtstate = PyThreadState_Swap(ainfo._tstate);
+		    	PySys_WriteStdout("returning from queue>0 and a buffer exists\n");
+		    	PyThreadState_Swap(_oldtstate); PyEval_ReleaseLock();
+		    }
             return ainfo.channel;
         }
         else if(!buf && ainfo.queue.size==0)
     int         channel;            //what channel the last buffer played on
     int         ended;              //whether or not we've "ended", so we know to output silence.
 	int			paused;
-    double      time_base;          //the base 1/frames per second value
     BufferQueue queue;              //queue of our buffers
     SDL_mutex   *mutex;
-
+	PyThreadState *_tstate;
+	int restart;
 }
 AudioInfo;
 
-int soundInit     (int freq, int size, int channels, int chunksize, double time_base);
+int soundInit     (int freq, int size, int channels, int chunksize, PyThreadState *_tstate);
 int soundQuit     (void);
 int soundStart    (void);
 int soundEnd      (void);