Author: rolf
Date: 2008-01-25 06:57:56 -0500 (Fri, 25 Jan 2008)
New Revision: 93887

Modified:
   trunk/moon/src/ChangeLog
   trunk/moon/src/media.cpp
   trunk/moon/src/media.h
   trunk/moon/src/mplayer.cpp
   trunk/moon/src/mplayer.h
   trunk/moon/src/pipeline-ffmpeg.cpp
   trunk/moon/src/pipeline.cpp
   trunk/moon/src/pipeline.h
   trunk/moon/src/src.mdp
Log:
* src.mdp: Updated.
* pipeline-ffmpeg.cpp: Commented out printf.
* pipeline.h, pipeline.cpp: Removed some dead code. Made all interfaces
  inherit from IMediaObject, and put the media field there. Commented
  out printfs.
* mplayer.h, mplayer.cpp: Unified Mute and UnMute into SetMuted to
  simplify code a bit. Encapsulated target_pts handling into separate
  methods.
* media.h, media.cpp: Create one and only one MediaPlayer for the
  life-time of the MediaElement. If not AutoPlay is set, then pause
  instead of play.

Modified: trunk/moon/src/ChangeLog
===================================================================
--- trunk/moon/src/ChangeLog    2008-01-25 11:49:22 UTC (rev 93886)
+++ trunk/moon/src/ChangeLog    2008-01-25 11:57:56 UTC (rev 93887)
@@ -1,5 +1,17 @@
 2008-01-25  Rolf Bjarne Kvinge <[EMAIL PROTECTED]> 
 
+       * src.mdp: Updated.
+       * pipeline-ffmpeg.cpp: Commented out printf.
+       * pipeline.h, pipeline.cpp: Removed some dead code. Made all interfaces
+         inherit from IMediaObject, and put the media field there. Commented 
out
+         printfs.
+       * mplayer.h, mplayer.cpp: Unified Mute and UnMute into SetMuted to 
simplify
+         code a bit. Encapsulated target_pts handling into separate methods.
+       * media.h, media.cpp: Create one and only one MediaPlayer for the 
life-time
+         of the MediaElement. If not AutoPlay is set, then pause instead of 
play.
+
+2008-01-25  Rolf Bjarne Kvinge <[EMAIL PROTECTED]> 
+
        * pipeline.cpp, pipeline.h: Media: Don't create the worker thread upon 
first
          usage, but on construction, and finish it upon destruction. Fixes race
          between creating and destructing the thread.

Modified: trunk/moon/src/media.cpp
===================================================================
--- trunk/moon/src/media.cpp    2008-01-25 11:49:22 UTC (rev 93886)
+++ trunk/moon/src/media.cpp    2008-01-25 11:57:56 UTC (rev 93887)
@@ -456,25 +456,37 @@
        mplayer = NULL;
        loaded = false;
        
-       Cleanup (true);
+       Cleanup ();
+       
+       mplayer = new MediaPlayer ();
+       
+       SetValue (MediaElement::AttributesProperty, Value::CreateUnref (new 
MediaAttributeCollection ()));              
+       SetValue (MediaElement::MarkersProperty, Value::CreateUnref (new 
TimelineMarkerCollection ()));
 }
 
