-
Notifications
You must be signed in to change notification settings - Fork 1
/
multicastsession.cpp
73 lines (62 loc) · 1.84 KB
/
multicastsession.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
#include "multicastsession.h"
#include "udpsocket.h"
MulticastSession::MulticastSession(UDPSocket* socket, QStringList* playlist)
: multicastSocket_(socket), playlist_(playlist),
playlistIterator_(new QStringListIterator(*playlist_)), current_(NULL), nextBuff_(NULL),
currentTimer_(0) {
QString multicastAddr("234.5.6.8");
multicastSocket_->listen(0);
multicastSocket_->setDest(multicastAddr, 7000);
}
MulticastSession::~MulticastSession(){
delete playlistIterator_;
delete current_;
delete nextBuff_;
}
void MulticastSession::start() {
playlistIterator_ = new QStringListIterator(*playlist_);
loadBuffer();
currentTimer_ = startTimer(35);
}
void MulticastSession::pause() {
if (currentTimer_ > 0) killTimer(currentTimer_);
}
void MulticastSession::timerEvent(QTimerEvent* event) {
multicastSocket_->write(*nextBuff_);
delete nextBuff_;
nextBuff_ = NULL;
loadBuffer();
}
void MulticastSession::loadBuffer() {
if (current_ == NULL) {
if (!playlistIterator_->hasNext()) {
endSession();
return;
}
current_ = new QFile(playlistIterator_->next());
if (!current_->open(QIODevice::ReadOnly)) {
endSession();
return;
}
header_ = current_->read(44);
}
nextBuff_ = generateBuffer();
}
QByteArray* MulticastSession::generateBuffer() {
QByteArray* tempArray = new QByteArray;
tempArray->append(header_);
tempArray->append(current_->read(1024*8));
if(current_->atEnd()){
current_->close();
current_->deleteLater();
current_=NULL;
}
return tempArray;
}
void MulticastSession::endSession() {
qDebug("Multicastsession::closeSession(); Multicast session closed.");
pause();
this->deleteLater();
multicastSocket_->deleteLater();
multicastSocket_ = NULL;
}