// We constantly update the track time to event about it to our clients.
static void *thread_update_track_time(void *userdata) {
	const gint64 one_sec_unit = 1000000000LL;
	char tbuf[32];
	gint64 last_duration = -1, last_position = -1;
	for (;;) {
		usleep(500000);  // 500ms
		service_lock();
		gint64 duration, position;
		const int pos_result = output_get_position(&duration, &position);
		if (pos_result == 0) {
			if (duration != last_duration) {
				print_upnp_time(tbuf, sizeof(tbuf), duration);
				replace_var(TRANSPORT_VAR_CUR_TRACK_DUR, tbuf);
				last_duration = duration;
			}
			if (position / one_sec_unit != last_position) {
				print_upnp_time(tbuf, sizeof(tbuf), position);
				replace_var(TRANSPORT_VAR_REL_TIME_POS, tbuf);
				last_position = position / one_sec_unit;
			}
		}
		service_unlock();
	}
	return NULL;  // not reached.
}
static void *thread_update_track_time(void *userdata) {
	const gint64 one_sec_unit = 1000000000LL;
	gint64 last_duration = -1, last_position = -1;
	for (;;) {
		usleep(500000);  // 500ms
		struct shared_metadata *sm = output_shared_metadata();
		if (sm != NULL) {
			gint64 duration, position;
			const int pos_result = output_get_position(&duration, &position);
			if (pos_result == 0) {
				duration /= one_sec_unit;
				position /= one_sec_unit;
				if (duration != last_duration || position != last_position) {
					last_duration = duration;
					last_position = position;
					shared_meta_time_notify(sm, duration, position);
				}
			}
		}
	}
	return NULL;  // not reached.
}
static int get_position_info(struct action_event *event)
{
	int rc;
	ENTER();

	if (obtain_instanceid(event, NULL)) {
		rc = -1;
		goto out;
	}
	
	gint64 duration, position;
	service_lock();
	const int pos_result = output_get_position(&duration, &position);
	service_unlock();
	if (pos_result == 0) {
		char tbuf[32];
		print_upnp_time_into_buffer(tbuf, sizeof(tbuf), duration);
		replace_var(TRANSPORT_VAR_CUR_TRACK_DUR, tbuf);
		print_upnp_time_into_buffer(tbuf, sizeof(tbuf), position);
		replace_var(TRANSPORT_VAR_REL_TIME_POS, tbuf);
	}

	rc = upnp_append_variable(event, TRANSPORT_VAR_CUR_TRACK, "Track");
	if (rc)
		goto out;

	rc = upnp_append_variable(event, TRANSPORT_VAR_CUR_TRACK_DUR,
				  "TrackDuration");
	if (rc)
		goto out;

	rc = upnp_append_variable(event, TRANSPORT_VAR_CUR_TRACK_META,
				  "TrackMetaData");
	if (rc)
		goto out;

	rc = upnp_append_variable(event, TRANSPORT_VAR_CUR_TRACK_URI,
				  "TrackURI");
	if (rc)
		goto out;

	rc = upnp_append_variable(event, TRANSPORT_VAR_REL_TIME_POS,
				  "RelTime");
	if (rc)
		goto out;

	rc = upnp_append_variable(event, TRANSPORT_VAR_ABS_TIME_POS,
				  "AbsTime");
	if (rc)
		goto out;

	rc = upnp_append_variable(event, TRANSPORT_VAR_REL_CTR_POS,
				  "RelCount");
	if (rc)
		goto out;

	rc = upnp_append_variable(event, TRANSPORT_VAR_ABS_CTR_POS,
				  "AbsCount");
	if (rc)
		goto out;

      out:
	LEAVE();
	return rc;
}