-void
-MediaElement::Cleanup (bool recreate)
+MediaElement::~MediaElement ()
 {
+       Cleanup ();
+       
+       delete mplayer;
+}
+
+void
+MediaElement::Cleanup ()
+{      
+       if (mplayer != NULL)
+               mplayer->Close ();
+
        if (media != NULL) {
+               // Media will delete its source upon destruction
+               // so clear out our reference if they're the same.
                if (media->GetSource () == downloaded_file)
                        downloaded_file = NULL;
                
-               if (mplayer != NULL && mplayer->media != media)
-                       delete media;
-               
+               delete media;
                media = NULL;
        }
        
-       delete mplayer;
-       mplayer = NULL;
-       
        state = Closed;
        previous_state = Closed;
        
@@ -498,53 +510,34 @@
        
        g_free (part_name);
        part_name = NULL;
-/*     
-       delete source;
-       source = NULL;
-*/
+
        if (streamed_markers)
                streamed_markers->unref ();
        streamed_markers = NULL;
+
+       Value *val;
+       val = GetValue (MediaElement::MarkersProperty);
+       if (val != NULL && val->AsCollection () != NULL)
+               val->AsCollection ()->Clear ();
        
-       if (recreate) {
-               mplayer = new MediaPlayer ();
-               mplayer->SetBalance (media_element_get_balance (this));
-               mplayer->SetVolume (media_element_get_volume (this));
-               
-               MediaAttributeCollection *attrs = new MediaAttributeCollection 
();
-               media_element_set_attributes (this, attrs);
-               attrs->unref ();
-               
-               TimelineMarkerCollection *markers = new 
TimelineMarkerCollection ();
-               media_element_set_markers (this, markers);
-               markers->unref ();
-       }
+       val = GetValue (MediaElement::AttributesProperty);
+       if (val != NULL && val->AsCollection () != NULL)
+               val->AsCollection ()->Clear ();
 }
 
 void
 MediaElement::DownloaderAbort ()
 {
        if (downloader) {
-               //Value *value = downloader->GetValue (Downloader::UriProperty);
-               //printf ("aborting downloader for %s\n", value ? 
value->AsString () : "(null)");
                downloader_abort (downloader);
                downloader->unref ();
                downloader = NULL;
        }
 }
 
-MediaElement::~MediaElement ()
-{
-       Cleanup (false);
-}
-
 void
 MediaElement::MediaOpened (Media *media)
 {
-       if (mplayer != NULL)
-               delete mplayer;
-       
-       mplayer = new MediaPlayer ();
        mplayer->Open (media);
        this->media = media;
        
@@ -557,6 +550,10 @@
        media_element_set_natural_video_height (this, mplayer->height);
        media_element_set_natural_video_width (this, mplayer->width);
        
+       mplayer->SetMuted (media_element_get_is_muted (this));
+       mplayer->SetVolume (media_element_get_volume (this));
+       mplayer->SetBalance (media_element_get_balance (this));
+
        Emit (MediaElement::MediaOpenedEvent);
 }
 
@@ -652,7 +649,7 @@
 }
 
 
-// TODO: make BUFFERING_SIZE configurable
+// TODO: Honor BufferTimeProperty and calculate the buffering size from that.
 #define BUFFERING_SIZE (1024 * 1024)
 
 
@@ -917,7 +914,10 @@
        case Buffering:
                // Media finished downloading before the buffering time was 
reached.
                // Play it.
-               Play ();
+               if (media_element_get_auto_play (this) || play_pending)
+                       Play ();
+               else
+                       Pause ();
                break;
        case Opening:
                // The media couldn't be buffered for some reason
