File: httpstream.cpp

package info (click to toggle)
cantata 3.3.1.ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 38,300 kB
  • sloc: cpp: 105,773; perl: 1,366; xml: 723; python: 139; lex: 110; sh: 105; yacc: 78; makefile: 8
file content (300 lines) | stat: -rw-r--r-- 7,063 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
 * Cantata
 *
 * Copyright (c) 2011-2022 Craig Drummond <craig.p.drummond@gmail.com>
 *
 * ----
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "httpstream.h"
#include "gui/settings.h"
#include "mpdconnection.h"
#include "mpdstatus.h"
#include "support/configuration.h"
#include "support/globalstatic.h"
#ifndef LIBVLC_FOUND
#include <QAudioOutput>
#include <QMediaPlayer>
#endif
#include <QTimer>

static const int constPlayerCheckPeriod = 250;
static const int constMaxPlayStateChecks = 2000 / constPlayerCheckPeriod;

#include <QDebug>
static bool debugEnabled = false;
#define DBUG \
	if (debugEnabled) qWarning() << metaObject()->className() << __FUNCTION__
void HttpStream::enableDebug()
{
	debugEnabled = true;
}

GLOBAL_STATIC(HttpStream, instance)

HttpStream::HttpStream(QObject* p)
	: QObject(p), enabled(false), muted(false), state(MPDState_Inactive), playStateChecks(0), currentVolume(50), unmuteVol(50), playStateCheckTimer(nullptr), player(nullptr)
{
}

void HttpStream::save() const
{
	Configuration config(metaObject()->className());
	config.set("volume", currentVolume);
}

void HttpStream::setEnabled(bool e)
{
	if (e == enabled) {
		return;
	}

	enabled = e;
	if (enabled) {
		connect(MPDConnection::self(), SIGNAL(streamUrl(QString)), this, SLOT(streamUrl(QString)));
		connect(MPDStatus::self(), SIGNAL(updated()), this, SLOT(updateStatus()));
		streamUrl(MPDConnection::self()->getDetails().streamUrl);
	}
	else {
		disconnect(MPDConnection::self(), SIGNAL(streamUrl(QString)), this, SLOT(streamUrl(QString)));
		disconnect(MPDStatus::self(), SIGNAL(updated()), this, SLOT(updateStatus()));
		if (player) {
			save();
#ifdef LIBVLC_FOUND
			libvlc_media_player_stop(player);
#else
			player->stop();
#endif
		}
	}
	emit isEnabled(enabled);
}

void HttpStream::setVolume(int vol)
{
	DBUG << vol;
	if (player) {
		currentVolume = vol;
#ifdef LIBVLC_FOUND
		libvlc_audio_set_volume(player, vol);
#else
		// QAudioOutput::volume is between 0 and 100
		player->audioOutput()->setVolume(static_cast<float>(vol) / 100);
#endif
		emit update();
	}
}

int HttpStream::volume()
{
	if (!enabled) {
		return -1;
	}

	int vol = currentVolume;
	if (player && !isMuted()) {
#ifdef LIBVLC_FOUND
		vol = libvlc_audio_get_volume(player);
#else
		// QAudioOutput::volume is between 0 and 100
		vol = int(player->audioOutput()->volume() * 100);
#endif
		if (vol < 0) {
			vol = currentVolume;
		}
		else {
			currentVolume = vol;
		}
	}
	DBUG << vol;
	return vol;
}

void HttpStream::toggleMute()
{
	DBUG << isMuted();
	if (player) {
		muted = !muted;
#ifdef LIBVLC_FOUND
		libvlc_audio_set_mute(player, muted);
#else
		player->audioOutput()->setMuted(muted);
#endif
		emit update();
	}
}

