Author: fejj
Date: 2007-10-11 15:52:20 -0400 (Thu, 11 Oct 2007)
New Revision: 87355

Modified:
   trunk/moon/src/ChangeLog
   trunk/moon/src/list.cpp
   trunk/moon/src/list.h
   trunk/moon/src/mplayer.cpp
Log:
2007-10-11  Jeffrey Stedfast  <[EMAIL PROTECTED]>

        * mplayer.cpp: Updated to use the new Queue class.
        (AdvanceFrame): Accessing the queue directly as a linked list
        allows us to simplify the loop a little so that we don't have to
        keep a reference to the previous packet's decoded frame.

        * list.cpp: Added a new Queue class which will replace GAsyncQueue
        in mplayer.cpp.



Modified: trunk/moon/src/ChangeLog
===================================================================
--- trunk/moon/src/ChangeLog    2007-10-11 19:15:33 UTC (rev 87354)
+++ trunk/moon/src/ChangeLog    2007-10-11 19:52:20 UTC (rev 87355)
@@ -1,5 +1,15 @@
 2007-10-11  Jeffrey Stedfast  <[EMAIL PROTECTED]>
 
+       * mplayer.cpp: Updated to use the new Queue class.
+       (AdvanceFrame): Accessing the queue directly as a linked list
+       allows us to simplify the loop a little so that we don't have to
+       keep a reference to the previous packet's decoded frame.
+
+       * list.cpp: Added a new Queue class which will replace GAsyncQueue
+       in mplayer.cpp.
+
+2007-10-11  Jeffrey Stedfast  <[EMAIL PROTECTED]>
+
        * mplayer.cpp: Added ctors/dtors for Audio and Video structs.
        (AdvanceFrame): Keep a reference to the previous frame in case we
        reach the end of the queue.

Modified: trunk/moon/src/list.cpp
===================================================================
--- trunk/moon/src/list.cpp     2007-10-11 19:15:33 UTC (rev 87354)
+++ trunk/moon/src/list.cpp     2007-10-11 19:52:20 UTC (rev 87355)
@@ -329,6 +329,91 @@
        }
 }
 
+
+Queue::Queue ()
+{
+       pthread_mutex_init (&lock, NULL);
+       list = new List ();
+}
+
+Queue::~Queue ()
+{
+       pthread_mutex_destroy (&lock);
+       delete list;
+}
+
+bool
+Queue::IsEmpty ()
+{
+       bool empty;
+       
+       Lock ();
+       empty = list->IsEmpty ();
+       Unlock ();
+       
+       return empty;
+}
+
+int
+Queue::Length ()
+{
+       int length;
+       
+       Lock ();
+       length = list->Length ();
+       Unlock ();
+       
+       return length;
+}
+
+void
+Queue::Clear (bool freeNodes)
+{
+       Lock ();
+       list->Clear (freeNodes);
+       Unlock ();
+}
+
+void
+Queue::Push (List::Node *node)
+{
+       Lock ();
+       list->Append (node);
+       Unlock ();
+}
+
+List::Node *
+Queue::Pop ()
+{
+       List::Node *node;
+       
+       Lock ();
+       if ((node = list->First ()))
+               list->Unlink (node);
+       Unlock ();
+       
+       return node;
+}
+
+void
+Queue::Lock ()
+{
+       pthread_mutex_lock (&lock);
+}
+
+void
+Queue::Unlock ()
+{
+       pthread_mutex_unlock (&lock);
+}
+
+List *
+Queue::LinkedList ()
+{
+       return list;
+}
+
+
 //#define TEST_PROGRAM
 #ifdef TEST_PROGRAM
 

Modified: trunk/moon/src/list.h
===================================================================
--- trunk/moon/src/list.h       2007-10-11 19:15:33 UTC (rev 87354)
+++ trunk/moon/src/list.h       2007-10-11 19:52:20 UTC (rev 87355)
@@ -12,7 +12,9 @@
 #ifndef __LIST_H__
 #define __LIST_H__
 
+#include <pthread.h>
 
+
 class List {
 public:
        class Node {
@@ -26,7 +28,7 @@
        };
        
        typedef bool (* NodeAction) (Node *node, void *data);
-       
+
 protected:
        Node *head;
        Node *tail;
@@ -60,8 +62,35 @@
        
        int IndexOf (Node *node);
        int IndexOf (NodeAction find, void *data);
-
+       
        void ForEach (NodeAction action, void *data);
 };
 
+class Queue {
+protected:
+       pthread_mutex_t lock;
+       List *list;
+       
+public:
+       
+       Queue ();
+       ~Queue ();
+       
+       // convenience properties
+       bool IsEmpty ();
+       int Length ();
+       
+       // convenience methods
+       void Clear (bool freeNodes);
+       
+       void Push (List::Node *node);
+       List::Node *Pop ();
+       
+       void Lock ();
+       void Unlock ();
+       
+       // accessing the internal linked list directly requires manual 
Locking/Unlocking.
+       List *LinkedList ();
+};
+
 #endif /* __LIST_H__ */

