Skip to content

Commit

Permalink
add track seconds for tafs
Browse files Browse the repository at this point in the history
  • Loading branch information
SciLor committed Oct 6, 2024
1 parent 0411205 commit eba44fb
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 3 deletions.
12 changes: 12 additions & 0 deletions include/contentJson.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ typedef struct

} contentJson_t;

typedef struct
{
size_t count;
uint32_t *pos;
} track_positions_t;

typedef struct
{
track_positions_t track_positions;
} tonie_info_additional_t;

typedef struct
{
char *contentPath;
Expand All @@ -56,6 +67,7 @@ typedef struct
bool_t locked;
contentJson_t json;
TonieboxAudioFileHeader *tafHeader;
tonie_info_additional_t additional;
} tonie_info_t;

#define CONTENT_JSON_VERSION 5
Expand Down
69 changes: 69 additions & 0 deletions src/handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,67 @@ bool_t isValidTaf(const char *contentPath)
return valid;
}

void readTrackPositions(tonie_info_t *tonieInfo, FsFile *file)
{
bool hasError = false;
track_positions_t *trackPos = &tonieInfo->additional.track_positions;
TonieboxAudioFileHeader *tafHeader = tonieInfo->tafHeader;
trackPos->count = tafHeader->n_track_page_nums;
trackPos->pos = osAllocMem(trackPos->count * sizeof(uint32_t));
trackPos->pos[0] = 0;
for (size_t i = 1; i < trackPos->count; i++)
{
uint8_t buffer[14];
size_t readBytes = 0;
uint32_t trackPageNum = tafHeader->track_page_nums[i];
size_t filePos = 4096 + 4096 * trackPageNum;

error_t error = fsSeekFile(file, filePos, SEEK_SET);
if (error != NO_ERROR)
{
hasError = true;
TRACE_ERROR("Failed to seek track position at %" PRIuSIZE " with error %s\r\n", filePos, error2text(error));
break;
}
error = fsReadFile(file, buffer, sizeof(buffer), &readBytes);
if (error != NO_ERROR)
{
hasError = true;
TRACE_ERROR("Failed to read track position at %" PRIuSIZE " with error %s\r\n", filePos, error2text(error));
break;
}

if (!osMemcmp(buffer, "OggS", 4) == 0)
{
hasError = true;
TRACE_ERROR("Invalid OggS header at %" PRIuSIZE "\r\n", filePos);
break;
}
if (buffer[4] != 0)
{ // Opus Version
hasError = true;
TRACE_ERROR("Invalid Opus Version %" PRIu8 " at %" PRIuSIZE "\r\n", buffer[4], filePos);
break;
}
if (buffer[5] != 0)
{ // Header Type
hasError = true;
TRACE_ERROR("Invalid Header Type %" PRIu8 " at %" PRIuSIZE "\r\n", buffer[5], filePos);
break;
}
uint64_t granulePosition = 0;
osMemcpy(&granulePosition, &buffer[6], 8);
trackPos->pos[i] = (uint32_t)(granulePosition / 48000); // 48000 samples per second
TRACE_VERBOSE("Track position %" PRIu32 "\r\n", trackPos->pos[i]);
}
if (hasError)
{
trackPos->count = 0;
osFreeMem(trackPos->pos);
trackPos->pos = NULL;
}
}

tonie_info_t *getTonieInfoFromUid(uint64_t uid, bool lock, settings_t *settings)
{
char *contentPath;
Expand Down Expand Up @@ -511,6 +572,7 @@ tonie_info_t *getTonieInfo(const char *contentPath, bool lock, settings_t *setti
tonieInfo->exists = false;
tonieInfo->locked = false;
osMemset(&tonieInfo->json, 0, sizeof(contentJson_t));
osMemset(&tonieInfo->additional, 0, sizeof(tonie_info_additional_t));

if (osStrstr(contentPath, ".json") == NULL)
{
Expand Down Expand Up @@ -559,6 +621,7 @@ tonie_info_t *getTonieInfo(const char *contentPath, bool lock, settings_t *setti
if (tonieInfo->tafHeader->sha1_hash.len == 20)
{
tonieInfo->valid = true;
readTrackPositions(tonieInfo, file);
if (tonieInfo->tafHeader->num_bytes == get_settings()->encode.stream_max_size)
{
tonieInfo->json._source_type = CT_SOURCE_TAF_INCOMPLETE;
Expand Down Expand Up @@ -634,6 +697,12 @@ void freeTonieInfo(tonie_info_t *tonieInfo)
osFreeMem(tonieInfo->jsonPath);
tonieInfo->jsonPath = NULL;
}
if (tonieInfo->additional.track_positions.pos)
{
osFreeMem(tonieInfo->additional.track_positions.pos);
tonieInfo->additional.track_positions.pos = NULL;
tonieInfo->additional.track_positions.count = 0;
}

free_content_json(&tonieInfo->json);
free(tonieInfo);
Expand Down
12 changes: 9 additions & 3 deletions src/handler_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -648,10 +648,10 @@ error_t handleApiFileIndexV2(HttpConnection *connection, const char_t *uri, cons
}
cJSON_AddStringToObject(tafHeaderEntry, "sha1Hash", sha1Hash);
cJSON_AddNumberToObject(tafHeaderEntry, "size", tafInfo->tafHeader->num_bytes);
cJSON *tracksArray = cJSON_AddArrayToObject(tafHeaderEntry, "tracks");
for (size_t i = 0; i < tafInfo->tafHeader->n_track_page_nums; i++)
cJSON *tracksArray = cJSON_AddArrayToObject(tafHeaderEntry, "tracks_seconds");
for (size_t i = 0; i < tafInfo->additional.track_positions.count; i++)
{
cJSON_AddItemToArray(tracksArray, cJSON_CreateNumber(tafInfo->tafHeader->track_page_nums[i]));
cJSON_AddItemToArray(tracksArray, cJSON_CreateNumber(tafInfo->additional.track_positions.pos[i]));
}

item = tonies_byAudioIdHashModel(tafInfo->tafHeader->audio_id, tafInfo->tafHeader->sha1_hash.data, tafInfo->json.tonie_model);
Expand Down Expand Up @@ -2629,6 +2629,12 @@ error_t getTagInfoJson(char ruid[17], cJSON *jsonTarget, client_ctx_t *client_ct
cJSON_AddBoolToObject(jsonEntry, "claimed", tafInfo->json.claimed);
cJSON_AddStringToObject(jsonEntry, "source", tafInfo->json.source);

cJSON *tracksArray = cJSON_AddArrayToObject(jsonEntry, "tracks_seconds");
for (size_t i = 0; i < tafInfo->additional.track_positions.count; i++)
{
cJSON_AddItemToArray(tracksArray, cJSON_CreateNumber(tafInfo->additional.track_positions.pos[i]));
}

char *downloadUrl = custom_asprintf("/content/download/%s?overlay=%s", tagPath, client_ctx->settings->internal.overlayUniqueId);
char *audioUrl = custom_asprintf("%s&skip_header=true", downloadUrl);
cJSON_AddStringToObject(jsonEntry, "audioUrl", audioUrl);
Expand Down

0 comments on commit eba44fb

Please sign in to comment.