Commits

Anonymous committed 7dd4689

Added fixes for stopping and restarting playback. Forgot to set
abort_request back to zero... whoops. Also fixed some issues in sound,
where I didn't call the right functions at the right times, as well as
checks within the sound module itself. Had to add a new channel int to
the audio portion of PyMovie.

In addition, fixed a few small bugs in my test script.

Comments (0)

Files changed (5)

 
 print "Please give an (absolute)filename of a movie file you'd like to play: ",
 #filename = raw_input()
-filename="/home/tyler/War3.avi"
+filename="/home/tyler/dhs1.avi"
 #initialization. It could also have a surface as a second argument, and every 
 # frame will be blitted to that surface. It is the programmer's responsibility
 # to be on time for rendering that surface.
 
 print m         #calls __repr__, which will show the filename, and the current 
                 # timestamp. 
-
+print "Playing infinitely"
 m.play(-1)      #We're going to use infinite play, so we can demonstrate all 
                 # the features.
 time.sleep(10)  #sleep for ten seconds to let one see the video play, and hear 
 #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.
+print "Resizing..."
 m.resize(m.width/2, m.height*2)
 time.sleep(10) #another ten second nap.
+print "Resizing again..."
 m.width = m.width*4 
 time.sleep(10)
+print "Back to normal!"
 m.width=m.width/2
 m.height = m.height/2
 #back to our original size
 time.sleep(10)
 #Here we demonstrate the use of pause. You pause, then call pause again to play
+print "Pausing..."
 m.pause()
 print m.paused
 print m.playing
 time.sleep(2)
+print "Unpausing..."
 m.pause()
 print m.paused
 print m.playing
+time.sleep(10)
 #Here is the stop function. Right now, rewind is the exact same as stop.
+print "Stopping..."
 m.stop()
 time.sleep(3)
 #And now we restart playing.
+print "Playing again..."
 m.play(-1)
 
+print "Surface time..."
+time.sleep(10)
 screen = pygame.display.set_mode((m.width, m.height))
 #This will move the movie player from overlay mode to blitting to the surface 
 # we've given it. This means it is our responsibility to update the display on 
 actions = {5: lambda x: x.pause(), 10: lambda x: x.pause(), 15: lambda x: x.resize(x.width/2, x.height/2), 20:lambda x: x.stop(), 22: lambda x: x.play(-1)}
 prev_time = time.time()
 m.surface = screen
+print "About to do surface gymnastics..."
 while(1):
     new_time=time.time()
     diff = int(new_time-prev_time)
     if(diff>=1):
         counter+=1
-    prev_time=new_time
+        print counter
+        prev_time=new_time
     if counter==30:
         break
     if actions.has_key(counter):
+        print "Performing action at counter value: %d" % counter
         actions[counter](m)
+        counter +=1
     pygame.display.update() #we can do this because we're blitting each frame of the movie to the main screen we instantiated.
     
+m.stop()
 del m
 #the end
 
 /* get the current video clock value */
  double get_video_clock(PyMovie *movie)
 {
+    DECLAREGIL
+	GRABGIL
     Py_INCREF( movie);
+    RELEASEGIL
     double delta;
     
     if (movie->paused) {
         delta = (av_gettime() - movie->video_current_pts_time) / 1000000.0;
     }
     double temp = movie->video_current_pts+delta;
+    GRABGIL
     Py_DECREF( movie);
+    RELEASEGIL
     return temp;
 }
 
 	GRABGIL
     Py_INCREF( movie);
     RELEASEGIL
+    int paused = movie->paused;
     movie->paused = !movie->paused;
