+ compute_chunk_time(mmd->chunks_sent, &mmd->afd.afhi.chunk_tv,
+ &mmd->stream_start, &due);
+ if (tv_diff(&due, now, NULL) <= 0) {
+ char *buf;
+ size_t len;
+
+ if (!mmd->chunks_sent) {
+ mmd->stream_start = *now;
+ mmd->events++;
+ set_mmd_offset();
+ }
+ /*
+ * We call the send function also in case of empty chunks as
+ * they might have still some data queued which can be sent in
+ * this case.
+ */
+ vss_get_chunk(mmd->current_chunk, vsst, &buf, &len);
+ for (i = 0; senders[i].name; i++) {
+ if (!senders[i].send)
+ continue;
+ senders[i].send(mmd->current_chunk, mmd->chunks_sent,
+ buf, len, vsst->header_buf, vsst->header_len);
+ }
+ /*
+ * Prefault next chunk(s)
+ *
+ * If the backing device of the memory-mapped audio file is
+ * slow and read-ahead is turned off or prevented for some
+ * reason, e.g. due to memory pressure, it may take much longer
+ * than the chunk interval to get the next chunk on the wire,
+ * causing buffer underruns on the client side. Mapping the
+ * file with MAP_POPULATE seems to help a bit, but it does not
+ * eliminate the delays completely. Moreover, it is supported
+ * only on Linux. So we do our own read-ahead here.
+ */
+ if (mmd->current_chunk > 0) { /* chunk 0 might be on the heap */
+ buf += len;
+ for (i = 0; i < 5 && buf < vsst->map + mmd->size; i++) {
+ __a_unused volatile char x = *buf;
+ buf += 4096;
+ }
+ }
+ mmd->chunks_sent++;
+ mmd->current_chunk++;
+ }
+}
+
+static void vss_post_select(struct sched *s, struct task *t)
+{
+ int ret, i;
+ struct vss_task *vsst = container_of(t, struct vss_task, task);
+
+
+ if (mmd->sender_cmd_data.cmd_num >= 0) {
+ int num = mmd->sender_cmd_data.cmd_num,
+ sender_num = mmd->sender_cmd_data.sender_num;
+
+ if (senders[sender_num].client_cmds[num]) {
+ ret = senders[sender_num].client_cmds[num]
+ (&mmd->sender_cmd_data);
+ if (ret < 0)
+ PARA_ERROR_LOG("%s\n", para_strerror(-ret));
+ }
+ mmd->sender_cmd_data.cmd_num = -1;
+ }
+ if (vsst->afsss != AFS_SOCKET_CHECK_FOR_WRITE)
+ recv_afs_result(vsst, &s->rfds);
+ else if (FD_ISSET(vsst->afs_socket, &s->wfds)) {
+ PARA_NOTICE_LOG("requesting new fd from afs\n");
+ ret = write_buffer(vsst->afs_socket, "new");
+ if (ret < 0)
+ PARA_CRIT_LOG("%s\n", para_strerror(-ret));
+ else
+ vsst->afsss = AFS_SOCKET_AFD_PENDING;
+ }
+ for (i = 0; senders[i].name; i++) {
+ if (!senders[i].post_select)
+ continue;
+ senders[i].post_select(&s->rfds, &s->wfds);
+ }
+ if ((vss_playing() && !(mmd->vss_status_flags & VSS_PLAYING)) ||
+ (vss_next() && vss_playing()))
+ tv_add(now, &vsst->announce_tv, &vsst->data_send_barrier);
+ vss_send(vsst);
+}
+
+/**
+ * Initialize the virtual streaming system task.
+ *
+ * \param afs_socket The fd for communication with afs.
+ * \param s The scheduler to register the vss task to.
+ *
+ * This also initializes all supported senders and starts streaming
+ * if the --autoplay command line flag was given.
+ */
+void init_vss_task(int afs_socket, struct sched *s)
+{
+ static struct vss_task vss_task_struct, *vsst = &vss_task_struct;
+ int i;
+ char *hn = para_hostname(), *home = para_homedir();
+ long unsigned announce_time = conf.announce_time_arg > 0?
+ conf.announce_time_arg : 300,
+ autoplay_delay = conf.autoplay_delay_arg > 0?
+ conf.autoplay_delay_arg : 0;
+ vsst->header_interval.tv_sec = 5; /* should this be configurable? */
+ vsst->afs_socket = afs_socket;
+ vsst->task.pre_select = vss_pre_select;
+ vsst->task.post_select = vss_post_select;
+ ms2tv(announce_time, &vsst->announce_tv);
+ PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&vsst->announce_tv));
+ INIT_LIST_HEAD(&fec_client_list);
+ for (i = 0; senders[i].name; i++) {
+ PARA_NOTICE_LOG("initializing %s sender\n", senders[i].name);
+ senders[i].init(&senders[i]);
+ }
+ free(hn);
+ free(home);
+ mmd->sender_cmd_data.cmd_num = -1;
+ if (conf.autoplay_given) {