1 /***************************************************************************
2  *   Copyright (C) 2008-2021 by Ilya Kotov                                 *
3  *   forkotov02@ya.ru                                                      *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20 #ifndef STATEHANDLER_H
21 #define STATEHANDLER_H
22 
23 #include <QObject>
24 #include <QMap>
25 #include <QHash>
26 #include <QMutex>
27 #include "abstractengine.h"
28 #include "audioparameters.h"
29 #include "qmmp.h"
30 
31 /*! @brief The StateHandler class allows one to track information about playback progress.
32  * @author Ilya Kotov <forkotov02@ya.ru>
33  */
34 class QMMP_EXPORT StateHandler : public QObject
35 {
36     Q_OBJECT
37 public:
38     /*!
39      * Object constructor.
40      * @param parent Parent object.
41      */
42     StateHandler(QObject *parent = nullptr);
43     /*!
44      * Destructor.
45      */
46     ~StateHandler();
47     /*!
48      * Sends information about playback progress.
49      * @param elapsed Current time (in milliseconds).
50      * @param bitrate Current bitrate (in kbps).
51      */
52     void dispatch(qint64 elapsed, int bitrate);
53     /*!
54      * Sends information about audio parameters.
55      * @param p Output audio patameters.
56      */
57     void dispatch(const AudioParameters &p);
58     /*!
59      * Sends information about song length
60      * @param duration track length in milliseconds
61      */
62     void dispatch(qint64 duration);
63     /*!
64      * Sends track information.
65      * @param info track information.
66      */
67     bool dispatch(const TrackInfo &info);
68     /*!
69      * Sends stream information \b info
70      */
71     void dispatch(const QHash<QString, QString> &info);
72     /*!
73      * Sends playback state.
74      */
75     void dispatch(Qmmp::State state);
76     /*!
77      * Sends buffering progress.
78      * @param percent Indicates the current percentage of buffering completed.
79      */
80     void dispatchBuffer(int percent);
81     /*!
82      * Returns the current time (in milliseconds).
83      */
84     qint64 elapsed() const;
85     /*!
86      * Returns duration in milliseconds
87      */
88     qint64 duration() const;
89     /*!
90      * Returns current bitrate (in kbps)
91      */
92     int bitrate() const;
93     /*!
94      * Returns output audio parameters.
95      */
96     AudioParameters audioParameters() const;
97     /*!
98      * Returns the current state.
99      */
100     Qmmp::State state() const;
101     /*!
102      * Sends next track request.
103      */
104     void sendNextTrackRequest();
105     /*!
106      * Sends playback finished event.
107      */
108     void sendFinished();
109     /*!
110      * Returns a pointer to the first created StateHandler instance.
111      */
112     static StateHandler* instance();
113 
114 signals:
115     /*!
116      * Tracks elapesed time.
117      * @param time New track position in milliseconds.
118      */
119     void elapsedChanged(qint64 time);
120     /*!
121      * Emitted when bitrate has changed.
122      * @param bitrate New bitrate (in kbps)
123      */
124     void bitrateChanged(int bitrate);
125     /*!
126      * Emitted when audio parameters have changed.
127      * @param p New audio parameters for output.
128      */
129     void audioParametersChanged(const AudioParameters &p);
130      /*!
131      * This signal is emitted when the stream reader fills it's buffer.
132      * The argument \b progress indicates the current percentage of buffering completed.
133      */
134     void bufferingProgress(int progress);
135 
136 
137 private:
138     qint64 m_elapsed;
139     qint64 m_duration;
140     bool m_sendAboutToFinish;
141     int m_bitrate;
142     static StateHandler* m_instance;
143     TrackInfo m_info;
144     QHash <QString, QString> m_streamInfo;
145     Qmmp::State m_state;
146     AudioParameters m_audioParameters;
147     mutable QMutex m_mutex;
148 };
149 
150 #endif
151