From: Ziqing Hui zhui@codeweavers.com
wg_muxer_start will autoplug gstreamer muxer and parser elements. It creates a pipeline like this:
------------------- ------- my_src 1 ==> |parser 1 (optional)| ==> | | ------------------- | | | | ------------------- | | my_src 2 ==> |parser 2 (optional)| ==> | | ------------------- | | | muxer | ==> my_sink | | [......] | | | | | | ------------------- | | my_src n ==> |parser n (optional)| ==> | | ------------------- ------- --- dlls/winegstreamer/gst_private.h | 1 + dlls/winegstreamer/main.c | 15 ++ dlls/winegstreamer/media_sink.c | 9 +- dlls/winegstreamer/unix_private.h | 1 + dlls/winegstreamer/unixlib.h | 1 + dlls/winegstreamer/wg_muxer.c | 296 +++++++++++++++++++++++++++--- dlls/winegstreamer/wg_parser.c | 2 + 7 files changed, 296 insertions(+), 29 deletions(-)
diff --git a/dlls/winegstreamer/gst_private.h b/dlls/winegstreamer/gst_private.h index 23b59766d0c..38516ba9024 100644 --- a/dlls/winegstreamer/gst_private.h +++ b/dlls/winegstreamer/gst_private.h @@ -113,6 +113,7 @@ HRESULT wg_transform_flush(wg_transform_t transform); HRESULT wg_muxer_create(const char *format, wg_muxer_t *muxer); void wg_muxer_destroy(wg_muxer_t muxer); HRESULT wg_muxer_add_stream(wg_muxer_t muxer, UINT32 stream_id, const struct wg_format *format); +HRESULT wg_muxer_start(wg_muxer_t muxer);
unsigned int wg_format_get_max_size(const struct wg_format *format);
diff --git a/dlls/winegstreamer/main.c b/dlls/winegstreamer/main.c index 55817922e9b..95c4296812b 100644 --- a/dlls/winegstreamer/main.c +++ b/dlls/winegstreamer/main.c @@ -507,6 +507,21 @@ HRESULT wg_muxer_add_stream(wg_muxer_t muxer, UINT32 stream_id, const struct wg_ return S_OK; }
+HRESULT wg_muxer_start(wg_muxer_t muxer) +{ + NTSTATUS status; + + TRACE("muxer %#I64x.\n", muxer); + + if ((status = WINE_UNIX_CALL(unix_wg_muxer_start, &muxer))) + { + WARN("Failed to start muxer, status %#lx.\n", status); + return HRESULT_FROM_NT(status); + } + + return S_OK; +} + #define ALIGN(n, alignment) (((n) + (alignment) - 1) & ~((alignment) - 1))
unsigned int wg_format_get_stride(const struct wg_format *format) diff --git a/dlls/winegstreamer/media_sink.c b/dlls/winegstreamer/media_sink.c index 344134d1633..489ede1d26a 100644 --- a/dlls/winegstreamer/media_sink.c +++ b/dlls/winegstreamer/media_sink.c @@ -498,7 +498,13 @@ static HRESULT media_sink_queue_stream_event(struct media_sink *media_sink, Medi
static HRESULT media_sink_start(struct media_sink *media_sink) { + HRESULT hr; + + if (FAILED(hr = wg_muxer_start(media_sink->muxer))) + return hr; + media_sink->state = STATE_STARTED; + return media_sink_queue_stream_event(media_sink, MEStreamSinkStarted); }
@@ -1007,7 +1013,8 @@ static HRESULT WINAPI media_sink_callback_Invoke(IMFAsyncCallback *iface, IMFAsy switch (command->op) { case ASYNC_START: - hr = media_sink_start(media_sink); + if (FAILED(hr = media_sink_start(media_sink))) + WARN("Failed to start media sink.\n"); break; case ASYNC_STOP: hr = media_sink_stop(media_sink); diff --git a/dlls/winegstreamer/unix_private.h b/dlls/winegstreamer/unix_private.h index 1f594200c2e..01fdc310bda 100644 --- a/dlls/winegstreamer/unix_private.h +++ b/dlls/winegstreamer/unix_private.h @@ -64,6 +64,7 @@ extern NTSTATUS wg_transform_flush(void *args) DECLSPEC_HIDDEN; extern NTSTATUS wg_muxer_create(void *args) DECLSPEC_HIDDEN; extern NTSTATUS wg_muxer_destroy(void *args) DECLSPEC_HIDDEN; extern NTSTATUS wg_muxer_add_stream(void *args) DECLSPEC_HIDDEN; +extern NTSTATUS wg_muxer_start(void *args) DECLSPEC_HIDDEN;
/* wg_allocator.c */
diff --git a/dlls/winegstreamer/unixlib.h b/dlls/winegstreamer/unixlib.h index d00790c6b05..9cb6359af26 100644 --- a/dlls/winegstreamer/unixlib.h +++ b/dlls/winegstreamer/unixlib.h @@ -422,6 +422,7 @@ enum unix_funcs unix_wg_muxer_create, unix_wg_muxer_destroy, unix_wg_muxer_add_stream, + unix_wg_muxer_start,
unix_wg_funcs_count, }; diff --git a/dlls/winegstreamer/wg_muxer.c b/dlls/winegstreamer/wg_muxer.c index c6e56793dc2..73d6443566c 100644 --- a/dlls/winegstreamer/wg_muxer.c +++ b/dlls/winegstreamer/wg_muxer.c @@ -32,10 +32,16 @@
#include "wine/list.h"
+#if !GST_CHECK_VERSION(1,20,0) +#define gst_element_request_pad_simple gst_element_get_request_pad +#endif + struct wg_muxer { GstElement *container, *muxer; GstPad *my_sink; + GstCaps *my_sink_caps; + struct list streams; };
@@ -47,6 +53,7 @@ struct wg_muxer_stream
GstPad *my_src; GstCaps *my_src_caps; + GstElement *parser; GstSegment segment;
struct list entry; @@ -57,6 +64,226 @@ static struct wg_muxer *get_muxer(wg_muxer_t muxer) return (struct wg_muxer *)(ULONG_PTR)muxer; }
+static const char *get_sink_name(GstStreamType type) +{ + switch (type) + { + case GST_STREAM_TYPE_VIDEO: + return "video_%u"; + case GST_STREAM_TYPE_AUDIO: + return "audio_%u"; + case GST_STREAM_TYPE_TEXT: + return "subtitle_%u"; + default: + return NULL; + } +} + +static GstCaps *element_factory_get_caps(GstElementFactory *factory, GstStreamType type) +{ + const GList *iter; + + for (iter = gst_element_factory_get_static_pad_templates(factory); iter; iter = iter->next) + { + GstStaticPadTemplate *static_template = iter->data; + + if (static_template->direction != GST_PAD_SINK) + continue; + + if ((static_template->presence == GST_PAD_REQUEST && !strcmp(static_template->name_template, get_sink_name(type))) + || static_template->presence == GST_PAD_ALWAYS) + return gst_static_pad_template_get_caps(static_template); + } + + return NULL; +} + +static bool stream_select_parser(struct wg_muxer_stream *stream, GstElementFactory *muxer_factory, GstRank min_rank) +{ + GList *parser_list, *tmp; + GstCaps *muxer_sink_caps; + bool ret = false; + + GST_DEBUG("stream %u %p, factory %s, min_rank %u, stream my_src_caps: %"GST_PTR_FORMAT".", + stream->id, stream, gst_plugin_feature_get_name(GST_PLUGIN_FEATURE(muxer_factory)), + min_rank, stream->my_src_caps); + + if (!(parser_list = gst_element_factory_list_get_elements(GST_ELEMENT_FACTORY_TYPE_PARSER, min_rank))) + return false; + + /* Filter parsers by my_src_caps. */ + tmp = gst_element_factory_list_filter(parser_list, stream->my_src_caps, GST_PAD_SINK, FALSE); + gst_plugin_feature_list_free(parser_list); + if (!(parser_list = tmp)) + return false; + + if (!(muxer_sink_caps = element_factory_get_caps(muxer_factory, stream_type_from_caps(stream->my_src_caps)))) + { + gst_plugin_feature_list_free(parser_list); + return false; + } + + /* Filter parsers by muxer sink caps. */ + if ((tmp = gst_element_factory_list_filter(parser_list, muxer_sink_caps, GST_PAD_SRC, FALSE))) + { + const gchar *parser_plugin_name = gst_plugin_feature_get_name(GST_PLUGIN_FEATURE(tmp->data)); + + if ((stream->parser = gst_element_factory_create(GST_ELEMENT_FACTORY(tmp->data), NULL))) + { + gchar *element_name = gst_element_get_name(stream->parser); + GST_DEBUG("Selected parser %s, created element %s.", parser_plugin_name, element_name); + g_free(element_name); + ret = true; + } + else + { + GST_WARNING("Failed to create %s element.", parser_plugin_name); + } + + gst_plugin_feature_list_free(tmp); + } + + gst_plugin_feature_list_free(parser_list); + gst_caps_unref(muxer_sink_caps); + + return ret; +} + +static bool stream_start(struct wg_muxer_stream *stream) +{ + GstStreamType type = stream_type_from_caps(stream->my_src_caps); + struct wg_muxer *muxer = stream->muxer; + GstPad *muxer_sink_pad = NULL; + char buffer[64]; + bool link_ok; + + /* Get muxer sink pad. */ + if (!(muxer_sink_pad = gst_element_get_static_pad(muxer->muxer, "sink"))) + { + if (!(muxer_sink_pad = gst_element_request_pad_simple(muxer->muxer, get_sink_name(type)))) + return false; + } + + /* Link my_src to muxer. */ + if (stream->parser) + { + link_ok = gst_bin_add(GST_BIN(muxer->container), stream->parser) + && link_src_to_element(stream->my_src, stream->parser) + && link_element_to_sink(stream->parser, muxer_sink_pad); + } + else + { + link_ok = !link_src_to_sink(stream->my_src, muxer_sink_pad); + } + gst_object_unref(muxer_sink_pad); + if (!link_ok) + return false; + + /* Active pad and push events to prepare for streaming. */ + sprintf(buffer, "wg_muxer_stream_src_%u", stream->id); + gst_segment_init(&stream->segment, GST_FORMAT_BYTES); + if (!gst_pad_set_active(stream->my_src, 1)) + return false; + if (!push_event(stream->my_src, gst_event_new_stream_start(buffer)) + || !push_event(stream->my_src, gst_event_new_caps(stream->my_src_caps)) + || !push_event(stream->my_src, gst_event_new_segment(&stream->segment))) + return false; + + GST_DEBUG("Started stream %u %p.", stream->id, stream); + + return true; +} + +static bool muxer_try(struct wg_muxer *muxer, GstElementFactory *muxer_factory, GstRank min_rank) +{ + char *element_name, *plugin_name = gst_plugin_feature_get_name(GST_PLUGIN_FEATURE(muxer_factory)); + struct wg_muxer_stream *stream; + unsigned int stream_index; + GstCaps *muxer_sink_caps; + + GST_DEBUG("Try muxer %s, muxer %p, min_rank %u.", plugin_name, muxer, min_rank); + + /* Investigate if all streams can connect to muxer_factory. */ + stream_index = 0; + LIST_FOR_EACH_ENTRY(stream, &muxer->streams, struct wg_muxer_stream, entry) + { + bool can_link_directly; + + if (!(muxer_sink_caps = element_factory_get_caps(muxer_factory, stream_type_from_caps(stream->my_src_caps)))) + break; + can_link_directly = gst_caps_can_intersect(stream->my_src_caps, muxer_sink_caps); + gst_caps_unref(muxer_sink_caps); + + /* Unable to link stream to muxer element directly, try inserting a parser. */ + if (!can_link_directly + && !stream_select_parser(stream, muxer_factory, min_rank)) + break; + + ++stream_index; + } + + /* Cannot satisfy all streams. */ + if (stream_index != list_count(&muxer->streams)) + { + LIST_FOR_EACH_ENTRY(stream, &muxer->streams, struct wg_muxer_stream, entry) + { + if (stream->parser) + { + gst_object_unref(stream->parser); + stream->parser = NULL; + } + } + GST_DEBUG("Cannot use %s.", plugin_name); + return false; + } + + muxer->muxer = gst_element_factory_create(GST_ELEMENT_FACTORY(muxer_factory), NULL); + + element_name = gst_element_get_name(muxer->muxer); + GST_DEBUG("Selected muxer %s, created element %s.", plugin_name, element_name); + g_free(element_name); + + return true; +} + +static bool muxer_autoplug(struct wg_muxer *muxer) +{ + GstElementFactoryListType muxer_type = GST_ELEMENT_FACTORY_TYPE_MUXER | GST_ELEMENT_FACTORY_TYPE_FORMATTER; + GstRank min_rank[] = {GST_RANK_PRIMARY, GST_RANK_SECONDARY, GST_RANK_MARGINAL, GST_RANK_NONE}; + GList *muxers, *tmp; + unsigned int i; + + GST_DEBUG("muxer %p.", muxer); + + /* Decreased min_rank in each cycle. */ + for (i = 0; i < ARRAY_SIZE(min_rank); ++i) + { + if (!(muxers = gst_element_factory_list_get_elements(muxer_type, min_rank[i]))) + continue; + + /* Filter muxers by my_sink_caps. */ + tmp = gst_element_factory_list_filter(muxers, muxer->my_sink_caps, GST_PAD_SRC, FALSE); + gst_plugin_feature_list_free(muxers); + if (!(muxers = tmp)) + continue; + muxers = g_list_sort(muxers, gst_plugin_feature_rank_compare_func); + + /* Try each muxer. */ + for (tmp = muxers; tmp; tmp = tmp->next) + { + if (muxer_try(muxer, GST_ELEMENT_FACTORY(tmp->data), min_rank[i])) + { + gst_plugin_feature_list_free(muxers); + return true; + } + } + + gst_plugin_feature_list_free(muxers); + } + + return false; +} + static gboolean muxer_src_query_cb(GstPad *pad, GstObject *parent, GstQuery *query) { struct wg_muxer_stream *stream = gst_pad_get_element_private(pad); @@ -95,10 +322,8 @@ static gboolean muxer_sink_query_cb(GstPad *pad, GstObject *parent, GstQuery *qu NTSTATUS wg_muxer_create(void *args) { struct wg_muxer_create_params *params = args; - GstElement *first = NULL, *last = NULL; NTSTATUS status = STATUS_UNSUCCESSFUL; GstPadTemplate *template = NULL; - GstCaps *sink_caps = NULL; struct wg_muxer *muxer;
/* Create wg_muxer object. */ @@ -109,12 +334,12 @@ NTSTATUS wg_muxer_create(void *args) goto out;
/* Create sink pad. */ - if (!(sink_caps = gst_caps_from_string(params->format))) + if (!(muxer->my_sink_caps = gst_caps_from_string(params->format))) { GST_ERROR("Failed to get caps from format string: "%s".", params->format); goto out; } - if (!(template = gst_pad_template_new("sink", GST_PAD_SINK, GST_PAD_ALWAYS, sink_caps))) + if (!(template = gst_pad_template_new("sink", GST_PAD_SINK, GST_PAD_ALWAYS, muxer->my_sink_caps))) goto out; muxer->my_sink = gst_pad_new_from_template(template, "wg_muxer_sink"); if (!muxer->my_sink) @@ -122,26 +347,7 @@ NTSTATUS wg_muxer_create(void *args) gst_pad_set_element_private(muxer->my_sink, muxer); gst_pad_set_query_function(muxer->my_sink, muxer_sink_query_cb);
- /* Create gstreamer muxer element. */ - if (!(muxer->muxer = find_element(GST_ELEMENT_FACTORY_TYPE_MUXER | GST_ELEMENT_FACTORY_TYPE_FORMATTER, - NULL, sink_caps))) - goto out; - if (!append_element(muxer->container, muxer->muxer, &first, &last)) - goto out; - - /* Link muxer to sink pad. */ - if (!link_element_to_sink(muxer->muxer, muxer->my_sink)) - goto out; - if (!gst_pad_set_active(muxer->my_sink, 1)) - goto out; - - /* Set to pause state. */ - gst_element_set_state(muxer->container, GST_STATE_PAUSED); - if (!gst_element_get_state(muxer->container, NULL, NULL, -1)) - goto out; - gst_object_unref(template); - gst_caps_unref(sink_caps);
GST_INFO("Created winegstreamer muxer %p.", muxer); params->muxer = (wg_transform_t)(ULONG_PTR)muxer; @@ -153,13 +359,10 @@ out: gst_object_unref(muxer->my_sink); if (template) gst_object_unref(template); - if (sink_caps) - gst_caps_unref(sink_caps); + if (muxer->my_sink_caps) + gst_caps_unref(muxer->my_sink_caps); if (muxer->container) - { - gst_element_set_state(muxer->container, GST_STATE_NULL); gst_object_unref(muxer->container); - } free(muxer);
return status; @@ -178,6 +381,7 @@ NTSTATUS wg_muxer_destroy(void *args) free(stream); } gst_object_unref(muxer->my_sink); + gst_caps_unref(muxer->my_sink_caps); gst_element_set_state(muxer->container, GST_STATE_NULL); gst_object_unref(muxer->container); free(muxer); @@ -234,3 +438,39 @@ out:
return status; } + +NTSTATUS wg_muxer_start(void *args) +{ + struct wg_muxer *muxer = get_muxer(*(wg_muxer_t *)args); + NTSTATUS status = STATUS_UNSUCCESSFUL; + struct wg_muxer_stream *stream; + + GST_DEBUG("muxer %p.", muxer); + + /* Autoplug gstreamer elements */ + if (!muxer_autoplug(muxer)) + return status; + + /* Link muxer element to my_sink */ + if (!gst_bin_add(GST_BIN(muxer->container), muxer->muxer) + || !link_element_to_sink(muxer->muxer, muxer->my_sink) + || !gst_pad_set_active(muxer->my_sink, 1)) + return status; + + /* Link my_src of each stream to muxer element. */ + LIST_FOR_EACH_ENTRY(stream, &muxer->streams, struct wg_muxer_stream, entry) + { + if (!stream_start(stream)) + return status; + } + + /* Set to pause state. */ + if (gst_element_set_state(muxer->container, GST_STATE_PAUSED) == GST_STATE_CHANGE_FAILURE) + return status; + if (gst_element_get_state(muxer->container, NULL, NULL, -1) == GST_STATE_CHANGE_FAILURE) + return status; + + GST_DEBUG("Started muxer %p.", muxer); + + return STATUS_SUCCESS; +} diff --git a/dlls/winegstreamer/wg_parser.c b/dlls/winegstreamer/wg_parser.c index 8fb39bdcb05..9821bab961b 100644 --- a/dlls/winegstreamer/wg_parser.c +++ b/dlls/winegstreamer/wg_parser.c @@ -1946,6 +1946,7 @@ const unixlib_entry_t __wine_unix_call_funcs[] = X(wg_muxer_create), X(wg_muxer_destroy), X(wg_muxer_add_stream), + X(wg_muxer_start), };
C_ASSERT(ARRAYSIZE(__wine_unix_call_funcs) == unix_wg_funcs_count); @@ -2233,6 +2234,7 @@ const unixlib_entry_t __wine_unix_call_wow64_funcs[] = X64(wg_muxer_create), X(wg_muxer_destroy), X64(wg_muxer_add_stream), + X(wg_muxer_start), };
C_ASSERT(ARRAYSIZE(__wine_unix_call_wow64_funcs) == unix_wg_funcs_count);