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>
29 void setup_video_sink_for_buffer_streaming(GstElement* video_sink)
33 IGBPWrapperHybris igbp = decoding_service_get_igraphicbufferproducer();
34 SurfaceTextureClientHybris stc = surface_texture_client_create_by_igbp(igbp);
36 surface_texture_client_set_hardware_rendering (stc, TRUE);
37 g_object_set (G_OBJECT (video_sink),
"surface", static_cast<gpointer>(stc), static_cast<char*>(NULL));
40 #else // MEDIA_HUB_HAVE_HYBRIS_MEDIA_COMPAT_LAYER
43 void setup_video_sink_for_buffer_streaming(GstElement*)
45 throw core::ubuntu::media::Player::Error::OutOfProcessBufferStreamingNotSupported{};
48 #endif // MEDIA_HUB_HAVE_HYBRIS_MEDIA_COMPAT_LAYER
52 bool is_mir_video_sink()
54 return g_strcmp0(::getenv(
"CORE_UBUNTU_MEDIA_SERVICE_VIDEO_SINK_NAME"),
"mirsink") == 0;
68 static const std::string s{
"playbin"};
74 auto thiz =
static_cast<Playbin*
>(user_data);
82 if (user_data ==
nullptr)
85 static_cast<Playbin*
>(user_data)->setup_source(source);
89 : pipeline(gst_element_factory_make(
"playbin", pipeline_name().c_str())),
91 file_type(MEDIA_FILE_TYPE_NONE),
93 on_new_message_connection(
94 bus.on_new_message.connect(
98 std::placeholders::_1))),
100 previous_position(0),
101 player_lifetime(media::Player::Lifetime::normal),
105 throw std::runtime_error(
"Could not create pipeline for playbin.");
111 setup_pipeline_for_audio_video();
116 G_CALLBACK(about_to_finish),
123 G_CALLBACK(source_setup),
131 gst_object_unref(pipeline);
136 std::cout <<
"Client died, resetting pipeline" << std::endl;
142 if (player_lifetime != media::Player::Lifetime::resumable) {
146 signals.client_disconnected();
151 std::cout << __PRETTY_FUNCTION__ << std::endl;
152 auto ret = gst_element_set_state(pipeline, GST_STATE_NULL);
155 case GST_STATE_CHANGE_FAILURE:
156 std::cout <<
"Failed to reset the pipeline state. Client reconnect may not function properly." << std::endl;
158 case GST_STATE_CHANGE_NO_PREROLL:
159 case GST_STATE_CHANGE_SUCCESS:
160 case GST_STATE_CHANGE_ASYNC:
163 std::cout <<
"Failed to reset the pipeline state. Client reconnect may not function properly." << std::endl;
165 file_type = MEDIA_FILE_TYPE_NONE;
172 case GST_MESSAGE_ERROR:
175 case GST_MESSAGE_WARNING:
178 case GST_MESSAGE_INFO:
181 case GST_MESSAGE_TAG:
184 if (gst_tag_list_get_string(message.
detail.
tag.
tag_list,
"image-orientation", &orientation))
187 signals.on_orientation_changed(orientation_lut(orientation));
188 g_free (orientation);
191 signals.on_tag_available(message.
detail.
tag);
194 case GST_MESSAGE_STATE_CHANGED:
197 case GST_MESSAGE_ASYNC_DONE:
201 signals.on_seeked_to(0);
205 case GST_MESSAGE_EOS:
207 signals.on_end_of_stream();
221 g_object_get (pipeline,
"flags", &flags,
nullptr);
222 flags |= GST_PLAY_FLAG_AUDIO;
223 flags |= GST_PLAY_FLAG_VIDEO;
224 flags &= ~GST_PLAY_FLAG_TEXT;
225 g_object_set (pipeline,
"flags", flags,
nullptr);
227 if (::getenv(
"CORE_UBUNTU_MEDIA_SERVICE_AUDIO_SINK_NAME") !=
nullptr)
229 auto audio_sink = gst_element_factory_make (
230 ::getenv(
"CORE_UBUNTU_MEDIA_SERVICE_AUDIO_SINK_NAME"),
233 std::cout <<
"audio_sink: " << ::getenv(
"CORE_UBUNTU_MEDIA_SERVICE_AUDIO_SINK_NAME") << std::endl;
242 if (::getenv(
"CORE_UBUNTU_MEDIA_SERVICE_VIDEO_SINK_NAME") !=
nullptr)
244 video_sink = gst_element_factory_make (
245 ::getenv(
"CORE_UBUNTU_MEDIA_SERVICE_VIDEO_SINK_NAME"),
248 std::cout <<
"video_sink: " << ::getenv(
"CORE_UBUNTU_MEDIA_SERVICE_VIDEO_SINK_NAME") << std::endl;
260 if (not video_sink)
throw std::logic_error
262 "No video sink configured for the current pipeline"
265 setup_video_sink_for_buffer_streaming(video_sink);
270 g_object_set (pipeline,
"volume", new_volume, NULL);
278 case media::Player::AudioStreamRole::alarm:
281 case media::Player::AudioStreamRole::alert:
284 case media::Player::AudioStreamRole::multimedia:
287 case media::Player::AudioStreamRole::phone:
298 if (g_strcmp0(orientation,
"rotate-0") == 0)
299 return media::Player::Orientation::rotate0;
300 else if (g_strcmp0(orientation,
"rotate-90") == 0)
301 return media::Player::Orientation::rotate90;
302 else if (g_strcmp0(orientation,
"rotate-180") == 0)
303 return media::Player::Orientation::rotate180;
304 else if (g_strcmp0(orientation,
"rotate-270") == 0)
305 return media::Player::Orientation::rotate270;
307 return media::Player::Orientation::rotate0;
313 GstElement *audio_sink = NULL;
314 g_object_get (pipeline,
"audio-sink", &audio_sink, NULL);
316 std::string role_str(
"props,media.role=" + get_audio_role_str(new_audio_role));
317 std::cout <<
"Audio stream role: " << role_str << std::endl;
319 GstStructure *props = gst_structure_from_string (role_str.c_str(), NULL);
320 if (audio_sink !=
nullptr && props !=
nullptr)
321 g_object_set (audio_sink,
"stream-properties", props, NULL);
325 "Warning: couldn't set audio stream role - couldn't get audio_sink from pipeline" <<
329 gst_structure_free (props);
334 player_lifetime = lifetime;
340 gst_element_query_position (pipeline, GST_FORMAT_TIME, &pos);
344 if ((static_cast<uint64_t>(pos) < duration()) && is_seeking && pos == 0)
346 return previous_position;
351 previous_position =
static_cast<uint64_t
>(pos);
354 return static_cast<uint64_t
>(pos);
360 gst_element_query_duration (pipeline, GST_FORMAT_TIME, &dur);
363 return static_cast<uint64_t
>(dur);
367 const std::string& uri,
372 g_object_set(pipeline,
"uri", uri.c_str(), NULL);
373 if (is_video_file(uri))
374 file_type = MEDIA_FILE_TYPE_VIDEO;
375 else if (is_audio_file(uri))
376 file_type = MEDIA_FILE_TYPE_AUDIO;
378 request_headers = headers;
383 if (source == NULL || request_headers.empty())
386 if (request_headers.find(
"Cookie") != request_headers.end()) {
387 if (g_object_class_find_property(G_OBJECT_GET_CLASS(source),
388 "cookies") != NULL) {
389 gchar ** cookies = g_strsplit(request_headers[
"Cookie"].c_str(),
";", 0);
390 g_object_set(source,
"cookies", cookies, NULL);
395 if (request_headers.find(
"User-Agent") != request_headers.end()) {
396 if (g_object_class_find_property(G_OBJECT_GET_CLASS(source),
397 "user-agent") != NULL) {
398 g_object_set(source,
"user-agent", request_headers[
"User-Agent"].c_str(), NULL);
405 gchar* data =
nullptr;
406 g_object_get(pipeline,
"current-uri", &data,
nullptr);
408 std::string result((data ==
nullptr ?
"" : data));
416 static const std::chrono::nanoseconds state_change_timeout
421 std::chrono::milliseconds{5000}
424 auto ret = gst_element_set_state(pipeline, new_state);
426 std::cout << __PRETTY_FUNCTION__ <<
": requested state change." << std::endl;
428 bool result =
false; GstState current, pending;
431 case GST_STATE_CHANGE_FAILURE:
432 result =
false;
break;
433 case GST_STATE_CHANGE_NO_PREROLL:
434 case GST_STATE_CHANGE_SUCCESS:
435 result =
true;
break;
436 case GST_STATE_CHANGE_ASYNC:
437 result = GST_STATE_CHANGE_SUCCESS == gst_element_get_state(
441 state_change_timeout.count());
448 if (result && new_state == GST_STATE_PLAYING && !is_eos)
453 signals.on_video_dimensions_changed(get_video_dimensions());
455 catch (
const std::exception& e)
457 std::cerr <<
"Problem querying video dimensions: " << e.what() << std::endl;
461 std::cerr <<
"Problem querying video dimensions." << std::endl;
464 #ifdef DEBUG_GST_PIPELINE
465 std::cout <<
"Dumping pipeline dot file" << std::endl;
466 GST_DEBUG_BIN_TO_DOT_FILE((GstBin*)pipeline, GST_DEBUG_GRAPH_SHOW_ALL,
"pipeline");
476 return gst_element_seek_simple(
479 (GstSeekFlags)(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT),
485 if (not video_sink || not is_mir_video_sink())
486 throw std::runtime_error
488 "Missing video sink or video sink does not support query of width and height."
492 uint32_t video_width = 0, video_height = 0;
493 g_object_get (video_sink,
"height", &video_height,
nullptr);
494 g_object_get (video_sink,
"width", &video_width,
nullptr);
506 return std::string();
508 std::string filename(uri);
509 std::cout <<
"filename: " << filename << std::endl;
510 size_t pos = uri.find(
"file://");
511 if (pos != std::string::npos)
512 filename = uri.substr(pos + 7, std::string::npos);
517 return std::string(
"audio/video/");
520 GError *error =
nullptr;
521 std::unique_ptr<GFile, void(*)(void *)> file(
522 g_file_new_for_path(filename.c_str()), g_object_unref);
523 std::unique_ptr<GFileInfo, void(*)(void *)> info(
525 file.get(), G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE
","
526 G_FILE_ATTRIBUTE_ETAG_VALUE, G_FILE_QUERY_INFO_NONE,
531 std::string error_str(error->message);
534 std::cout <<
"Failed to query the URI for the presence of video content: "
535 << error_str << std::endl;
536 return std::string();
539 std::string content_type(g_file_info_get_attribute_string(
540 info.get(), G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE));
550 if (get_file_content_type(uri).find(
"audio/") == 0)
552 std::cout <<
"Found audio content" << std::endl;
564 if (get_file_content_type(uri).find(
"video/") == 0)
566 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)
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()
void set_uri(const std::string &uri, const core::ubuntu::media::Player::HeadersType &headers)
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
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