60446e2488f2a2a7e6fb8c0c261bb87fd0b0d65b
[platform/upstream/gstreamer.git] / docs / random / autoplug1
1 A little explanation of the first autoplugger in GStreamer:
2
3 Autoplugging is implemented in the following places:
4
5  gstpipeline.c : construction of the pipeline
6  gstautoplug.c : selection of the elementfactories needed for autoplugging
7
8 1) pipeline setup
9 -----------------
10
11 before any autoplugging will take place, a new GstPipeline has to be created.
12 The autoplugger needs to have a src element and one or more sink elements. the
13 autoplugger will try to find the elements needed to connect the src element
14 to the sinks.
15
16 using:
17
18   gst_pipeline_add_src (GstPipeline *pipeline, GstElement *element);
19
20 a source element is added to the pipeline. only one src element can be added
21 for now.
22
23 using:
24
25   gst_pipeline_add_sink (GstPipeline *pipeline, GstElement *element);
26
27 a sink element can be added to the pipeline.
28
29 2) starting autoplug
30 --------------------
31
32 when the pipeline has been set up as above, you will call
33
34   gst_pipeline_autoplug (GstPipeline *pipeline);
35
36 to start the autoplugger. this will be done in four phases
37
38 ex. we are going to autoplug an mpeg1 system stream.
39
40 2a) phase1: figure out the type (GstCaps) of the src element.
41 -------------------------------------------------------------
42
43 the gsttypefind element is connected to the "src" pad of the source
44 element. gst_bin_iterate is called in a loop until gsttypefind
45 signals "have_type". the gst_bin_iterate is stopped and the GstCaps
46 is retrieved from the gsttypefind element.
47
48 gsttypefind is disconnected from the src element and removed from the 
49 bin.
50
51 the GstCaps of the source element is called src_caps later on.
52
53 ex. all typefind functions are tried and the one in mpeg1types will
54      return a GstCaps:
55
56         video/mpeg,
57         "systemstream", GST_PROPS_BOOLEAN (TRUE),
58         "mpegversion",  GST_PROPS_INT (1),
59         NULL
60
61
62 2b) phase2: create lists of factories.
63 ---------------------------------------
64
65 for each sink:
66 {
67    sinkpad = take the first sinkpad of the sink (HACK)
68    call
69    
70    list[i] = gst_autoplug_caps (src_caps, sinkpad->caps);
71      
72    I++;
73 }
74
75 gst_autoplug_caps will figure out (based on the padtemplates)
76 which elementfactories are needed to connect src_caps to sinkpad->caps
77 and will return them in a list.
78
79 ex. we have two sinks with following caps:
80
81         video/raw                    audio/raw
82         "...."                       "...."
83
84  gst_autoplug_caps will figure out that for the first sink the following
85  elements are needed:
86
87    mpeg1parse, mp1videoparse, mpeg_play
88
89  for the second sink the following is needed:
90
91    mpeg1parse, mp3parse, mpg123
92
93  We now have two lists of elementfactories.
94
95 2c) phase3: collect common elements from the lists.
96 ---------------------------------------------------
97
98 the rationale is that from the lists we have created in phase2, there
99 must be some element that is a splitter and that it has to come first (HACK)
100 We try to find that element by comparing the lists until an element differs.
101
102 we add the common elements to the bin and run gst_pipeline_pads_autoplug. this
103 function will loop over the pads of the previous element and the one we
104 just added, and tries to connect src to sink if possible. 
105
106 If a connection between the two elements could not be made, a signal "new_pad"
107 is connected to the element so that pad connection can occur later on when
108 the pad is actually created.
109
110 ex. when we compare the two lists we see that we have common element: mpeg1parse.
111
112  we add this element to the bin and try to connect it to the previous element in
113  the bin, the disksrc.
114
115  we see that the src pad of the disksrc and the sinkpad of the mpeg1parse element
116  can be connected because they are compatible. We have a pipeline like:
117
118   ---------)         (--------
119    disksrc !         ! mpeg1parse
120            src --- sink
121   ---------)         (--------
122
123
124 2d) phase4: add remaining elements
125 ----------------------------------
126
127 now we loop over all the list and try to add the remaining elements
128
129 (HACK) we always use a new thread for the elements when there is a common
130 element found.
131
132 if a new thread is needed (either bacuase the previous element is a common
133 element or the object flag of the next element is set to GST_SUGGEST_THREAD)
134 we add a queue to the bin and we add a new thread. We add the elements to
135 the bin and connect them using gst_pipeline_pads_autoplug.
136
137 If we add a queue, we have to copy the caps of the sink element of the queue
138 to the src pad of the queue (else they won't connect)
139
140 we finally arrive at the sink element and we're done.
141
142 ex.
143     
144      we have just found our mpeg1parse common element, so we start a thread.
145      We add a queue to the bin and a new thread, we add the elements
146      mp1videoparse and mpeg_play to the thread. We arrive at the videosink, we
147      see that the SUGGEST_THREAD flag is set, we add a queue and a thread and
148      add the videosink in the thread.
149  
150      the same procedure happens for the audio part. We are now left with the
151      following pipeline:
152
153      We will also have set a signal "new_pad" on the mpeg1parse element bacause
154      the element mp1videoparse could not be connected to the element just yet.
155
156                                                  (------------------------------------)         (----------
157                                                  !thread                              !         ! thread
158                                                  ! (-------------)       (---------)  !         ! (---------)
159                                                  ! !mp1videoparse!       !mpeg_play!  !         ! !videosink!
160                                    videoqueue--sink             src -- sink      src -- queue --- sink     !
161   ---------)         (-----------)               ! (-------------)       (---------)  !         ! (---------)
162    disksrc !         ! mpeg1parse!               (------------------------------------)         (-------------
163            src --- sink          !
164   ---------)         (-----------)    
165                                       queue-----  same for audio
166
167
168    then we play, create_plan happens, data is flowing and the "new_pad" signal is called
169    from mpeg1parse, gst_pipeline_pad_autoplug is called and the connection between
170    mpeg1parse and the videoqueue is made. same for audio.
171
172    voila. smame procedure for mp3/vorbis/avi/qt/mpeg2 etc...
173
174
175 Problems:
176 ---------
177
178 this is obviously a very naive solution. the creation of the elements actually happens
179 beforehand. MPEG2, for one, fails bacause there are multiple possibilities to go
180 from the mpeg demuxer to audio/raw (ac3, mp3)
181
182 Also any intermedia elements like mixers (subtitles) are not possible because we 
183 assume that after the common elements, the streams to not converge anymore.
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198  
199  
200
201
202