File indexing completed on 2024-04-28 04:43:21

0001 /*
0002     Copyright (C) 2007-2008 Tanguy Krotoff <tkrotoff@gmail.com>
0003     Copyright (C) 2008 Lukas Durfina <lukas.durfina@gmail.com>
0004     Copyright (C) 2009 Fathi Boudra <fabo@kde.org>
0005     Copyright (C) 2009-2011 vlc-phonon AUTHORS <kde-multimedia@kde.org>
0006 
0007     This library is free software; you can redistribute it and/or
0008     modify it under the terms of the GNU Lesser General Public
0009     License as published by the Free Software Foundation; either
0010     version 2.1 of the License, or (at your option) any later version.
0011 
0012     This library is distributed in the hope that it will be useful,
0013     but WITHOUT ANY WARRANTY; without even the implied warranty of
0014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015     Lesser General Public License for more details.
0016 
0017     You should have received a copy of the GNU Lesser General Public
0018     License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0019 */
0020 
0021 #ifndef PHONON_STREAMREADER_H
0022 #define PHONON_STREAMREADER_H
0023 
0024 #include <phonon/mediasource.h>
0025 #include <phonon/streaminterface.h>
0026 
0027 #include <stdint.h>
0028 
0029 #include <QtCore/QMutex>
0030 #include <QtCore/QWaitCondition>
0031 
0032 #ifndef QT_NO_PHONON_ABSTRACTMEDIASTREAM
0033 
0034 namespace Phonon
0035 {
0036 
0037 class MediaSource;
0038 
0039 namespace VLC
0040 {
0041 
0042 class Media;
0043 class MediaObject;
0044 
0045 /** \brief Class for supporting custom data streams to the backend
0046  *
0047  * This class receives data from a Phonon MediaSource that is a stream.
0048  * When data is requested, it fetches it from the media source and passes it further.
0049  * MediaObject uses this class to pass stream data to libVLC.
0050  *
0051  * It implements Phonon::StreamInterface, necessary for the connection with an
0052  * Phonon::AbstractMediaStream owned by the Phonon::MediaSource. See the Phonon
0053  * documentation for details.
0054  *
0055  * There are callbacks implemented in streamhooks.cpp, for libVLC.
0056  */
0057 class StreamReader : public QObject, public Phonon::StreamInterface
0058 {
0059     Q_OBJECT
0060     Q_INTERFACES(Phonon::StreamInterface)
0061 public:
0062     explicit StreamReader(MediaObject *parent);
0063     ~StreamReader();
0064 
0065     void addToMedia(Media *media);
0066 
0067     void lock();
0068     void unlock();
0069 
0070     static int readCallback(void *data, const char *cookie,
0071                             int64_t *dts, int64_t *pts, unsigned *flags, // krazy:exclude=typedefs
0072                             size_t *bufferSize, void **buffer);
0073 
0074     static int readDoneCallback(void *data, const char *cookie,
0075                                 size_t bufferSize, void *buffer);
0076 
0077     static int seekCallback(void *data, const uint64_t pos);
0078 
0079     quint64 currentBufferSize() const;
0080     void writeData(const QByteArray &data) override;
0081     quint64 currentPos() const;
0082     void setCurrentPos(qint64 pos);
0083 
0084     /**
0085      * Requests data from this stream. The stream requests data from the
0086      * Phonon::MediaSource's abstract media stream with the needData() signal.
0087      * If the requested data is available, it is copied into the buffer.
0088      *
0089      * \param pos Position in the stream
0090      * \param length Length of the data requested
0091      * \param buffer A buffer to put the data
0092      */
0093     bool read(quint64 offset, int *length, char *buffer);
0094 
0095     void endOfData() override;
0096     void setStreamSize(qint64 newSize) override;
0097     qint64 streamSize() const;
0098     void setStreamSeekable(bool seekable) override;
0099     bool streamSeekable() const;
0100 
0101 Q_SIGNALS:
0102     void streamSeekableChanged(bool seekable);
0103 
0104 protected:
0105     QByteArray m_buffer;
0106     quint64 m_pos;
0107     quint64 m_size;
0108     bool m_eos;
0109     bool m_seekable;
0110     bool m_unlocked;
0111     QMutex m_mutex;
0112     QWaitCondition m_waitingForData;
0113     MediaObject *m_mediaObject;
0114 };
0115 
0116 }
0117 }
0118 
0119 #endif //QT_NO_PHONON_ABSTRACTMEDIASTREAM
0120 
0121 #endif // PHONON_STREAMREADER_H