Commits

Anonymous committed 93e070f

Fixed occassional speedup after unpausing. Now we just save and then restore the audio clock after unpausing.

Comments (0)

Files changed (6)

 
 print m         #calls __repr__, which will show the filename, and the current 
                 # timestamp. 
-#print "repeated looping plays.."
-m.play(10)
-time.sleep(10*30)
+#print "repeated looping plays.."#
+#m.play(10)
+#time.sleep(10*30)
 
 
 print "Playing infinitely"
 print m.playing
 print m
 
+m.pause()
+time.sleep(4)
+m.pause()
+time.sleep(4)
+m.pause()
+time.sleep(4)
+m.pause()
+
 #Now we're going to play with the size of the window, affecting the video on 
 # the fly. resize(width, height) is the main function, changes them both at
 # the same time.
 m.stop()
 time.sleep(3)
 #And now we restart playing.
-print "Playing again..."
+print "Playing again..." 
 m.play(-1)
 print "done restart play..."
 time.sleep(10)
             /* if video is slave, we try to correct big delays by
                duplicating or deleting a frame */
             ref_clock = get_master_clock(movie);
-            //GRABGIL
-			//PySys_WriteStdout("Audio Clock: %f\n", ref_clock);
+            GRABGIL
+			PySys_WriteStdout("Audio Clock: %f\n", ref_clock);
             diff = movie->video_current_pts - ref_clock;
-            //PySys_WriteStdout("diff at call %i: %f\n", movie->diff_co, diff);
-            //RELEASEGIL
+            PySys_WriteStdout("diff at call %i: %f\n", movie->diff_co, diff);
+            RELEASEGIL
             /* skip or repeat frame. We take into account the
                delay to compute the threshold. I still don't know
                if it is the best guess */
             actual_delay = 0.010;
         }
         GRABGIL
-        //PySys_WriteStdout("frame_timer: %f\ndelay: %f\n",movie->frame_timer, delay);
+        PySys_WriteStdout("frame_timer: %f\ndelay: %f\n",movie->frame_timer, delay);
         movie->timing = (actual_delay*1000.0)+0.5;
         RELEASEGIL
     }
     int co = 0;
     for(;co<2;co++)
     {
-        if      (!movie->paused && movie->audio_paused)
+        if (!movie->paused && movie->audio_paused)
         {
             pauseBuffer(movie->channel);
             movie->audio_paused = 0;
     ic=movie->ic;
     int co=0;
     movie->last_showtime = av_gettime()/1000.0;
-    int state = 0;
     //RELEASEGIL
     for(;;)
     {
         if (movie->paused != movie->last_paused)
         {
             movie->last_paused = movie->paused;
+            if(!movie->audio_paused)
+            {
+            	//we do this in case we haven't reached the audio thread yet... which will cause this function to just loop without ever touching the audio_thread function.
+	            pauseBuffer(movie->channel);
+            }
             if (movie->paused)
             {
+            	
                 av_read_pause(ic);
             }
             else
 AVPacket flush_pkt;
 
 /* Queues for already-loaded pictures, for rapid display */
-#define VIDEO_PICTURE_QUEUE_SIZE 16
+#define VIDEO_PICTURE_QUEUE_SIZE 8
 
 //included from ffmpeg header files, as the header file is not publically available.
 #if defined(__ICC) || defined(__SUNPRO_C)
 #define _MIXER_DEFAULT_CHANNELS 2
 #define _MIXER_DEFAULT_CHUNKSIZE 4096
 
-
-
 int queue_get(BufferQueue *q, BufferNode **pkt1)
 {
     int ret;
     mix->alen = (Uint32 )len;
     mix->volume = 127;
     ainfo.playing = 1;
-    if(mix->alen==0)
-    {
-    	PySys_WriteStdout("hahahah");	
-    }
- 	if(!ainfo.ended)
+ 	if(!info->ended)
 	{
-    	int bytes_per_sec = ainfo.channels*ainfo.sample_rate*2;
-    	ainfo.audio_clock+= (double) len/(double) bytes_per_sec;
+    	int bytes_per_sec = info->channels*info->sample_rate*2;
+    	info->audio_clock+= (double) len/(double) bytes_per_sec;
 	}
     ainfo.current_frame_size =len;
     int chan = ainfo.channel;
     
     //SDL_mutexV(ainfo.mutex);
     
-    char s[1024];
-    char *s2;
     int playing = Mix_Playing(chan);
     if(playing && allocated &&false)
     {
     	return chan;
     }
     int ret = Mix_PlayChannel(chan, mix, 0);
-    if(ret==-1)
-    {
-    	s2 = Mix_GetError();
-    	int i;
-    	for(i=0;i<1024;i++)
-    	{
-    		s[i]=*s2;
-    		s2++;
-    	}
-    	PySys_WriteStdout(s2);
-    }
+
     ainfo.channel = ret;
     //if buffer was allocated, we gotta clean it up.
     if(allocated)
     //Mix_HaltChannel(channel);
     return 0;
 }
+
 int pauseBuffer(int channel)
 {
+	AudioInfo *info = &ainfo;
     if(channel<=-1)
         return 0;
     int paused = Mix_Paused(channel);
     if(paused)
     {
-    	ainfo.ended=0;
+    	info->audio_clock=info->old_clock;
+    	info->ended=0;
         Mix_Resume(-1);
     }
     else
     {
+    	ainfo.old_clock = ainfo.audio_clock;
     	ainfo.ended=1;
         Mix_Pause(-1);
     }
 }
 BufferNode;
 
+
 typedef struct BufferQueue
 {
     BufferNode *first, *last;
 typedef struct AudioInfo
 {
     double      audio_clock;        //keeps track of our PTS, in seconds
+    double      old_clock;          //for when the video is paused
     int         channels;           //data for keeping track of the fraction of a second the current frame will take
     int         sample_rate;        //''
     int         current_frame_size; //''
     //movie has been stopped, need to close down streams and that.
     if(movie->stop)
     {
+    	//first we release the GIL, then we release all the resources associated with the streams, if they exist.
+    	PyEval_ReleaseLock();
         if(movie->video_st)
-            stream_component_close(movie, movie->video_st->index);
+            stream_component_end(movie, movie->video_st->index);
         if(movie->audio_st)
-            stream_component_close(movie, movie->audio_st->index);
+            stream_component_end(movie, movie->audio_st->index);
+        PyEval_AcquireLock();
         movie->stop = 0;
     }
 
     Py_BEGIN_ALLOW_THREADS
     SDL_LockMutex(movie->dest_mutex);
     stream_pause(movie);
-    movie->seek_req = 1;
-    movie->seek_pos = 0;
-    movie->seek_flags =AVSEEK_FLAG_BACKWARD;
     movie->stop = 1;
     Py_END_ALLOW_THREADS
     SDL_UnlockMutex(movie->dest_mutex);
     }
     movie->height = h;
     movie->width  = w;
+    //status indicators for rendering and that. Very important!
     movie->resize_w =  movie->resize_h= 1;
     Py_RETURN_NONE;
 
     {
         SDL_FreeSurface(movie->canon_surf);
     }
-    //PySurface_Check doesn't really work right for some reason... so we skip it for now.
     if(PySurface_Check(surface))
     {
         movie->canon_surf=PySurface_AsSurface(surface);