+
+input_item_node_t *input_item_node_Create( input_item_t *p_input )
+{
+ input_item_node_t* p_node = malloc( sizeof( input_item_node_t ) );
+ if( !p_node )
+ return NULL;
+
+ assert( p_input );
+
+ p_node->p_item = p_input;
+ vlc_gc_incref( p_input );
+
+ p_node->p_parent = NULL;
+ p_node->i_children = 0;
+ p_node->pp_children = NULL;
+
+ return p_node;
+}
+
+static void RecursiveNodeDelete( input_item_node_t *p_node )
+{
+ for( int i = 0; i < p_node->i_children; i++ )
+ RecursiveNodeDelete( p_node->pp_children[i] );
+
+ vlc_gc_decref( p_node->p_item );
+ free( p_node->pp_children );
+ free( p_node );
+}
+
+void input_item_node_Delete( input_item_node_t *p_node )
+{
+ if( p_node->p_parent )
+ for( int i = 0; i < p_node->p_parent->i_children; i++ )
+ if( p_node->p_parent->pp_children[i] == p_node )
+ {
+ REMOVE_ELEM( p_node->p_parent->pp_children,
+ p_node->p_parent->i_children,
+ i );
+ break;
+ }
+
+ RecursiveNodeDelete( p_node );
+}
+
+input_item_node_t *input_item_node_AppendItem( input_item_node_t *p_node, input_item_t *p_item )
+{
+ input_item_node_t *p_new_child = input_item_node_Create( p_item );
+ if( !p_new_child ) return NULL;
+ input_item_node_AppendNode( p_node, p_new_child );
+ return p_new_child;
+}
+
+void input_item_node_AppendNode( input_item_node_t *p_parent, input_item_node_t *p_child )
+{
+ assert( p_parent && p_child && p_child->p_parent == NULL );
+ INSERT_ELEM( p_parent->pp_children,
+ p_parent->i_children,
+ p_parent->i_children,
+ p_child );
+ p_child->p_parent = p_parent;
+}
+
+void input_item_node_PostAndDelete( input_item_node_t *p_root )
+{
+ post_subitems( p_root );
+
+ vlc_event_t event;
+ event.type = vlc_InputItemSubItemTreeAdded;
+ event.u.input_item_subitem_tree_added.p_root = p_root;
+ vlc_event_send( &p_root->p_item->event_manager, &event );
+
+ input_item_node_Delete( p_root );
+}
+
+/* Called by es_out when a new Elementary Stream is added or updated. */
+void input_item_UpdateTracksInfo(input_item_t *item, const es_format_t *fmt)
+{
+ int i;
+ es_format_t *fmt_copy = malloc(sizeof *fmt_copy);
+ if (!fmt_copy)
+ return;
+
+ es_format_Copy(fmt_copy, fmt);
+ /* XXX: we could free p_extra to save memory, we will likely not need
+ * the decoder specific data */
+
+ vlc_mutex_lock( &item->lock );
+
+ for( i = 0; i < item->i_es; i++ )
+ {
+ if (item->es[i]->i_id != fmt->i_id)
+ continue;
+
+ /* We've found the right ES, replace it */
+ es_format_Clean(item->es[i]);
+ free(item->es[i]);
+ item->es[i] = fmt_copy;
+ vlc_mutex_unlock( &item->lock );
+ return;
+ }
+
+ /* ES not found, insert it */
+ TAB_APPEND(item->i_es, item->es, fmt_copy);
+ vlc_mutex_unlock( &item->lock );
+}