+ if (client->state == Client::SENDING_ERROR) {
+ // We're done sending the error, so now close.
+ // This is postcondition #1.
+ close_client(client);
+ return;
+ }
+
+ // Start sending from the first keyframe we get. In other
+ // words, we won't send any of the backlog, but we'll start
+ // sending immediately as we get the next keyframe block.
+ // This is postcondition #3.
+ if (client->stream_pos == size_t(-2)) {
+ client->stream_pos = std::min<size_t>(
+ client->stream->bytes_received - client->stream->backlog_size,
+ 0);
+ client->state = Client::SENDING_DATA;
+ } else {
+ // client->stream_pos should be -1, but it might not be,
+ // if we have clients from an older version.
+ client->stream_pos = client->stream->bytes_received;
+ client->state = Client::WAITING_FOR_KEYFRAME;
+ }
+ client->stream->put_client_to_sleep(client);
+ return;
+ }
+ case Client::WAITING_FOR_KEYFRAME: {
+ Stream *stream = client->stream;
+ if (ssize_t(client->stream_pos) > stream->last_suitable_starting_point) {
+ // We haven't received a keyframe since this stream started waiting,
+ // so keep on waiting for one.
+ // This is postcondition #3.
+ stream->put_client_to_sleep(client);
+ return;
+ }
+ client->stream_pos = stream->last_suitable_starting_point;