File indexing completed on 2024-04-21 15:38:24

0001 /**
0002   This file belong to the KMPlayer project, a movie player plugin for Konqueror
0003   Copyright (C) 2007  Koos Vriezen <koos.vriezen@gmail.com>
0004 
0005   This library is free software; you can redistribute it and/or
0006   modify it under the terms of the GNU Lesser General Public
0007   License as published by the Free Software Foundation; either
0008   version 2 of the License, or (at your option) any later version.
0009 
0010   This library is distributed in the hope that it will be useful,
0011   but WITHOUT ANY WARRANTY; without even the implied warranty of
0012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013   Lesser General Public License for more details.
0014 
0015   You should have received a copy of the GNU Lesser General Public
0016   License along with this library; if not, write to the Free Software
0017   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
0018 **/
0019 
0020 #ifndef _KMPLAYER__MEDIA_OBJECT_H_
0021 #define _KMPLAYER__MEDIA_OBJECT_H_
0022 
0023 #include <config-kmplayer.h>
0024 
0025 #include <qobject.h>
0026 #include <QtCore/QPair>
0027 #include <qmap.h>
0028 #include <qstring.h>
0029 #include <QMovie>
0030 #include <QList>
0031 
0032 #include "kmplayer_def.h"
0033 #include "kmplayerplaylist.h"
0034 
0035 class QMovie;
0036 class QImage;
0037 class QSvgRenderer;
0038 class QBuffer;
0039 class QByteArray;
0040 class KJob;
0041 namespace KIO {
0042     class Job;
0043 }
0044 
0045 namespace KMPlayer {
0046 
0047 class IViewer;
0048 class PartBase;
0049 class ProcessUser;
0050 class ProcessInfo;
0051 class MediaManager;
0052 class AudioVideoMedia;
0053 class PreferencesPage;
0054 class MediaObject;
0055 class CalculatedSizer;
0056 class Surface;
0057 
0058 
0059 class KMPLAYER_EXPORT IProcess {
0060 public:
0061     enum State { NotRunning = 0, Ready, Buffering, Playing, Paused };
0062 
0063     virtual ~IProcess () {}
0064 
0065     virtual bool ready () = 0;
0066     virtual bool play () = 0;
0067     virtual void pause () = 0;
0068     virtual void unpause () = 0;
0069     virtual bool grabPicture (const QString &file, int frame) = 0;
0070     virtual void stop () = 0;
0071     virtual void quit () = 0;
0072     /* seek (pos, abs) seek position in deci-seconds */
0073     virtual bool seek (int pos, bool absolute) = 0;
0074     /* volume from 0 to 100 */
0075     virtual void volume (int pos, bool absolute) = 0;
0076     virtual bool saturation (int pos, bool absolute) = 0;
0077     virtual bool hue (int pos, bool absolute) = 0;
0078     virtual bool contrast (int pos, bool absolute) = 0;
0079     virtual bool brightness (int pos, bool absolute) = 0;
0080     virtual void setAudioLang (int id) = 0;
0081     virtual void setSubtitle (int id) = 0;
0082     virtual bool running () const = 0;
0083 
0084     State state () const { return m_state; }
0085     ProcessUser *user;
0086     ProcessInfo *process_info;
0087 
0088 protected:
0089     IProcess (ProcessInfo *pinfo);
0090 
0091     State m_state;
0092 
0093 private:
0094     IProcess (const IViewer &);
0095 };
0096 
0097 class KMPLAYER_EXPORT ProcessUser {
0098 public:
0099     virtual ~ProcessUser () {}
0100 
0101     virtual void starting (IProcess *) = 0;
0102     virtual void stateChange (IProcess *, IProcess::State, IProcess::State) = 0;
0103     virtual void processDestroyed (IProcess*) = 0;
0104     virtual IViewer *viewer () = 0;
0105     virtual Mrl *getMrl () = 0;
0106 };
0107 
0108 class KMPLAYER_EXPORT ProcessInfo {
0109 public:
0110     ProcessInfo (const char *nm, const QString &lbl, const char **supported,
0111             MediaManager *, PreferencesPage *);
0112     virtual ~ProcessInfo ();
0113 
0114     bool supports (const char *source) const;
0115     virtual IProcess *create (PartBase*, ProcessUser*) = 0;
0116     virtual void quitProcesses () {};
0117 
0118     const char *name;
0119     QString label;
0120     const char **supported_sources;
0121     MediaManager *manager;
0122     PreferencesPage *config_page;
0123 };
0124 
0125 /*
0126  * Class that creates MediaObject and keeps track objects
0127  */
0128 class KMPLAYER_EXPORT MediaManager {
0129 public:
0130     enum MediaType { Any, Audio, AudioVideo, Image, Text, Data };
0131     typedef QMap <QString, ProcessInfo *> ProcessInfoMap;
0132     typedef QList <IProcess *> ProcessList;
0133     typedef QList <MediaObject *> MediaList;
0134 
0135     MediaManager (PartBase *player);
0136     ~MediaManager ();
0137 
0138     MediaObject *createAVMedia (Node *node, const QByteArray &b);
0139 
0140     // backend process state changed
0141     void stateChange (AudioVideoMedia *m, IProcess::State o, IProcess::State n);
0142     void playAudioVideo (AudioVideoMedia *m);
0143     void grabPicture (AudioVideoMedia *m);
0144 
0145     void processDestroyed (IProcess *process);
0146     ProcessInfoMap &processInfos () { return m_process_infos; }
0147     ProcessList &processes () { return m_processes; }
0148     ProcessInfoMap &recorderInfos () { return m_record_infos; }
0149     ProcessList &recorders () { return m_recorders; }
0150     MediaList &medias () { return m_media_objects; }
0151     PartBase *player () const { return m_player; }
0152 
0153 private:
0154     MediaList m_media_objects;
0155     ProcessInfoMap m_process_infos;
0156     ProcessList m_processes;
0157     ProcessInfoMap m_record_infos;
0158     ProcessList m_recorders;
0159     PartBase *m_player;
0160 };
0161 
0162 
0163 //------------------------%<----------------------------------------------------
0164 
0165 /*
0166  * Abstract base of MediaObject types, handles downloading
0167  */
0168 
0169 class KMPLAYER_NO_EXPORT DataCache : public QObject {
0170     Q_OBJECT
0171     typedef QMap <QString, QPair <QString, QByteArray> > DataMap;
0172     typedef QMap <QString, bool> PreserveMap;
0173     DataMap cache_map;
0174     PreserveMap preserve_map;
0175 public:
0176     DataCache () {}
0177     ~DataCache () {}
0178     void add (const QString &, const QString &, const QByteArray &);
0179     bool get (const QString &, QString &, QByteArray &);
0180     bool preserve (const QString &);
0181     bool unpreserve (const QString &);
0182     bool isPreserved (const QString &);
0183 signals:
0184     void preserveRemoved (const QString &); // ready or canceled
0185 };
0186 
0187 class KMPLAYER_EXPORT MediaObject : public QObject {
0188     Q_OBJECT
0189     friend class MediaManager;
0190 public:
0191     virtual MediaManager::MediaType type () const = 0;
0192 
0193     virtual bool play () = 0;
0194     virtual void pause () {}
0195     virtual void unpause () {}
0196     virtual void stop () {}
0197     virtual void destroy() KDE_NO_EXPORT;
0198 
0199     Mrl *mrl ();
0200 
0201 protected:
0202     MediaObject (MediaManager *manager, Node *node);
0203     virtual ~MediaObject ();
0204 
0205     MediaManager *m_manager;
0206     NodePtrW m_node;
0207 };
0208 
0209 //------------------------%<----------------------------------------------------
0210 
0211 class KMPLAYER_EXPORT MediaInfo : public QObject {
0212     Q_OBJECT
0213 public:
0214     MediaInfo (Node *node, MediaManager::MediaType type);
0215     ~MediaInfo ();
0216 
0217     bool wget(const QString& url, const QString& from_domain=QString());
0218     void killWGet() KDE_NO_EXPORT;
0219     void clearData() KDE_NO_EXPORT;
0220     QString mimetype() KDE_NO_EXPORT;
0221     bool downloading() const KDE_NO_EXPORT;
0222     void create ();
0223 
0224     QByteArray &rawData () { return data; }
0225     MediaObject *media;
0226     QString url;
0227     QByteArray data;
0228     QString mime;
0229     MediaManager::MediaType type;
0230 
0231 private slots:
0232     void slotResult(KJob*) KDE_NO_EXPORT;
0233     void slotData(KIO::Job*, const QByteArray& qb) KDE_NO_EXPORT;
0234     void slotMimetype (KIO::Job* job, const QString& mimestr) KDE_NO_EXPORT;
0235     void cachePreserveRemoved(const QString&) KDE_NO_EXPORT;
0236 
0237 private:
0238     void ready() KDE_NO_EXPORT;
0239     bool readChildDoc() KDE_NO_EXPORT;
0240     void setMimetype(const QString&) KDE_NO_EXPORT;
0241 
0242     Node *node;
0243     KIO::Job *job;
0244     QString cross_domain;
0245     QString access_from;
0246     bool preserve_wait;
0247     bool check_access;
0248 };
0249 
0250 //------------------------%<----------------------------------------------------
0251 
0252 /*
0253  * MediaObject for audio/video, groups Mrl, Process and Viewer
0254  */
0255 
0256 typedef unsigned long WindowId;
0257 
0258 class AudioVideoMedia;
0259 
0260 class KMPLAYER_NO_EXPORT IViewer {
0261 public:
0262     enum Monitor {
0263         MonitorNothing = 0, MonitorMouse = 1, MonitorKey = 2 , MonitorAll = 3
0264     };
0265     IViewer () {}
0266     virtual ~IViewer () {}
0267 
0268     virtual WindowId windowHandle () = 0;
0269     virtual WindowId clientHandle () = 0;
0270     virtual WindowId ownHandle() = 0;
0271     virtual void setGeometry (const IRect &rect) = 0;
0272     virtual void setAspect (float a) = 0;
0273     virtual float aspect () = 0;
0274     virtual void useIndirectWidget (bool) = 0;
0275     virtual void setMonitoring (Monitor) = 0;
0276     virtual void map () = 0;
0277     virtual void unmap () = 0;
0278     virtual void embedded(WindowId handle) = 0;
0279 private:
0280     IViewer (const IViewer &);
0281 };
0282 
0283 class KMPLAYER_NO_EXPORT AudioVideoMedia : public MediaObject, ProcessUser {
0284     friend class MediaManager;
0285 public:
0286     enum Request {
0287         ask_nothing, ask_play, ask_pause, ask_grab, ask_stop, ask_delete
0288     };
0289 
0290     AudioVideoMedia (MediaManager *manager, Node *node);
0291 
0292     MediaManager::MediaType type () const { return MediaManager::AudioVideo; }
0293 
0294     virtual bool play ();
0295     virtual bool grabPicture (const QString &file, int frame);
0296     virtual void stop ();
0297     virtual void pause ();
0298     virtual void unpause ();
0299     virtual void destroy ();
0300 
0301     virtual void starting (IProcess *);
0302     virtual void stateChange (IProcess *, IProcess::State, IProcess::State);
0303     virtual void processDestroyed (IProcess *p);
0304     virtual IViewer *viewer ();
0305     virtual Mrl *getMrl ();
0306 
0307     void setViewer (IViewer *v) { m_viewer = v; }
0308 
0309     IProcess *process;
0310     IViewer *m_viewer;
0311     QString m_grab_file;
0312     int m_frame;
0313     Request request;
0314 
0315 protected:
0316     ~AudioVideoMedia ();
0317 };
0318 
0319 
0320 //------------------------%<----------------------------------------------------
0321 
0322 /*
0323  * MediaObject for (animated)images
0324  */
0325 
0326 struct KMPLAYER_NO_EXPORT ImageData {
0327     enum ImageFlags {
0328         ImageAny=0, ImagePixmap=0x1, ImageAnimated=0x2, ImageScalable=0x4
0329     };
0330     ImageData( const QString & img);
0331     ~ImageData();
0332     void setImage (QImage *img);
0333 #ifdef KMPLAYER_WITH_CAIRO
0334     void copyImage (Surface *s, const SSize &sz, cairo_surface_t *similar, CalculatedSizer *zoom=NULL);
0335 #endif
0336     bool isEmpty () const {
0337         return !image
0338 #ifdef KMPLAYER_WITH_CAIRO
0339             && !surface
0340 #endif
0341             ;
0342     }
0343 
0344     unsigned short width;
0345     unsigned short height;
0346     short flags;
0347     bool has_alpha;
0348 private:
0349     QImage *image;
0350 #ifdef KMPLAYER_WITH_CAIRO
0351     cairo_surface_t *surface;
0352 #endif
0353     QString url;
0354 };
0355 
0356 typedef SharedPtr <ImageData> ImageDataPtr;
0357 typedef WeakPtr <ImageData> ImageDataPtrW;
0358 
0359 class KMPLAYER_NO_EXPORT ImageMedia : public MediaObject {
0360     Q_OBJECT
0361 public:
0362     ImageMedia (MediaManager *manager, Node *node,
0363             const QString &url, const QByteArray &data);
0364     ImageMedia (Node *node, ImageDataPtr id = NULL);
0365 
0366     MediaManager::MediaType type () const { return MediaManager::Image; }
0367 
0368     bool play ();
0369     void stop ();
0370     void pause ();
0371     void unpause ();
0372 
0373     bool wget (const QString &url);
0374     bool isEmpty () const;
0375     void render (const ISize &size);
0376     void sizes (SSize &size);
0377     void updateRender ();
0378 
0379     ImageDataPtr cached_img;
0380 
0381 private slots:
0382     void svgUpdated();
0383     void movieUpdated (const QRect &);
0384     void movieStatus (QMovie::MovieState);
0385     void movieResize (const QSize &);
0386 
0387 protected:
0388     ~ImageMedia ();
0389 
0390 private:
0391     void setupImage (const QString &url);
0392 
0393     QByteArray data;
0394     QBuffer *buffer;
0395     QMovie *img_movie;
0396     QSvgRenderer *svg_renderer;
0397     int frame_nr;
0398     bool update_render;
0399     bool paused;
0400 };
0401 
0402 //------------------------%<----------------------------------------------------
0403 
0404 /*
0405  * MediaObject for text
0406  */
0407 class KMPLAYER_NO_EXPORT TextMedia : public MediaObject {
0408 public:
0409     TextMedia (MediaManager *manager, Node *node, const QByteArray &ba);
0410 
0411     MediaManager::MediaType type () const { return MediaManager::Text; }
0412 
0413     bool play ();
0414 
0415     QString text;
0416     static int defaultFontSize ();
0417 
0418 protected:
0419     ~TextMedia ();
0420 };
0421 
0422 } // namespace
0423 
0424 #endif