How to correctly add DsDirection Plug-in and NVOF to Deepstream-app.c?

I have been trying to integrate the DsDirection plugin to Deepstream-app.c.
Specs-
• Hardware - Jetson Xavier
• DeepStream Version5.0

In the gstdsdirection.cpp file, in gst_dsdirection_transform_ip() function fmeta_list always comes as NULL.

static GstFlowReturn
gst_dsdirection_transform_ip (GstBaseTransform * btrans, GstBuffer * inbuf)
{

  GstDsDirection *dsdirection = GST_DSDIRECTION (btrans);
  DsDirectionOutput *output;
  NvDsBatchMeta *batch_meta = NULL;
  NvDsFrameMeta *frame_meta = NULL;
  NvDsMetaList *l_frame = NULL;
  NvDsMetaList *l_obj = NULL;
  NvDsObjectMeta *obj_meta = NULL;
  dsdirection->frame_num++;

  GST_DEBUG_OBJECT (dsdirection,
      "Processing Frame %lu", dsdirection->frame_num);

  batch_meta = gst_buffer_get_nvds_batch_meta (inbuf);
  if (batch_meta == nullptr) {
    GST_ELEMENT_ERROR (dsdirection, STREAM, FAILED,
        ("NvDsBatchMeta not found for input buffer."), (NULL));
    return GST_FLOW_ERROR;
  }
  //Iterating through frames in batched meta from diff. sources
  for (l_frame = batch_meta->frame_meta_list; l_frame != NULL;
      l_frame = l_frame->next) {

    frame_meta = (NvDsFrameMeta *) (l_frame->data);
    NvDsFrameMetaList *fmeta_list = NULL;
    //Iterating through each frame of the batched meta
            printf("\n&&&&&&& Reached 1 &&&&&&&"); 

    for (fmeta_list = frame_meta->frame_user_meta_list; fmeta_list != NULL;
        fmeta_list = fmeta_list->next) {
          printf("\n~~~~~~~ Reached 2 ~~~~~~~~~"); 

      NvDsUserMeta *of_user_meta = NULL;
      //previous meta + optical flow meta
      of_user_meta = (NvDsUserMeta *) fmeta_list->data;

      if (of_user_meta
          && of_user_meta->base_meta.meta_type == NVDS_OPTICAL_FLOW_META) {

            printf("\n!!!!!!!!!!!! HIIII !!!!!!!!"); 
        //optical flow meta for each frame
        NvDsOpticalFlowMeta *ofmeta =
            (NvDsOpticalFlowMeta *) (of_user_meta->user_meta_data);
        if (ofmeta) {
          //Iterating through each object in the frame
          for (l_obj = frame_meta->obj_meta_list; l_obj != NULL;
              l_obj = l_obj->next) {
            obj_meta = (NvDsObjectMeta *) (l_obj->data);

            //processing the meta data for direction detection
          
            output =
                DsDirectionProcess ((NvOFFlowVector *) ofmeta->data,
                ofmeta->cols, ofmeta->rows, NVOF_BLK_SIZE,
                &obj_meta->rect_params);
            // Attach direction to the object
            attach_metadata_object (dsdirection, obj_meta, output);


          }
        }
      }
    }
  }
  return GST_FLOW_OK;
}

In this code, printf("\n&&&&&&&&&&&&&&&&&&& JOJO &&&&&&&&&&&"); gets executed but after that, all lines are ignored because, for (fmeta_list = frame_meta->frame_user_meta_list; fmeta_list != NULL; fmeta_list = fmeta_list->next) - this loop never executes as fmeta_list is always NULL.
I suspected that it is because the deepstreamapp.c doesn’t have any nvof (optical flow) added to the pipeline. So, in deepstreamapp.c, I made the following changes in the create_pipeline function.

if (config->dsdirection_config.enable)
  {
    printf("full-frame: %d \n", config->dsdirection_config.processing_width); 
    // goto done;  
    // Create dsexample element bin and set properties
    if (!create_dsdirection_bin(&config->dsdirection_config,
                              &pipeline->dsdirection_bin))
    {
      goto done;
    }

    GstElement  *nvof = NULL, *of_queue = NULL; 
      /* create nv optical flow element */
    nvof = gst_element_factory_make ("nvof", "nvopticalflow");
    of_queue = gst_element_factory_make ("queue", "q_after_of");


    if (!nvof ) {
      g_printerr ("nvof could not be created. Exiting.\n");
      return -1;
    }

    // Add dsexample bin to instance bin
    gst_bin_add(GST_BIN(pipeline->pipeline), nvof); // my added line
    gst_bin_add(GST_BIN(pipeline->pipeline), of_queue);  // my added line


    // Link this bin to the last element in the bin

    NVGSTDS_LINK_ELEMENT(pipeline->dsdirection_bin.bin, nvof);  // my added line
    NVGSTDS_LINK_ELEMENT(nvof, of_queue);  // my added line
    NVGSTDS_LINK_ELEMENT(of_queue, last_elem);  // my added line

    NVGSTDS_ELEM_ADD_PROBE (dsdirection_probe_id,
                              pipeline->dsdirection_bin.bin, "src",
                              dsdirection_buf_probe, GST_PAD_PROBE_TYPE_BUFFER,
                              appCtx);

    // Set this bin as the last element
    last_elem = pipeline->dsdirection_bin.bin;
  }

I think I am doing something wrong in the gst_bin_add and NVGSTDS_LINK_ELEMENT functions. But can’t really put my fingers around it.
Could anyone please help me about it? I went through the dsexample.cpp files but can’t find the nvof problem.

Can the sample https://github.com/NVIDIA-AI-IOT/deepstream_reference_apps/tree/541a21e8ed90e408d0568e2baf19e1d5cb8483a2/anomaly/apps/deepstream-anomaly-detection-test work on your platform?

Since the codes you published is so limited, we can not know some related information with your implementation. But there is an obvious problem here.
The sample codes pipeline is source->nvstreammux->nvinfer->nvof->queue->dsdirection->…, but you put dsdirection before nvof in your codes.