Exemplo n.º 1
0
        private static byte[] GetApplicationKey(SessionConfig config)
        {
            ThrowHelper.ThrowIfNull(config, "config");
            if (string.IsNullOrEmpty(config.ApplicationKeyFile) && config.ApplicationKey == null)
            {
                throw new ArgumentException("both ApplicationKey and ApplicationKeyFile are unset in SessionConfig");
            }

            byte[] key = config.ApplicationKey;
            if (key == null)
            {
                key = File.ReadAllBytes(config.ApplicationKeyFile);
            }
            return(key);
        }
Exemplo n.º 2
0
        public static Session Create(SessionConfig sessionConfig)
        {
            Session session = new Session();

            IntPtr applicationKeyHandle = IntPtr.Zero;
            IntPtr callbacksHandle      = IntPtr.Zero;

            try
            {
                LibSpotify.sp_session_config config = new LibSpotify.sp_session_config();
                config.api_version = sessionConfig.ApiVersion;

                byte[] key = GetApplicationKey(sessionConfig);
                applicationKeyHandle = Marshal.AllocHGlobal(key.Length);
                Marshal.Copy(key, 0, applicationKeyHandle, key.Length);
                config.application_key = applicationKeyHandle;

                config.application_key_size             = new IntPtr(key.Length);
                config.cache_location                   = sessionConfig.CacheLocation;
                config.compress_playlists               = sessionConfig.CompressPlaylists;
                config.device_id                        = sessionConfig.DeviceId;
                config.dont_dave_metadata_for_playlists = sessionConfig.DontSaveMetadatForPlaylists;
                config.initially_unload_playlist        = sessionConfig.InitiallyUnloadPlaylists;
                config.proxy             = sessionConfig.Proxy;
                config.proxy_password    = sessionConfig.ProxyPassword;
                config.proxy_username    = sessionConfig.ProxyUserName;
                config.settings_location = sessionConfig.SettingsLocation;
                config.tracefile         = sessionConfig.TraceFile;
                config.user_agent        = sessionConfig.UserAgent;
                config.userdata          = IntPtr.Zero;

                session._callbacks                    = new LibSpotify.sp_session_callbacks();
                session._callbacks.logged_in          = session.OnLoggedInEvent;
                session._callbacks.logged_out         = session.OnLoggedOutEvent;
                session._callbacks.notify_main_thread = session.OnNotifyMainThread;

                session._callbacks.log_message                  = session.OnLogMessage;
                session._callbacks.metadata_updated             = session.OnMetadataUpdated;
                session._callbacks.connection_error             = session.OnConnectionError;
                session._callbacks.message_to_user              = session.OnMessageToUser;
                session._callbacks.music_delivery               = session.OnMusicDelivery;
                session._callbacks.play_token_lost              = session.OnPlayTokenLost;
                session._callbacks.log_message                  = session.OnLogMessage;
                session._callbacks.end_of_track                 = session.OnEndOfTrack;
                session._callbacks.streaming_error              = session.OnStreamingError;
                session._callbacks.userinfo_updated             = session.OnUserInfoUpdated;
                session._callbacks.start_playback               = session.OnStartPlayback;
                session._callbacks.stop_playback                = session.OnStopPlayback;
                session._callbacks.offline_status_updated       = session.OnOfflineStatusUpdated;
                session._callbacks.get_audio_buffer_stats       = session.OnGetAudioBufferStats;
                session._callbacks.offline_error                = session.OnOfflineError;
                session._callbacks.credentials_blob_updated     = session.OnCredentialsBlobUpdated;
                session._callbacks.connectionstate_updated      = session.OnConnectionStateUpdated;
                session._callbacks.scrobble_error               = session.OnScrobbleError;
                session._callbacks.private_session_mode_changed = session.OnPrivateSessionModeChanged;

                callbacksHandle = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(LibSpotify.sp_session_callbacks)));
                Marshal.StructureToPtr(session._callbacks, callbacksHandle, false);
                config.callbacks = callbacksHandle;

                IntPtr ptr = IntPtr.Zero;

                // This call results in a callback to OnNotifyMainThread, but the
                // actual _handle has not been set yet.
                session._running = true;
                ThrowHelper.ThrowIfError(LibSpotify.sp_session_create_r(ref config, ref ptr));
                session.Handle = ptr;
            }
            finally
            {
                if (applicationKeyHandle != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(applicationKeyHandle);
                }

                if (callbacksHandle != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(callbacksHandle);
                }
            }

            return(session);
        }