2011-04-26 15:43:13 +02:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2011, David Sansome <me@davidsansome.com>
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Note: this file is licensed under the Apache License instead of GPL because
|
|
|
|
// it is used by the Spotify blob which links against libspotify and is not GPL
|
|
|
|
// compatible.
|
|
|
|
|
2011-04-25 21:16:26 +02:00
|
|
|
#ifndef SPOTIFYCLIENT_H
|
|
|
|
#define SPOTIFYCLIENT_H
|
|
|
|
|
2011-04-26 19:06:36 +02:00
|
|
|
#include "spotifymessages.pb.h"
|
2012-01-08 00:30:33 +01:00
|
|
|
#include "core/messagehandler.h"
|
2011-04-26 15:42:58 +02:00
|
|
|
|
2011-04-27 18:38:28 +02:00
|
|
|
#include <QMap>
|
2011-04-25 21:16:26 +02:00
|
|
|
#include <QObject>
|
|
|
|
|
2011-04-26 15:42:58 +02:00
|
|
|
#include <libspotify/api.h>
|
|
|
|
|
2011-04-25 21:16:26 +02:00
|
|
|
class QTcpSocket;
|
2011-04-26 15:42:58 +02:00
|
|
|
class QTimer;
|
2011-04-25 21:16:26 +02:00
|
|
|
|
2011-11-28 19:11:09 +01:00
|
|
|
class MediaPipeline;
|
2011-04-25 21:16:26 +02:00
|
|
|
class ResponseMessage;
|
|
|
|
|
2021-02-20 22:20:04 +01:00
|
|
|
class SpotifyClient : public AbstractMessageHandler<cpb::spotify::Message> {
|
2011-04-25 21:16:26 +02:00
|
|
|
Q_OBJECT
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
public:
|
2014-02-10 16:03:54 +01:00
|
|
|
SpotifyClient(QObject* parent = nullptr);
|
2011-04-26 15:42:58 +02:00
|
|
|
~SpotifyClient();
|
2011-04-25 21:16:26 +02:00
|
|
|
|
2011-04-29 15:41:42 +02:00
|
|
|
static const int kSpotifyImageIDSize;
|
2011-09-25 20:24:44 +02:00
|
|
|
static const int kWaveHeaderSize;
|
2011-04-29 15:41:42 +02:00
|
|
|
|
2011-04-25 21:16:26 +02:00
|
|
|
void Init(quint16 port);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
protected:
|
2021-02-20 22:20:04 +01:00
|
|
|
void MessageArrived(const cpb::spotify::Message& message);
|
2012-01-22 19:42:40 +01:00
|
|
|
void DeviceClosed();
|
2012-01-08 00:30:33 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
private slots:
|
2011-04-26 15:42:58 +02:00
|
|
|
void ProcessEvents();
|
2011-04-25 21:16:26 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
private:
|
2011-08-27 23:01:28 +02:00
|
|
|
void SendLoginCompleted(bool success, const QString& error,
|
2021-02-20 22:20:04 +01:00
|
|
|
cpb::spotify::LoginResponse_Error error_code);
|
2011-04-26 19:06:46 +02:00
|
|
|
void SendPlaybackError(const QString& error);
|
2011-09-17 16:57:34 +02:00
|
|
|
void SendSearchResponse(sp_search* result);
|
2011-04-26 15:42:58 +02:00
|
|
|
|
|
|
|
// Spotify session callbacks.
|
2011-04-30 20:22:14 +02:00
|
|
|
static void SP_CALLCONV LoggedInCallback(sp_session* session, sp_error error);
|
|
|
|
static void SP_CALLCONV NotifyMainThreadCallback(sp_session* session);
|
2014-02-07 16:34:20 +01:00
|
|
|
static void SP_CALLCONV
|
|
|
|
LogMessageCallback(sp_session* session, const char* data);
|
|
|
|
static void SP_CALLCONV
|
|
|
|
SearchCompleteCallback(sp_search* result, void* userdata);
|
2011-04-30 20:22:14 +02:00
|
|
|
static void SP_CALLCONV MetadataUpdatedCallback(sp_session* session);
|
2014-02-07 16:34:20 +01:00
|
|
|
static int SP_CALLCONV
|
|
|
|
MusicDeliveryCallback(sp_session* session, const sp_audioformat* format,
|
|
|
|
const void* frames, int num_frames);
|
2011-04-30 20:22:14 +02:00
|
|
|
static void SP_CALLCONV EndOfTrackCallback(sp_session* session);
|
2014-02-07 16:34:20 +01:00
|
|
|
static void SP_CALLCONV
|
|
|
|
StreamingErrorCallback(sp_session* session, sp_error error);
|
2011-05-25 16:22:49 +02:00
|
|
|
static void SP_CALLCONV OfflineStatusUpdatedCallback(sp_session* session);
|
2014-02-07 16:34:20 +01:00
|
|
|
static void SP_CALLCONV
|
|
|
|
ConnectionErrorCallback(sp_session* session, sp_error error);
|
|
|
|
static void SP_CALLCONV
|
|
|
|
UserMessageCallback(sp_session* session, const char* message);
|
2011-11-24 14:19:10 +01:00
|
|
|
static void SP_CALLCONV StartPlaybackCallback(sp_session* session);
|
|
|
|
static void SP_CALLCONV StopPlaybackCallback(sp_session* session);
|
2011-04-26 15:42:58 +02:00
|
|
|
|
|
|
|
// Spotify playlist container callbacks.
|
2014-02-07 16:34:20 +01:00
|
|
|
static void SP_CALLCONV PlaylistAddedCallback(sp_playlistcontainer* pc,
|
|
|
|
sp_playlist* playlist,
|
|
|
|
int position, void* userdata);
|
|
|
|
static void SP_CALLCONV PlaylistRemovedCallback(sp_playlistcontainer* pc,
|
|
|
|
sp_playlist* playlist,
|
|
|
|
int position, void* userdata);
|
|
|
|
static void SP_CALLCONV
|
|
|
|
PlaylistMovedCallback(sp_playlistcontainer* pc, sp_playlist* playlist,
|
|
|
|
int position, int new_position, void* userdata);
|
|
|
|
static void SP_CALLCONV
|
|
|
|
PlaylistContainerLoadedCallback(sp_playlistcontainer* pc, void* userdata);
|
2011-04-26 15:42:58 +02:00
|
|
|
|
2011-04-27 18:38:28 +02:00
|
|
|
// Spotify playlist callbacks - when loading the list of playlists
|
|
|
|
// initially
|
2014-02-07 16:34:20 +01:00
|
|
|
static void SP_CALLCONV
|
|
|
|
PlaylistStateChangedForGetPlaylists(sp_playlist* pl, void* userdata);
|
2011-04-27 18:38:28 +02:00
|
|
|
|
2011-04-26 19:06:36 +02:00
|
|
|
// Spotify playlist callbacks - when loading a playlist
|
2014-02-07 16:34:20 +01:00
|
|
|
static void SP_CALLCONV
|
|
|
|
PlaylistStateChangedForLoadPlaylist(sp_playlist* pl, void* userdata);
|
2011-04-26 19:06:36 +02:00
|
|
|
|
2011-04-29 15:41:42 +02:00
|
|
|
// Spotify image callbacks.
|
2011-04-30 20:22:14 +02:00
|
|
|
static void SP_CALLCONV ImageLoaded(sp_image* image, void* userdata);
|
2011-04-29 15:41:42 +02:00
|
|
|
|
2011-08-29 04:26:59 +02:00
|
|
|
// Spotify album browse callbacks.
|
2014-02-07 16:34:20 +01:00
|
|
|
static void SP_CALLCONV
|
|
|
|
SearchAlbumBrowseComplete(sp_albumbrowse* result, void* userdata);
|
|
|
|
static void SP_CALLCONV
|
|
|
|
AlbumBrowseComplete(sp_albumbrowse* result, void* userdata);
|
2011-08-29 04:26:59 +02:00
|
|
|
|
2012-06-12 15:34:23 +02:00
|
|
|
// Spotify toplist browse callbacks.
|
2014-02-07 16:34:20 +01:00
|
|
|
static void SP_CALLCONV
|
|
|
|
ToplistBrowseComplete(sp_toplistbrowse* result, void* userdata);
|
2012-06-12 15:34:23 +02:00
|
|
|
|
2011-04-26 15:42:58 +02:00
|
|
|
// Request handlers.
|
2021-02-20 22:20:04 +01:00
|
|
|
void Login(const cpb::spotify::LoginRequest& req);
|
|
|
|
void Search(const cpb::spotify::SearchRequest& req);
|
|
|
|
void LoadPlaylist(const cpb::spotify::LoadPlaylistRequest& req);
|
|
|
|
void SyncPlaylist(const cpb::spotify::SyncPlaylistRequest& req);
|
|
|
|
void AddTracksToPlaylist(const cpb::spotify::AddTracksToPlaylistRequest& req);
|
2014-10-15 21:57:57 +02:00
|
|
|
void RemoveTracksFromPlaylist(
|
2021-02-20 22:20:04 +01:00
|
|
|
const cpb::spotify::RemoveTracksFromPlaylistRequest& req);
|
|
|
|
void StartPlayback(const cpb::spotify::PlaybackRequest& req);
|
2014-09-21 16:13:00 +02:00
|
|
|
void Seek(qint64 offset_nsec);
|
2011-04-29 15:41:42 +02:00
|
|
|
void LoadImage(const QString& id_b64);
|
2011-08-29 04:26:59 +02:00
|
|
|
void BrowseAlbum(const QString& uri);
|
2021-02-20 22:20:04 +01:00
|
|
|
void BrowseToplist(const cpb::spotify::BrowseToplistRequest& req);
|
|
|
|
void SetPlaybackSettings(const cpb::spotify::PlaybackSettings& req);
|
|
|
|
void SetPaused(const cpb::spotify::PauseRequest& req);
|
2011-04-26 19:06:36 +02:00
|
|
|
|
2011-04-27 18:38:28 +02:00
|
|
|
void SendPlaylistList();
|
|
|
|
|
2021-02-20 22:20:04 +01:00
|
|
|
void ConvertTrack(sp_track* track, cpb::spotify::Track* pb);
|
|
|
|
void ConvertAlbum(sp_album* album, cpb::spotify::Track* pb);
|
|
|
|
void ConvertAlbumBrowse(sp_albumbrowse* browse, cpb::spotify::Track* pb);
|
2011-08-29 03:00:59 +02:00
|
|
|
|
2011-05-25 16:22:49 +02:00
|
|
|
// Gets the appropriate sp_playlist* but does not load it.
|
2021-02-20 22:20:04 +01:00
|
|
|
sp_playlist* GetPlaylist(cpb::spotify::PlaylistType type, int user_index);
|
2011-04-26 19:06:36 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
private:
|
2011-04-26 19:06:36 +02:00
|
|
|
struct PendingLoadPlaylist {
|
2021-02-20 22:20:04 +01:00
|
|
|
cpb::spotify::LoadPlaylistRequest request_;
|
2011-04-26 19:06:36 +02:00
|
|
|
sp_playlist* playlist_;
|
|
|
|
QList<sp_track*> tracks_;
|
2011-05-25 16:22:49 +02:00
|
|
|
bool offline_sync;
|
2011-04-26 19:06:36 +02:00
|
|
|
};
|
2011-04-25 21:16:26 +02:00
|
|
|
|
2011-04-28 17:11:10 +02:00
|
|
|
struct PendingPlaybackRequest {
|
2021-02-20 22:20:04 +01:00
|
|
|
cpb::spotify::PlaybackRequest request_;
|
2011-04-28 17:11:10 +02:00
|
|
|
sp_link* link_;
|
|
|
|
sp_track* track_;
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
bool operator==(const PendingPlaybackRequest& other) const {
|
2011-04-28 17:11:10 +02:00
|
|
|
return request_.track_uri() == other.request_.track_uri() &&
|
|
|
|
request_.media_port() == other.request_.media_port();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-04-29 15:41:42 +02:00
|
|
|
struct PendingImageRequest {
|
|
|
|
QString id_b64_;
|
|
|
|
QByteArray id_;
|
|
|
|
sp_image* image_;
|
|
|
|
};
|
|
|
|
|
2011-04-28 17:11:10 +02:00
|
|
|
void TryPlaybackAgain(const PendingPlaybackRequest& req);
|
2011-04-29 15:41:42 +02:00
|
|
|
void TryImageAgain(sp_image* image);
|
2011-05-25 16:22:49 +02:00
|
|
|
int GetDownloadProgress(sp_playlist* playlist);
|
2021-02-20 22:20:04 +01:00
|
|
|
void SendDownloadProgress(cpb::spotify::PlaylistType type, int index,
|
2014-02-07 16:34:20 +01:00
|
|
|
int download_progress);
|
2011-04-28 17:11:10 +02:00
|
|
|
|
2011-04-26 15:43:13 +02:00
|
|
|
QByteArray api_key_;
|
|
|
|
|
2011-04-26 19:06:46 +02:00
|
|
|
QTcpSocket* protocol_socket_;
|
2011-04-26 15:42:58 +02:00
|
|
|
|
|
|
|
sp_session_config spotify_config_;
|
|
|
|
sp_session_callbacks spotify_callbacks_;
|
|
|
|
sp_playlistcontainer_callbacks playlistcontainer_callbacks_;
|
2011-04-27 18:38:28 +02:00
|
|
|
sp_playlist_callbacks get_playlists_callbacks_;
|
2011-04-26 19:06:36 +02:00
|
|
|
sp_playlist_callbacks load_playlist_callbacks_;
|
2011-04-26 15:42:58 +02:00
|
|
|
sp_session* session_;
|
|
|
|
|
|
|
|
QTimer* events_timer_;
|
2011-04-26 19:06:36 +02:00
|
|
|
|
|
|
|
QList<PendingLoadPlaylist> pending_load_playlists_;
|
2011-04-28 17:11:10 +02:00
|
|
|
QList<PendingPlaybackRequest> pending_playback_requests_;
|
2011-04-29 15:41:42 +02:00
|
|
|
QList<PendingImageRequest> pending_image_requests_;
|
|
|
|
QMap<sp_image*, int> image_callbacks_registered_;
|
2021-02-20 22:20:04 +01:00
|
|
|
QMap<sp_search*, cpb::spotify::SearchRequest> pending_searches_;
|
2011-08-29 04:26:59 +02:00
|
|
|
QMap<sp_albumbrowse*, QString> pending_album_browses_;
|
2021-02-20 22:20:04 +01:00
|
|
|
QMap<sp_toplistbrowse*, cpb::spotify::BrowseToplistRequest>
|
2014-02-07 16:34:20 +01:00
|
|
|
pending_toplist_browses_;
|
2011-04-26 19:06:46 +02:00
|
|
|
|
2014-10-15 21:57:57 +02:00
|
|
|
QMap<sp_search*, QList<sp_albumbrowse*>> pending_search_album_browses_;
|
2011-09-17 16:57:34 +02:00
|
|
|
QMap<sp_albumbrowse*, sp_search*> pending_search_album_browse_responses_;
|
|
|
|
|
2011-11-28 19:11:09 +01:00
|
|
|
QScopedPointer<MediaPipeline> media_pipeline_;
|
2011-04-25 21:16:26 +02:00
|
|
|
};
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
#endif // SPOTIFYCLIENT_H
|