void ApplicationControl::serverPooling() { if (server->getStatus() == SS_CONNECTED) { emit setConnectionStatus("connected"); } else { // if (server->getStatus() == SS_DISCONNECTED) server->connectToServer(); connectionTimer.start(); } }
/** * @brief Disconnect from the Chromecast */ void intf_sys_t::disconnectChromecast() { if (p_tls) { vlc_tls_SessionDelete(p_tls); vlc_tls_Delete(p_creds); p_tls = NULL; setConnectionStatus(CHROMECAST_DISCONNECTED); } }
static void setNodeConnectionStatus(Node * node, boolean status) { Connection *connect; for (connect = getConnection(node); connect; connect = getNextConnection(connect)) if (getNodeStatus (getTwinNode(getConnectionDestination(connect)))) setConnectionStatus(connect, status); }
bool QSpotifySession::event(QEvent *e) { if (e->type() == NotifyMainThreadEventType) { qDebug() << "Process spotify event"; processSpotifyEvents(); e->accept(); return true; } else if (e->type() == QEvent::Timer) { qDebug() << "Timer, start spotify events"; QTimerEvent *te = static_cast<QTimerEvent *>(e); if (te->timerId() == m_timerID) { processSpotifyEvents(); e->accept(); return true; } } else if (e->type() == ConnectionErrorEventType) { qDebug() << "Connection error"; QSpotifyConnectionErrorEvent *ev = static_cast<QSpotifyConnectionErrorEvent *>(e); setConnectionError(ConnectionError(ev->error()), QString::fromUtf8(sp_error_message(ev->error()))); e->accept(); return true; } else if (e->type() == MetaDataEventType) { qDebug() << "Meta data"; emit metadataUpdated(); e->accept(); return true; } else if (e->type() == EndOfTrackEventType) { qDebug() << "End track"; m_trackChangedAutomatically = true; playNext(); e->accept(); return true; } else if (e->type() == StopEventType) { qDebug() << "Stop"; stop(); e->accept(); return true; } else if (e->type() == TrackProgressEventType) { qDebug() << "Track progress"; if(!m_isPlaying) { e->accept(); return true; } // Track progressed QSpotifyTrackProgressEvent *ev = static_cast<QSpotifyTrackProgressEvent *>(e); int currentTrackPositionDelta = ev->delta(); if (m_previousTrackRemaining > 0) { // We're still playing the previous back from our buffer int fromPreviousTrack = qMin(currentTrackPositionDelta, m_previousTrackRemaining); currentTrackPositionDelta -= fromPreviousTrack; m_previousTrackRemaining -= fromPreviousTrack; } m_currentTrackPosition += currentTrackPositionDelta; m_currentTrackPlayedDuration += currentTrackPositionDelta; emit currentTrackPositionChanged(); e->accept(); return true; } else if (e->type() == SendImageRequestEventType) { qDebug() << "Send image request"; QSpotifyRequestImageEvent *ev = static_cast<QSpotifyRequestImageEvent *>(e); sendImageRequest(ev->imageId()); e->accept(); return true; } else if (e->type() == ReceiveImageRequestEventType) { qDebug() << "Receive image request"; QSpotifyReceiveImageEvent *ev = static_cast<QSpotifyReceiveImageEvent *>(e); receiveImageResponse(ev->image()); e->accept(); return true; } else if (e->type() == PlayTokenLostEventType) { qDebug() << "Play token lost"; emit playTokenLost(); pause(); e->accept(); return true; } else if (e->type() == LoggedInEventType) { qDebug() << "Logged in 1"; onLoggedIn(); e->accept(); return true; } else if (e->type() == LoggedOutEventType) { qDebug() << "Logged out"; onLoggedOut(); e->accept(); return true; } else if (e->type() == OfflineErrorEventType) { qDebug() << "Offline error"; QSpotifyOfflineErrorEvent *ev = static_cast<QSpotifyOfflineErrorEvent *>(e); m_offlineErrorMessage = QString::fromUtf8(sp_error_message(ev->error())); emit offlineErrorMessageChanged(); e->accept(); return true; } else if (e->type() == ScrobbleLoginErrorEventType) { qDebug() << "Scrobble login error"; m_lfmLoggedIn = false; emit lfmLoggedInChanged(); emit lfmLoginError(); e->accept(); return true; } else if (e->type() == ConnectionStateUpdateEventType) { qDebug() << "Connectionstate update event"; setConnectionStatus(ConnectionStatus(sp_session_connectionstate(m_sp_session))); if (m_offlineMode && m_connectionStatus == LoggedIn) { setConnectionRules(m_connectionRules | AllowNetwork); setConnectionRules(m_connectionRules & ~AllowNetwork); } e->accept(); return true; } return QObject::event(e); }
void ApplicationControl::onServerDisconnected(QString reason) { if (connectionState == ApplicationConnectionState::CONNECTED) connectionState = ApplicationConnectionState::CONNECTION_LOST; emit setConnectionStatus(reason); }
void intf_sys_t::handleMessages() { unsigned i_received = 0; uint8_t p_packet[PACKET_MAX_LEN]; bool b_pingTimeout = false; int i_waitdelay = PING_WAIT_TIME; int i_retries = PING_WAIT_RETRIES; bool b_msgReceived = false; uint32_t i_payloadSize = 0; int i_ret = recvPacket(p_stream, b_msgReceived, i_payloadSize, i_sock_fd, p_tls, &i_received, p_packet, &b_pingTimeout, &i_waitdelay, &i_retries); int canc = vlc_savecancel(); // Not cancellation-safe part. #if defined(_WIN32) if ((i_ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK) || (i_ret == 0)) #else if ((i_ret < 0 && errno != EAGAIN) || i_ret == 0) #endif { msg_Err(p_stream, "The connection to the Chromecast died (receiving)."); vlc_mutex_locker locker(&lock); setConnectionStatus(CHROMECAST_CONNECTION_DEAD); vlc_restorecancel(canc); return; } if (b_pingTimeout) { msgPing(); msgReceiverGetStatus(); } if (b_msgReceived) { castchannel::CastMessage msg; msg.ParseFromArray(p_packet + PACKET_HEADER_LEN, i_payloadSize); processMessage(msg); } // Send the answer messages if there is any. if (!messagesToSend.empty()) { i_ret = sendMessages(); #if defined(_WIN32) if ((i_ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK) || (i_ret == 0)) #else if ((i_ret < 0 && errno != EAGAIN) || i_ret == 0) #endif { msg_Err(p_stream, "The connection to the Chromecast died (sending)."); vlc_mutex_locker locker(&lock); setConnectionStatus(CHROMECAST_CONNECTION_DEAD); } } vlc_restorecancel(canc); }
/** * @brief Process a message received from the Chromecast * @param msg the CastMessage to process * @return 0 if the message has been successfuly processed else -1 */ void intf_sys_t::processMessage(const castchannel::CastMessage &msg) { const std::string & namespace_ = msg.namespace_(); #ifndef NDEBUG msg_Dbg(p_stream,"processMessage: %s->%s %s", namespace_.c_str(), msg.destination_id().c_str(), msg.payload_utf8().c_str()); #endif if (namespace_ == NAMESPACE_DEVICEAUTH) { castchannel::DeviceAuthMessage authMessage; authMessage.ParseFromString(msg.payload_binary()); if (authMessage.has_error()) { msg_Err(p_stream, "Authentification error: %d", authMessage.error().error_type()); } else if (!authMessage.has_response()) { msg_Err(p_stream, "Authentification message has no response field"); } else { vlc_mutex_locker locker(&lock); setConnectionStatus(CHROMECAST_AUTHENTICATED); msgConnect(DEFAULT_CHOMECAST_RECEIVER); msgReceiverLaunchApp(); } } else if (namespace_ == NAMESPACE_HEARTBEAT) { json_value *p_data = json_parse(msg.payload_utf8().c_str()); std::string type((*p_data)["type"]); if (type == "PING") { msg_Dbg(p_stream, "PING received from the Chromecast"); msgPong(); } else if (type == "PONG") { msg_Dbg(p_stream, "PONG received from the Chromecast"); } else { msg_Warn(p_stream, "Heartbeat command not supported: %s", type.c_str()); } json_value_free(p_data); } else if (namespace_ == NAMESPACE_RECEIVER) { json_value *p_data = json_parse(msg.payload_utf8().c_str()); std::string type((*p_data)["type"]); if (type == "RECEIVER_STATUS") { json_value applications = (*p_data)["status"]["applications"]; const json_value *p_app = NULL; vlc_mutex_locker locker(&lock); for (unsigned i = 0; i < applications.u.array.length; ++i) { std::string appId(applications[i]["appId"]); if (appId == APP_ID) { const char *pz_transportId = applications[i]["transportId"]; if (pz_transportId != NULL) { appTransportId = std::string(pz_transportId); p_app = &applications[i]; } break; } } if ( p_app ) { if (!appTransportId.empty() && getConnectionStatus() == CHROMECAST_AUTHENTICATED) { msgConnect(appTransportId); setConnectionStatus(CHROMECAST_APP_STARTED); msgPlayerLoad(); setConnectionStatus(CHROMECAST_MEDIA_LOAD_SENT); vlc_cond_signal(&loadCommandCond); } } else { switch(getConnectionStatus()) { /* If the app is no longer present */ case CHROMECAST_APP_STARTED: case CHROMECAST_MEDIA_LOAD_SENT: msg_Warn(p_stream, "app is no longer present. closing"); msgReceiverClose(appTransportId); setConnectionStatus(CHROMECAST_CONNECTION_DEAD); break; case CHROMECAST_AUTHENTICATED: msg_Dbg(p_stream, "Chromecast was running no app, launch media_app"); appTransportId = ""; msgReceiverLaunchApp(); break; default: break; } } } else if (type == "LAUNCH_ERROR") { json_value reason = (*p_data)["reason"]; msg_Err(p_stream, "Failed to start the MediaPlayer: %s", (const char *)reason); } else { msg_Warn(p_stream, "Receiver command not supported: %s", msg.payload_utf8().c_str()); } json_value_free(p_data); } else if (namespace_ == NAMESPACE_MEDIA) { json_value *p_data = json_parse(msg.payload_utf8().c_str()); std::string type((*p_data)["type"]); if (type == "MEDIA_STATUS") { json_value status = (*p_data)["status"]; msg_Dbg(p_stream, "Player state: %s sessionId:%d", status[0]["playerState"].operator const char *(), (int)(json_int_t) status[0]["mediaSessionId"]); } else if (type == "LOAD_FAILED") { msg_Err(p_stream, "Media load failed"); msgReceiverClose(appTransportId); vlc_mutex_locker locker(&lock); setConnectionStatus(CHROMECAST_CONNECTION_DEAD); } else if (type == "INVALID_REQUEST") { msg_Dbg(p_stream, "We sent an invalid request reason:%s", (*p_data)["reason"].operator const char *()); } else { msg_Warn(p_stream, "Media command not supported: %s", msg.payload_utf8().c_str()); } json_value_free(p_data); } else if (namespace_ == NAMESPACE_CONNECTION) { json_value *p_data = json_parse(msg.payload_utf8().c_str()); std::string type((*p_data)["type"]); json_value_free(p_data); if (type == "CLOSE") { msg_Warn(p_stream, "received close message"); vlc_mutex_locker locker(&lock); setConnectionStatus(CHROMECAST_CONNECTION_DEAD); } else { msg_Warn(p_stream, "Connection command not supported: %s", type.c_str()); } } else { msg_Err(p_stream, "Unknown namespace: %s", msg.namespace_().c_str()); } }