Skip to content

Commit

Permalink
fixed read directory tree error
Browse files Browse the repository at this point in the history
  • Loading branch information
ravachol committed Feb 29, 2024
1 parent 56b4289 commit bf201e4
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 29 deletions.
4 changes: 3 additions & 1 deletion src/directorytree.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,12 @@ void writeTreeToFile(FileSystemEntry *node, FILE *file, int parentId)
fprintf(file, "%d\t%s\t%d\t%d\n", node->id, node->name, node->isDirectory, parentId);

FileSystemEntry *child = node->children;
FileSystemEntry *tmp = NULL;
while (child)
{
tmp = child->next;
writeTreeToFile(child, file, node->id);
child = child->next;
child = tmp;
}

free(node->name);
Expand Down
27 changes: 14 additions & 13 deletions src/m4a.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ extern "C"
#define MAX_SAMPLES 4800 // Maximum expected frame size
#define MAX_SAMPLE_SIZE 4
static uint8_t leftoverBuffer[MAX_SAMPLES * MAX_CHANNELS * MAX_SAMPLE_SIZE];
// static float leftoverBuffer[MAX_SAMPLES * MAX_CHANNELS];

static ma_uint64 leftoverSampleCount = 0;

Expand Down Expand Up @@ -118,7 +117,6 @@ int m4a_ffmpeg_read(void *opaque, uint8_t *buf, int buf_size)
}
else
{
// Translate MiniAudio error result to FFmpeg error codes
switch (result)
{
case MA_IO_ERROR:
Expand All @@ -127,7 +125,7 @@ int m4a_ffmpeg_read(void *opaque, uint8_t *buf, int buf_size)
return AVERROR(EINVAL);

default:
return AVERROR(EIO); // Default to a generic I/O error code
return AVERROR(EIO);
}
}
}
Expand Down Expand Up @@ -259,9 +257,8 @@ MA_API ma_result m4a_decoder_init(
return result;
}

