+void Client_codec_add(client_t *client, int codec)
+{
+ codec_t *cd = malloc(sizeof(codec_t));
+ if (cd == NULL)
+ Log_fatal("Out of memory");
+ init_list_entry(&cd->node);
+ cd->codec = codec;
+ list_add_tail(&cd->node, &client->codecs);
+}
+
+void Client_codec_free(client_t *client)
+{
+ struct dlist *itr, *save;
+ list_iterate_safe(itr, save, &client->codecs) {
+ list_del(&list_get_entry(itr, codec_t, node)->node);
+ free(list_get_entry(itr, codec_t, node));
+ }
+}
+
+codec_t *Client_codec_iterate(client_t *client, codec_t **codec_itr)
+{
+ codec_t *cd = *codec_itr;
+
+ if (list_empty(&client->codecs))
+ return NULL;
+
+ if (cd == NULL) {
+ cd = list_get_entry(list_get_first(&client->codecs), codec_t, node);
+ } else {
+ if (list_get_next(&cd->node) == &client->codecs)
+ cd = NULL;
+ else
+ cd = list_get_entry(list_get_next(&cd->node), codec_t, node);
+ }
+ *codec_itr = cd;
+ return cd;
+}
+
+void recheckCodecVersions()
+{
+ client_t *client_itr = NULL;
+ int max = 0, version, current_version;
+ message_t *sendmsg;
+ struct dlist codec_list, *itr, *save;
+ codec_t *codec_itr, *cd;
+ bool_t found;
+
+ init_list_entry(&codec_list);
+
+ while (Client_iterate(&client_itr) != NULL) {
+ codec_itr = NULL;
+ while (Client_codec_iterate(client_itr, &codec_itr) != NULL) {
+ found = false;
+ list_iterate(itr, &codec_list) {
+ cd = list_get_entry(itr, codec_t, node);
+ if (cd->codec == codec_itr->codec) {
+ cd->count++;
+ found = true;
+ }
+ }
+ if (!found) {
+ cd = malloc(sizeof(codec_t));
+ if (!cd)
+ Log_fatal("Out of memory");
+ memset(cd, 0, sizeof(codec_t));
+ init_list_entry(&cd->node);
+ cd->codec = codec_itr->codec;
+ cd->count = 1;
+ list_add_tail(&cd->node, &codec_list);
+ }
+ }
+ }
+
+ list_iterate(itr, &codec_list) {
+ cd = list_get_entry(itr, codec_t, node);
+ if (cd->count > max) {
+ max = cd->count;
+ version = cd->codec;
+ }
+ }
+ list_iterate_safe(itr, save, &codec_list) {
+ list_del(&list_get_entry(itr, codec_t, node)->node);
+ free(list_get_entry(itr, codec_t, node));
+ }
+
+ current_version = bPreferAlpha ? iCodecAlpha : iCodecBeta;
+ if (current_version == version)
+ return;
+ // If we don't already use the compat bitstream version set
+ // it as alpha and announce it. If another codec now got the
+ // majority set it as the opposite of the currently valid bPreferAlpha
+ // and announce it.
+ if (version == (uint32_t)0x8000000a)
+ bPreferAlpha = true;
+ else
+ bPreferAlpha = ! bPreferAlpha;
+
+ if (bPreferAlpha)
+ iCodecAlpha = version;
+ else
+ iCodecBeta = version;
+
+ sendmsg = Msg_create(CodecVersion);
+ sendmsg->payload.codecVersion->alpha = version;
+ sendmsg->payload.codecVersion->beta = version;
+ sendmsg->payload.codecVersion->prefer_alpha = bPreferAlpha;
+ Client_send_message_except(NULL, sendmsg);
+
+ Log_info("CELT codec switch 0x%x 0x%x (prefer 0x%x)", iCodecAlpha, iCodecBeta,
+ bPreferAlpha ? iCodecAlpha : iCodecBeta);
+
+}
+
+static int findFreeSessionId()
+{
+ int id;
+ client_t *itr = NULL;
+
+ for (id = 1; id < INT_MAX; id++) {
+ itr = NULL;
+ while ((itr = Client_iterate(&itr)) != NULL) {
+ if (itr->sessionId == id)
+ break;
+ }
+ if (itr == NULL) /* Found free id */
+ return id;
+ }
+ return -1;
+}
+