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* pipeline)
35 IGBPWrapperHybris igbp = decoding_service_get_igraphicbufferproducer();
36 SurfaceTextureClientHybris stc = surface_texture_client_create_by_igbp(igbp);
39 surface_texture_client_set_hardware_rendering(stc, TRUE);
41 GstContext *context = gst_context_new(
"gst.mir.MirContext", TRUE);
42 GstStructure *structure = gst_context_writable_structure(context);
43 gst_structure_set(structure,
"gst_mir_context", G_TYPE_POINTER, stc, NULL);
46 gst_element_set_context(pipeline, context);
49 #else // MEDIA_HUB_HAVE_HYBRIS_MEDIA_COMPAT_LAYER
52 void setup_video_sink_for_buffer_streaming(GstElement*)
57 #endif // MEDIA_HUB_HAVE_HYBRIS_MEDIA_COMPAT_LAYER
61 bool is_mir_video_sink()
63 return g_strcmp0(::getenv(
"CORE_UBUNTU_MEDIA_SERVICE_VIDEO_SINK_NAME"),
"mirsink") == 0;
77 static const std::string s{
"playbin"};
83 auto thiz =
static_cast<Playbin*
>(user_data);
91 if (user_data ==
nullptr)
94 static_cast<Playbin*
>(user_data)->setup_source(source);
98 : pipeline(gst_element_factory_make(
"playbin", pipeline_name().c_str())),
100 file_type(MEDIA_FILE_TYPE_NONE),
102 on_new_message_connection_async(
103 bus.on_new_message_async.connect(
107 std::placeholders::_1))),
109 previous_position(0),
110 cached_video_dimensions{
113 player_lifetime(media::Player::Lifetime::normal),
114 about_to_finish_handler_id(0),
115 source_setup_handler_id(0)
118 throw std::runtime_error(
"Could not create pipeline for playbin.");
122 setup_pipeline_for_audio_video();
124 about_to_finish_handler_id = g_signal_connect(
127 G_CALLBACK(about_to_finish),
131 source_setup_handler_id = g_signal_connect(
134 G_CALLBACK(source_setup),
141 g_signal_handler_disconnect(pipeline, about_to_finish_handler_id);
142 g_signal_handler_disconnect(pipeline, source_setup_handler_id);
145 gst_object_unref(pipeline);
150 std::cout <<
"Client died, resetting pipeline" << std::endl;
156 if (player_lifetime != media::Player::Lifetime::resumable) {
160 signals.client_disconnected();
165 std::cout << __PRETTY_FUNCTION__ << std::endl;
166 auto ret = gst_element_set_state(pipeline, GST_STATE_NULL);
169 case GST_STATE_CHANGE_FAILURE:
170 std::cout <<
"Failed to reset the pipeline state. Client reconnect may not function properly." << std::endl;
172 case GST_STATE_CHANGE_NO_PREROLL:
173 case GST_STATE_CHANGE_SUCCESS:
174 case GST_STATE_CHANGE_ASYNC:
177 std::cout <<
"Failed to reset the pipeline state. Client reconnect may not function properly." << std::endl;
179 file_type = MEDIA_FILE_TYPE_NONE;
186 case GST_MESSAGE_ERROR:
189 case GST_MESSAGE_WARNING:
192 case GST_MESSAGE_INFO:
195 case GST_MESSAGE_STATE_CHANGED:
198 case GST_MESSAGE_TAG:
201 if (gst_tag_list_get_string(message.
detail.
tag.
tag_list,
"image-orientation", &orientation))
204 signals.on_orientation_changed(orientation_lut(orientation));
205 g_free (orientation);
208 signals.on_tag_available(message.
detail.
tag);
211 case GST_MESSAGE_ASYNC_DONE:
215 signals.on_seeked_to(0);
219 case GST_MESSAGE_EOS:
220 signals.on_end_of_stream();
234 g_object_get (pipeline,
"flags", &flags,
nullptr);
235 flags |= GST_PLAY_FLAG_AUDIO;
236 flags |= GST_PLAY_FLAG_VIDEO;
237 flags &= ~GST_PLAY_FLAG_TEXT;
238 g_object_set (pipeline,
"flags", flags,
nullptr);
240 if (::getenv(
"CORE_UBUNTU_MEDIA_SERVICE_AUDIO_SINK_NAME") !=
nullptr)
242 auto audio_sink = gst_element_factory_make (
243 ::getenv(
"CORE_UBUNTU_MEDIA_SERVICE_AUDIO_SINK_NAME"),
246 std::cout <<
"audio_sink: " << ::getenv(
"CORE_UBUNTU_MEDIA_SERVICE_AUDIO_SINK_NAME") << std::endl;
255 if (::getenv(
"CORE_UBUNTU_MEDIA_SERVICE_VIDEO_SINK_NAME") !=
nullptr)
257 video_sink = gst_element_factory_make (
258 ::getenv(
"CORE_UBUNTU_MEDIA_SERVICE_VIDEO_SINK_NAME"),
261 std::cout <<
"video_sink: " << ::getenv(
"CORE_UBUNTU_MEDIA_SERVICE_VIDEO_SINK_NAME") << std::endl;
273 if (not video_sink)
throw std::logic_error
275 "No video sink configured for the current pipeline"
278 setup_video_sink_for_buffer_streaming(pipeline);
283 g_object_set (pipeline,
"volume", new_volume, NULL);
291 case media::Player::AudioStreamRole::alarm:
294 case media::Player::AudioStreamRole::alert:
297 case media::Player::AudioStreamRole::multimedia:
300 case media::Player::AudioStreamRole::phone:
311 if (g_strcmp0(orientation,
"rotate-0") == 0)
312 return media::Player::Orientation::rotate0;
313 else if (g_strcmp0(orientation,
"rotate-90") == 0)
314 return media::Player::Orientation::rotate90;
315 else if (g_strcmp0(orientation,
"rotate-180") == 0)
316 return media::Player::Orientation::rotate180;
317 else if (g_strcmp0(orientation,
"rotate-270") == 0)
318 return media::Player::Orientation::rotate270;
320 return media::Player::Orientation::rotate0;
326 GstElement *audio_sink = NULL;
327 g_object_get (pipeline,
"audio-sink", &audio_sink, NULL);
329 std::string role_str(
"props,media.role=" + get_audio_role_str(new_audio_role));
330 std::cout <<
"Audio stream role: " << role_str << std::endl;
332 GstStructure *props = gst_structure_from_string (role_str.c_str(), NULL);
333 if (audio_sink !=
nullptr && props !=
nullptr)
334 g_object_set (audio_sink,
"stream-properties", props, NULL);
338 "Warning: couldn't set audio stream role - couldn't get audio_sink from pipeline" <<
342 gst_structure_free (props);
347 player_lifetime = lifetime;
353 gst_element_query_position (pipeline, GST_FORMAT_TIME, &pos);
357 if ((static_cast<uint64_t>(pos) < duration()) && is_seeking && pos == 0)
359 return previous_position;
364 previous_position =
static_cast<uint64_t
>(pos);
367 return static_cast<uint64_t
>(pos);
373 gst_element_query_duration (pipeline, GST_FORMAT_TIME, &dur);
376 return static_cast<uint64_t
>(dur);
380 const std::string& uri,
382 bool do_pipeline_reset)
384 if (do_pipeline_reset)
387 g_object_set(pipeline,
"uri", uri.c_str(), NULL);
388 if (is_video_file(uri))
389 file_type = MEDIA_FILE_TYPE_VIDEO;
390 else if (is_audio_file(uri))
391 file_type = MEDIA_FILE_TYPE_AUDIO;
393 request_headers = headers;
398 if (source == NULL || request_headers.empty())
401 if (request_headers.find(
"Cookie") != request_headers.end()) {
402 if (g_object_class_find_property(G_OBJECT_GET_CLASS(source),
403 "cookies") != NULL) {
404 gchar ** cookies = g_strsplit(request_headers[
"Cookie"].c_str(),
";", 0);
405 g_object_set(source,
"cookies", cookies, NULL);
410 if (request_headers.find(
"User-Agent") != request_headers.end()) {
411 if (g_object_class_find_property(G_OBJECT_GET_CLASS(source),
412 "user-agent") != NULL) {
413 g_object_set(source,
"user-agent", request_headers[
"User-Agent"].c_str(), NULL);
420 gchar* data =
nullptr;
421 g_object_get(pipeline,
"current-uri", &data,
nullptr);
423 std::string result((data ==
nullptr ?
"" : data));
431 static const std::chrono::nanoseconds state_change_timeout
436 std::chrono::milliseconds{5000}
439 auto ret = gst_element_set_state(pipeline, new_state);
441 std::cout << __PRETTY_FUNCTION__ <<
": requested state change." << std::endl;
443 bool result =
false; GstState current, pending;
446 case GST_STATE_CHANGE_FAILURE:
447 result =
false;
break;
448 case GST_STATE_CHANGE_NO_PREROLL:
449 case GST_STATE_CHANGE_SUCCESS:
450 result =
true;
break;
451 case GST_STATE_CHANGE_ASYNC:
452 result = GST_STATE_CHANGE_SUCCESS == gst_element_get_state(
456 state_change_timeout.count());
462 if (result && new_state == GST_STATE_PLAYING)
468 emit_video_dimensions_changed_if_changed(new_dimensions);
469 cached_video_dimensions = new_dimensions;
471 catch (
const std::exception& e)
473 std::cerr <<
"Problem querying video dimensions: " << e.what() << std::endl;
477 std::cerr <<
"Problem querying video dimensions." << std::endl;
480 #ifdef DEBUG_GST_PIPELINE
481 std::cout <<
"Dumping pipeline dot file" << std::endl;
482 GST_DEBUG_BIN_TO_DOT_FILE((GstBin*)pipeline, GST_DEBUG_GRAPH_SHOW_ALL,
"pipeline");
492 return gst_element_seek_simple(
495 (GstSeekFlags)(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT),
501 if (not video_sink || not is_mir_video_sink())
502 throw std::runtime_error
504 "Missing video sink or video sink does not support query of width and height."
508 uint32_t video_width = 0, video_height = 0;
509 g_object_get (video_sink,
"height", &video_height,
nullptr);
510 g_object_get (video_sink,
"width", &video_width,
nullptr);
524 if (new_dimensions != cached_video_dimensions)
525 signals.on_video_dimensions_changed(new_dimensions);
531 return std::string();
533 std::string filename(uri);
534 size_t pos = uri.find(
"file://");
535 if (pos != std::string::npos)
536 filename = uri.substr(pos + 7, std::string::npos);
541 return std::string(
"audio/video/");
544 GError *error =
nullptr;
545 std::unique_ptr<GFile, void(*)(void *)> file(
546 g_file_new_for_path(filename.c_str()), g_object_unref);
547 std::unique_ptr<GFileInfo, void(*)(void *)> info(
549 file.get(), G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE
","
550 G_FILE_ATTRIBUTE_ETAG_VALUE, G_FILE_QUERY_INFO_NONE,
555 std::string error_str(error->message);
558 std::cout <<
"Failed to query the URI for the presence of video content: "
559 << error_str << std::endl;
560 return std::string();
563 std::string content_type(g_file_info_get_attribute_string(
564 info.get(), G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE));
574 if (get_file_content_type(uri).find(
"audio/") == 0)
576 std::cout <<
"Found audio content" << std::endl;
588 if (get_file_content_type(uri).find(
"video/") == 0)
590 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