@@ -936,7 +936,7 @@
 {
        g_return_if_fail (dl->GetObjectType () == Type::DOWNLOADER);
                
-       Cleanup (true);
+       Cleanup ();
        
        downloader = (Downloader *) dl;
        downloader->ref ();
@@ -1152,11 +1152,7 @@
        } else if (prop == MediaElement::CurrentStateProperty) {
                Emit (CurrentStateChangedEvent);
        } else if (prop == MediaElement::IsMutedProperty) {
-               bool muted = media_element_get_is_muted (this);
-               if (!muted)
-                       mplayer->UnMute ();
-               else
-                       mplayer->Mute ();
+               mplayer->SetMuted (media_element_get_is_muted (this));
        } else if (prop == MediaElement::MarkersProperty) {
                // FIXME: keep refs to these?
        } else if (prop == MediaElement::NaturalDurationProperty) {

Modified: trunk/moon/src/media.h
===================================================================
--- trunk/moon/src/media.h      2008-01-25 11:49:22 UTC (rev 93886)
+++ trunk/moon/src/media.h      2008-01-25 11:57:56 UTC (rev 93887)
@@ -244,7 +244,7 @@
        //MediaSource *source;
        char *part_name;
        
-       void Cleanup (bool recreate);
+       void Cleanup ();
        void DataWrite (void *data, int32_t offset, int32_t n);
        void DownloaderAbort ();
        void DownloaderComplete ();

Modified: trunk/moon/src/mplayer.cpp
===================================================================
--- trunk/moon/src/mplayer.cpp  2008-01-25 11:49:22 UTC (rev 93886)
+++ trunk/moon/src/mplayer.cpp  2008-01-25 11:57:56 UTC (rev 93887)
@@ -1,5 +1,5 @@
 /*
- * mplayer2.cpp: 
+ * mplayer.cpp: 
  *
  * Authors
  *     Jeffrey Stedfast <[EMAIL PROTECTED]>
@@ -215,7 +215,6 @@
        
        delete audio;
        delete video;
-       delete media;
 }
 
 MediaResult
@@ -283,7 +282,7 @@
        IMediaDecoder *encoding;
        IMediaStream *stream;
 
-       //printf ("MediaPlayer2::Open ().\n");
+       //printf ("MediaPlayer::Open ().\n");
 
        if (media == NULL) {
                printf ("MediaPlayer::Open (): media is NULL.\n");
@@ -362,14 +361,14 @@
                // 2 bytes per channel, we always calculate as 2-channel audio 
because it gets converted into such
                audio->pts_per_frame = (buf_size * 2 * 2) / 
(audio->stream->sample_rate / 100);
                
-               target_pts = audio->stream->start_time;
+               SetTargetPts (audio->stream->start_time);
                //printf ("initial pts (according to audio): %lld\n", 
target_pts);
        }
        
        if (HasVideo ()) {
                media_player_enqueue_frames (this, 0, 10);
                
-               target_pts = video->stream->start_time;
+               SetTargetPts (video->stream->start_time);
                //printf ("initial pts (according to video): %lld\n", 
target_pts);
        }
        
@@ -393,7 +392,6 @@
                video->surface = NULL;
        }
        
-       delete media;
        media = NULL;
        
        playing = false;
@@ -413,7 +411,6 @@
        current_pts = 0;
        target_pts = 0;
        start_pts = 0;
-//     seek_pts = 0;
        
        height = 0;
        width = 0;
@@ -476,17 +473,15 @@
                target_pts = start_pts + elapsed_pts;
                
                // (no need to lock since there's no audio thread) 
pthread_mutex_lock (&target_pts_lock);
-               this->target_pts = target_pts;
+               SetTargetPts (target_pts);
                // pthread_mutex_unlock (&target_pts_lock);
        } else if (!HasVideo ()) {
                // No video, return false if we've reached the end of the audio 
or true otherwise
                return !MediaEnded ();
        } else {
                // use target_pts as set by audio thread
-               pthread_mutex_lock (&target_pts_lock);
-               target_pts = this->target_pts;
+               target_pts = GetTargetPts ();
                //printf ("AdvanceFrame (), syncing to audio, target_pts: 
%lld\n", target_pts);
-               pthread_mutex_unlock (&target_pts_lock);
        }
        
        if (current_pts >= target_pts) {
@@ -710,7 +705,33 @@
                pthread_mutex_lock (&pause_mutex);
 }
 
+uint64_t
+MediaPlayer::GetTargetPts ()
+{
+       uint64_t result;
+       pthread_mutex_lock (&target_pts_lock);
+       result = target_pts;
+       pthread_mutex_unlock (&target_pts_lock);
+       return result;
+}
+
 void
+MediaPlayer::SetTargetPts (uint64_t pts)
+{
+       pthread_mutex_lock (&target_pts_lock);
+       target_pts = pts;
+       pthread_mutex_unlock (&target_pts_lock);
+}
+
+void
+MediaPlayer::IncTargetPts (uint64_t value)
+{
+       pthread_mutex_lock (&target_pts_lock);
+       target_pts += value;
+       pthread_mutex_unlock (&target_pts_lock);
+}
+
+void
 MediaPlayer::StopThreads ()
 {
        uint64_t initial_pts;
@@ -745,7 +766,6 @@
                initial_pts = 0;
        
        current_pts = initial_pts;
-       target_pts = initial_pts;
        
        stop = false;
        eof = false;
@@ -754,6 +774,7 @@
 void
 MediaPlayer::Stop ()
 {
+       //printf ("MediaPlayer::Stop (), paused = %s, opened = %s, playing = 
%s\n", paused ? "true" : "false", opened ? "true" : "false", playing ? "true" : 
"false");
        StopThreads ();
        
        playing = false;
@@ -762,6 +783,8 @@
                media->ClearQueue ();
                media->SeekAsync (0);
        }
+       
+       SetTargetPts (initial_pts);
 }
 
 bool
@@ -774,6 +797,7 @@
 void
 MediaPlayer::Seek (uint64_t position)
 {
+       //printf ("MediaPlayer::Seek (%lld), paused = %s, opened = %s, playing 
= %s, current position: %lld\n", position, paused ? "true" : "false", opened ? 
"true" : "false", playing ? "true" : "false", Position ());
        uint64_t initial_pts, duration;
        bool resume = !paused;
        
@@ -806,7 +830,7 @@
        }
        
        current_pts = position;
-       target_pts = position;
+       SetTargetPts (position);
        start_pts = position;
        
        if (HasVideo ()) {
@@ -841,7 +865,7 @@
 uint64_t
 MediaPlayer::Position ()
 {
-       uint64_t position = target_pts;
+       uint64_t position = GetTargetPts ();
        
        if (audio->pcm != NULL && HasAudio ())
                return position - audio->stream->start_time;
@@ -865,17 +889,11 @@
 }
 
 void
-MediaPlayer::Mute ()
+MediaPlayer::SetMuted (bool value)
 {
-       audio->muted = true;
+       audio->muted = value;
 }
 
-void
-MediaPlayer::UnMute ()
-{
-       audio->muted = false;
-}
-
 bool
 MediaPlayer::IsMuted ()
 {
@@ -1110,9 +1128,7 @@
                
                if ((frame_pts = audio_play (audio, ufds, ndfs)) > 0) {
                        // calculated pts
-                       pthread_mutex_lock (&mplayer->target_pts_lock);
-                       mplayer->target_pts += frame_pts;
-                       pthread_mutex_unlock (&mplayer->target_pts_lock);
+                       mplayer->IncTargetPts (frame_pts);
                } else {
                        if (!pkt && (pkt = (Packet *) audio->queue->Pop ())) {
                                // decode an audio packet
@@ -1125,10 +1141,7 @@
                                if (!frame->IsDecoded ())
                                        stream->decoder->DecodeFrame (frame);
                                
-                               pthread_mutex_lock (&mplayer->target_pts_lock);
-                               mplayer->target_pts = frame->pts;
-                               pthread_mutex_unlock 
(&mplayer->target_pts_lock);
-                               //printf ("setting target_pts to %llu\n", 
mplayer->target_pts);
+                               mplayer->SetTargetPts (frame->pts);
                                
                                inleft = frame->buflen;
                                inptr = frame->buffer;

Modified: trunk/moon/src/mplayer.h
===================================================================
--- trunk/moon/src/mplayer.h    2008-01-25 11:49:22 UTC (rev 93886)
+++ trunk/moon/src/mplayer.h    2008-01-25 11:57:56 UTC (rev 93887)
@@ -81,8 +81,7 @@
        uint64_t Position ();
        uint64_t Duration ();
        
-       void Mute ();
-       void UnMute ();
+       void SetMuted (bool muted);
        bool IsMuted ();
        
        int GetAudioStreamCount ();
@@ -95,9 +94,11 @@
        
        double GetVolume ();
        void SetVolume (double volume);
+       void SetTargetPts (uint64_t pts);
+       uint64_t GetTargetPts ();
+       void IncTargetPts (uint64_t value);
        
 private:
-       
        void StopThreads ();
 };
 

Modified: trunk/moon/src/pipeline-ffmpeg.cpp
===================================================================
--- trunk/moon/src/pipeline-ffmpeg.cpp  2008-01-25 11:49:22 UTC (rev 93886)
+++ trunk/moon/src/pipeline-ffmpeg.cpp  2008-01-25 11:57:56 UTC (rev 93887)
@@ -71,7 +71,7 @@
        : IMediaDecoder (media, stream),
        audio_buffer (NULL)
 {
-       printf ("FfmpegDecoder::FfmpegDecoder (%p, %p).\n", media, stream);
+       //printf ("FfmpegDecoder::FfmpegDecoder (%p, %p).\n", media, stream);
        
        if (stream->min_padding < FF_INPUT_BUFFER_PADDING_SIZE)
                stream->min_padding = FF_INPUT_BUFFER_PADDING_SIZE;

Modified: trunk/moon/src/pipeline.cpp
===================================================================
--- trunk/moon/src/pipeline.cpp 2008-01-25 11:49:22 UTC (rev 93886)
+++ trunk/moon/src/pipeline.cpp 2008-01-25 11:57:56 UTC (rev 93887)
@@ -324,7 +324,7 @@
 {
        MediaResult result;
        
-       printf ("Media::Open ().\n");
+       //printf ("Media::Open ().\n");
        
        if (source == NULL || IsOpened ()) // Initialize wasn't called (or 
didn't succeed) or already open.
                return MEDIA_INVALID_ARGUMENT;
@@ -354,9 +354,9 @@
                return result;
        }
        
-       printf ("Media::Open (): Found %i streams in this source.\n", 
demuxer->GetStreamCount ());
+       //printf ("Media::Open (): Found %i streams in this source.\n", 
demuxer->GetStreamCount ());
        
-       printf ("Media::Open (): Starting to select codecs...\n");
+       //printf ("Media::Open (): Starting to select codecs...\n");
        
        result = MEDIA_FAIL; // Only set to SUCCESS if at least 1 stream can be 
used
        
@@ -369,18 +369,18 @@
                const char* codec = stream->GetCodec ();
                IMediaDecoder* decoder = NULL;
                
-               printf ("Media::Open (): Selecting codec for codec %s, id 
%i.\n", codec, stream->codec_id);
+               //printf ("Media::Open (): Selecting codec for codec %s, id 
%i.\n", codec, stream->codec_id);
                
                DecoderInfo* current_decoder = registered_decoders;
                while (current_decoder != NULL && !current_decoder->Supports 
(codec)) {
-                       printf ("Checking registered decoder '%s' if it 
supports codec '%s': no.\n", current_decoder->GetName (), codec);
+                       //printf ("Checking registered decoder '%s' if it 
supports codec '%s': no.\n", current_decoder->GetName (), codec);
                        current_decoder = (DecoderInfo*) current_decoder->next;
                }
 
                if (current_decoder == NULL) {
                        AddMessage (MEDIA_UNKNOWN_CODEC, g_strdup_printf 
("Unknown codec: '%s'.", codec));      
                } else {
-                       printf ("Checking registered decoder '%s' if it 
supports codec '%s': yes.\n", current_decoder->GetName (), codec);
+                       //printf ("Checking registered decoder '%s' if it 
supports codec '%s': yes.\n", current_decoder->GetName (), codec);
                        decoder = current_decoder->Create (this, stream);
                }
 
@@ -400,14 +400,14 @@
                                
                                ConverterInfo* current_conv = 
registered_converters;
                                while (current_conv != NULL && 
!current_conv->Supports (decoder->pixel_format, MoonPixelFormatRGB32)) {
-                                       printf ("Checking registered converter 
'%s' if it supports input '%i' and output '%i': no.\n", current_conv->GetName 
(), decoder->pixel_format, MoonPixelFormatRGB32);
+                                       //printf ("Checking registered 
converter '%s' if it supports input '%i' and output '%i': no.\n", 
current_conv->GetName (), decoder->pixel_format, MoonPixelFormatRGB32);
                                        current_conv = (ConverterInfo*) 
current_conv->next;
                                }
 
                                if (current_conv == NULL) {
                                        AddMessage (MEDIA_UNKNOWN_CONVERTER, 
g_strdup_printf ("Can't convert from %i to %i: No converter found.", 
vs->decoder->pixel_format, MoonPixelFormatRGB32));    
                                } else {
-                                       printf ("Checking registered converter 
'%s' if it supports input '%i' and output '%i': yes.\n", current_conv->GetName 
(), decoder->pixel_format, MoonPixelFormatRGB32);
+                                       //printf ("Checking registered 
converter '%s' if it supports input '%i' and output '%i': yes.\n", 
current_conv->GetName (), decoder->pixel_format, MoonPixelFormatRGB32);
                                        converter = current_conv->Create (this, 
vs);
                                        converter->input_format = 
decoder->pixel_format;
                                        converter->output_format = 
MoonPixelFormatRGB32;
@@ -2324,7 +2324,7 @@
  * IMediaStream
  */
 
-IMediaStream::IMediaStream (Media *media)
+IMediaStream::IMediaStream (Media *media) : IMediaObject (media)
 {
        context = NULL;
        
@@ -2406,7 +2406,6 @@
  
 IMediaObject::IMediaObject (Media *media)
 {
-       this->callback = NULL;
        this->media = media;
 }
 
@@ -2434,9 +2433,8 @@
  * IMediaDecoder
  */
 
-IMediaDecoder::IMediaDecoder (Media *media, IMediaStream *stream)
+IMediaDecoder::IMediaDecoder (Media *media, IMediaStream *stream) : 
IMediaObject (media)
 {
-       this->media = media;
        this->stream = stream;
 }
 
@@ -2444,12 +2442,11 @@
  * IImageConverter
  */
 
-IImageConverter::IImageConverter (Media *media, VideoStream *stream)
+IImageConverter::IImageConverter (Media *media, VideoStream *stream) : 
IMediaObject (media)
 {
        output_format = MoonPixelFormatNone;
        input_format = MoonPixelFormatNone;
        this->stream = stream;
-       this->media = media;
 }
 
 /*

Modified: trunk/moon/src/pipeline.h
===================================================================
--- trunk/moon/src/pipeline.h   2008-01-25 11:49:22 UTC (rev 93886)
+++ trunk/moon/src/pipeline.h   2008-01-25 11:57:56 UTC (rev 93887)
@@ -390,8 +390,6 @@
 // Interfaces
 
 class IMediaObject {
-       MediaClosure *callback;
-       
 protected:
        Media *media;
        
@@ -399,14 +397,11 @@
        IMediaObject (Media *med);
        virtual ~IMediaObject ();
        
-       // Sets the callback to call when the frame is read
-       void SetFrameReadCallback (MediaClosure *callback);
-       
        Media *GetMedia () { return media; }
 };
 
 
-class IMediaStream  {
+class IMediaStream : public IMediaObject {
        void *context;
        bool enabled;
        
@@ -468,7 +463,7 @@
        }
 };
 
-class IMediaDecoder {
+class IMediaDecoder : public IMediaObject {
 public:
        IMediaDecoder (Media *media, IMediaStream *stream);
        virtual ~IMediaDecoder () {}
@@ -481,19 +476,17 @@
        
        MoonPixelFormat pixel_format; // The pixel format this codec outputs. 
Open () should fill this in.
        IMediaStream *stream;
-       Media *media;
 }; // Set when this is the callback in Media::GetNextFrameAsync
 
 
 /*
  * Inherit from this class to provide image converters (yuv->rgb for instance) 
  */
-class IImageConverter {
+class IImageConverter : public IMediaObject {
 public:
        MoonPixelFormat output_format;
        MoonPixelFormat input_format;
        VideoStream *stream;
-       Media *media;
        
        IImageConverter (Media *media, VideoStream *stream);
        virtual ~IImageConverter () {}
@@ -505,12 +498,9 @@
 // read data, with the possibility of returning a 'wait a bit, need more data 
first' error value. 
 // Another way is to always do the read/demux/decode stuff on another thread, 
 // in which case we can block here
-class IMediaSource {
-protected:
-       Media *media;
-       
+class IMediaSource : public IMediaObject {
 public:
-       IMediaSource (Media *med) : media (med) {}
+       IMediaSource (Media *media) : IMediaObject (media) {}
        virtual ~IMediaSource () {}
        
        // Initializes this stream (and if it succeeds, it can be read from 
later on).

Modified: trunk/moon/src/src.mdp
===================================================================
--- trunk/moon/src/src.mdp      2008-01-25 11:49:22 UTC (rev 93886)
+++ trunk/moon/src/src.mdp      2008-01-25 11:57:56 UTC (rev 93887)
@@ -115,8 +115,6 @@
     <File name="asf/asf.cpp" subtype="Code" buildaction="Compile" />
     <File name="asf/asf.cs" subtype="Code" buildaction="Nothing" />
     <File name="asf/asf.h" subtype="Code" buildaction="Nothing" />
-    <File name="asf/asf-ffmpeg.cpp" subtype="Code" buildaction="Compile" />
-    <File name="asf/asf-ffmpeg.h" subtype="Code" buildaction="Nothing" />
     <File name="asf/asf-generated.cpp" subtype="Code" buildaction="Compile" />
     <File name="asf/asf-generated.h" subtype="Code" buildaction="Nothing" />
     <File name="asf/asf-guids.cpp" subtype="Code" buildaction="Compile" />

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

Reply via email to