forked from andreagrandi/sailsoma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
servercomm.cpp
145 lines (119 loc) · 4.03 KB
/
servercomm.cpp
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
#include "servercomm.h"
#include "playlistreader.h"
#include <QDebug>
#include <QUrl>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QMediaPlaylist>
#include <QTimer>
#include <QDomDocument>
ServerComm::ServerComm(QObject *parent) :
QObject(parent)
{
playlistNetworkReader = new QNetworkAccessManager(this);
connect(playlistNetworkReader, SIGNAL(finished(QNetworkReply*)), this, SLOT(finishLoadingChannel(QNetworkReply*)));
channelInfoReader = new QNetworkAccessManager(this);
connect(channelInfoReader, SIGNAL(finished(QNetworkReply*)), this, SLOT(finishReadingChannelInfo(QNetworkReply*)));
player = new QMediaPlayer(this);
mediaplaylist = new QMediaPlaylist;
player->setPlaylist(mediaplaylist);
connect(player, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)), this, SLOT(setMediaStatus(QMediaPlayer::MediaStatus)));
connect(player, SIGNAL(positionChanged(qint64)), this, SLOT(updateProgress(qint64)));
connect(player, SIGNAL(stateChanged(QMediaPlayer::State)), this, SLOT(playerState(QMediaPlayer::State)));
nowPlayingSongTimer = new QTimer(this);
connect(nowPlayingSongTimer, SIGNAL(timeout()), this, SLOT(checkSongUpdates()));
}
void ServerComm::play()
{
player->play();
songPlaying();
}
void ServerComm::pause()
{
player->pause();
songPaused();
}
void ServerComm::loadChannel(QString channelUrl)
{
channelLoading();
playlistNetworkReader->get(QNetworkRequest(channelUrl));
}
void ServerComm::finishLoadingChannel(QNetworkReply *reply)
{
QByteArray playlistData = reply->readAll();
PlaylistReader playlistReader(playlistData);
PlaylistReader::StreamUrls streamUrls = playlistReader.getStreamUrls();
QString streamUrl = streamUrls[0];
mediaplaylist->clear();
mediaplaylist->addMedia(QUrl(streamUrl));
player->play();
songPlaying();
}
void ServerComm::updateProgress(qint64 time)
{
int minutes = 0;
int seconds = 0;
minutes = (time / 1000) / 60;
seconds = (time / 1000) % 60;
QString min_str = QString("%1").arg(minutes, 2, 10, QLatin1Char('0'));
QString sec_str = QString("%1").arg(seconds, 2, 10, QLatin1Char('0'));
positionUpdate(min_str, sec_str);
}
void ServerComm::setMediaStatus(QMediaPlayer::MediaStatus state)
{
switch (state) {
case QMediaPlayer::LoadedMedia:
case QMediaPlayer::BufferedMedia:
emit channelLoaded();
break;
case QMediaPlayer::StalledMedia:
case QMediaPlayer::BufferingMedia:
case QMediaPlayer::LoadingMedia:
emit channelLoading();
break;
default:
break;
}
}
void ServerComm::checkSongUpdates()
{
updateSong();
}
void ServerComm::playerState(QMediaPlayer::State state)
{
switch (state)
{
case QMediaPlayer::StoppedState:
nowPlayingSongTimer->stop();
break;
case QMediaPlayer::PlayingState:
nowPlayingSongTimer->start(SONGS_POLL_TIME);
break;
case QMediaPlayer::PausedState:
nowPlayingSongTimer->stop();
break;
}
}
void ServerComm::updateChannelInfo(QString channelId)
{
QNetworkRequest request = QNetworkRequest(QUrl(CHANNEL_REFRESH_URL));
request.setAttribute(QNetworkRequest::User, QVariant(channelId));
channelInfoReader->get(request);
}
void ServerComm::finishReadingChannelInfo(QNetworkReply *reply)
{
QString channelId = reply->request().attribute(QNetworkRequest::User).toString();
QString lastPlaying = "";
QByteArray data = reply->readAll();
QString xmldata = QString::fromUtf8(data);
QDomDocument doc("Channels");
doc.setContent(xmldata.toUtf8());
QDomNodeList channels = doc.documentElement().elementsByTagName("channels").at(1).toElement().elementsByTagName("channel");
for(int i = 0; i < channels.count(); i++)
{
if(channels.at(i).toElement().attribute("id") == channelId)
lastPlaying = channels.at(i).toElement().elementsByTagName("lastPlaying").at(0).toElement().text();
}
channelSongUpdate(lastPlaying);
}