SoundBand/sync/sync.h

276 lines
6.5 KiB
C
Raw Normal View History

2017-10-29 14:47:36 +03:00
#ifndef SYNC_H
#define SYNC_H
2017-11-12 13:26:37 +03:00
#include "song.h"
2017-11-20 00:37:12 +03:00
#include "node.h"
2017-11-22 22:34:55 +03:00
#include "LocalScanner.h"
2017-11-12 13:26:37 +03:00
#include <chrono>
2017-11-27 19:43:11 +03:00
#include "config.h"
2017-12-24 00:20:53 +03:00
#include "mysql.h"
2017-12-13 22:40:29 +03:00
#include "player.h"
2017-12-03 23:08:43 +03:00
2017-10-29 14:47:36 +03:00
namespace syncLib {
2017-11-09 23:09:59 +03:00
2017-11-12 13:26:37 +03:00
typedef std::chrono::time_point<std::chrono::high_resolution_clock> Clock;
2017-11-09 23:09:59 +03:00
class Node;
2017-11-11 20:35:30 +03:00
2017-11-11 14:03:14 +03:00
/**
* @brief The Sync class is main class of this library.
* the 'sync' has supported synced playning media files on network and saving media data into local database.
*/
2017-11-22 20:05:53 +03:00
class Sync : public QObject
2017-10-29 14:47:36 +03:00
{
2017-11-22 20:05:53 +03:00
Q_OBJECT
2017-10-29 14:47:36 +03:00
private:
2017-11-09 23:09:59 +03:00
Node *node;
2017-12-13 22:40:29 +03:00
Player *player;
2017-11-27 02:01:47 +03:00
QList<SongHeader> playList;
2018-03-08 16:08:40 +03:00
QString lastUsedPlayList;
2018-02-10 20:52:21 +03:00
int curentSongIndex;
2017-11-22 20:05:53 +03:00
QList<ETcpSocket*> servers;
2017-11-20 00:37:12 +03:00
bool fbroadcaster;
2018-01-14 15:29:42 +03:00
int resyncCount;
2018-01-14 15:49:26 +03:00
int lastSyncTime;
int ping;
2017-11-22 22:34:55 +03:00
LocalScanner deepScaner;
2017-12-24 22:04:40 +03:00
MySql *sql;
2017-11-27 19:43:11 +03:00
int port;
2017-12-03 23:08:43 +03:00
/**
* @brief findHeader set curent song if playList have playng song
* @return true if all done
*/
bool findHeader(const Song& song);
2017-11-22 20:05:53 +03:00
/**
* @brief rescan - search for existing servers
* result saved in servers
*/
2017-11-22 22:34:55 +03:00
void rescan(bool deep = false);
2017-12-24 22:04:40 +03:00
2017-11-20 00:37:12 +03:00
/**
* @brief createPackage - Create a package that shows current state of the node
* @param type - Type of an answer
* @param pac - the resulting value
* @return true if everything's done
*/
2018-01-09 22:14:54 +03:00
bool createPackage(Type type , package& pac);
2017-11-22 20:05:53 +03:00
private slots:
2018-03-06 22:10:46 +03:00
/**
* @brief updateSongs use method update avelable songs from sql database
* @return true if all done
*/
bool updateSongs(QList<SongHeader> &list, const QString &playList = "");
2017-11-22 20:05:53 +03:00
/**
* @brief packageRender - the handler of all messages received.
* @param socket
*/
void packageRender(ETcpSocket* socket);
2017-12-03 23:08:43 +03:00
2017-11-22 22:34:55 +03:00
/**
* @brief deepScaned scaning in local network
*/
void deepScaned(QList<ETcpSocket *> *);
2017-11-22 20:05:53 +03:00
2017-12-03 23:08:43 +03:00
/**
* @brief endPlay signal has ending playing
*/
void endPlay(QMediaPlayer::State state);
2017-10-29 14:47:36 +03:00
public:
2018-01-19 15:03:07 +03:00
/**
* @brief getSqlApi
* @return pointer of sql api
*/
MySql* getSqlApi();
2017-11-20 00:37:12 +03:00
/**
* @brief Play song in this device, if device has not supported playning media data this method throw MediaExcrption.
* @param header of song
2018-01-09 22:14:54 +03:00
* @param fbroadcast - server broadcasting sound.
2017-11-20 00:37:12 +03:00
* @return true if all done else false.
*/
2018-01-09 22:14:54 +03:00
bool play(const SongHeader &header, bool fbroadcast = true);
2017-11-11 14:03:14 +03:00
/**
* @brief Play song in this device, if device has not supported playning media data this method throw MediaExcrption.
* @param song playning media data.
2018-01-09 22:14:54 +03:00
* @param fbroadcast - server broadcasting sound.
2017-11-11 14:03:14 +03:00
* @return true if all done else false.
*/
2018-01-09 22:14:54 +03:00
bool play(const Song &song, bool fbroadcast = true);
2017-11-11 14:03:14 +03:00
/**
* @brief Play song from local media file.
* @param url of local media file.
* @return true if all done else false.
*/
2017-11-24 21:17:41 +03:00
bool play(QString url);
2017-11-11 14:03:14 +03:00
/**
* @brief Play song from local database by id.
* @param id_song of song.
2018-01-09 22:14:54 +03:00
* @param fbroadcast - server broadcasting sound.
2017-11-11 14:03:14 +03:00
* @return true if all done else false.
*/
2018-01-09 22:14:54 +03:00
bool play(int id_song, bool fbroadcast = true);
2017-11-11 14:03:14 +03:00
/**
* @brief Pause playning song.
2017-12-03 17:22:59 +03:00
* state - state of playning
2017-11-11 14:03:14 +03:00
*/
2018-01-09 22:14:54 +03:00
bool pause(bool state);
2017-11-11 14:03:14 +03:00
/**
* @brief stop playning song.
*/
2017-11-11 20:35:30 +03:00
void stop();
2018-01-09 22:14:54 +03:00
2017-11-11 14:03:14 +03:00
/**
* @brief jump - jump to new position of playning media data.
* @param seek - a new position of media data.
*/
2017-12-03 23:08:43 +03:00
void jump(const qint64 seek);
2018-01-09 22:14:54 +03:00
2017-11-20 00:37:12 +03:00
/**
* @brief sync with server
* @param sync - data of sync
*/
2018-01-09 22:14:54 +03:00
bool sync(const Syncer& sync, milliseconds ping);
2018-01-14 15:29:42 +03:00
/**
* @brief isReadyToSync
* @return true if node ready to sync;
*/
bool isReadyToSync()const;
2018-01-09 22:14:54 +03:00
/**
* @brief sync with clients
*/
void sync();
2017-11-27 19:43:11 +03:00
/**
* @brief addNode add new connect
* @param ip of connection
* @param port of connection
* @return true if all done
*/
bool addNode(const QString ip, int port);
/**
* @brief scan - search for existing servers
* result saved in servers
*/
void scan();
/**
* @brief getServersList
* @return list of servers
*/
const QList<ETcpSocket*>& getServersList() const;
2018-01-19 15:03:07 +03:00
/**
* @brief listen - a server
* @param server - host
* @return true id all done
*/
2017-11-27 19:43:11 +03:00
bool listen(ETcpSocket* server);
2017-11-26 19:19:43 +03:00
/**
* @brief getVersion
* @return curent version of library
*/
QString getVersion();
2017-11-29 20:29:34 +03:00
2017-12-03 17:22:59 +03:00
/**
* @brief setValume max valume is 100
* @return true if all done
*/
bool setValume(unsigned int value);
2017-12-03 23:08:43 +03:00
2017-12-03 17:22:59 +03:00
/**
* @brief getvalume
* This property holds the current playback volume.
* The playback volume is scaled linearly,
* ranging from 0 (silence) to 100 (full volume).
* Values outside this range will be clamped.
*/
unsigned int getValume() const;
2017-12-03 23:08:43 +03:00
2017-12-03 17:22:59 +03:00
/**
* @brief seek
* @return curent playning milisecond
*/
unsigned int seek()const;
2017-12-03 23:08:43 +03:00
/**
* @brief getPlayList
* @return list of available songs
*/
const QList<SongHeader> *getPlayList() const;
2018-02-10 20:52:21 +03:00
/**
* @brief SongHeader::getCurentSongIndex
* @return
*/
int getCurentSongIndex()const;
2017-12-03 23:08:43 +03:00
/**
* @brief getCurentSong
* @return playing song.
*/
const SongHeader *getCurentSong() const;
2017-12-24 22:04:40 +03:00
/**
* @brief getEndPoint
* @return end point of playng song.
*/
qint64 getEndPoint() const;
2017-12-03 23:08:43 +03:00
/**
* @brief addNewSong push a new song into local database.
* @param name - name of pushed song
2017-12-06 12:10:04 +03:00
* @return id of song.
2017-12-03 23:08:43 +03:00
*/
2017-12-06 12:10:04 +03:00
int addNewSong(const QString &url);
2017-12-03 23:08:43 +03:00
2018-01-22 18:27:59 +03:00
/**
* @brief updatePlayList this method set a new playlist
* @param id - id of playlist
* @return true if all don
*/
bool updatePlayList(const QString& _playList);
2018-01-19 15:03:07 +03:00
Sync(const QString &address = DEFAULT_ADRESS, int port = DEFAULT_PORT, const QString& datadir = DATABASE_NAME);
2017-10-29 14:47:36 +03:00
~Sync();
2017-12-03 17:22:59 +03:00
signals:
/**
* @brief seekChanged
* @param seek
* Signal the position of the content has changed to position,
* expressed in milliseconds.
*/
void seekChanged(qint64 seek);
/**
* @brief networkStateChange
* signal if changed count of activity servers.
*/
void networkStateChange();
2017-12-03 23:08:43 +03:00
2018-03-06 22:10:46 +03:00
/**
* @brief curentPlayListChanged
* emited when added new songs into active playlist
*/
void curentPlayListChanged();
2018-03-08 16:08:40 +03:00
/**
* @brief curentPlayListChanged
* emited when selected a new playList
*/
void selectedNewPlatList();
2017-10-29 14:47:36 +03:00
};
}
#endif // SYNC_H