How to properly seek a .wav file?

Hi team,

I am trying to loop over a .wav file using gst_element_seek and GST_MESSAGE_EOS. But when trying this, it seems like only the first time is playing the WAV file completely, starting from the second time, it seems like it starts with some offset. Below is the snapshot of the code I’m using, this code works on on Ubuntu but not on an embedded Linux arm platform. Is there a way I can check if the seek is done, so I can change the state to playing? what else can be happening?

Thanks in advance.

#include <gst/gst.h>
#include <iostream>
#include <pthread.h>
#include <atomic>

GstElement *pipeline;
std::atomic<bool> ready_to_replay(false);
bool play_continously = false;

gboolean start_pipeline(gpointer data) {
    GstElement *pipeline = (GstElement *)data;
    gst_element_set_state(pipeline, GST_STATE_PLAYING);
    return FALSE;  // Remove the timeout
}

void rewind_and_play (int wait)
{
  std::cout<<"Pausing the stream\n";
  gst_element_set_state (pipeline, GST_STATE_PAUSED);
  std::cout<<"Rewinding and playing again stream\n";
  if (!gst_element_seek (pipeline, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
      GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE)) {
      g_printerr ("Seek failed!\n");
  }
  g_timeout_add_seconds(wait, start_pipeline, pipeline);  // Delay for x seconds
}

bool ready_state()
{
  GstState state;
  GstStateChangeReturn ret = gst_element_get_state(pipeline, &state, NULL, GST_CLOCK_TIME_NONE);
  return ((GST_STATE_CHANGE_SUCCESS != ret) || (GST_STATE_READY == state));
}

void* keyboard_monitor(void* arg) {
    char c;
    do {
        c = std::cin.get();
        if (c == '\n') {

          if (ready_state())
          { 
            gst_element_set_state (pipeline, GST_STATE_PLAYING);
          }
          else
          {
            if (ready_to_replay && !play_continously)
            {
              rewind_and_play(0);
              ready_to_replay = false;
            }
            else
            {
              gst_element_set_state (pipeline, GST_STATE_NULL);
              gst_element_set_state (pipeline, GST_STATE_READY);
            }
          }
        }
    } while (c != 'q');
    return NULL;
}

static void cb_message (GstBus *bus, GstMessage *msg, GstElement *pipeline) {
  GError *err;
  gchar *debug_info;

  switch (GST_MESSAGE_TYPE (msg)) {
    case GST_MESSAGE_ERROR:
      gst_message_parse_error (msg, &err, &debug_info);
      g_printerr ("Error received from element %s: %s\n", GST_OBJECT_NAME (msg->src), err->message);
      g_printerr ("Debugging information: %s\n", debug_info ? debug_info : "none");
      g_clear_error (&err);
      g_free (debug_info);
      gst_element_set_state (pipeline, GST_STATE_READY);
      break;
    case GST_MESSAGE_EOS:
      /* end-of-stream */
      g_print ("End-Of-Stream reached.\n");

      if(play_continously) rewind_and_play(1);
      else ready_to_replay = true;
      
      break;
    default:
      /* unhandled message */
      break;
  }
}

int main(int argc, char *argv[]) {
    GstElement *source, *parser, *volume, *converter, *sink;
    GstBus *bus;
    GstMessage *msg;
    pthread_t tid;

    /* Initialize GStreamer */
    gst_init (&argc, &argv);

    /* Create elements */
    source = gst_element_factory_make ("filesrc", "source");
    parser = gst_element_factory_make ("wavparse", "parser");
    volume = gst_element_factory_make ("volume", "volume");
    sink = gst_element_factory_make ("alsasink", "sink");

    char path[512];
    realpath(argv[1], path);

    if (argv[2]) play_continously = true;

    /* Set source file location */
    g_object_set (source, "location", path, NULL);

    g_object_set(volume, "volume", 1.0, NULL);

    /* Create pipeline and add elements */
    pipeline = gst_pipeline_new ("audio-player");
    gst_bin_add_many (GST_BIN (pipeline), source, parser, volume, sink, NULL);

    /* Link elements */
    gst_element_link_many (source, parser, volume, sink, NULL);

    /* Start playing */
    gst_element_set_state (pipeline, GST_STATE_READY);

    /* Wait until error or EOS */
    bus = gst_element_get_bus (pipeline);
    gst_bus_add_signal_watch(bus);
    g_signal_connect(G_OBJECT(bus), "message", G_CALLBACK(cb_message), pipeline);

    /* Start keyboard monitor thread */
    pthread_create(&tid, NULL, keyboard_monitor, NULL);

    /* Wait until error or EOS */
    g_main_loop_run(g_main_loop_new(NULL, FALSE));

    /* Free resources */
    gst_object_unref(bus);
    gst_element_set_state(pipeline, GST_STATE_NULL);
    gst_object_unref(pipeline);
    return 0;
}