[vlc-devel] [PATCH 05/15] mp4: Extract attachments upon first request

Hugo Beauzée-Luyssen hugo at beauzee.fr
Tue Nov 10 18:40:15 CET 2020


Instead of each time DEMUX_GET_ATTACHMENT gets invoked
---
 modules/demux/mp4/attachments.c |  5 +++--
 modules/demux/mp4/attachments.h |  2 +-
 modules/demux/mp4/mp4.c         | 20 ++++++++++++++++++--
 3 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/modules/demux/mp4/attachments.c b/modules/demux/mp4/attachments.c
index 20fa4ea512..2e00c0468e 100644
--- a/modules/demux/mp4/attachments.c
+++ b/modules/demux/mp4/attachments.c
@@ -233,11 +233,11 @@ int MP4_GetCoverMetaURI( const MP4_Box_t *p_root,
     return VLC_SUCCESS;
 }
 
-int MP4_GetAttachments( const MP4_Box_t *p_root, input_attachment_t ***ppp_attach )
+size_t MP4_GetAttachments( const MP4_Box_t *p_root, input_attachment_t ***ppp_attach )
 {
     const MP4_Box_t *p_metaroot = NULL;
     const char *psz_metarootpath;
-    unsigned i_count = 0;
+    size_t i_count = 0;
     input_attachment_t **pp_attach = NULL;
     *ppp_attach = NULL;
 
@@ -371,6 +371,7 @@ int MP4_GetAttachments( const MP4_Box_t *p_root, input_attachment_t ***ppp_attac
     if ( i_count == 0 )
     {
         free( pp_attach );
+        **ppp_attach = NULL;
         return 0;
     }
 
diff --git a/modules/demux/mp4/attachments.h b/modules/demux/mp4/attachments.h
index ee0eb771a7..fe8eaf1b7f 100644
--- a/modules/demux/mp4/attachments.h
+++ b/modules/demux/mp4/attachments.h
@@ -21,7 +21,7 @@
 #ifndef VLC_MP4_ATTACHMENTS_H_
 #define VLC_MP4_ATTACHMENTS_H_
 
-int MP4_GetAttachments( const MP4_Box_t *, input_attachment_t *** );
+size_t MP4_GetAttachments( const MP4_Box_t *, input_attachment_t *** );
 const MP4_Box_t *MP4_GetMetaRoot( const MP4_Box_t *, const char ** );
 int MP4_GetCoverMetaURI( const MP4_Box_t *,  const MP4_Box_t *,
                          const char *, vlc_meta_t * );
diff --git a/modules/demux/mp4/mp4.c b/modules/demux/mp4/mp4.c
index 152441750d..fb3191af05 100644
--- a/modules/demux/mp4/mp4.c
+++ b/modules/demux/mp4/mp4.c
@@ -145,6 +145,9 @@ typedef struct
     } hacks;
 
     mp4_fragments_index_t *p_fragsindex;
+
+    ssize_t i_attachments;
+    input_attachment_t **pp_attachments;
 } demux_sys_t;
 
 #define DEMUX_INCREMENT VLC_TICK_FROM_MS(250) /* How far the pcr will go, each round */
@@ -804,6 +807,7 @@ static int Open( vlc_object_t * p_this )
     p_demux->pf_control = Control;
 
     p_sys->context.i_lastseqnumber = UINT32_MAX;
+    p_sys->i_attachments = -1;
 
     p_demux->p_sys = p_sys;
 
@@ -2044,9 +2048,17 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
             input_attachment_t ***ppp_attach = va_arg( args, input_attachment_t*** );
             int *pi_int = va_arg( args, int * );
 
-            *pi_int = MP4_GetAttachments( p_sys->p_root, ppp_attach );
-            for( int i=0; i<*pi_int; i++ )
+            if( p_sys->i_attachments == -1 )
+                p_sys->i_attachments = MP4_GetAttachments( p_sys->p_root, &p_sys->pp_attachments );
+            *ppp_attach = calloc( p_sys->i_attachments, sizeof(**ppp_attach ) );
+            if( !*ppp_attach )
+                return VLC_ENOMEM;
+            for ( size_t i = 0; i < p_sys->i_attachments; ++i )
+            {
+                (*ppp_attach)[i] = vlc_input_attachment_Hold( p_sys->pp_attachments[i] );
                 msg_Dbg( p_demux, "adding attachment %s", (*ppp_attach)[i]->psz_name );
+            }
+            *pi_int = p_sys->i_attachments;
 
             return VLC_SUCCESS;
         }
@@ -2169,6 +2181,10 @@ static void Close ( vlc_object_t * p_this )
         MP4_TrackClean( p_demux->out, &p_sys->track[i_track] );
     free( p_sys->track );
 
+    for ( size_t i = 0; i < p_sys->i_attachments; ++i )
+        vlc_input_attachment_Release( p_sys->pp_attachments[i] );
+    free( p_sys->pp_attachments );
+
     free( p_sys );
 }
 
-- 
2.28.0



More information about the vlc-devel mailing list