~phablet-team/media-hub/fix-1510219

« back to all changes in this revision

Viewing changes to src/core/media/gstreamer/meta_data_extractor.h

  • Committer: CI Train Bot
  • Author(s): Simon Fels
  • Date: 2015-09-23 14:23:07 UTC
  • mfrom: (154.1.2 fix-leaking-fds)
  • Revision ID: ci-train-bot@canonical.com-20150923142307-3uluu4vflxhtw1um
Correctly cleanup gstreamer elements to prevent us from leaking memory/file descriptors. Fixes: #1496894

Show diffs side-by-side

added added

removed removed

Lines of Context:
147
147
    MetaDataExtractor()
148
148
        : pipe(gst_pipeline_new("meta_data_extractor_pipeline")),
149
149
          decoder(gst_element_factory_make ("uridecodebin", NULL)),
150
 
          bus(GST_ELEMENT_BUS(pipe))
 
150
          bus(gst_element_get_bus(pipe))
151
151
    {
152
152
        gst_bin_add(GST_BIN(pipe), decoder);
153
153
 
159
159
 
160
160
    ~MetaDataExtractor()
161
161
    {
162
 
        gst_element_set_state(pipe, GST_STATE_NULL);
163
 
        // gst_object_unref(pipe);
 
162
        set_state_and_wait(GST_STATE_NULL);
 
163
        gst_object_unref(pipe);
 
164
    }
 
165
 
 
166
    bool set_state_and_wait(GstState new_state)
 
167
    {
 
168
        static const std::chrono::nanoseconds state_change_timeout
 
169
        {
 
170
            // We choose a quite high value here as tests are run under valgrind
 
171
            // and gstreamer pipeline setup/state changes take longer in that scenario.
 
172
            // The value does not negatively impact runtime performance.
 
173
            std::chrono::milliseconds{5000}
 
174
        };
 
175
 
 
176
        auto ret = gst_element_set_state(pipe, new_state);
 
177
 
 
178
        bool result = false; GstState current, pending;
 
179
        switch(ret)
 
180
        {
 
181
            case GST_STATE_CHANGE_FAILURE:
 
182
                result = false; break;
 
183
            case GST_STATE_CHANGE_NO_PREROLL:
 
184
            case GST_STATE_CHANGE_SUCCESS:
 
185
                result = true; break;
 
186
            case GST_STATE_CHANGE_ASYNC:
 
187
                result = GST_STATE_CHANGE_SUCCESS == gst_element_get_state(
 
188
                    pipe,
 
189
                    &current,
 
190
                    &pending,
 
191
                    state_change_timeout.count());
 
192
                break;
 
193
        }
 
194
 
 
195
        return result;
164
196
    }
165
197
 
166
198
    core::ubuntu::media::Track::MetaData meta_data_for_track_with_uri(const core::ubuntu::media::Track::UriType& uri)
193
225
 
194
226
        if (std::future_status::ready != future.wait_for(std::chrono::seconds(4)))
195
227
        {
196
 
            gst_element_set_state(pipe, GST_STATE_NULL);
 
228
            set_state_and_wait(GST_STATE_NULL);
197
229
            throw std::runtime_error("Problem extracting meta data for track");
198
230
        } else
199
231
        {
200
 
            gst_element_set_state(pipe, GST_STATE_NULL);
 
232
            set_state_and_wait(GST_STATE_NULL);
201
233
        }
202
234
 
203
235
        return future.get();