コード例 #1
0
ファイル: test_util.cpp プロジェクト: jizhongqing/cobalt-vad
string makeAndPrintJson(const VadEvents& events)
{
    JsonValuePtr jsonEvents = serializeVadEvents(events);
    Json::FastWriter fastWriter;
    fastWriter.omitEndingLineFeed();
    string serializedEvents = fastWriter.write(*jsonEvents);
    cout << "Serialized vad events " << endl << serializedEvents << endl;
    return serializedEvents;
}
コード例 #2
0
ファイル: chromecast.cpp プロジェクト: redanium/c8tsender
void ChromeCast::_read()
{
	while (true)
	{
		char pktlen[4];
		int r;
		for (r = 0; r < sizeof pktlen; ++r)
			if (SSL_read(m_ssl, pktlen + r, 1) < 1)
				break;
		if (r != sizeof pktlen) {
			syslog(LOG_ERR, "SSL_read error");
			_release_waiters();
			return;
		}

		unsigned int len;
		memcpy(&len, pktlen, sizeof len);
		len = ntohl(len);

		std::string buf;
		while (buf.size() < len) {
			char b[2048];
			int r = SSL_read(m_ssl, b, sizeof b);
			if (r < 1)
				break;
			buf.append(b, r);
		}
		if (buf.size() != len) {
			syslog(LOG_ERR, "SSL_read error");
			_release_waiters();
			return;
		}

		extensions::core_api::cast_channel::CastMessage msg;
		msg.ParseFromString(buf);

		syslog(LOG_DEBUG, "%s -> %s (%s): %s",
				msg.source_id().c_str(),
				msg.destination_id().c_str(),
				msg.namespace_().c_str(),
				msg.payload_utf8().c_str()
			  );

		Json::Value response;
		Json::Reader reader;
		if (!reader.parse(msg.payload_utf8(), response, false))
			continue;

		if (msg.namespace_() == "urn:x-cast:com.google.cast.tp.heartbeat")
		{
			Json::FastWriter fw;
			fw.omitEndingLineFeed();

			extensions::core_api::cast_channel::CastMessage reply(msg);
			Json::Value msg;
			msg["type"] = "PONG";
			reply.set_payload_utf8(fw.write(msg));
			std::string data;
			reply.SerializeToString(&data);
			unsigned int len = htonl(data.size());
			SSL_write(m_ssl, &len, sizeof len);
			SSL_write(m_ssl, data.c_str(), data.size());
			continue;
		}

		if (msg.namespace_() == "urn:x-cast:com.google.cast.tp.connection")
		{
			if (response["type"].asString() == "CLOSE")
			{
				_release_waiters();
				m_init = false;
			}
		}

		if (msg.namespace_() == "urn:x-cast:com.google.cast.media")
		{
			if (response["type"].asString() == "MEDIA_STATUS")
			{
				if (response.isMember("status") &&
					response["status"].isValidIndex(0u))
				{
					Json::Value& status = response["status"][0u];
					m_media_session_id = status["mediaSessionId"].asUInt();
				}
			}
		}

		if (response.isMember("requestId"))
		{
			m_mutex.lock();
			auto waitIter = m_wait.find(response["requestId"].asUInt());
			if (waitIter != m_wait.end())
			{
				waitIter->second.second = response;
				waitIter->second.first->notify_all();
			}
			m_mutex.unlock();
		}

		if (msg.namespace_() == "urn:x-cast:com.google.cast.media")
		{
			if (response["type"].asString() == "MEDIA_STATUS")
			{
				if (response.isMember("status") &&
					response["status"].isValidIndex(0u))
				{
					std::string uuid = m_uuid;
					Json::Value& status = response["status"][0u];
					if (status.isMember("activeTrackIds"))
						m_subtitles = status["activeTrackIds"].isValidIndex(0u);
					m_player_state = status["playerState"].asString();
					m_player_current_time = status["currentTime"].asDouble();
					m_player_current_time_update = time(NULL);
					if (status["playerState"] == "IDLE")
						m_uuid = "";
					if (status["playerState"] != "IDLE" &&
							!status["media"]["customData"]["uuid"].asString().empty())
						uuid = m_uuid = status["media"]["customData"]["uuid"].asString();
					if (m_mediaStatusCallback)
						m_mediaStatusCallback(status["playerState"].asString(),
								status["idleReason"].asString(),
								uuid);
				}
			}
		}
	}
}
コード例 #3
0
ファイル: chromecast.cpp プロジェクト: redanium/c8tsender
Json::Value ChromeCast::send(const std::string& namespace_, const Json::Value& payload)
{
	Json::FastWriter fw;
	fw.omitEndingLineFeed();

	extensions::core_api::cast_channel::CastMessage msg;
	msg.set_payload_type(msg.STRING);
	msg.set_protocol_version(msg.CASTV2_1_0);
	msg.set_namespace_(namespace_);
	msg.set_source_id(m_source_id);
	msg.set_destination_id(m_destination_id);
	msg.set_payload_utf8(fw.write(payload));

	std::string data, foo;
	char pktlen[4];
	unsigned int len = htonl(msg.ByteSize());
	memcpy(&pktlen, &len, sizeof len);
	foo.append(pktlen, sizeof pktlen);
	msg.SerializeToString(&data);
	foo += data;

	std::condition_variable f;
	bool wait = false;
	unsigned int requestId;
	if (payload.isMember("requestId"))
	{
		requestId = payload["requestId"].asUInt();
		m_mutex.lock();
		m_wait[requestId] = std::make_pair(&f, std::string());
		wait = true;
		m_mutex.unlock();
	}

	syslog(LOG_DEBUG, "%s -> %s (%s): %s",
			msg.source_id().c_str(),
			msg.destination_id().c_str(),
			msg.namespace_().c_str(),
			msg.payload_utf8().c_str()
		  );

	int w;
	m_ssl_mutex.lock();
	if (m_ssl) {
		w = SSL_write(m_ssl, foo.c_str(), foo.size());
		if (w == -1) {
			syslog(LOG_DEBUG, "SSL_write error");
			disconnect();
		}
	} else
		w = -1;
	m_ssl_mutex.unlock();

	if (wait && w == -1)
	{
		m_mutex.lock();
		m_wait.erase(requestId);
		m_mutex.unlock();
		wait = false;
	}

	Json::Value ret;
	if (wait) {
		std::unique_lock<std::mutex> wl(m_mutex);
		f.wait(wl);
		ret = m_wait[requestId].second;
		m_wait.erase(requestId);
		m_mutex.unlock();
	}

	return ret;
}