duplicate.c 6.81 KB
Newer Older
1
2
3
4
/*****************************************************************************
 * duplicate.c
 *****************************************************************************
 * Copyright (C) 2001, 2002 VideoLAN
5
 * $Id: duplicate.c,v 1.7 2003/12/14 17:58:37 gbazin Exp $
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>
#include <string.h>

#include <vlc/vlc.h>
#include <vlc/sout.h>

/*****************************************************************************
 * Exported prototypes
 *****************************************************************************/
static int      Open    ( vlc_object_t * );
static void     Close   ( vlc_object_t * );

39
static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
40
static int               Del ( sout_stream_t *, sout_stream_id_t * );
gbazin's avatar
   
gbazin committed
41
42
static int               Send( sout_stream_t *, sout_stream_id_t *,
                               sout_buffer_t* );
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
vlc_module_begin();
    set_description( _("Duplicate stream") );
    set_capability( "sout stream", 50 );
    add_shortcut( "duplicate" );
    add_shortcut( "dup" );
    set_callbacks( Open, Close );
vlc_module_end();


struct sout_stream_sys_t
{
    int             i_nb_streams;
    sout_stream_t   **pp_streams;
};

struct sout_stream_id_t
{
    int                 i_nb_ids;
    void                **pp_ids;
};

/*****************************************************************************
 * Open:
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    sout_stream_t     *p_stream = (sout_stream_t*)p_this;
    sout_stream_sys_t *p_sys;
    sout_cfg_t        *p_cfg;

    msg_Dbg( p_stream, "creating a duplication" );

    p_sys = malloc( sizeof( sout_stream_sys_t ) );
    p_sys->i_nb_streams = 0;
    p_sys->pp_streams   = NULL;

    for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
    {
        if( !strncmp( p_cfg->psz_name, "dst", strlen( "dst" ) ) )
        {
            sout_stream_t *s;

            msg_Dbg( p_stream, " * adding `%s'", p_cfg->psz_value );
            s = sout_stream_new( p_stream->p_sout, p_cfg->psz_value );

gbazin's avatar
   
gbazin committed
92
93
94
95
            if( s )
            {
                TAB_APPEND( p_sys->i_nb_streams, p_sys->pp_streams, s );
            }
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
        }
    }

    if( p_sys->i_nb_streams == 0 )
    {
        msg_Err( p_stream, "no destination given" );
        free( p_sys );

        return VLC_EGENERIC;
    }

    p_stream->pf_add    = Add;
    p_stream->pf_del    = Del;
    p_stream->pf_send   = Send;

    p_stream->p_sys     = p_sys;

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close:
 *****************************************************************************/

static void Close( vlc_object_t * p_this )
{
    sout_stream_t     *p_stream = (sout_stream_t*)p_this;
    sout_stream_sys_t *p_sys = p_stream->p_sys;

    int i;

gbazin's avatar
   
gbazin committed
127
    msg_Dbg( p_stream, "closing a duplication");
128
129
130
131
132
133
134
135
136
    for( i = 0; i < p_sys->i_nb_streams; i++ )
    {
        sout_stream_delete( p_sys->pp_streams[i] );
    }
    free( p_sys->pp_streams );

    free( p_sys );
}

137
static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
138
139
140
{
    sout_stream_sys_t *p_sys = p_stream->p_sys;
    sout_stream_id_t  *id;
141
    int i_stream, i_valid_streams = 0;
142
143
144
145
146
147
148
149
150
151

    id = malloc( sizeof( sout_stream_id_t ) );
    id->i_nb_ids = 0;
    id->pp_ids   = NULL;

    for( i_stream = 0; i_stream < p_sys->i_nb_streams; i_stream++ )
    {
        void *id_new;

        /* XXX not the same sout_stream_id_t definition ... */
gbazin's avatar
   
gbazin committed
152
153
        id_new = (void*)p_sys->pp_streams[i_stream]->pf_add(
                            p_sys->pp_streams[i_stream], p_fmt );
154
155

        /* Append failed attempts as well to keep track of which pp_id
156
	 * belongs to which duplicated stream */
157
158
        TAB_APPEND( id->i_nb_ids, id->pp_ids, id_new );
        if( id_new ) i_valid_streams++;
159
160
    }

161
    if( i_valid_streams <= 0 )
162
    {
163
        Del( p_stream, id );
164
165
166
167
168
169
        return NULL;
    }

    return id;
}

gbazin's avatar
   
gbazin committed
170
static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
171
172
173
174
175
176
{
    sout_stream_sys_t *p_sys = p_stream->p_sys;
    int               i_stream;

    for( i_stream = 0; i_stream < p_sys->i_nb_streams; i_stream++ )
    {
177
        if( id->pp_ids[i_stream] )
178
179
180
181
182
183
184
185
186
187
188
        {
            p_sys->pp_streams[i_stream]->pf_del( p_sys->pp_streams[i_stream],
                                                 id->pp_ids[i_stream] );
        }
    }

    free( id->pp_ids );
    free( id );
    return VLC_SUCCESS;
}

gbazin's avatar
   
gbazin committed
189
190
static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
                 sout_buffer_t *p_buffer )
191
192
{
    sout_stream_sys_t *p_sys = p_stream->p_sys;
193
    sout_stream_t     *p_dup_stream;
194
195
    int               i_stream;

196
197
    /* Loop through the linked list of buffers */
    while( p_buffer )
198
    {
199
        sout_buffer_t *p_next = p_buffer->p_next;
200

201
202
203
        p_buffer->p_next = NULL;

        for( i_stream = 0; i_stream < p_sys->i_nb_streams - 1; i_stream++ )
204
        {
205
206
207
208
209
210
            sout_buffer_t *p_dup;
            p_dup_stream = p_sys->pp_streams[i_stream];

            if( id->pp_ids[i_stream] )
            {
                p_dup = sout_BufferDuplicate( p_stream->p_sout, p_buffer );
211

212
213
214
                p_dup_stream->pf_send( p_dup_stream, id->pp_ids[i_stream],
                                       p_dup );
            }
215
216
        }

217
218
219
220
221
222
223
224
225
226
        if( i_stream < p_sys->i_nb_streams && id->pp_ids[i_stream] )
        {
            p_dup_stream = p_sys->pp_streams[i_stream];
            p_dup_stream->pf_send( p_dup_stream, id->pp_ids[i_stream],
                                   p_buffer );
        }
        else
        {
            sout_BufferDelete( p_stream->p_sout, p_buffer );
        }
227

228
229
        p_buffer = p_next;
    }
230
231
    return VLC_SUCCESS;
}