Gnash 0.8.10dev
MediaParserGst.h
Go to the documentation of this file.
00001 // MediaParserGst.h: gstreamer media parsers, for Gnash
00002 // 
00003 //   Copyright (C) 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
00004 // 
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation; either version 3 of the License, or
00008 // (at your option) any later version.
00009 // 
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 
00019 #ifndef GNASH_MEDIAPARSER_GST_H
00020 #define GNASH_MEDIAPARSER_GST_H
00021 
00022 #include "MediaParser.h" // for inheritance
00023 
00024 #include <deque>
00025 #include <boost/scoped_array.hpp>
00026 #include <memory>
00027 #include <queue>
00028 
00029 #include <gst/gst.h>
00030 #include "ClockTime.h"
00031 
00032 
00033 // Forward declaration
00034 namespace gnash {
00035         class IOChannel;
00036 }
00037 
00038 namespace gnash {
00039 namespace media {
00040 namespace gst {
00041 
00043 struct ExtraInfoGst : public AudioInfo::ExtraInfo, VideoInfo::ExtraInfo,
00044         boost::noncopyable
00045 {
00046     ExtraInfoGst(GstCaps* gstcaps)
00047     :
00048         caps(gstcaps)
00049     {
00050         gst_caps_ref(caps);
00051     }
00052 
00053     ~ExtraInfoGst()
00054     {
00055         gst_caps_unref(caps);
00056     }
00057 
00058     GstCaps* caps;
00059 };
00060 
00062 struct EncodedExtraGstData : public EncodedExtraData, boost::noncopyable
00063 {
00064     EncodedExtraGstData(GstBuffer* buf)
00065     : buffer(buf)
00066     {
00067         gst_buffer_ref(buffer);
00068     }
00069     ~EncodedExtraGstData()
00070     {
00071         gst_buffer_unref(buffer);
00072     }
00073 
00074     GstBuffer* buffer;
00075 };
00076 
00077 
00079 //
00083 class SimpleTimer : public boost::noncopyable
00084 {
00085 public:
00086     SimpleTimer()
00087         : _start_time(clocktime::getTicks())
00088     {
00089     }
00090     
00091     bool expired() const
00092     {
00093         return (clocktime::getTicks() - _start_time) > 1000;
00094     }
00095 
00096 private:
00097     boost::uint64_t _start_time;
00098 };
00099 
00100 
00101 
00103 class MediaParserGst: public MediaParser
00104 {
00105 public:
00106 
00108     //
00111     MediaParserGst(std::auto_ptr<IOChannel> stream);
00112 
00113     ~MediaParserGst();
00114 
00115     // See dox in MediaParser.h
00116     bool seek(boost::uint32_t&);
00117 
00118     // See dox in MediaParser.h
00119     bool parseNextChunk();
00120 
00121     // See dox in MediaParser.h
00122     boost::uint64_t getBytesLoaded() const;
00123 
00124     void rememberAudioFrame(EncodedAudioFrame* frame);
00125     void rememberVideoFrame(EncodedVideoFrame* frame);
00126 
00127 private:
00128     bool foundAllStreams();
00129 
00130     bool probingConditionsMet(const SimpleTimer& timer);    
00131     
00132     void link_to_fakesink(GstPad* pad);
00133 
00134     static void cb_typefound (GstElement *typefind, guint probability,
00135                               GstCaps *caps, gpointer data);
00136                               
00137     static void cb_pad_added(GstElement* element,
00138         GstPad* new_pad, gpointer user_data);
00139     static void cb_no_more_pads (GstElement* element, gpointer data);
00140 
00141     static GstFlowReturn cb_chain_func_audio (GstPad *pad, GstBuffer *buffer);
00142     static GstFlowReturn cb_chain_func_video (GstPad *pad, GstBuffer *buffer);
00143 
00144     bool pushGstBuffer();
00145     bool emitEncodedFrames();
00146 
00147 
00148     GstElement* _bin;
00149     GstPad* _srcpad;
00150     GstPad* _audiosink;
00151     GstPad* _videosink;
00152     
00153     bool _demux_probe_ended;
00154 
00155     std::deque<EncodedAudioFrame*> _enc_audio_frames;
00156     std::deque<EncodedVideoFrame*> _enc_video_frames;
00157 };
00158 
00159 
00160 } // gnash.media.gst namespace
00161 } // gnash.media namespace 
00162 } // namespace gnash
00163 
00164 #endif // __MEDIAPARSER_GST_H__