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 player_lifetime(media::Player::Lifetime::normal),
105 about_to_finish_handler_id(0),
106 source_setup_handler_id(0)
109 throw std::runtime_error(
"Could not create pipeline for playbin.");
113 setup_pipeline_for_audio_video();
115 about_to_finish_handler_id = g_signal_connect(
118 G_CALLBACK(about_to_finish),
122 source_setup_handler_id = g_signal_connect(
125 G_CALLBACK(source_setup),
132 g_signal_handler_disconnect(pipeline, about_to_finish_handler_id);
133 g_signal_handler_disconnect(pipeline, source_setup_handler_id);
136 gst_object_unref(pipeline);
141 std::cout <<
"Client died, resetting pipeline" << std::endl;
147 if (player_lifetime != media::Player::Lifetime::resumable) {
151 signals.client_disconnected();
156 std::cout << __PRETTY_FUNCTION__ << std::endl;
157 auto ret = gst_element_set_state(pipeline, GST_STATE_NULL);
160 case GST_STATE_CHANGE_FAILURE:
161 std::cout <<
"Failed to reset the pipeline state. Client reconnect may not function properly." << std::endl;
163 case GST_STATE_CHANGE_NO_PREROLL:
164 case GST_STATE_CHANGE_SUCCESS:
165 case GST_STATE_CHANGE_ASYNC:
168 std::cout <<
"Failed to reset the pipeline state. Client reconnect may not function properly." << std::endl;
170 file_type = MEDIA_FILE_TYPE_NONE;
177 case GST_MESSAGE_ERROR:
180 case GST_MESSAGE_WARNING:
183 case GST_MESSAGE_INFO:
186 case GST_MESSAGE_STATE_CHANGED:
189 case GST_MESSAGE_TAG:
192 if (gst_tag_list_get_string(message.
detail.
tag.
tag_list,
"image-orientation", &orientation))
195 signals.on_orientation_changed(orientation_lut(orientation));
196 g_free (orientation);
199 signals.on_tag_available(message.
detail.
tag);
202 case GST_MESSAGE_ASYNC_DONE:
206 signals.on_seeked_to(0);
210 case GST_MESSAGE_EOS:
212 signals.on_end_of_stream();
226 g_object_get (pipeline,
"flags", &flags,
nullptr);
227 flags |= GST_PLAY_FLAG_AUDIO;
228 flags |= GST_PLAY_FLAG_VIDEO;
229 flags &= ~GST_PLAY_FLAG_TEXT;
230 g_object_set (pipeline,
"flags", flags,
nullptr);
232 if (::getenv(
"CORE_UBUNTU_MEDIA_SERVICE_AUDIO_SINK_NAME") !=
nullptr)
234 auto audio_sink = gst_element_factory_make (
235 ::getenv(
"CORE_UBUNTU_MEDIA_SERVICE_AUDIO_SINK_NAME"),
238 std::cout <<
"audio_sink: " << ::getenv(
"CORE_UBUNTU_MEDIA_SERVICE_AUDIO_SINK_NAME") << std::endl;
247 if (::getenv(
"CORE_UBUNTU_MEDIA_SERVICE_VIDEO_SINK_NAME") !=
nullptr)
249 video_sink = gst_element_factory_make (
250 ::getenv(
"CORE_UBUNTU_MEDIA_SERVICE_VIDEO_SINK_NAME"),
253 std::cout <<
"video_sink: " << ::getenv(
"CORE_UBUNTU_MEDIA_SERVICE_VIDEO_SINK_NAME") << std::endl;
265 if (not video_sink)
throw std::logic_error
267 "No video sink configured for the current pipeline"
270 setup_video_sink_for_buffer_streaming(video_sink);
275 g_object_set (pipeline,
"volume", new_volume, NULL);
283 case media::Player::AudioStreamRole::alarm:
286 case media::Player::AudioStreamRole::alert:
289 case media::Player::AudioStreamRole::multimedia:
292 case media::Player::AudioStreamRole::phone:
303 if (g_strcmp0(orientation,
"rotate-0") == 0)
304 return media::Player::Orientation::rotate0;
305 else if (g_strcmp0(orientation,
"rotate-90") == 0)
306 return media::Player::Orientation::rotate90;
307 else if (g_strcmp0(orientation,
"rotate-180") == 0)
308 return media::Player::Orientation::rotate180;
309 else if (g_strcmp0(orientation,
"rotate-270") == 0)
310 return media::Player::Orientation::rotate270;
312 return media::Player::Orientation::rotate0;
318 GstElement *audio_sink = NULL;
319 g_object_get (pipeline,
"audio-sink", &audio_sink, NULL);
321 std::string role_str(
"props,media.role=" + get_audio_role_str(new_audio_role));
322 std::cout <<
"Audio stream role: " << role_str << std::endl;
324 GstStructure *props = gst_structure_from_string (role_str.c_str(), NULL);
325 if (audio_sink !=
nullptr && props !=
nullptr)
326 g_object_set (audio_sink,
"stream-properties", props, NULL);
330 "Warning: couldn't set audio stream role - couldn't get audio_sink from pipeline" <<
334 gst_structure_free (props);
339 player_lifetime = lifetime;
345 gst_element_query_position (pipeline, GST_FORMAT_TIME, &pos);
349 if ((static_cast<uint64_t>(pos) < duration()) && is_seeking && pos == 0)
351 return previous_position;
356 previous_position =
static_cast<uint64_t
>(pos);
359 return static_cast<uint64_t
>(pos);
365 gst_element_query_duration (pipeline, GST_FORMAT_TIME, &dur);
368 return static_cast<uint64_t
>(dur);
372 const std::string& uri,
374 bool do_pipeline_reset)
376 if (do_pipeline_reset)
379 g_object_set(pipeline,
"uri", uri.c_str(), NULL);
380 if (is_video_file(uri))
381 file_type = MEDIA_FILE_TYPE_VIDEO;
382 else if (is_audio_file(uri))
383 file_type = MEDIA_FILE_TYPE_AUDIO;
385 request_headers = headers;
390 if (source == NULL || request_headers.empty())
393 if (request_headers.find(
"Cookie") != request_headers.end()) {
394 if (g_object_class_find_property(G_OBJECT_GET_CLASS(source),
395 "cookies") != NULL) {
396 gchar ** cookies = g_strsplit(request_headers[
"Cookie"].c_str(),
";", 0);
397 g_object_set(source,
"cookies", cookies, NULL);
402 if (request_headers.find(
"User-Agent") != request_headers.end()) {
403 if (g_object_class_find_property(G_OBJECT_GET_CLASS(source),
404 "user-agent") != NULL) {
405 g_object_set(source,
"user-agent", request_headers[
"User-Agent"].c_str(), NULL);
412 gchar* data =
nullptr;
413 g_object_get(pipeline,
"current-uri", &data,
nullptr);
415 std::string result((data ==
nullptr ?
"" : data));
423 static const std::chrono::nanoseconds state_change_timeout
428 std::chrono::milliseconds{5000}
431 auto ret = gst_element_set_state(pipeline, new_state);
433 std::cout << __PRETTY_FUNCTION__ <<
": requested state change." << std::endl;
435 bool result =
false; GstState current, pending;
438 case GST_STATE_CHANGE_FAILURE:
439 result =
false;
break;
440 case GST_STATE_CHANGE_NO_PREROLL:
441 case GST_STATE_CHANGE_SUCCESS:
442 result =
true;
break;
443 case GST_STATE_CHANGE_ASYNC:
444 result = GST_STATE_CHANGE_SUCCESS == gst_element_get_state(
448 state_change_timeout.count());
455 if (result && new_state == GST_STATE_PLAYING && !is_eos)
460 signals.on_video_dimensions_changed(get_video_dimensions());
462 catch (
const std::exception& e)
464 std::cerr <<
"Problem querying video dimensions: " << e.what() << std::endl;
468 std::cerr <<
"Problem querying video dimensions." << std::endl;
471 #ifdef DEBUG_GST_PIPELINE
472 std::cout <<
"Dumping pipeline dot file" << std::endl;
473 GST_DEBUG_BIN_TO_DOT_FILE((GstBin*)pipeline, GST_DEBUG_GRAPH_SHOW_ALL,
"pipeline");
483 return gst_element_seek_simple(
486 (GstSeekFlags)(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT),
492 if (not video_sink || not is_mir_video_sink())
493 throw std::runtime_error
495 "Missing video sink or video sink does not support query of width and height."
499 uint32_t video_width = 0, video_height = 0;
500 g_object_get (video_sink,
"height", &video_height,
nullptr);
501 g_object_get (video_sink,
"width", &video_width,
nullptr);
513 return std::string();
515 std::string filename(uri);
516 size_t pos = uri.find(
"file://");
517 if (pos != std::string::npos)
518 filename = uri.substr(pos + 7, std::string::npos);
523 return std::string(
"audio/video/");
526 GError *error =
nullptr;
527 std::unique_ptr<GFile, void(*)(void *)> file(
528 g_file_new_for_path(filename.c_str()), g_object_unref);
529 std::unique_ptr<GFileInfo, void(*)(void *)> info(
531 file.get(), G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE
","
532 G_FILE_ATTRIBUTE_ETAG_VALUE, G_FILE_QUERY_INFO_NONE,
537 std::string error_str(error->message);
540 std::cout <<
"Failed to query the URI for the presence of video content: "
541 << error_str << std::endl;
542 return std::string();
545 std::string content_type(g_file_info_get_attribute_string(
546 info.get(), G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE));
556 if (get_file_content_type(uri).find(
"audio/") == 0)
558 std::cout <<
"Found audio content" << std::endl;
570 if (get_file_content_type(uri).find(
"video/") == 0)
572 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 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