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(
96 bus.on_new_message.connect(
100 std::placeholders::_1))),
102 previous_position(0),
103 player_lifetime(media::Player::Lifetime::normal),
107 throw std::runtime_error(
"Could not create pipeline for playbin.");
113 setup_pipeline_for_audio_video();
118 G_CALLBACK(about_to_finish),
125 G_CALLBACK(source_setup),
133 gst_object_unref(pipeline);
138 std::cout <<
"Client died, resetting pipeline" << std::endl;
144 if (player_lifetime != media::Player::Lifetime::resumable) {
148 signals.client_disconnected();
153 std::cout << __PRETTY_FUNCTION__ << std::endl;
154 auto ret = gst_element_set_state(pipeline, GST_STATE_NULL);
157 case GST_STATE_CHANGE_FAILURE:
158 std::cout <<
"Failed to reset the pipeline state. Client reconnect may not function properly." << std::endl;
160 case GST_STATE_CHANGE_NO_PREROLL:
161 case GST_STATE_CHANGE_SUCCESS:
162 case GST_STATE_CHANGE_ASYNC:
165 std::cout <<
"Failed to reset the pipeline state. Client reconnect may not function properly." << std::endl;
167 file_type = MEDIA_FILE_TYPE_NONE;
174 case GST_MESSAGE_ERROR:
177 case GST_MESSAGE_WARNING:
180 case GST_MESSAGE_INFO:
183 case GST_MESSAGE_TAG:
186 if (gst_tag_list_get_string(message.
detail.
tag.
tag_list,
"image-orientation", &orientation))
189 signals.on_orientation_changed(orientation_lut(orientation));
190 g_free (orientation);
193 signals.on_tag_available(message.
detail.
tag);
196 case GST_MESSAGE_STATE_CHANGED:
199 case GST_MESSAGE_ASYNC_DONE:
203 signals.on_seeked_to(0);
207 case GST_MESSAGE_EOS:
209 signals.on_end_of_stream();
223 g_object_get (pipeline,
"flags", &flags,
nullptr);
224 flags |= GST_PLAY_FLAG_AUDIO;
225 flags |= GST_PLAY_FLAG_VIDEO;
226 flags &= ~GST_PLAY_FLAG_TEXT;
227 g_object_set (pipeline,
"flags", flags,
nullptr);
229 if (::getenv(
"CORE_UBUNTU_MEDIA_SERVICE_AUDIO_SINK_NAME") !=
nullptr)
231 auto audio_sink = gst_element_factory_make (
232 ::getenv(
"CORE_UBUNTU_MEDIA_SERVICE_AUDIO_SINK_NAME"),
235 std::cout <<
"audio_sink: " << ::getenv(
"CORE_UBUNTU_MEDIA_SERVICE_AUDIO_SINK_NAME") << std::endl;
244 if (::getenv(
"CORE_UBUNTU_MEDIA_SERVICE_VIDEO_SINK_NAME") !=
nullptr)
246 video_sink = gst_element_factory_make (
247 ::getenv(
"CORE_UBUNTU_MEDIA_SERVICE_VIDEO_SINK_NAME"),
250 std::cout <<
"video_sink: " << ::getenv(
"CORE_UBUNTU_MEDIA_SERVICE_VIDEO_SINK_NAME") << std::endl;
262 if (not video_sink)
throw std::logic_error
264 "No video sink configured for the current pipeline"
267 setup_video_sink_for_buffer_streaming(video_sink);
272 g_object_set (pipeline,
"volume", new_volume, NULL);
280 case media::Player::AudioStreamRole::alarm:
283 case media::Player::AudioStreamRole::alert:
286 case media::Player::AudioStreamRole::multimedia:
289 case media::Player::AudioStreamRole::phone:
300 if (g_strcmp0(orientation,
"rotate-0") == 0)
301 return media::Player::Orientation::rotate0;
302 else if (g_strcmp0(orientation,
"rotate-90") == 0)
303 return media::Player::Orientation::rotate90;
304 else if (g_strcmp0(orientation,
"rotate-180") == 0)
305 return media::Player::Orientation::rotate180;
306 else if (g_strcmp0(orientation,
"rotate-270") == 0)
307 return media::Player::Orientation::rotate270;
309 return media::Player::Orientation::rotate0;
315 GstElement *audio_sink = NULL;
316 g_object_get (pipeline,
"audio-sink", &audio_sink, NULL);
318 std::string role_str(
"props,media.role=" + get_audio_role_str(new_audio_role));
319 std::cout <<
"Audio stream role: " << role_str << std::endl;
321 GstStructure *props = gst_structure_from_string (role_str.c_str(), NULL);
322 if (audio_sink !=
nullptr && props !=
nullptr)
323 g_object_set (audio_sink,
"stream-properties", props, NULL);
327 "Warning: couldn't set audio stream role - couldn't get audio_sink from pipeline" <<
331 gst_structure_free (props);
336 player_lifetime = lifetime;
342 gst_element_query_position (pipeline, GST_FORMAT_TIME, &pos);
346 if ((static_cast<uint64_t>(pos) < duration()) && is_seeking && pos == 0)
348 return previous_position;
353 previous_position =
static_cast<uint64_t
>(pos);
356 return static_cast<uint64_t
>(pos);
362 gst_element_query_duration (pipeline, GST_FORMAT_TIME, &dur);
365 return static_cast<uint64_t
>(dur);
369 const std::string& uri,
371 bool do_pipeline_reset)
373 if (do_pipeline_reset)
376 g_object_set(pipeline,
"uri", uri.c_str(), NULL);
377 if (is_video_file(uri))
378 file_type = MEDIA_FILE_TYPE_VIDEO;
379 else if (is_audio_file(uri))
380 file_type = MEDIA_FILE_TYPE_AUDIO;
382 request_headers = headers;
387 if (source == NULL || request_headers.empty())
390 if (request_headers.find(
"Cookie") != request_headers.end()) {
391 if (g_object_class_find_property(G_OBJECT_GET_CLASS(source),
392 "cookies") != NULL) {
393 gchar ** cookies = g_strsplit(request_headers[
"Cookie"].c_str(),
";", 0);
394 g_object_set(source,
"cookies", cookies, NULL);
399 if (request_headers.find(
"User-Agent") != request_headers.end()) {
400 if (g_object_class_find_property(G_OBJECT_GET_CLASS(source),
401 "user-agent") != NULL) {
402 g_object_set(source,
"user-agent", request_headers[
"User-Agent"].c_str(), NULL);
409 gchar* data =
nullptr;
410 g_object_get(pipeline,
"current-uri", &data,
nullptr);
412 std::string result((data ==
nullptr ?
"" : data));
420 static const std::chrono::nanoseconds state_change_timeout
425 std::chrono::milliseconds{5000}
428 auto ret = gst_element_set_state(pipeline, new_state);
430 std::cout << __PRETTY_FUNCTION__ <<
": requested state change." << std::endl;
432 bool result =
false; GstState current, pending;
435 case GST_STATE_CHANGE_FAILURE:
436 result =
false;
break;
437 case GST_STATE_CHANGE_NO_PREROLL:
438 case GST_STATE_CHANGE_SUCCESS:
439 result =
true;
break;
440 case GST_STATE_CHANGE_ASYNC:
441 result = GST_STATE_CHANGE_SUCCESS == gst_element_get_state(
445 state_change_timeout.count());
452 if (result && new_state == GST_STATE_PLAYING && !is_eos)
457 signals.on_video_dimensions_changed(get_video_dimensions());
459 catch (
const std::exception& e)
461 std::cerr <<
"Problem querying video dimensions: " << e.what() << std::endl;
465 std::cerr <<
"Problem querying video dimensions." << std::endl;
468 #ifdef DEBUG_GST_PIPELINE
469 std::cout <<
"Dumping pipeline dot file" << std::endl;
470 GST_DEBUG_BIN_TO_DOT_FILE((GstBin*)pipeline, GST_DEBUG_GRAPH_SHOW_ALL,
"pipeline");
480 return gst_element_seek_simple(
483 (GstSeekFlags)(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT),
489 if (not video_sink || not is_mir_video_sink())
490 throw std::runtime_error
492 "Missing video sink or video sink does not support query of width and height."
496 uint32_t video_width = 0, video_height = 0;
497 g_object_get (video_sink,
"height", &video_height,
nullptr);
498 g_object_get (video_sink,
"width", &video_width,
nullptr);
510 return std::string();
512 std::string filename(uri);
513 size_t pos = uri.find(
"file://");
514 if (pos != std::string::npos)
515 filename = uri.substr(pos + 7, std::string::npos);
520 return std::string(
"audio/video/");
523 GError *error =
nullptr;
524 std::unique_ptr<GFile, void(*)(void *)> file(
525 g_file_new_for_path(filename.c_str()), g_object_unref);
526 std::unique_ptr<GFileInfo, void(*)(void *)> info(
528 file.get(), G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE
","
529 G_FILE_ATTRIBUTE_ETAG_VALUE, G_FILE_QUERY_INFO_NONE,
534 std::string error_str(error->message);
537 std::cout <<
"Failed to query the URI for the presence of video content: "
538 << error_str << std::endl;
539 return std::string();
542 std::string content_type(g_file_info_get_attribute_string(
543 info.get(), G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE));
553 if (get_file_content_type(uri).find(
"audio/") == 0)
555 std::cout <<
"Found audio content" << std::endl;
567 if (get_file_content_type(uri).find(
"video/") == 0)
569 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)
void on_new_message(const Bus::Message &message)
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 create_video_sink(uint32_t texture_id)
uint64_t position() const
MediaFileType media_file_type() const
struct gstreamer::Bus::Message::Detail::StateChanged state_changed