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

0001 /* This file is part of the KDE project
0002  *
0003  * Copyright (C) 2003 Koos Vriezen <koos.vriezen@xs4all.nl>
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Library 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  * Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, write to
0017  * the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019  */
0020 
0021 #ifndef _KMPLAYERPROCESS_H_
0022 #define _KMPLAYERPROCESS_H_
0023 
0024 #include <qobject.h>
0025 #include <qstring.h>
0026 #include <qlist.h>
0027 #include <qbytearray.h>
0028 #include <qstringlist.h>
0029 #include <qregexp.h>
0030 #include <qprocess.h>
0031 
0032 #include <kio/global.h>
0033 
0034 #include "kmplayer_def.h"
0035 #include "kmplayerplaylist.h"
0036 #include "kmplayerpartbase.h"
0037 #include "mediaobject.h"
0038 
0039 class QWidget;
0040 class KJob;
0041 
0042 namespace KIO {
0043     class Job;
0044     class TransferJob;
0045 }
0046 
0047 namespace KMPlayer {
0048 
0049 class Settings;
0050 class View;
0051 class MediaManager;
0052 class Source;
0053 class Callback;
0054 class Backend_stub;
0055 class NpPlayer;
0056 class MPlayerPreferencesPage;
0057 class MPlayerPreferencesFrame;
0058 class XMLPreferencesPage;
0059 class XMLPreferencesFrame;
0060 
0061 
0062 /*
0063  * Base class for all backend processes
0064  */
0065 class KMPLAYER_EXPORT Process : public QObject, public IProcess {
0066     Q_OBJECT
0067 public:
0068     Process (QObject *parent, ProcessInfo *, Settings *settings);
0069     virtual ~Process ();
0070 
0071     virtual void init ();
0072     virtual void initProcess ();
0073     virtual void setAudioLang (int id);
0074     virtual void setSubtitle (int id);
0075     virtual bool running () const;
0076     KDE_NO_EXPORT QProcess * process () const { return m_process; }
0077     KDE_NO_EXPORT Source * source () const { return m_source; }
0078     View *view () const;
0079     WId widget ();
0080     void setSource (Source * src) { m_source = src; }
0081     void setState (IProcess::State newstate);
0082     virtual bool grabPicture (const QString &file, int frame) KDE_NO_EXPORT;
0083     Mrl *mrl () const;
0084 
0085     virtual bool ready ();
0086     virtual bool play ();
0087     virtual void stop ();
0088     virtual void quit ();
0089     virtual void pause ();
0090     virtual void unpause ();
0091     /* seek (pos, abs) seek position in deci-seconds */
0092     virtual bool seek (int pos, bool absolute);
0093     /* volume from 0 to 100 */
0094     virtual void volume (int pos, bool absolute);
0095     /* saturation/hue/contrast/brightness from -100 to 100 */
0096     virtual bool saturation (int pos, bool absolute);
0097     virtual bool hue (int pos, bool absolute);
0098     virtual bool contrast (int pos, bool absolute);
0099     virtual bool brightness (int pos, bool absolute);
0100 signals:
0101     void grabReady (const QString & path);
0102 protected slots:
0103     void rescheduledStateChanged () KDE_NO_EXPORT;
0104     void result (KJob *);
0105     void processStateChanged (QProcess::ProcessState);
0106 protected:
0107     virtual bool deMediafiedPlay ();
0108     virtual void terminateJobs ();
0109     void startProcess (const QString &program, const QStringList &args);
0110 
0111     Source * m_source;
0112     Settings * m_settings;
0113     State m_old_state;
0114     QProcess * m_process;
0115     KIO::Job * m_job;
0116     QString m_url;
0117     int m_request_seek;
0118     QProcess::ProcessState m_process_state;
0119 };
0120 
0121 
0122 /*
0123  * Base class for all MPlayer based processes
0124  */
0125 class MPlayerBase : public Process {
0126     Q_OBJECT
0127 public:
0128     MPlayerBase (QObject *parent, ProcessInfo *, Settings *);
0129     ~MPlayerBase ();
0130     void initProcess () KDE_NO_EXPORT;
0131     virtual void stop () KDE_NO_EXPORT;
0132     virtual void quit () KDE_NO_EXPORT;
0133 protected:
0134     bool sendCommand (const QString &) KDE_NO_EXPORT;
0135     bool removeQueued (const char *cmd);
0136     QList<QByteArray> commands;
0137     bool m_needs_restarted;
0138 protected slots:
0139     virtual void processStopped () KDE_NO_EXPORT;
0140 private slots:
0141     void dataWritten (qint64) KDE_NO_EXPORT;
0142     void processStopped (int, QProcess::ExitStatus) KDE_NO_EXPORT;
0143 };
0144 
0145 /*
0146  * MPlayer process
0147  */
0148 class KMPLAYER_NO_EXPORT MPlayerProcessInfo : public ProcessInfo {
0149 public:
0150     MPlayerProcessInfo (MediaManager *);
0151     virtual IProcess *create (PartBase*, ProcessUser*);
0152 };
0153 
0154 class KDE_EXPORT MPlayer : public MPlayerBase {
0155     Q_OBJECT
0156 public:
0157     MPlayer (QObject *parent, ProcessInfo *pinfo, Settings *settings) KDE_NO_CDTOR_EXPORT;
0158     ~MPlayer () KDE_NO_CDTOR_EXPORT;
0159 
0160     virtual void init () KDE_NO_EXPORT;
0161     virtual bool grabPicture (const QString &file, int pos) KDE_NO_EXPORT;
0162     virtual void setAudioLang (int id);
0163     virtual void setSubtitle (int id);
0164     virtual bool deMediafiedPlay () KDE_NO_EXPORT;
0165     virtual void stop () KDE_NO_EXPORT;
0166     virtual void pause () KDE_NO_EXPORT;
0167     virtual void unpause () KDE_NO_EXPORT;
0168     virtual bool seek (int pos, bool absolute) KDE_NO_EXPORT;
0169     virtual void volume (int pos, bool absolute) KDE_NO_EXPORT;
0170     virtual bool saturation (int pos, bool absolute) KDE_NO_EXPORT;
0171     virtual bool hue (int pos, bool absolute) KDE_NO_EXPORT;
0172     virtual bool contrast (int pos, bool absolute) KDE_NO_EXPORT;
0173     virtual bool brightness (int pos, bool absolute) KDE_NO_EXPORT;
0174     bool ready () KDE_NO_EXPORT;
0175 protected:
0176     void processStopped () KDE_NO_EXPORT;
0177 private slots:
0178     void processOutput () KDE_NO_EXPORT;
0179 private:
0180     QString m_process_output;
0181     QString m_grab_file;
0182     QString m_grab_dir;
0183     QWidget * m_widget;
0184     QString m_tmpURL;
0185     Source::LangInfoPtr alanglist;
0186     WeakPtr <Source::LangInfo> alanglist_end;
0187     Source::LangInfoPtr slanglist;
0188     WeakPtr <Source::LangInfo> slanglist_end;
0189     State m_transition_state;
0190     int aid, sid;
0191     int old_volume;
0192 };
0193 
0194 #ifdef _KMPLAYERCONFIG_H_
0195 /*
0196  * MPlayer preferences page
0197  */
0198 class KMPLAYER_NO_EXPORT MPlayerPreferencesPage : public PreferencesPage {
0199 public:
0200     enum Pattern {
0201         pat_size = 0, pat_cache, pat_pos, pat_index,
0202         pat_refurl, pat_ref, pat_start,
0203         pat_vcdtrack, pat_cdromtracks,
0204         pat_last
0205     };
0206     MPlayerPreferencesPage ();
0207     KDE_NO_CDTOR_EXPORT ~MPlayerPreferencesPage () {}
0208     void write (KSharedConfigPtr);
0209     void read (KSharedConfigPtr);
0210     void sync (bool fromUI);
0211     void prefLocation (QString & item, QString & icon, QString & tab);
0212     QFrame * prefPage (QWidget * parent);
0213     QRegExp m_patterns[pat_last];
0214     int cachesize;
0215     QString mplayer_path;
0216     QString additionalarguments;
0217     bool alwaysbuildindex;
0218 private:
0219     MPlayer * m_process;
0220     MPlayerPreferencesFrame *m_configframe;
0221 };
0222 
0223 #endif
0224 /*
0225  * Base class for all recorders
0226  */
0227 class KMPLAYER_NO_EXPORT RecordDocument : public SourceDocument {
0228 public:
0229     RecordDocument (const QString &url, const QString &rurl, const QString &rec,
0230                     Source *source);
0231 
0232     virtual void begin ();
0233     virtual void message (MessageType msg, void *);
0234     virtual void deactivate ();
0235 
0236     QString record_file;
0237     QString recorder;
0238 };
0239 
0240 /*
0241  * MEncoder recorder
0242  */
0243 class KMPLAYER_NO_EXPORT MEncoderProcessInfo : public ProcessInfo {
0244 public:
0245     MEncoderProcessInfo (MediaManager *);
0246     virtual IProcess *create (PartBase*, ProcessUser*);
0247 };
0248 
0249 class MEncoder : public MPlayerBase {
0250     Q_OBJECT
0251 public:
0252     MEncoder (QObject *parent, ProcessInfo *pinfo, Settings *settings);
0253     ~MEncoder ();
0254     virtual void init ();
0255     virtual bool deMediafiedPlay ();
0256     virtual void stop ();
0257 };
0258 
0259 /*
0260  * MPlayer recorder, runs 'mplayer -dumpstream'
0261  */
0262 class KMPLAYER_NO_EXPORT MPlayerDumpProcessInfo : public ProcessInfo {
0263 public:
0264     MPlayerDumpProcessInfo (MediaManager *);
0265     virtual IProcess *create (PartBase*, ProcessUser*);
0266 };
0267 
0268 class KMPLAYER_NO_EXPORT MPlayerDumpstream : public MPlayerBase {
0269     Q_OBJECT
0270 public:
0271     MPlayerDumpstream (QObject *parent, ProcessInfo *pinfo, Settings *settings);
0272     ~MPlayerDumpstream ();
0273     virtual void init ();
0274     virtual bool deMediafiedPlay ();
0275     virtual void stop ();
0276 };
0277 
0278 class KMPLAYER_NO_EXPORT MasterProcessInfo : public QObject, public ProcessInfo {
0279     Q_OBJECT
0280 public:
0281     MasterProcessInfo (const char *nm, const QString &lbl,
0282             const char **supported,MediaManager *, PreferencesPage *);
0283     ~MasterProcessInfo ();
0284 
0285     virtual void quitProcesses ();
0286 
0287     void running (const QString &srv);
0288 
0289     QString m_service;
0290     QString m_path;
0291     QString m_slave_service;
0292     QProcess *m_slave;
0293 
0294 private slots:
0295     void slaveStopped (int, QProcess::ExitStatus);
0296     void slaveOutput ();
0297 
0298 protected:
0299     virtual void initSlave ();
0300     virtual bool startSlave () = 0;
0301     virtual void stopSlave ();
0302 };
0303 
0304 class KMPLAYER_NO_EXPORT MasterProcess : public Process {
0305     Q_OBJECT
0306 public:
0307     MasterProcess (QObject *p, ProcessInfo *pi, Settings *s);
0308     ~MasterProcess ();
0309 
0310     virtual void init ();
0311     virtual bool deMediafiedPlay ();
0312     virtual bool running () const;
0313 
0314     void streamInfo (uint64_t length, double aspect);
0315     void streamMetaInfo (QString info);
0316     void loading (int p);
0317     void playing ();
0318     void progress (uint64_t pos);
0319     void pause ();
0320     void unpause ();
0321     bool seek (int pos, bool absolute);
0322     void volume (int pos, bool absolute);
0323     void eof ();
0324     void stop ();
0325 
0326 private:
0327     QString m_slave_path;
0328 };
0329 
0330 class KMPLAYER_NO_EXPORT PhononProcessInfo : public MasterProcessInfo {
0331 public:
0332     PhononProcessInfo (MediaManager *);
0333 
0334     virtual IProcess *create (PartBase*, ProcessUser*);
0335 
0336     virtual bool startSlave ();
0337 };
0338 
0339 class KMPLAYER_NO_EXPORT Phonon : public MasterProcess {
0340     Q_OBJECT
0341 public:
0342     Phonon (QObject *parent, ProcessInfo*, Settings *settings);
0343 
0344     virtual bool ready ();
0345 };
0346 
0347 /*
0348  * Config document as used by kxineplayer backend
0349  */
0350 struct KMPLAYER_NO_EXPORT ConfigDocument : public Document {
0351     ConfigDocument ();
0352     ~ConfigDocument ();
0353     Node *childFromTag (const QString & tag);
0354 };
0355 
0356 /*
0357  * Element for ConfigDocument
0358  */
0359 struct KMPLAYER_NO_EXPORT ConfigNode : public DarkNode {
0360     ConfigNode (NodePtr & d, const QString & tag);
0361     KDE_NO_CDTOR_EXPORT ~ConfigNode () {}
0362     Node *childFromTag (const QString & tag);
0363     QWidget * w;
0364 };
0365 
0366 /*
0367  * Element for ConfigDocument, defining type of config item
0368  */
0369 struct KMPLAYER_NO_EXPORT TypeNode : public ConfigNode {
0370     TypeNode (NodePtr & d, const QString & t);
0371     KDE_NO_CDTOR_EXPORT ~TypeNode () {}
0372     Node *childFromTag (const QString & tag);
0373     void changedXML (QTextStream & out);
0374     QWidget * createWidget (QWidget * parent);
0375     const char * nodeName () const { return tag.toAscii (); }
0376     QString tag;
0377 };
0378 
0379 /*
0380  * ffmpeg backend recorder
0381  */
0382 class KMPLAYER_NO_EXPORT FFMpegProcessInfo : public ProcessInfo {
0383 public:
0384     FFMpegProcessInfo (MediaManager *);
0385     virtual IProcess *create (PartBase*, ProcessUser*);
0386 };
0387 
0388 class KMPLAYER_EXPORT FFMpeg : public Process {
0389     Q_OBJECT
0390 public:
0391     FFMpeg (QObject *parent, ProcessInfo *pinfo, Settings *settings) KDE_NO_CDTOR_EXPORT;
0392     ~FFMpeg () KDE_NO_CDTOR_EXPORT;
0393     virtual void init () KDE_NO_EXPORT;
0394     virtual bool deMediafiedPlay ();
0395     virtual void stop () KDE_NO_EXPORT;
0396     virtual void quit () KDE_NO_EXPORT;
0397 private slots:
0398     void processStopped (int, QProcess::ExitStatus) KDE_NO_EXPORT;
0399 };
0400 
0401 /*
0402  * npplayer backend
0403  */
0404 
0405 class KMPLAYER_NO_EXPORT NpStream : public QObject {
0406     Q_OBJECT
0407 public:
0408     enum Reason {
0409         NoReason = -1,
0410         BecauseDone = 0, BecauseError = 1, BecauseStopped = 2
0411     };
0412 
0413     NpStream (NpPlayer *parent, uint32_t stream_id, const QString &url, const QByteArray &post);
0414     ~NpStream ();
0415 
0416     void open () KMPLAYER_NO_MBR_EXPORT;
0417     void close () KMPLAYER_NO_MBR_EXPORT;
0418 
0419     void destroy () KMPLAYER_NO_MBR_EXPORT;
0420 
0421     QString url;
0422     QByteArray post;
0423     QByteArray pending_buf;
0424     KIO::TransferJob *job;
0425     timeval data_arrival;
0426     uint32_t bytes;
0427     uint32_t stream_id;
0428     uint32_t content_length;
0429     Reason finish_reason;
0430     QString mimetype;
0431     QString http_headers;
0432     bool received_data;
0433 signals:
0434     void stateChanged ();
0435     void redirected(uint32_t, const QUrl&);
0436 private slots:
0437     void slotResult (KJob*);
0438     void slotData (KIO::Job*, const QByteArray& qb);
0439     void redirection(KIO::Job*, const QUrl& url);
0440     void slotMimetype (KIO::Job *, const QString &mime);
0441     void slotTotalSize (KJob *, qulonglong sz);
0442 };
0443 
0444 class KMPLAYER_NO_EXPORT NppProcessInfo : public ProcessInfo {
0445 public:
0446     NppProcessInfo (MediaManager *);
0447     virtual IProcess *create (PartBase*, ProcessUser*);
0448 };
0449 
0450 class KMPLAYER_NO_EXPORT NpPlayer : public Process {
0451     Q_OBJECT
0452 public:
0453     NpPlayer (QObject *, KMPlayer::ProcessInfo*, Settings *);
0454     ~NpPlayer ();
0455 
0456     static const char *name;
0457     static const char *supports [];
0458     static IProcess *create (PartBase *, ProcessUser *);
0459 
0460     virtual void init ();
0461     virtual bool deMediafiedPlay ();
0462     virtual void initProcess ();
0463 
0464     using Process::running;
0465     void running (const QString &srv) KMPLAYER_NO_MBR_EXPORT;
0466     void plugged () KMPLAYER_NO_MBR_EXPORT;
0467     void request_stream (const QString &path, const QString &url, const QString &target, const QByteArray &post) KMPLAYER_NO_MBR_EXPORT;
0468     QString evaluate (const QString &script, bool store) KMPLAYER_NO_MBR_EXPORT;
0469     QString cookie (const QString &url);
0470     void dimension (int w, int h) KMPLAYER_NO_MBR_EXPORT;
0471 
0472     void destroyStream (uint32_t sid);
0473 
0474     KDE_NO_EXPORT const QString & destination () const { return service; }
0475     KDE_NO_EXPORT const QString & interface () const { return iface; }
0476     KDE_NO_EXPORT QString objectPath () const { return path; }
0477     virtual void stop ();
0478     virtual void quit ();
0479     bool ready ();
0480 signals:
0481     void evaluate (const QString & scr, bool store, QString & result);
0482     void loaded ();
0483 public slots:
0484     void requestGet (const uint32_t, const QString &, QString *);
0485     void requestCall (const uint32_t, const QString &, const QStringList &, QString *);
0486 private slots:
0487     void processOutput ();
0488     void processStopped (int, QProcess::ExitStatus);
0489     void wroteStdin (qint64);
0490     void streamStateChanged ();
0491     void streamRedirected(uint32_t, const QUrl&);
0492 protected:
0493     virtual void terminateJobs ();
0494 private:
0495     void sendFinish (uint32_t sid, uint32_t total, NpStream::Reason because);
0496     void processStreams ();
0497     QString service;
0498     QString iface;
0499     QString path;
0500     QString filter;
0501     typedef QMap <uint32_t, NpStream *> StreamMap;
0502     StreamMap streams;
0503     QString remote_service;
0504     QString m_base_url;
0505     QByteArray send_buf;
0506     bool write_in_progress;
0507     bool in_process_stream;
0508 };
0509 
0510 } // namespace
0511 
0512 #endif //_KMPLAYERPROCESS_H_