nega0 / pianobarfly

pandora2[mp3|m4a]
https://github.com/nega0/pianobarfly
Other
62 stars 31 forks source link

fix norecord branch segfault and conflicts (patch included) #66

Open fengshaun opened 7 years ago

fengshaun commented 7 years ago

seems like the norecord branch was abandoned, so I got sad and fixed it. This patch is on top of my previous two patches (sorry! I didn't want to revert all my changes to make a clean patch which would then make more conflicts).

at some point I'll fork this repo and apply the patches and make a big pull request, but I'm slowly fixing little stuff for now.

diff --git a/src/fly.c b/src/fly.c
index 1fb6de3..4cd64fb 100644
--- a/src/fly.c
+++ b/src/fly.c
@@ -1180,19 +1180,21 @@ int BarFlyClose(BarFly_t* fly, BarSettings_t const* settings)
    int status;

    /* closing the tmpfile() will delete it automatically */
-   fclose(fly->temp_file);
+   if (fly->temp_file) {
+       fclose(fly->temp_file);
+   }

    /*
     * Free the audio file name.
     */
-   if (fly->audio_file_path != NULL) {
+   if (fly->audio_file_path) {
        free(fly->audio_file_path);
    }

    /*
     * Free the cover art URL.
     */
-   if (fly->cover_art_url != NULL) {
+   if (fly->cover_art_url) {
        free(fly->cover_art_url);
    }

diff --git a/src/main.c b/src/main.c
index 71523b3..db60ec0 100644
--- a/src/main.c
+++ b/src/main.c
@@ -31,6 +31,7 @@ THE SOFTWARE.
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
+#include <getopt.h>
 /* fork () */
 #include <unistd.h>
 #include <sys/select.h>
@@ -146,11 +147,11 @@ static bool BarMainGetLoginCredentials (BarSettings_t *settings,

                close (pipeFd[1]);
                memset (passBuf, 0, sizeof (passBuf));
-               ssize_t len = read (pipeFd[0], passBuf, sizeof (passBuf)-1);
+               status = read (pipeFd[0], passBuf, sizeof (passBuf)-1);
                close (pipeFd[0]);

                /* drop trailing newlines */
-               len = strlen (passBuf)-1;
+               ssize_t len = strlen (passBuf)-1;
                while (len >= 0 && passBuf[len] == '\n') {
                    passBuf[len] = '\0';
                    --len;
@@ -256,6 +257,7 @@ static void BarMainStartPlayback (BarApp_t *app, pthread_t *playerThread) {
    } else {
        /* setup player */
        memset (&app->player, 0, sizeof (app->player));
+       app->player.record = app->settings.record;

        WaitressInit (&app->player.waith);
        WaitressSetUrl (&app->player.waith, app->playlist->audioUrl);
@@ -275,7 +277,8 @@ static void BarMainStartPlayback (BarApp_t *app, pthread_t *playerThread) {
        strcpy(app->player.fly.stationName, app->curStation->name);

        /* Open the audio file. */
-       BarFlyOpen (&app->player.fly, app->playlist, &app->settings);
+       if(app->settings.record)
+           BarFlyOpen (&app->player.fly, app->playlist, &app->settings);

        /* throw event */
        BarUiStartEventCmd (&app->settings, "songstart",
@@ -319,7 +322,9 @@ static void BarMainPlayerCleanup (BarApp_t *app, pthread_t *playerThread) {
    }

    /* Close the output file. */
-   BarFlyClose (&app->player.fly, &app->settings);
+   if (app->settings.record) {
+       BarFlyClose (&app->player.fly, &app->settings);
+   }

    memset (&app->player, 0, sizeof (app->player));
 }
@@ -434,6 +439,30 @@ int main (int argc, char **argv) {
    BarSettingsInit (&app.settings);
    BarSettingsRead (&app.settings);

+   /* provide commandline options */
+   static struct option longopts[] = {
+       { "record",         no_argument, NULL, 'r' },
+       { "no-record",      no_argument, NULL, 'n'},
+       { NULL,             0,           NULL, 0 }
+   };
+
+   int ch;
+
+   while ((ch = getopt_long(argc, argv, "rn", longopts, NULL)) != -1) {
+       switch(ch) {
+           case 'r':
+               app.settings.record = true;
+               break;
+
+           case 'n':
+               app.settings.record = false;
+               break;
+
+           default:
+               break;
+     }
+   }
+
    PianoReturn_t pret;
    if ((pret = PianoInit (&app.ph, app.settings.partnerUser,
            app.settings.partnerPassword, app.settings.device,
diff --git a/src/player.c b/src/player.c
index 2ec35b1..ba5e643 100644
--- a/src/player.c
+++ b/src/player.c
@@ -116,8 +116,11 @@ static inline int BarPlayerBufferFill (struct audioPlayer *player,
    }

    /* Write the stream to the output file. */
-   if (BarFlyWrite(&player->fly, data, dataSize) != 0) {
-       BarUiMsg (player->settings, MSG_ERR, "Error writting audio file.\n");
+   if(player->record) {
+       int status = BarFlyWrite(&player->fly, data, dataSize);
+       if (status != 0) {
+           BarUiMsg (player->settings, MSG_ERR, "Error writing audio file.\n");
+       }
    }

    memcpy (player->buffer+player->bufferFilled, data, dataSize);
diff --git a/src/player.h b/src/player.h
index cbff295..15bcb0c 100644
--- a/src/player.h
+++ b/src/player.h
@@ -52,6 +52,7 @@ THE SOFTWARE.
 struct audioPlayer {
    bool doQuit; /* protected by pauseMutex */
    bool doPause; /* protected by pauseMutex */
+   bool record;
    unsigned char channels;
    unsigned char aoError;

diff --git a/src/settings.c b/src/settings.c
index a3452e0..2ee4390 100644
--- a/src/settings.c
+++ b/src/settings.c
@@ -129,6 +129,7 @@ void BarSettingsRead (BarSettings_t *settings) {
    settings->audioQuality = PIANO_AQ_HIGH;
    settings->autoselect = true;
    settings->downloadOnlyLoved = true;
+   settings->record = true;
    settings->history = 5;
    settings->volume = 0;
    settings->maxPlayerErrors = 5;
@@ -312,6 +313,8 @@ void BarSettingsRead (BarSettings_t *settings) {
                settings->autoselect = atoi (val);
            } else if (streq ("download_only_loved", key)) {
                settings->downloadOnlyLoved = atoi (val);
+           } else if (streq ("record", key)) {
+               settings->record = atoi (val);
            } else if (streq ("tls_fingerprint", key)) {
                /* expects 40 byte hex-encoded sha1 */
                if (strlen (val) == 40) {
diff --git a/src/settings.h b/src/settings.h
index 5c9467f..07bff14 100644
--- a/src/settings.h
+++ b/src/settings.h
@@ -84,6 +84,7 @@ typedef struct {
 } BarMsgFormatStr_t;

 typedef struct {
+   bool record;
    bool autoselect;
    bool downloadOnlyLoved;
    unsigned int history, maxPlayerErrors;