23 #if defined(MEDIA_HUB_HAVE_HYBRIS_MEDIA_COMPAT_LAYER)
24 #include <hybris/media/surface_texture_client_hybris.h>
25 #include <hybris/media/media_codec_layer.h>
31 void setup_video_sink_for_buffer_streaming(GstElement* video_sink)
35 IGBPWrapperHybris igbp = decoding_service_get_igraphicbufferproducer();
36 SurfaceTextureClientHybris stc = surface_texture_client_create_by_igbp(igbp);
38 surface_texture_client_set_hardware_rendering (stc, TRUE);
39 g_object_set (G_OBJECT (video_sink),
"surface", static_cast<gpointer>(stc), static_cast<char*>(NULL));
42 #else // MEDIA_HUB_HAVE_HYBRIS_MEDIA_COMPAT_LAYER
45 void setup_video_sink_for_buffer_streaming(GstElement*)
47 throw core::ubuntu::media::Player::Error::OutOfProcessBufferStreamingNotSupported{};
50 #endif // MEDIA_HUB_HAVE_HYBRIS_MEDIA_COMPAT_LAYER
54 bool is_mir_video_sink()
56 return g_strcmp0(::getenv(
"CORE_UBUNTU_MEDIA_SERVICE_VIDEO_SINK_NAME"),
"mirsink") == 0;
70 static const std::string s{
"playbin"};
76 auto thiz =
static_cast<Playbin*
>(user_data);
84 if (user_data ==
nullptr)
87 static_cast<Playbin*
>(user_data)->setup_source(source);
91 : pipeline(gst_element_factory_make(
"playbin", pipeline_name().c_str())),
93 file_type(MEDIA_FILE_TYPE_NONE),
95 on_new_message_connection_async(
96 bus.on_new_message_async.connect(
100 std::placeholders::_1))),
102 previous_position(0),
103 cached_video_dimensions{
106 player_lifetime(media::Player::Lifetime::normal),
107 about_to_finish_handler_id(0),
108 source_setup_handler_id(0)
111 throw std::runtime_error(
"Could not create pipeline for playbin.");
115 setup_pipeline_for_audio_video();
117 about_to_finish_handler_id = g_signal_connect(
120 G_CALLBACK(about_to_finish),
124 source_setup_handler_id = g_signal_connect(
127 G_CALLBACK(source_setup),
134 g_signal_handler_disconnect(pipeline, about_to_finish_handler_id);
135 g_signal_handler_disconnect(pipeline, source_setup_handler_id);
138 gst_object_unref(pipeline);
143 std::cout <<
"Client died, resetting pipeline" << std::endl;
149 if (player_lifetime != media::Player::Lifetime::resumable) {
153 signals.client_disconnected();
158 std::cout << __PRETTY_FUNCTION__ << std::endl;
159 auto ret = gst_element_set_state(pipeline, GST_STATE_NULL);
162 case GST_STATE_CHANGE_FAILURE:
163 std::cout <<
"Failed to reset the pipeline state. Client reconnect may not function properly." << std::endl;
165 case GST_STATE_CHANGE_NO_PREROLL:
166 case GST_STATE_CHANGE_SUCCESS:
167 case GST_STATE_CHANGE_ASYNC:
170 std::cout <<
"Failed to reset the pipeline state. Client reconnect may not function properly." << std::endl;
172 file_type = MEDIA_FILE_TYPE_NONE;
179 case GST_MESSAGE_ERROR:
182 case GST_MESSAGE_WARNING:
185 case GST_MESSAGE_INFO:
188 case GST_MESSAGE_STATE_CHANGED:
191 case GST_MESSAGE_TAG:
194 if (gst_tag_list_get_string(message.
detail.
tag.
tag_list,
"image-orientation", &orientation))
197 signals.on_orientation_changed(orientation_lut(orientation));
198 g_free (orientation);
201 signals.on_tag_available(message.
detail.
tag);
204 case GST_MESSAGE_ASYNC_DONE:
208 signals.on_seeked_to(0);
212 case GST_MESSAGE_EOS:
213 signals.on_end_of_stream();
227 g_object_get (pipeline,
"flags", &flags,
nullptr);
228 flags |= GST_PLAY_FLAG_AUDIO;
229 flags |= GST_PLAY_FLAG_VIDEO;
230 flags &= ~GST_PLAY_FLAG_TEXT;
231 g_object_set (pipeline,
"flags", flags,
nullptr);
233 if (::getenv(
"CORE_UBUNTU_MEDIA_SERVICE_AUDIO_SINK_NAME") !=
nullptr)
235 auto audio_sink = gst_element_factory_make (
236 ::getenv(
"CORE_UBUNTU_MEDIA_SERVICE_AUDIO_SINK_NAME"),
239 std::cout <<
"audio_sink: " << ::getenv(
"CORE_UBUNTU_MEDIA_SERVICE_AUDIO_SINK_NAME") << std::endl;
248 if (::getenv(
"CORE_UBUNTU_MEDIA_SERVICE_VIDEO_SINK_NAME") !=
nullptr)
250 video_sink = gst_element_factory_make (
251 ::getenv(
"CORE_UBUNTU_MEDIA_SERVICE_VIDEO_SINK_NAME"),
254 std::cout <<
"video_sink: " << ::getenv(
"CORE_UBUNTU_MEDIA_SERVICE_VIDEO_SINK_NAME") << std::endl;
266 if (not video_sink)
throw std::logic_error
268 "No video sink configured for the current pipeline"
271 setup_video_sink_for_buffer_streaming(video_sink);
276 g_object_set (pipeline,
"volume", new_volume, NULL);
284 case media::Player::AudioStreamRole::alarm:
287 case media::Player::AudioStreamRole::alert:
290 case media::Player::AudioStreamRole::multimedia:
293 case media::Player::AudioStreamRole::phone:
304 if (g_strcmp0(orientation,
"rotate-0") == 0)
305 return media::Player::Orientation::rotate0;
306 else if (g_strcmp0(orientation,
"rotate-90") == 0)
307 return media::Player::Orientation::rotate90;
308 else if (g_strcmp0(orientation,
"rotate-180") == 0)
309 return media::Player::Orientation::rotate180;
310 else if (g_strcmp0(orientation,
"rotate-270") == 0)
311 return media::Player::Orientation::rotate270;
313 return media::Player::Orientation::rotate0;
319 GstElement *audio_sink = NULL;
320 g_object_get (pipeline,
"audio-sink", &audio_sink, NULL);
322 std::string role_str(
"props,media.role=" + get_audio_role_str(new_audio_role));
323 std::cout <<
"Audio stream role: " << role_str << std::endl;
325 GstStructure *props = gst_structure_from_string (role_str.c_str(), NULL);
326 if (audio_sink !=
nullptr && props !=
nullptr)
327 g_object_set (audio_sink,
"stream-properties", props, NULL);
331 "Warning: couldn't set audio stream role - couldn't get audio_sink from pipeline" <<
335 gst_structure_free (props);
340 player_lifetime = lifetime;
346 gst_element_query_position (pipeline, GST_FORMAT_TIME, &pos);
350 if ((static_cast<uint64_t>(pos) < duration()) && is_seeking && pos == 0)
352 return previous_position;
357 previous_position =
static_cast<uint64_t
>(pos);
360 return static_cast<uint64_t
>(pos);
366 gst_element_query_duration (pipeline, GST_FORMAT_TIME, &dur);
369 return static_cast<uint64_t
>(dur);
373 const std::string& uri,
375 bool do_pipeline_reset)
377 if (do_pipeline_reset)
380 g_object_set(pipeline,
"uri", uri.c_str(), NULL);
381 if (is_video_file(uri))
382 file_type = MEDIA_FILE_TYPE_VIDEO;
383 else if (is_audio_file(uri))
384 file_type = MEDIA_FILE_TYPE_AUDIO;
386 request_headers = headers;
391 if (source == NULL || request_headers.empty())
394 if (request_headers.find(
"Cookie") != request_headers.end()) {
395 if (g_object_class_find_property(G_OBJECT_GET_CLASS(source),
396 "cookies") != NULL) {
397 gchar ** cookies = g_strsplit(request_headers[
"Cookie"].c_str(),
";", 0);
398 g_object_set(source,
"cookies", cookies, NULL);
403 if (request_headers.find(
"User-Agent") != request_headers.end()) {
404 if (g_object_class_find_property(G_OBJECT_GET_CLASS(source),
405 "user-agent") != NULL) {
406 g_object_set(source,
"user-agent", request_headers[
"User-Agent"].c_str(), NULL);
413 gchar* data =
nullptr;
414 g_object_get(pipeline,
"current-uri", &data,
nullptr);
416 std::string result((data ==
nullptr ?
"" : data));
424 static const std::chrono::nanoseconds state_change_timeout
429 std::chrono::milliseconds{5000}
432 auto ret = gst_element_set_state(pipeline, new_state);
434 std::cout << __PRETTY_FUNCTION__ <<
": requested state change." << std::endl;
436 bool result =
false; GstState current, pending;
439 case GST_STATE_CHANGE_FAILURE:
440 result =
false;
break;
441 case GST_STATE_CHANGE_NO_PREROLL:
442 case GST_STATE_CHANGE_SUCCESS:
443 result =
true;
break;
444 case GST_STATE_CHANGE_ASYNC:
445 result = GST_STATE_CHANGE_SUCCESS == gst_element_get_state(
449 state_change_timeout.count());
455 if (result && new_state == GST_STATE_PLAYING)
461 emit_video_dimensions_changed_if_changed(new_dimensions);
462 cached_video_dimensions = new_dimensions;
464 catch (
const std::exception& e)
466 std::cerr <<
"Problem querying video dimensions: " << e.what() << std::endl;
470 std::cerr <<
"Problem querying video dimensions." << std::endl;
473 #ifdef DEBUG_GST_PIPELINE
474 std::cout <<
"Dumping pipeline dot file" << std::endl;
475 GST_DEBUG_BIN_TO_DOT_FILE((GstBin*)pipeline, GST_DEBUG_GRAPH_SHOW_ALL,
"pipeline");
485 return gst_element_seek_simple(
488 (GstSeekFlags)(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT),
494 if (not video_sink || not is_mir_video_sink())
495 throw std::runtime_error
497 "Missing video sink or video sink does not support query of width and height."
501 uint32_t video_width = 0, video_height = 0;
502 g_object_get (video_sink,
"height", &video_height,
nullptr);
503 g_object_get (video_sink,
"width", &video_width,
nullptr);
517 if (new_dimensions != cached_video_dimensions)
518 signals.on_video_dimensions_changed(new_dimensions);
524 return std::string();
526 std::string filename(uri);
527 size_t pos = uri.find(
"file://");
528 if (pos != std::string::npos)
529 filename = uri.substr(pos + 7, std::string::npos);
534 return std::string(
"audio/video/");
537 GError *error =
nullptr;
538 std::unique_ptr<GFile, void(*)(void *)> file(
539 g_file_new_for_path(filename.c_str()), g_object_unref);
540 std::unique_ptr<GFileInfo, void(*)(void *)> info(
542 file.get(), G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE
","
543 G_FILE_ATTRIBUTE_ETAG_VALUE, G_FILE_QUERY_INFO_NONE,
548 std::string error_str(error->message);
551 std::cout <<
"Failed to query the URI for the presence of video content: "
552 << error_str << std::endl;
553 return std::string();
556 std::string content_type(g_file_info_get_attribute_string(
557 info.get(), G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE));
567 if (get_file_content_type(uri).find(
"audio/") == 0)
569 std::cout <<
"Found audio content" << std::endl;
581 if (get_file_content_type(uri).find(
"video/") == 0)
583 std::cout <<
"Found video content" << std::endl;
static std::string get_audio_role_str(core::ubuntu::media::Player::AudioStreamRole audio_role)
bool set_state_and_wait(GstState new_state)
static void source_setup(GstElement *, GstElement *source, gpointer user_data)
void setup_source(GstElement *source)
void set_uri(const std::string &uri, const core::ubuntu::media::Player::HeadersType &headers, bool do_pipeline_reset=true)
core::ubuntu::media::video::Dimensions get_video_dimensions() const
bool seek(const std::chrono::microseconds &ms)
uint64_t duration() const
static const std::string & pipeline_name()
void emit_video_dimensions_changed_if_changed(const core::ubuntu::media::video::Dimensions &new_dimensions)
void set_lifetime(core::ubuntu::media::Player::Lifetime)
struct gstreamer::Playbin::@12 signals
void set_audio_stream_role(core::ubuntu::media::Player::AudioStreamRole new_audio_role)
core::ubuntu::media::Player::Orientation orientation_lut(const gchar *orientation)
gstreamer::Bus & message_bus()
core::Signal< void > about_to_finish
void setup_pipeline_for_audio_video()
union gstreamer::Bus::Message::Detail detail
std::string get_file_content_type(const std::string &uri) const
struct gstreamer::Bus::Message::Detail::Tag tag
boost::flyweight< std::string > source
bool is_audio_file(const std::string &uri) const
bool is_video_file(const std::string &uri) const
struct gstreamer::Bus::Message::Detail::ErrorWarningInfo error_warning_info
void set_volume(double new_volume)
void on_new_message_async(const Bus::Message &message)
void create_video_sink(uint32_t texture_id)
uint64_t position() const
MediaFileType media_file_type() const
struct gstreamer::Bus::Message::Detail::StateChanged state_changed