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

0001 /*
0002     Copyright (C) 2007 Matthias Kretz <kretz@kde.org>
0003     Copyright (C) 2011 Harald Sitter <sitter@kde.org>
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.1 of the License, or (at your option) version 3, or any
0009     later version accepted by the membership of KDE e.V. (or its
0010     successor approved by the membership of KDE e.V.), Nokia Corporation
0011     (or its successors, if any) and the KDE Free Qt Foundation, which shall
0012     act as a proxy defined in Section 6 of version 3 of the license.
0013 
0014     This library is distributed in the hope that it will be useful,
0015     but WITHOUT ANY WARRANTY; without even the implied warranty of
0016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0017     Lesser General Public License for more details.
0018 
0019     You should have received a copy of the GNU Lesser General Public
0020     License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0021 */
0022 
0023 #ifndef PHONON_STREAMINTERFACE_H
0024 #define PHONON_STREAMINTERFACE_H
0025 
0026 #include "phonon_export.h"
0027 #include <QObject>
0028 
0029 
0030 #ifndef QT_NO_PHONON_ABSTRACTMEDIASTREAM
0031 
0032 namespace Phonon
0033 {
0034 class StreamInterfacePrivate;
0035 class MediaSource;
0036 
0037 /** \class StreamInterface streaminterface.h phonon/StreamInterface
0038  * \brief Backend interface to handle media streams (AbstractMediaStream).
0039  *
0040  * All functions relay their calls to the AbstractMediaStream using invokeMethod on the
0041  * AbstractMediaStream's QMetaObject. This means that every function call will
0042  * actually be executed in the thread context of the AbstractMediaStream (which
0043  * usually is a thread Phonon also lives in, could however also be another one).
0044  * This protectes the AbstractMediaStream against calls from different threads,
0045  * such as a callback thread.
0046  * This is very important as most IO implementations are by default not
0047  * thread-safe.
0048  *
0049  * This protection is only established in one direction though, meaning that a
0050  * backend implementation of this interface must be made thread-safe at all costs
0051  * as it can get called from any thread.
0052  *
0053  * \author Matthias Kretz <kretz@kde.org>
0054  */
0055 class PHONON_EXPORT StreamInterface
0056 {
0057     friend class StreamInterfacePrivate;
0058     friend class AbstractMediaStreamPrivate;
0059     public:
0060         virtual ~StreamInterface();
0061 
0062         /**
0063          * Called by the application to send a chunk of (encoded) media data.
0064          *
0065          * It is recommended to keep the QByteArray object until the data is consumed so that no
0066          * memcopy is needed.
0067          */
0068         virtual void writeData(const QByteArray &data) = 0;
0069 
0070         /**
0071          * Called when no more media data is available and writeData will not be called anymore.
0072          */
0073         virtual void endOfData() = 0;
0074 
0075         /**
0076          * Called at the start of the stream to tell how many bytes will be sent through writeData
0077          * (if no seeks happen, of course). If this value is negative the stream size cannot be
0078          * determined (might be a "theoretically infinite" stream - like webradio).
0079          */
0080         virtual void setStreamSize(qint64 newSize) = 0;
0081 
0082         /**
0083          * Tells whether the stream is seekable.
0084          */
0085         virtual void setStreamSeekable(bool s) = 0;
0086 
0087         /**
0088          * Call this function from the constructor of your StreamInterface implementation (or as
0089          * soon as you get the MediaSource object). This will connect your object to the
0090          * AbstractMediaStream object. Only after the connection is done will the following
0091          * functions have an effect.
0092          */
0093         void connectToSource(const MediaSource &mediaSource);
0094 
0095         /**
0096          * Call this function to tell the AbstractMediaStream that you need more data. The data will
0097          * arrive through writeData.
0098          * writeData() will not be called from needData() due to the thread protection of
0099          * the AbstractMediaStream.
0100          *
0101          * Depending on the buffering you need you either treat needData as a replacement for a
0102          * read call like QIODevice::read, or you start calling needData whenever your buffer
0103          * reaches a certain lower threshold.
0104          */
0105         void needData();
0106 
0107         /**
0108          * Call this function to tell the AbstractMediaStream that you have enough data in your
0109          * buffer and that it should pause calling writeData if possible.
0110          */
0111         void enoughData();
0112 
0113         /**
0114          * If the stream is seekable, calling this function will make the next call to writeData
0115          * pass data that starts at the byte offset \p seekTo.
0116          */
0117         void seekStream(qint64 seekTo);
0118 
0119         /**
0120          * Resets the AbstractMediaStream. E.g. this can be useful for non-seekable streams to start
0121          * over again.
0122          */
0123         void reset();
0124 
0125     protected:
0126         StreamInterface();
0127 
0128         StreamInterfacePrivate *const d;
0129 };
0130 } // namespace Phonon
0131 
0132 Q_DECLARE_INTERFACE(Phonon::StreamInterface, "StreamInterface1.phonon.kde.org")
0133 
0134 #endif //QT_NO_PHONON_ABSTRACTMEDIASTREAM
0135 
0136 
0137 #endif // PHONON_STREAMINTERFACE_H