-    if (!movie->paused) {
-
-       movie->video_current_pts = get_video_clock(movie);
-        
-        movie->frame_timer += (av_gettime() - movie->video_current_pts_time) / 1000000.0;
+    if (!movie->paused) 
+    {
+    	movie->video_current_pts = get_video_clock(movie);
+		movie->frame_timer += (av_gettime() - movie->video_current_pts_time) / 1000000.0;
     }
+    movie->last_paused=paused;
     GRABGIL
     Py_DECREF( movie);
 	RELEASEGIL
 	DECLAREGIL
 	GRABGIL
 	Py_INCREF(movie);
-	//PySys_WriteStdout("Inside audio_thread\n");
 	RELEASEGIL
     double pts;
 	AVPacket *pkt = &movie->audio_pkt;
 	int co = 0;
 	for(;co<10;co++)
 	{
-		/*GRABGIL
-		PySys_WriteStdout("audio_thread: infinite looping(%i)...\n", co);
-		RELEASEGIL*/
 		//fill up the buffer
 		while(movie->audio_pkt_size > 0)
         {
-        	//GRABGIL
-	        //PySys_WriteStdout("audio_thread: filling up the buffer...\n");
-			//RELEASEGIL
 			data_size = sizeof(movie->audio_buf1);
             len1 += avcodec_decode_audio2(dec, (int16_t *)movie->audio_buf1, &data_size, movie->audio_pkt_data, movie->audio_pkt_size);
             if (len1 < 0) {
         if (pkt->data)
             av_free_packet(pkt);
 
-        //if (movie->paused) {
-        //    continue;
-        //}
+        if (movie->paused) {
+        	pauseBuffer(movie->channel);
+            goto closing;
+        }
         //check if the movie has ended
 		if(movie->stop)
+		{
+			stopBuffer(movie->channel);
 			goto closing;
+		}
         /* read next packet */
         if (packet_queue_get(&movie->audioq, pkt, 1) < 0)
         {
-			SDL_Delay(10);         
-            continue;
+            goto closing;
         }
         if(pkt->data == flush_pkt.data){
             avcodec_flush_buffers(dec);
-            continue;
+            goto closing;
         }
 
         movie->audio_pkt_data = pkt->data;
         	/* 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);
         	//SDL_CondWait(movie->audio_sig, movie->audio_mutex);
-        	playBuffer(movie->audio_buf1, data_size);
+        	int chan = playBuffer(movie->audio_buf1, data_size);
+        	movie->channel = chan;
         	filled=0;
         	len1=0;
         	SDL_UnlockMutex(movie->audio_mutex);
     //movie->subpq_cond = SDL_CreateCond();
     
     movie->paused = 1;
+    //in case we've called stream open once before...
+    movie->abort_request = 0;
     movie->av_sync_type = AV_SYNC_VIDEO_MASTER;
 	
     video_index = -1;
 	DECLAREGIL
 	GRABGIL	
 	Py_INCREF(movie);
+	//PySys_WriteStdout("Inside decoder_wrapper\n");
 	RELEASEGIL
 	int state=0;
 	int eternity =0;
 	{
 		eternity=1;
 	}
-	while((movie->loops>-1||eternity) )
+	while((movie->loops>-1||eternity) && !movie->stop )
 	{
 		movie->loops--;
 		movie=stream_open(movie, movie->filename, NULL, 1);
         { 
             break;
         }
+        if(movie->stop)
+        {
+        	break;
+        }
         if (movie->paused != movie->last_paused) {
             movie->last_paused = movie->paused;
             if (movie->paused)
                 seek_target= av_rescale_q(seek_target, AV_TIME_BASE_Q, ic->streams[stream_index]->time_base);
             }
 
-            ret = av_seek_frame(movie->ic, stream_index, seek_target, movie->seek_flags);
+            ret = av_seek_frame(movie->ic, stream_index, seek_target, movie->seek_flags|AVSEEK_FLAG_ANY);
             if (ret < 0) {
                 PyErr_Format(PyExc_IOError, "%s: error while seeking", movie->ic->filename);
             }else{
-                if (movie->audio_stream >= 0) {
+            	//this is done because for some reason, the movie "loses" the values in these variables
+            	int aud_stream = movie->audio_stream;
+            	int vid_stream = movie->video_stream;
+            	
+                if (aud_stream >= 0) {
                     packet_queue_flush(&movie->audioq);
                     packet_queue_put(&movie->audioq, &flush_pkt);
                 }
-                if (movie->subtitle_stream >= 0) {
-		            packet_queue_flush(&movie->subtitleq);
-                    packet_queue_put(&movie->subtitleq, &flush_pkt);
-                }
-                if (movie->video_stream >= 0) {
+                if (vid_stream >= 0) {
                     packet_queue_flush(&movie->videoq);
                     packet_queue_put(&movie->videoq, &flush_pkt);
                 }
+                movie->audio_stream = aud_stream;
+            	movie->video_stream = vid_stream;
+            	
             }
             movie->seek_req = 0;
         }
 	SDL_cond *audio_sig;
 	SDL_mutex *audio_mutex;
 	SDL_Thread *audio_tid;
+	int channel;
 	
 	/* Frame/Video Management members */
     int frame_count;
 }
 int stopBuffer (int channel)
 {
+	if(!channel)
+		return 0;
 	Mix_Chunk *mix;
 	mix = Mix_GetChunk(channel);
 	Mix_HaltChannel(channel);
 }
 int pauseBuffer(int channel)
 {
+	if(!channel)
+		return 0;
 	int paused = Mix_Paused(channel);
 	if(paused)
 	{
     {
     	loops = 0;
     }
+	//movie has been stopped, need to close down streams and that.
+    if(movie->stop)
+    {
+    	if(movie->video_st)
+			stream_component_close(movie, movie->video_st->index);
+		if(movie->audio_st)
+			stream_component_close(movie, movie->audio_st->index);
+		movie->stop = 0;
+    }
+    
     SDL_LockMutex(movie->dest_mutex);
     movie->loops =loops;
     movie->paused = 0;
 {
     Py_INCREF(movie);
     SDL_LockMutex(movie->dest_mutex);
+	//PyThreadState_Swap(NULL);
+    PyEval_ReleaseLock();
     stream_pause(movie);
+    PyEval_AcquireLock();
     movie->seek_req = 1;
     movie->seek_pos = 0;
     movie->seek_flags =AVSEEK_FLAG_BACKWARD;
 	{
 		movie->canon_surf=PySurface_AsSurface(surface);
 		movie->overlay=0;
-		return 1;
+		return 0;
 	}
 	return -1;
 }