]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
Fix audio-only timeshift memory usage
authorFlole998 <Flole998@users.noreply.github.com>
Fri, 2 Feb 2024 22:40:42 +0000 (22:40 +0000)
committerFlole998 <Flole998@users.noreply.github.com>
Sat, 3 Feb 2024 20:29:48 +0000 (21:29 +0100)
src/timeshift/private.h
src/timeshift/timeshift_writer.c

index 16536229cc5be50169629d2e287d7ade2c74ca33..c6a5609f260b3bb2c58b8c6cf78d7e004594a512 100644 (file)
@@ -132,6 +132,8 @@ typedef struct timeshift {
 
   int                         vididx;     ///< Index of (current) video stream
   int                         audidx;     ///< Index of (current) audio stream
+  
+  uint8_t                         audio_packet_counter; ///< Counter for audio packets in audio-only streams
 
   streaming_start_t          *smt_start;  ///< Streaming start info
 
index 6f15a38c162088a572a1c2c2350c96fe69620c96..9c6591c37f421bd54ca909d96608d8efaeaadf75 100644 (file)
@@ -243,6 +243,8 @@ static void _update_smt_start ( timeshift_t *ts, streaming_start_t *ss )
   streaming_start_ref(ss);
   ts->smt_start = ss;
 
+  ts->audio_packet_counter = 255;
+
   /* Update video index */
   for (i = 0; i < ss->ss_num_components; i++)
     if (SCT_ISVIDEO(ss->ss_components[i].es_type)) {
@@ -271,6 +273,31 @@ static void _handle_sstart ( timeshift_t *ts, timeshift_file_t *tsf, streaming_m
   TAILQ_INSERT_TAIL(&tsf->sstart, ti, link);
 }
 
+/*
+ * Index i-frames and every 100th audio frame
+ */
+static void add_frame_to_index ( timeshift_t *ts, timeshift_file_t *tsf, streaming_message_t *sm )
+{
+  th_pkt_t *pkt = sm->sm_data;
+
+  /* Index video iframes or audio frames for audio-only streams*/
+  if ((pkt->pkt_componentindex == ts->vididx && pkt->v.pkt_frametype == PKT_I_FRAME) ||
+      (ts->vididx == -1 && pkt->pkt_componentindex == ts->audidx)) {
+
+    if(ts->vididx != -1 || ts->audio_packet_counter > 100) {    
+      timeshift_index_iframe_t *ti = calloc(1, sizeof(timeshift_index_iframe_t));
+      memoryinfo_alloc(&timeshift_memoryinfo, sizeof(*ti));
+      ti->pos  = tsf->size;
+      ti->time = sm->sm_time;
+      TAILQ_INSERT_TAIL(&tsf->iframes, ti, link);
+      if(ts->vididx == -1)
+        ts->audio_packet_counter = 0;
+    }
+    if(ts->vididx == -1)
+      ts->audio_packet_counter++;
+  }
+}
+
 /* **************************************************************************
  * Thread
  * *************************************************************************/
@@ -283,28 +310,19 @@ static inline ssize_t _process_msg0
   if (sm->sm_type == SMT_START) {
     err = 0;
     _handle_sstart(ts, tsf, streaming_msg_clone(sm));
-  } else if (sm->sm_type == SMT_SIGNAL_STATUS)
+  } else if (sm->sm_type == SMT_SIGNAL_STATUS) {
     err = timeshift_write_sigstat(tsf, sm->sm_time, sm->sm_data);
-  else if (sm->sm_type == SMT_PACKET) {
+  else if (sm->sm_type == SMT_PACKET) {
     err = timeshift_write_packet(tsf, sm->sm_time, sm->sm_data);
     if (err > 0) {
-      th_pkt_t *pkt = sm->sm_data;
-
-      /* Index video iframes or audio frames for audio-only streams*/
-      if ((pkt->pkt_componentindex == ts->vididx && pkt->v.pkt_frametype == PKT_I_FRAME) ||
-          (ts->vididx == -1 && pkt->pkt_componentindex == ts->audidx)) {
-        timeshift_index_iframe_t *ti = calloc(1, sizeof(timeshift_index_iframe_t));
-        memoryinfo_alloc(&timeshift_memoryinfo, sizeof(*ti));
-        ti->pos  = tsf->size;
-        ti->time = sm->sm_time;
-        TAILQ_INSERT_TAIL(&tsf->iframes, ti, link);
-      }
+      add_frame_to_index(ts, tsf, sm);
     }
   } else if (sm->sm_type == SMT_MPEGTS) {
     err = timeshift_write_mpegts(tsf, sm->sm_time, sm->sm_data);
   }
-  else
+  else {
     err = 0;
+  }
 
   /* OK */
   if (err > 0) {