libdvr: unplug HDD when recording channel,plug HDD,no files in the PVR list. [1/1]

PD#SWPL-65538

Problem:
Get segment info error,if we unplug device when recording,segment all info file
is not store segment ifo, only store info when stop segment record.

Solution:
1 if all info file open error.we will open sub info file to load info
and to play file.

Verify:
t5

Signed-off-by: hualing chen <hualing.chen@amlogic.com>
Change-Id: I290b65e5542a71b96bb95a704ba73a0eb48cd10f
diff --git a/src/dvr_wrapper.c b/src/dvr_wrapper.c
index 37a6656..9491a04 100644
--- a/src/dvr_wrapper.c
+++ b/src/dvr_wrapper.c
@@ -1555,13 +1555,59 @@
           int found = 0;
           list_for_each_entry(seg_info, &info_list, head)
           {
-            if (seg_info->id == i) {
+            if (seg_info->id == p_segment_ids[i]) {
               found = 1;
               DVR_WRAPPER_DEBUG(1, "get segment info::%d", i);
               break;
             }
           }
           if (!found) {
+            //last info is not found if when recording occured power off.
+            if (p_segment_ids[i] == segment_nb - 1) {
+              DVR_RecordSegmentInfo_t seg_info;
+              DVR_PlaybackSegmentFlag_t flags;
+
+              error = dvr_segment_get_info(ctx->playback.param_open.location, p_segment_ids[i], &seg_info);
+              if (error) {
+                error = DVR_FAILURE;
+                DVR_WRAPPER_DEBUG(1, "fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
+                  ctx->playback.param_open.location, p_segment_ids[i], error);
+                break;
+              }
+              //
+              //add check if has audio  or video pid. if not exist. not add segment to playback
+              int ii = 0;
+              int has_av = 0;
+              for (ii = 0; ii < seg_info.nb_pids; ii++) {
+                int type = (seg_info.pids[ii].type >> 24) & 0x0f;
+                if (type == DVR_STREAM_TYPE_VIDEO ||
+                  type == DVR_STREAM_TYPE_AUDIO ||
+                  type == DVR_STREAM_TYPE_AD) {
+                DVR_WRAPPER_DEBUG(1, "success to get seg av info \n");
+                  DVR_WRAPPER_DEBUG(1, "success to get seg av info type[0x%x][%d] [%d][%d][%d]\n",(seg_info.pids[ii].type >> 24)&0x0f,seg_info.pids[ii].pid,
+                  DVR_STREAM_TYPE_VIDEO,
+                  DVR_STREAM_TYPE_AUDIO,
+                  DVR_STREAM_TYPE_AD);
+                  has_av = 1;
+                  //break;
+                } else {
+                  DVR_WRAPPER_DEBUG(1, "error to get seg av info type[0x%x][%d] [%d][%d][%d]\n",(seg_info.pids[ii].type >> 24)&0x0f,seg_info.pids[ii].pid,
+                  DVR_STREAM_TYPE_VIDEO,
+                  DVR_STREAM_TYPE_AUDIO,
+                  DVR_STREAM_TYPE_AD);
+                }
+              }
+              if (has_av == 0) {
+                DVR_WRAPPER_DEBUG(1, "fail to get seg av info \n");
+                continue;
+              } else {
+                DVR_WRAPPER_DEBUG(1, "success to get seg av info \n");
+              }
+              flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
+              error = wrapper_addPlaybackSegment(ctx, &seg_info, p_pids, flags);
+              if (error)
+                break;
+            }
             continue;
           }
 
@@ -2122,7 +2168,7 @@
   uint32_t n_ids;
   uint64_t *p_ids;
 
-  error = dvr_segment_get_list(fpath, &n_ids, &p_ids);
+  error = dvr_segment_get_list(location, &n_ids, &p_ids);
 
   if (!error) {
      int i;
@@ -2130,23 +2176,23 @@
     INIT_LIST_HEAD(&info_list);
 
     //we need free info list buf when we used end.
-    error = dvr_segment_get_allInfo(fpath, &info_list);
-    if (error) {
+    error = dvr_segment_get_allInfo(location, &info_list);
+    if (error == DVR_FAILURE) {
       DVR_RecordSegmentInfo_t info;
 
       memset(&info, 0, sizeof(info));
       error = DVR_FAILURE;
       DVR_WRAPPER_DEBUG(1, "fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
-        fpath, 0, error);
+        location, 0, error);
 
       for (i = 0; i < n_ids; i++) {
-          error = dvr_segment_get_info(fpath, p_ids[i], &info);
+          error = dvr_segment_get_info(location, p_ids[i], &info);
           if (!error) {
             p_info->size += info.size;
             p_info->time += info.duration;
             p_info->pkts += info.nb_packets;
           } else {
-            DVR_WRAPPER_DEBUG(1, "%s:%lld get seg info fail.\n", fpath, p_ids[i]);
+            DVR_WRAPPER_DEBUG(1, "%s:%lld get seg info fail.\n", location, p_ids[i]);
             break;
           }
       }
@@ -2159,13 +2205,26 @@
           int found = 0;
           list_for_each_entry(seg_info, &info_list, head)
           {
-            if (seg_info->id == i) {
+            if (seg_info->id == p_ids[i]) {
               found = 1;
               break;
             }
           }
           if (!found) {
-              DVR_WRAPPER_DEBUG(1, "get segment info::%d error", i);
+              DVR_WRAPPER_DEBUG(1, "get segment info::%d [%d]n_ids[%d]error", i, p_ids[i], n_ids);
+              if (p_ids[i] == n_ids - 1) {
+                DVR_RecordSegmentInfo_t info;
+                DVR_WRAPPER_DEBUG(1, "get last segment info::%d [%d]n_ids[%d] from subfile", i, p_ids[i], n_ids);
+                error = dvr_segment_get_info(location, p_ids[i], &info);
+                if (!error) {
+                  p_info->size += info.size;
+                  p_info->time += info.duration;
+                  p_info->pkts += info.nb_packets;
+                } else {
+                  DVR_WRAPPER_DEBUG(1, "%s:%lld get seg info fail.\n", location, p_ids[i]);
+                  break;
+                }
+              }
               continue;
           }
 
@@ -2174,7 +2233,7 @@
             p_info->time += seg_info->duration;
             p_info->pkts += seg_info->nb_packets;
           } else {
-            DVR_WRAPPER_DEBUG(1, "%s:%lld get seg info fail.\n", fpath, p_ids[i]);
+            DVR_WRAPPER_DEBUG(1, "%s:%lld get seg info fail.\n", location, p_ids[i]);
             break;
           }
       }
diff --git a/src/segment.c b/src/segment.c
index 58866f7..e0c11eb 100644
--- a/src/segment.c
+++ b/src/segment.c
@@ -146,6 +146,7 @@
     p_ctx->index_fp = fopen(index_fname, "w+");
     p_ctx->dat_fp = fopen(dat_fname, "w+");
     p_ctx->all_dat_fp = fopen(all_dat_fname, "a+");
+    DVR_DEBUG(1, "%s dir %s is opened", __func__, all_dat_fname);
     p_ctx->ongoing_fp = fopen(going_name, "w+");
     p_ctx->first_pts = ULLONG_MAX;
     p_ctx->last_pts = ULLONG_MAX;