void HttpStream::streamUrl(const QString& url)
{
	DBUG << url;
#ifdef LIBVLC_FOUND
	if (player) {
		libvlc_media_player_stop(player);
		libvlc_media_player_release(player);
		libvlc_release(instance);
		player = 0;
	}
#else
	if (player) {
		if (player->source().url() != url) {
			player->stop();
			player->audioOutput()->deleteLater();
			player->deleteLater();
			player = nullptr;
		}
	}
#endif
	QUrl qUrl(url);
	if (!url.isEmpty() && qUrl.isValid() && qUrl.scheme().startsWith("http") && !player) {
#ifdef LIBVLC_FOUND
		instance = libvlc_new(0, NULL);
		QByteArray byteArrayUrl = url.toUtf8();
		media = libvlc_media_new_location(instance, byteArrayUrl.constData());
		player = libvlc_media_player_new_from_media(media);
		libvlc_media_release(media);
#else
		player = new QMediaPlayer(this);
		QAudioOutput* audioOutput = new QAudioOutput(this);
		player->setAudioOutput(audioOutput);
		player->setSource(qUrl);
		connect(player, &QMediaPlayer::mediaStatusChanged, this, &HttpStream::mediaStatus);
#endif
		muted = false;
		setVolume(Configuration(metaObject()->className()).get("volume", currentVolume));
	}
	if (player) {
		state = 0xFFFF;// Force an update...
		updateStatus();
	}
	else {
		state = MPDState_Inactive;
	}
	emit update();
}

#ifndef LIBVLC_FOUND
void HttpStream::mediaStatus(QMediaPlayer::MediaStatus status)
{
	MPDStatus* const mpdstatus = MPDStatus::self();
	DBUG << status;
	if (mpdstatus->state() == MPDState_Playing) {
		if (status == QMediaPlayer::BufferingMedia || status == QMediaPlayer::BufferedMedia) {
			player->play();
		}
	}
}
#endif

void HttpStream::updateStatus()
{
	if (!player) {
		return;
	}

	MPDStatus* const status = MPDStatus::self();
	DBUG << status->state() << state;

	// evaluates to true when it is needed to start or restart media player
	bool playerNeedsToStart = status->state() == MPDState_Playing;
#ifdef LIBVLC_FOUND
	playerNeedsToStart = playerNeedsToStart && libvlc_media_player_get_state(player) != libvlc_Playing;
#else
	playerNeedsToStart = playerNeedsToStart && player->playbackState() == QMediaPlayer::StoppedState;
#endif

	if (status->state() == state && !playerNeedsToStart) {
#ifndef LIBVLC_FOUND
		player->play();
#endif
		return;
	}

	state = status->state();
	switch (status->state()) {
	case MPDState_Playing:
		// Only start playback if not aready playing
		if (playerNeedsToStart) {
#ifdef LIBVLC_FOUND
			libvlc_media_player_play(player);
			startTimer();
#else
			QUrl url = player->source().url();
			player->setSource(url);
			player->play();
#endif
		}
		break;
	case MPDState_Paused:
	case MPDState_Inactive:
	case MPDState_Stopped:
#ifdef LIBVLC_FOUND
		libvlc_media_player_stop(player);
		stopTimer();
#else
		player->stop();
#endif
		break;
	default:
#ifdef LIBVLC_FOUND
		stopTimer();
#endif
		break;
	}
}

void HttpStream::checkPlayer()
{
#ifdef LIBVLC_FOUND
	if (0 == --playStateChecks) {
		stopTimer();
		DBUG << "Max checks reached";
	}
	if (libvlc_media_player_get_state(player) == libvlc_Playing) {
		DBUG << "Playing";
		stopTimer();
	}
	else {
		DBUG << "Try again";
		libvlc_media_player_play(player);
	}
#endif
}

void HttpStream::startTimer()
{
	if (!playStateCheckTimer) {
		playStateCheckTimer = new QTimer(this);
		playStateCheckTimer->setSingleShot(false);
		playStateCheckTimer->setInterval(constPlayerCheckPeriod);
		connect(playStateCheckTimer, SIGNAL(timeout()), SLOT(checkPlayer()));
	}
	playStateChecks = constMaxPlayStateChecks;
	DBUG << playStateChecks;
	playStateCheckTimer->start();
}

void HttpStream::stopTimer()
{
	if (playStateCheckTimer) {
		DBUG;
		playStateCheckTimer->stop();
	}
	playStateChecks = 0;
}

#include "moc_httpstream.cpp"