| 1 | /* |
| 2 | Copyright (C) 2007-2008 Tanguy Krotoff <tkrotoff@gmail.com> |
| 3 | Copyright (C) 2008 Lukas Durfina <lukas.durfina@gmail.com> |
| 4 | Copyright (C) 2009 Fathi Boudra <fabo@kde.org> |
| 5 | Copyright (C) 2009-2011 vlc-phonon AUTHORS <kde-multimedia@kde.org> |
| 6 | |
| 7 | This library is free software; you can redistribute it and/or |
| 8 | modify it under the terms of the GNU Lesser General Public |
| 9 | License as published by the Free Software Foundation; either |
| 10 | version 2.1 of the License, or (at your option) any later version. |
| 11 | |
| 12 | This library is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | Lesser General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU Lesser General Public |
| 18 | License along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #ifndef PHONON_STREAMREADER_H |
| 22 | #define PHONON_STREAMREADER_H |
| 23 | |
| 24 | #include <phonon/mediasource.h> |
| 25 | #include <phonon/streaminterface.h> |
| 26 | |
| 27 | #include <stdint.h> |
| 28 | |
| 29 | #include <QtCore/QMutex> |
| 30 | #include <QtCore/QWaitCondition> |
| 31 | |
| 32 | #ifndef QT_NO_PHONON_ABSTRACTMEDIASTREAM |
| 33 | |
| 34 | namespace Phonon |
| 35 | { |
| 36 | |
| 37 | class MediaSource; |
| 38 | |
| 39 | namespace VLC |
| 40 | { |
| 41 | |
| 42 | class Media; |
| 43 | class MediaObject; |
| 44 | |
| 45 | /** \brief Class for supporting custom data streams to the backend |
| 46 | * |
| 47 | * This class receives data from a Phonon MediaSource that is a stream. |
| 48 | * When data is requested, it fetches it from the media source and passes it further. |
| 49 | * MediaObject uses this class to pass stream data to libVLC. |
| 50 | * |
| 51 | * It implements Phonon::StreamInterface, necessary for the connection with an |
| 52 | * Phonon::AbstractMediaStream owned by the Phonon::MediaSource. See the Phonon |
| 53 | * documentation for details. |
| 54 | * |
| 55 | * There are callbacks implemented in streamhooks.cpp, for libVLC. |
| 56 | */ |
| 57 | class StreamReader : public QObject, public Phonon::StreamInterface |
| 58 | { |
| 59 | Q_OBJECT |
| 60 | Q_INTERFACES(Phonon::StreamInterface) |
| 61 | public: |
| 62 | explicit StreamReader(MediaObject *parent); |
| 63 | ~StreamReader(); |
| 64 | |
| 65 | void addToMedia(Media *media); |
| 66 | |
| 67 | void lock(); |
| 68 | void unlock(); |
| 69 | |
| 70 | static int readCallback(void *data, const char *cookie, |
| 71 | int64_t *dts, int64_t *pts, unsigned *flags, // krazy:exclude=typedefs |
| 72 | size_t *bufferSize, void **buffer); |
| 73 | |
| 74 | static int readDoneCallback(void *data, const char *cookie, |
| 75 | size_t bufferSize, void *buffer); |
| 76 | |
| 77 | static int seekCallback(void *data, const uint64_t pos); |
| 78 | |
| 79 | quint64 currentBufferSize() const; |
| 80 | void writeData(const QByteArray &data) override; |
| 81 | quint64 currentPos() const; |
| 82 | void setCurrentPos(qint64 pos); |
| 83 | |
| 84 | /** |
| 85 | * Requests data from this stream. The stream requests data from the |
| 86 | * Phonon::MediaSource's abstract media stream with the needData() signal. |
| 87 | * If the requested data is available, it is copied into the buffer. |
| 88 | * |
| 89 | * \param pos Position in the stream |
| 90 | * \param length Length of the data requested |
| 91 | * \param buffer A buffer to put the data |
| 92 | */ |
| 93 | bool read(quint64 offset, int *length, char *buffer); |
| 94 | |
| 95 | void endOfData() override; |
| 96 | void setStreamSize(qint64 newSize) override; |
| 97 | qint64 streamSize() const; |
| 98 | void setStreamSeekable(bool seekable) override; |
| 99 | bool streamSeekable() const; |
| 100 | |
| 101 | Q_SIGNALS: |
| 102 | void streamSeekableChanged(bool seekable); |
| 103 | |
| 104 | protected: |
| 105 | QByteArray m_buffer; |
| 106 | quint64 m_pos; |
| 107 | quint64 m_size; |
| 108 | bool m_eos; |
| 109 | bool m_seekable; |
| 110 | bool m_unlocked; |
| 111 | QMutex m_mutex; |
| 112 | QWaitCondition m_waitingForData; |
| 113 | MediaObject *m_mediaObject; |
| 114 | }; |
| 115 | |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | #endif //QT_NO_PHONON_ABSTRACTMEDIASTREAM |
| 120 | |
| 121 | #endif // PHONON_STREAMREADER_H |
| 122 | |