summaryrefslogtreecommitdiff
path: root/src/liana
diff options
context:
space:
mode:
authorAndrew Opalach <andrew@akon.city> 2025-03-18 10:56:12 -0400
committerAndrew Opalach <andrew@akon.city> 2025-03-18 10:56:12 -0400
commit191d98d306d48ba36d5a5a2eb3583b805a03b2c3 (patch)
tree3c917adaf66e6d348046beee510a9a1ed1dc1a41 /src/liana
parentf0accac221a53633380f15b4953ca4f4fdfbeca7 (diff)
downloadcamu-191d98d306d48ba36d5a5a2eb3583b805a03b2c3.tar.gz
camu-191d98d306d48ba36d5a5a2eb3583b805a03b2c3.tar.bz2
camu-191d98d306d48ba36d5a5a2eb3583b805a03b2c3.zip
Cleanup
Signed-off-by: Andrew Opalach <andrew@akon.city>
Diffstat (limited to 'src/liana')
-rw-r--r--src/liana/list.c97
-rw-r--r--src/liana/server.c2
2 files changed, 36 insertions, 63 deletions
diff --git a/src/liana/list.c b/src/liana/list.c
index 47b8545..3e2a350 100644
--- a/src/liana/list.c
+++ b/src/liana/list.c
@@ -1,9 +1,9 @@
-#include <nnwt/thread.h>
-#include <al/random.h>
-#include <al/lib.h>
#define AL_LOG_SECTION "list"
//#define AL_LOG_ENABLE_TRACE
#include <al/log.h>
+#include <al/lib.h>
+#include <al/random.h>
+#include <nnwt/thread.h>
#include "list.h"
#include "list_cmp.h"
@@ -269,27 +269,12 @@ static s32 get_sequence_from_entry_id(struct lia_list *list, u32 id)
return -1;
}
-static struct lia_list_entry *get_entry_from_id(struct lia_list *list, u32 id, s32 *sequence)
-{
- struct lia_list_entry *entry;
- al_array_foreach(list->entries, i, entry) {
- if (entry->id == id) {
- *sequence = (s32)i;
- return entry;
- }
- }
- return NULL;
-}
-
static bool handle_skipto(struct lia_list *list, s32 sequence, s32 index)
{
- if (index == list->current) return true;
if (sequence == LIANA_SEQUENCE_ANY) sequence = list->current;
if (sequence < 0) return true; // list->current = -1
if (sequence != list->current) {
- // Skipping from an entry other than current is not handled and will cause very
- // confusing errors. To handle it wouldn't make sense anyway because the outcome
- // would likely be unexpected to the user.
+ warn("Discarding out of date skip().");
return true;
}
@@ -383,20 +368,16 @@ static bool handle_skipto(struct lia_list *list, s32 sequence, s32 index)
static bool handle_skip(struct lia_list *list, s32 sequence, s32 n)
{
if (sequence == LIANA_SEQUENCE_ANY) sequence = list->current;
- struct lia_list_cmd *cmd = list->cmd;
- cmd->op = SKIPTO;
- cmd->sequence = sequence;
- cmd->arg0.i = sequence + n;
- return handle_skipto(list, cmd->sequence, cmd->arg0.i);
+ if (sequence < 0) return true; // list->current = -1
+ return handle_skipto(list, sequence, sequence + n);
}
static void handle_toggle_pause(struct lia_list *list, s32 sequence, f64 pts)
{
- // pts should be treated as a hint.
if (sequence == LIANA_SEQUENCE_ANY) sequence = list->current;
if (sequence < 0) return; // list->current = -1
if (sequence != list->current) {
- // A non-current entry should always be paused.
+ warn("Discarding out of date toggle_pause().");
return;
}
@@ -404,7 +385,7 @@ static void handle_toggle_pause(struct lia_list *list, s32 sequence, f64 pts)
al_assert(entry && !entry->held);
u64 now = nn_get_timestamp();
- u8 pause = entry->paused_at == LIANA_TIMESTAMP_INVALID ? LIANA_PAUSE_PAUSE : LIANA_PAUSE_RESUME;
+ u8 pause = (entry->paused_at == LIANA_TIMESTAMP_INVALID) ? LIANA_PAUSE_PAUSE : LIANA_PAUSE_RESUME;
u64 at;
bool ended = assume_ended(entry, now);
if (ended) return;
@@ -463,7 +444,7 @@ static void handle_seek(struct lia_list *list, s32 sequence, u32 id, u64 pos)
pos = CLAMP(pos, (u64)0, entry->duration);
u64 now = nn_get_timestamp();
u64 at = now + LIANA_BASE_DELAY;
- u8 pause = entry->paused_at == LIANA_TIMESTAMP_INVALID ? LIANA_PAUSE_RESUME : LIANA_PAUSE_NONE;
+ u8 pause = (entry->paused_at == LIANA_TIMESTAMP_INVALID) ? LIANA_PAUSE_RESUME : LIANA_PAUSE_NONE;
entry->ended = false;
entry->reset_id = get_incremental_id(list);
@@ -520,9 +501,9 @@ static bool handle_end(struct lia_list *list, u32 id, u32 reset_id)
cmd->op = SEEK;
cmd->sequence = sequence;
cmd->arg0.u = id;
- cmd->argf = 0.0;
- pump_queue(list);
- return false;
+ cmd->arg1.u = 0;
+ handle_seek(list, cmd->sequence, cmd->arg0.u, cmd->arg1.u);
+ return true;
#endif
s32 size = (s32)list->entries.count;
@@ -543,8 +524,7 @@ static bool handle_end(struct lia_list *list, u32 id, u32 reset_id)
cmd->op = SKIPTO;
cmd->sequence = sequence;
cmd->arg0.i = next;
- pump_queue(list);
- return false;
+ return handle_skipto(list, cmd->sequence, cmd->arg0.i);
} else {
list->idle = true;
}
@@ -570,59 +550,52 @@ static bool adjust_current(struct lia_list *list, struct lia_list_entry *previou
break;
}
}
- al_assert(cmd->op == SKIPTO && entry && !entry->held);
- pump_queue(list);
- return false;
+ al_assert(entry && cmd->op == SKIPTO);
+ return handle_skipto(list, cmd->sequence, cmd->arg0.i);
}
static bool handle_reverse(struct lia_list *list)
{
if (list->current < 0) return true;
-
struct lia_list_entry *previous = al_array_at(list->entries, list->current);
-
u32 size = list->entries.count;
for (u32 i = 0; i < size; i++) {
u32 tail = size - (i + 1);
if (tail <= i) break;
SWAP(al_array_at(list->entries, i), al_array_at(list->entries, tail));
}
-
return adjust_current(list, previous);
}
static bool handle_sort(struct lia_list *list)
{
if (list->current < 0) return true;
-
struct lia_list_entry *previous = al_array_at(list->entries, list->current);
-
al_array_sort(list->entries, struct lia_list_entry *, camu_db_compare);
-
return adjust_current(list, previous);
}
static bool handle_shuffle(struct lia_list *list)
{
if (list->current < 0) return true;
-
u32 size = list->entries.count;
if (size <= 1) return false;
-
struct lia_list_entry *previous = al_array_at(list->entries, list->current);
-
/* https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
for i from 0 to nāˆ’2 do
j ← random integer such that i ≤ j ≤ n-1
exchange a[i] and a[j]
*/
- // Changed size - 2 to size - 1 to support 2 entry lists.
- // I assume that alters the algorithm, not sure how badly.
- for (u32 i = 0; i < size - 1; i++) {
- u32 j = i + (al_rand() % (size - i));
- SWAP(al_array_at(list->entries, i), al_array_at(list->entries, j));
+ if (size == 2) {
+ if (al_rand() % 2) {
+ SWAP(al_array_at(list->entries, 0), al_array_at(list->entries, 1));
+ }
+ } else {
+ for (u32 i = 0; i < size - 2; i++) {
+ u32 j = i + (al_rand() % (size - i));
+ SWAP(al_array_at(list->entries, i), al_array_at(list->entries, j));
+ }
}
-
return adjust_current(list, previous);
}
@@ -642,10 +615,12 @@ static void handle_unset(struct lia_list *list)
static void run_queue(struct lia_list *list)
{
- // @TODO: What does current = -1/currentless really mean.
if (!list->cmd) {
- if (!list->command_queue.count) return;
- al_array_pop_at(list->command_queue, 0, list->cmd);
+ if (list->command_queue.count) {
+ al_array_pop_at(list->command_queue, 0, list->cmd);
+ } else {
+ return;
+ }
}
struct lia_list_cmd *cmd = list->cmd;
switch (cmd->op) {
@@ -662,15 +637,14 @@ static void run_queue(struct lia_list *list)
return;
}
break;
+ // Return on SKIPTO/SKIP: Target entry not loaded.
case SKIPTO:
if (!handle_skipto(list, cmd->sequence, cmd->arg0.i)) {
- // Target entry not loaded.
return;
}
break;
case SKIP:
if (!handle_skip(list, cmd->sequence, cmd->arg0.i)) {
- // Converted to skipto and target entry not loaded.
return;
}
break;
@@ -682,10 +656,11 @@ static void run_queue(struct lia_list *list)
break;
case END:
if (!handle_end(list, cmd->arg0.u, cmd->arg1.u)) {
- // End was converted to a skip.
+ // Converted to a SKIP and target entry not loaded.
return;
}
break;
+ // Return on order change: Converted to SKIPTO and new current not loaded.
case REVERSE:
if (!handle_reverse(list)) {
return;
@@ -764,9 +739,9 @@ void lia_list_add(struct lia_list *list, void *opaque, u64 duration, wstr *name)
pump_queue(list);
}
-
void lia_list_skipto(struct lia_list *list, s32 sequence, s32 index)
{
+ if (sequence == index || index < 0) return;
struct lia_list_cmd *cmd = al_alloc_object(struct lia_list_cmd);
cmd->op = SKIPTO;
cmd->sequence = sequence;
@@ -777,6 +752,7 @@ void lia_list_skipto(struct lia_list *list, s32 sequence, s32 index)
void lia_list_skip(struct lia_list *list, s32 sequence, s32 n)
{
+ if (n == 0) return;
struct lia_list_cmd *cmd = al_alloc_object(struct lia_list_cmd);
cmd->op = SKIP;
cmd->sequence = sequence;
@@ -875,20 +851,17 @@ void lia_list_free(struct lia_list *list)
}
al_array_free(list->command_queue);
if (list->cmd) al_free(list->cmd);
-
struct lia_list_entry *entry;
al_array_foreach(list->entries, i, entry) {
al_wstr_free(&entry->name);
al_free(entry);
}
al_array_free(list->entries);
-
struct lia_list_sink *sink;
al_array_foreach(list->sinks, i, sink) {
al_free(sink);
}
al_array_free(list->sinks);
-
al_str_free(&list->name);
}
@@ -929,7 +902,7 @@ static void set_queued(struct lia_list *list)
}
queued->start = current->start + (current->duration - current->offset);
- u8 pause = queued->paused_at == LIANA_TIMESTAMP_INVALID ? LIANA_PAUSE_RESUME : LIANA_PAUSE_NONE;
+ u8 pause = (queued->paused_at == LIANA_TIMESTAMP_INVALID) ? LIANA_PAUSE_RESUME : LIANA_PAUSE_NONE;
struct lia_timing time = {
.at = queued->start,
.seek_pos = queued->offset,
diff --git a/src/liana/server.c b/src/liana/server.c
index 9d3661c..808f810 100644
--- a/src/liana/server.c
+++ b/src/liana/server.c
@@ -47,7 +47,7 @@ static void packet_pool_callback(void *userdata, struct nn_packet *packet)
{
struct lia_node_connection *conn = (struct lia_node_connection *)userdata;
if (!nn_packet_stream_send_packet(conn->stream, packet)) {
- data_packet_sent_callback(conn, packet);
+ nn_packet_pool_return(&conn->pool, packet);
}
}