// Setup Custom I/O
unsigned char *avio_ctx_buffer = NULL;
size_t avio_ctx_buffer_size = 4096; // Use a buffer size that you find appropriate
size_t avio_ctx_buffer_size = 4096;
AVIOContext *avio_ctx = avio_alloc_context(
avio_ctx_buffer,
avio_ctx_buffer_size,
Expand Down Expand Up @@ -315,18 +312,15 @@ MA_API ma_result m4a_decoder_init_file(const char *pFilePath, const ma_decoding_
AVFormatContext *format_context = NULL;
if (avformat_open_input(&format_context, pFilePath, NULL, NULL) != 0)
{
// Cleanup if initialization failed.
return MA_INVALID_FILE;
}

if (avformat_find_stream_info(format_context, NULL) < 0)
{
// Cannot find stream info, clean up.
avformat_close_input(&format_context);
return MA_ERROR;
}

// Find the best audio stream.
const AVCodec *decoder = NULL;
int stream_index = av_find_best_stream(format_context, AVMEDIA_TYPE_AUDIO, -1, -1, &decoder, 0);
if (stream_index < 0)
Expand All @@ -335,7 +329,6 @@ MA_API ma_result m4a_decoder_init_file(const char *pFilePath, const ma_decoding_
return MA_ERROR;
}

// Initialize the codec context for the audio stream.
AVStream *audio_stream = format_context->streams[stream_index];

AVCodecContext *codec_context = avcodec_alloc_context3(decoder);
Expand All @@ -359,7 +352,6 @@ MA_API ma_result m4a_decoder_init_file(const char *pFilePath, const ma_decoding_
return MA_ERROR;
}

// Assign the important objects to pM4a fields
pM4a->codec_context = codec_context;

switch (pM4a->codec_context->sample_fmt)
Expand All @@ -372,14 +364,13 @@ MA_API ma_result m4a_decoder_init_file(const char *pFilePath, const ma_decoding_
case AV_SAMPLE_FMT_FLT:
pM4a->sampleSize = sizeof(float); // 32-bit float samples
break;
// ... handle other formats as needed ...
default:
pM4a->sampleSize = 0; // Unknown or unsupported format
pM4a->sampleSize = 0;
break;
}

pM4a->format_context = format_context;
pM4a->mf = NULL; // This might be used for raw file operations which are not needed with FFmpeg.
pM4a->mf = NULL;
pM4a->format = ffmpeg_to_mini_al_format(pM4a->codec_context->sample_fmt);

return MA_SUCCESS;
Expand Down Expand Up @@ -498,8 +489,18 @@ ma_result m4a_decoder_read_pcm_frames(m4a_decoder *pM4a, void *pFramesOut, ma_ui
{
int byteOffset = (i * channels + c) * pM4a->sampleSize;

if (frame->extended_data == NULL || frame->extended_data[c] == NULL)
{
continue;
}

if (pM4a->format == ma_format_s16)
{
if (pM4a->codec_context->sample_fmt != AV_SAMPLE_FMT_S16)
{
continue;
}

int16_t sample = ((int16_t *)frame->extended_data[c])[i];
memcpy(output_buffer + byteOffset, &sample, sizeof(int16_t));
}
Expand Down
34 changes: 21 additions & 13 deletions src/player.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ typedef struct
} PixelData;
#endif

const char VERSION[] = "2.4.1";
const char VERSION[] = "2.4.2";
int mainColor = 6;
int titleColor = 6;
int artistColor = 6;
Expand Down Expand Up @@ -220,14 +220,14 @@ void printHelp()
printf(" Use + (or =), - to adjust volume.\n");
printf(" Use a, d to seek in a song.\n");
printf(" Press space or p to pause.\n");
printf(" Press u to update the library.\n");
printf(" Press u to update the library.\n");
printf(" Press F2 to display playlist.\n");
printf(" Press F3 to display music library.\n");
printf(" Press F4 to display song info.\n");
printf(" Press F5 to display key bindings.\n");
printf(" Press F5 to display key bindings.\n");
printf(" Press . to add the currently playing song to kew.m3u.\n");
printf(" Press q to quit.\n");
printf(" Press . to add the currently playing song to kew.m3u.\n");
printf(" Press q to quit.\n");
printf("\n");
}

Expand Down Expand Up @@ -927,7 +927,7 @@ int showPlaylist(SongData *songData)
for (int i = (startFromCurrent ? startIter : 0); i < (startFromCurrent ? startIter : 0) + maxListSize; i++)
{
setDefaultTextColor();

if (node == NULL)
break;
char filePath[MAXPATHLEN];
Expand All @@ -947,7 +947,7 @@ int showPlaylist(SongData *songData)
if (i == chosenSong)
{
chosenNodeId = node->id;

printf("\x1b[7m");
}

Expand Down Expand Up @@ -1113,7 +1113,7 @@ void processName(const char *name, char *output, int maxWidth)
{
strncpy(output, name, maxWidth);
output[maxWidth] = '\0';
removeUnneededChars(output);
removeUnneededChars(output);
}
}

Expand All @@ -1123,6 +1123,11 @@ void setCurrentAsChosenDir()
chosenDir = currentEntry;
}

void resetChosenDir()
{
chosenDir = NULL;
}

int displayTree(FileSystemEntry *root, int depth, int maxListSize, int maxNameWidth)
{
char dirName[maxNameWidth + 1];
Expand Down Expand Up @@ -1172,6 +1177,9 @@ int displayTree(FileSystemEntry *root, int depth, int maxListSize, int maxNameWi
if (chosenLibRow < 0)
startLibIter = chosenLibRow = libIter = 0;

if (root == NULL)
return 0;

if (root->isDirectory ||
(!root->isDirectory && depth == 1) ||
(chosenDir != NULL && allowChooseSongs && root->parent != NULL && (strcmp(root->parent->fullPath, chosenDir->fullPath) == 0 || strcmp(root->fullPath, chosenDir->fullPath) == 0)))
Expand All @@ -1190,7 +1198,7 @@ int displayTree(FileSystemEntry *root, int depth, int maxListSize, int maxNameWi
}
else
{
setDefaultTextColor();
setDefaultTextColor();
}

if (depth >= 2)
Expand All @@ -1216,7 +1224,7 @@ int displayTree(FileSystemEntry *root, int depth, int maxListSize, int maxNameWi
currentEntry = root;

if (allowChooseSongs == true && (chosenDir == NULL ||
(strcmp(currentEntry->parent->fullPath, chosenDir->fullPath) != 0 &&
(currentEntry != NULL && currentEntry->parent != NULL && chosenDir != NULL && (strcmp(currentEntry->parent->fullPath, chosenDir->fullPath) != 0) &&
strcmp(root->fullPath, chosenDir->fullPath) != 0)))
{
chosenLibRow -= libSongIter;
Expand Down Expand Up @@ -1255,7 +1263,7 @@ int displayTree(FileSystemEntry *root, int depth, int maxListSize, int maxNameWi
filename[0] = '\0';
processName(root->name, filename, maxNameWidth);
printf(" └─%s \n", filename);

libSongIter++;
}

Expand Down Expand Up @@ -1288,7 +1296,7 @@ char *getLibraryFilePath()
strcat(filepath, "/");
strcat(filepath, LIBRARY_FILE);
free(configdir);
return filepath;
return filepath;
}

void showLibrary(SongData *songData)
Expand Down Expand Up @@ -1358,7 +1366,7 @@ int printPlayer(SongData *songdata, double elapsedSeconds, AppSettings *settings

if (songdata != NULL && songdata->metadata != NULL && !songdata->hasErrors && (songdata->hasErrors < 1))
{
metadata = *songdata->metadata;
metadata = *songdata->metadata;
duration = songdata->duration;

if (songdata->cover != NULL && coverEnabled)
Expand Down Expand Up @@ -1446,5 +1454,5 @@ void freeMainDirectoryTree()
else
freeTree(library);

free(filepath);
free(filepath);
}
3 changes: 3 additions & 0 deletions src/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,7 @@ void freeMainDirectoryTree();

char *getLibraryFilePath();

void resetChosenDir();

#endif

5 changes: 3 additions & 2 deletions src/playerops.c
Original file line number Diff line number Diff line change
Expand Up @@ -1289,13 +1289,14 @@ void *updateLibraryThread(void *arg)

FileSystemEntry *temp = createDirectoryTree(path, &tmpDirectoryTreeEntries);

pthread_mutex_lock(&(playlist.mutex));
pthread_mutex_lock(&switchMutex);

freeTree(library);
library = temp;
numDirectoryTreeEntries = tmpDirectoryTreeEntries;
resetChosenDir();

pthread_mutex_unlock(&(playlist.mutex));
pthread_mutex_unlock(&switchMutex);

refresh = true;

Expand Down

0 comments on commit bf201e4

Please sign in to comment.