4
0
mirror of https://github.com/QuasarApp/SoundBand.git synced 2025-05-13 23:49:33 +00:00

added feedback on package

This commit is contained in:
Andrei Yankovich 2017-12-09 21:55:37 +03:00
parent 2f0b649f52
commit 62ac4ce7c1
5 changed files with 52 additions and 22 deletions

@ -15,8 +15,11 @@
#define DEFAULT_NRTWORK 0 // the network number from which the address will be taken.
#define LOCAL_HOST "127.0.0.1"
#define DEFAULT_PORT 1994
#define MAX_SYNC_TIME 20 * 1000 // 10 sec on microsec
#define SYNC_TIME 5 * 1000 // 5 sec on microsec
#define MAX_SYNC_TIME 20 * 1000 // 10 sec on millisec
#define SYNC_TIME 5 * 1000 // 5 sec on millisec
#define DEEP_SCANER_INTERVAL 10000 // 10 sec
// sync
#define MIN_DIFFERENCE 100 // millisec
#endif // CONFIG_H

@ -38,12 +38,12 @@ bool package::isValid() const{
}
if(type & TypePackage::t_play){
ret = ret && true;
if(type & TypePackage::t_feedback & TypePackage::t_sync){
return false;
}
if(type & TypePackage::t_sync && type & t_brodcaster){
if((type & TypePackage::t_sync || type & TypePackage::t_feedback) && type & t_brodcaster){
ret = ret && (playdata.run > 0 && playdata.seek > 0);
}
@ -76,7 +76,7 @@ QByteArray package::parseTo(){
if(isValid()){
stream << static_cast<unsigned char>(type);
if(type & TypePackage::t_sync && type & t_brodcaster){
if((type & TypePackage::t_sync || type & TypePackage::t_feedback) && type & t_brodcaster){
stream << playdata.run;
stream << playdata.seek;
@ -104,7 +104,7 @@ bool package::parseFrom(QByteArray &array){
stream >> temp_type;
type = static_cast<TypePackage> (temp_type);
if(type & TypePackage::t_sync){
if(type & TypePackage::t_sync || type & TypePackage::t_feedback){
stream >> playdata.run;
stream >> playdata.seek;

@ -12,7 +12,7 @@ typedef unsigned char Type;
/**
* @brief The TypePackage enum
* t_void = this package empty and not valid.
* t_play = play curent audio file.
* t_feedback = feedback for synced.
* t_song_h = the header of playing audio file.
* t_song = the package with this type is necessary for translite media data on network.
* t_sync = the infomation about sync playning media file on network.
@ -24,7 +24,7 @@ typedef unsigned char Type;
enum TypePackage{
t_void = 0x00,
t_play = 0x01,
t_feedback = 0x01,
t_song_h = 0x02,
t_song = 0x04,
t_sync = 0x08,

@ -3,8 +3,9 @@
#include <QMultimedia>
#include <QSqlQuery>
#include "exaptions.h"
#include "time.h"
#include "thread"
#include <ctime>
#include <thread>
#include <cmath>
#include "config.h"
#ifdef QT_DEBUG
@ -39,6 +40,10 @@ Sync::Sync(const QString address, int port, const QString &datadir):
connect(player, SIGNAL(stateChanged(QMediaPlayer::State)), SLOT(endPlay(QMediaPlayer::State)));
}
unsigned int Sync::abs(int number) const{
return number & ~ 0x82000000;
}
bool Sync::findHeader(const Song &song){
for(SongHeader & header: playList){
@ -261,7 +266,7 @@ bool Sync::play(int id_song, Syncer *syncdata){
song.name = qyery->value(1).toString();
song.size = qyery->value(2).toInt();
song.source = qyery->value(3).toByteArray();
return Sync::play(song,syncdata);
return Sync::play(song ,syncdata);
}
bool Sync::play(QString url){
@ -269,7 +274,6 @@ bool Sync::play(QString url){
if(id < 0){
return false;
}
return Sync::play(id);
}
@ -341,13 +345,20 @@ bool Sync::createPackage(Type type, package &pac){
pac.type = type;
if(type & TypePackage::t_sync && fbroadcaster){
if(type & TypePackage::t_sync && fbroadcaster){
pac.playdata.run = now() + SYNC_TIME;
pac.playdata.seek = player->position() + SYNC_TIME;
}
if( type & TypePackage::t_feedback && fbroadcaster){
pac.playdata.run = now();
pac.playdata.seek = player->position();
}
if(type & TypePackage::t_song_h && fbroadcaster){
if(!curentSong)
return false;
@ -403,10 +414,6 @@ void Sync::packageRender(ETcpSocket *socket){
// if requst from server
if(pkg.getType() & t_play){
player->play();
}
if(pkg.getType() & t_sync && !play(pkg.getHeader(), &pkg.getPlayData()) && !play(pkg.getSong(), &pkg.getPlayData())){
Type requestType = t_song_h;
@ -438,14 +445,27 @@ void Sync::packageRender(ETcpSocket *socket){
}else{
if(pkg.getType() & t_sync){
if(pkg.getType() & t_sync ){
if(!curentSong){
throw SyncError();
return ;
}
}
Type fnoSynced = t_void;
if(pkg.getType() & t_feedback){
if(!curentSong){
return ;
}
unsigned int diff = abs(static_cast<unsigned int>(player->position() - (pkg.getPlayData().seek + (now() - pkg.getPlayData().run))));
if(diff < MIN_DIFFERENCE){
fnoSynced = t_feedback;
}
}
package answer;
if(!createPackage(pkg.getType() & ~t_what & ~t_play & ~t_stop & ~t_brodcaster, answer)){
if(!createPackage(pkg.getType() & ~t_what & ~fnoSynced & ~t_stop & ~t_brodcaster, answer)){
throw CreatePackageExaption();
}
socket->Write(answer.parseTo());

@ -37,6 +37,12 @@ private:
LocalScanner deepScaner;
int port;
QString dataBaseName;
/**
* @brief abs
* @return module of numver
*/
unsigned int abs(int number)const;
/**
* @brief findHeader set curent song if playList have playng song
* @return true if all done
@ -121,10 +127,11 @@ public:
/**
* @brief Play song in this device, if device has not supported playning media data this method throw MediaExcrption.
* @param header of song
* @param feedback - information about result of palying (cell default constructor if paying failed)
* @param syncdata data of synbced playning of media data.
* @return true if all done else false.
*/
bool play(const SongHeader &header, const Syncer *syncdata = nullptr);
bool play(const SongHeader &header, const Syncer *syncdata = nullptr);
/**
* @brief Play song in this device, if device has not supported playning media data this method throw MediaExcrption.
* @param song playning media data.