Modified: trunk/moon/src/mplayer.cpp
===================================================================
--- trunk/moon/src/mplayer.cpp  2007-10-11 19:15:33 UTC (rev 87354)
+++ trunk/moon/src/mplayer.cpp  2007-10-11 19:52:20 UTC (rev 87355)
@@ -34,6 +34,7 @@
 
 #include "mplayer.h"
 //#include "stream.h"
+#include "list.h"
 
 #if GLIB_SIZEOF_VOID_P == 8
 #define ALIGN(addr,size) (uint8_t *) (((uint64_t) (((uint8_t *) (addr)) + 
(size) - 1)) & ~((size) - 1))
@@ -45,16 +46,37 @@
 
 #define SET_VOLUME(chdata, volume) CLAMP (((((int32_t) (chdata)) * volume) >> 
13), SHRT_MIN, SHRT_MAX)
 
-struct Packet {
+class Packet : public List::Node {
+public:
        int stream_id;
        int duration;
        int64_t pts;
        size_t size;
-       uint8_t data[1];
+       uint8_t *data;
+       
+       Packet (AVPacket *pkt);
+       ~Packet ();
 };
 
+Packet::Packet (AVPacket *pkt)
+{
+       stream_id = pkt->stream_index;
+       duration = pkt->duration;
+       size = pkt->size;
+       pts = pkt->pts;
+       
+       data = (uint8_t *) g_malloc (size);
+       memcpy (data, pkt->data, size);
+}
+
+Packet::~Packet ()
+{
+       g_free (data);
+}
+
+
 struct Audio {
-       GAsyncQueue *queue;
+       Queue *queue;
        
        pthread_mutex_t init_mutex;
        pthread_cond_t init_cond;
@@ -96,7 +118,7 @@
        pthread_mutex_init (&init_mutex, NULL);
        pthread_cond_init (&init_cond, NULL);
        
-       queue = g_async_queue_new ();
+       queue = new Queue ();
        
        pkt = NULL;
        inleft = 0;
@@ -131,11 +153,11 @@
        if (pcm != NULL)
                snd_pcm_close (pcm);
        
-       g_async_queue_unref (queue);
+       delete queue;
 }
 
 struct Video {
-       GAsyncQueue *queue;
+       Queue *queue;
        
        // input
        AVStream *stream;
@@ -158,7 +180,7 @@
 
 Video::Video ()
 {
-       queue = g_async_queue_new ();
+       queue = new Queue ();
        
        stream = NULL;
        codec = NULL;
@@ -175,7 +197,7 @@
 
 Video::~Video ()
 {
-       g_async_queue_unref (queue);
+       delete queue;
 }
 
 
@@ -183,10 +205,6 @@
 
 static void ffmpeg_init (void);
 
-// packets in a/v queues
-static Packet *pkt_new (AVPacket *avpkt);
-#define pkt_free(x) g_free (x)
-
 // threads
 static void *audio_loop (void *data);
 static void *io_loop (void *data);
@@ -450,10 +468,10 @@
 MediaPlayer::AdvanceFrame ()
 {
        AVFrame *frame = NULL;
-       AVFrame *prev = NULL;
        bool update = false;
        int64_t target_pts;
-       Packet *pkt;
+       Packet *pkt, *npkt;
+       List *list;
        int redraw;
        
        if (paused) {
@@ -490,46 +508,52 @@
        }
        
        if (current_pts >= seek_pts && current_pts >= target_pts)
-               return g_async_queue_length (video->queue) > 0;
+               return !video->queue->IsEmpty ();
        
-       while ((pkt = (Packet *) g_async_queue_try_pop (video->queue))) {
-               // always decode the frame or we get glitches in the screen
-               frame = avcodec_alloc_frame ();
-               
-               redraw = 0;
-               avcodec_decode_video (video->stream->codec, frame,
-                                     &redraw, pkt->data, pkt->size);
-               
-               update = update || redraw;
-               
-               current_pts = pkt->pts;
-               pkt_free (pkt);
-               
-               if (current_pts >= target_pts) {
-                       // we are in sync (or ahead) of audio playback
-                       break;
-               }
-               
-               // we are lagging behind, drop this frame
-               
-               if (prev)
-                       av_free (prev);
-               
-               prev = frame;
-               frame = NULL;
-       }
+       video->queue->Lock ();
        
-       if (!frame) {
-               frame = prev;
-               prev = NULL;
+       list = video->queue->LinkedList ();
+       
+       if ((pkt = (Packet *) list->First ())) {
+               do {
+                       // always decode the frame or we get glitches in the 
screen
+                       frame = avcodec_alloc_frame ();
+                       
+                       redraw = 0;
+                       avcodec_decode_video (video->stream->codec, frame,
+                                             &redraw, pkt->data, pkt->size);
+                       
+                       update = update || redraw;
+                       
+                       current_pts = pkt->pts;
+                       
+                       npkt = (Packet *) pkt->next;
+                       list->Unlink (pkt);
+                       delete pkt;
+                       
+                       if (!npkt) {
+                               // no more packets in queue, this frame is the 
most recent we have available
+                               break;
+                       }
+                       
+                       if (current_pts >= target_pts) {
+                               // we are in sync (or ahead) of audio playback
+                               break;
+                       }
+                       
+                       // we are lagging behind, drop this frame
+                       av_free (frame);
+                       frame = NULL;
+                       
+                       pkt = npkt;
+               } while (pkt);
        }
        
+       video->queue->Unlock ();
+       
        if (update)
                convert_to_rgb (video, frame);
        
-       if (prev != NULL)
-               av_free (prev);
-       
        if (frame != NULL) {
                av_free (frame);
                return true;
@@ -613,10 +637,10 @@
        if (!eof)
                return false;
        
-       if ((audio->queue && g_async_queue_length (audio->queue) > 0))
+       if ((audio->queue && !audio->queue->IsEmpty ()))
                return false;
        
-       if ((video->queue && g_async_queue_length (video->queue) > 0))
+       if ((video->queue && !video->queue->IsEmpty ()))
                return false;
        
        return true;
@@ -690,7 +714,6 @@
 MediaPlayer::StopThreads ()
 {
        int64_t initial_pts;
-       Packet *pkt;
        
        stop = true;
        
@@ -712,19 +735,16 @@
        
        // de-queue audio/video packets
        if (audio->pkt != NULL) {
-               pkt_free (audio->pkt);
+               delete audio->pkt;
+               audio->pkt = NULL;
+               
                audio->inptr = NULL;
                audio->inleft = 0;
-               audio->pkt = NULL;
-               audio->inleft = 0;
        }
        
-       while ((pkt = (Packet *) g_async_queue_try_pop (audio->queue)))
-               pkt_free (pkt);
+       audio->queue->Clear (true);
+       video->queue->Clear (true);
        
-       while ((pkt = (Packet *) g_async_queue_try_pop (video->queue)))
-               pkt_free (pkt);
-       
        // enter paused state
        pthread_mutex_lock (&pause_mutex);
        paused = true;
@@ -939,23 +959,7 @@
        ffmpeg_initialized = true;
 }
 
-static Packet *
-pkt_new (AVPacket *av_pkt)
-{
-       Packet *pkt;
-       
-       pkt = (Packet *) g_malloc (sizeof (Packet) + av_pkt->size);
-       pkt->stream_id = av_pkt->stream_index;
-       pkt->duration = av_pkt->duration;
-       pkt->size = av_pkt->size;
-       pkt->pts = av_pkt->pts;
-       
-       memcpy (pkt->data, av_pkt->data, av_pkt->size);
-       
-       return pkt;
-}
 
-
 // Audio playback thread
 
 // Returns true if finished decoding, false otherwise
@@ -1184,7 +1188,7 @@
                        //printf ("calculated target_pts = %llu\n", 
mplayer->target_pts);
                } else {
                        // decode an audio packet
-                       if (!audio->pkt && (pkt = (Packet *) 
g_async_queue_try_pop (audio->queue))) {
+                       if (!audio->pkt && (pkt = (Packet *) audio->queue->Pop 
())) {
                                audio->inleft = pkt->size;
                                audio->inptr = pkt->data;
                                audio->pkt = pkt;
@@ -1196,7 +1200,7 @@
                        }
                        
                        if (audio->pkt && audio_decode (audio)) {
-                               pkt_free (audio->pkt);
+                               delete audio->pkt;
                                audio->pkt = NULL;
                        }
                }
@@ -1216,7 +1220,7 @@
 io_loop (void *data)
 {
        MediaPlayer *mplayer = (MediaPlayer *) data;
-       GAsyncQueue *audio_q, *video_q;
+       Queue *audio_q, *video_q;
        int audio_id, video_id;
        AVPacket pkt;
        
@@ -1227,8 +1231,8 @@
        video_q = video_id != -1 ? mplayer->video->queue : NULL;
        
        while (!mplayer->stop) {
-               if ((!audio_q || g_async_queue_length (audio_q) > 100) &&
-                   (!video_q || g_async_queue_length (video_q) > 100)) {
+               if ((!audio_q || audio_q->Length () > 100) &&
+                   (!video_q || video_q->Length () > 100)) {
                        // throttle ourselves
                        g_usleep (1000);
                        continue;
@@ -1242,10 +1246,10 @@
                
                if (pkt.stream_index == audio_id) {
                        if (audio_q)
-                               g_async_queue_push (audio_q, pkt_new (&pkt));
+                               audio_q->Push (new Packet (&pkt));
                } else if (pkt.stream_index == video_id) {
                        if (video_q)
-                               g_async_queue_push (video_q, pkt_new (&pkt));
+                               video_q->Push (new Packet (&pkt));
                } else {
                        // unhandled stream
                }

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to