1 /*
2  * This file is part of Wireless Display Software for Linux OS
3  *
4  * Copyright (C) 2014 Intel Corporation.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19  * 02110-1301 USA
20  */
21 
22 #include "mirac-gst-sink.hpp"
23 #include "mirac-gst-bus-handler.hpp"
24 #include "libwds/public/logging.h"
25 
26 #include <cassert>
27 
_set_udp_caps(GstElement * playbin,GstElement * source,gpointer user_data)28 void _set_udp_caps(GstElement *playbin, GstElement *source, gpointer    user_data)
29 {
30     GstCaps* caps = gst_caps_new_simple ("application/x-rtp",
31         "media", G_TYPE_STRING, "video",
32         "clock-rate", G_TYPE_INT, 1,
33         "encoding-name", G_TYPE_STRING, "MP2T",
34         NULL);
35 
36     g_object_set(source, "caps", caps, NULL);
37     gst_caps_unref(caps);
38 }
39 
MiracGstSink(std::string hostname,int port)40 MiracGstSink::MiracGstSink (std::string hostname, int port) {
41   // todo (shalamov): move out pipeline initialization
42   // from constructor, otherwise we can't check error conditions
43   std::string gst_pipeline;
44 
45   std::string url =  "udp://" + (!hostname.empty() ? hostname  : "::") + (port > 0 ? ":" + std::to_string(port) : ":");
46   gst_pipeline = "playbin uri=" + url;
47 
48   GError *err = NULL;
49   gst_elem = gst_parse_launch(gst_pipeline.c_str(), &err);
50   if (err != NULL) {
51       WDS_ERROR("Cannot initialize gstreamer pipeline: [%s] %s", g_quark_to_string(err->domain), err->message);
52   }
53 
54   if (gst_elem) {
55       GstBus* bus = gst_pipeline_get_bus (GST_PIPELINE (gst_elem));
56       bus_watch_id = gst_bus_add_watch (bus, mirac_gstbus_callback, this);
57       gst_object_unref (bus);
58 
59       g_signal_connect(gst_elem, "source-setup", G_CALLBACK(_set_udp_caps), NULL);
60       gst_element_set_state (gst_elem, GST_STATE_PLAYING);
61   }
62 }
63 
sink_udp_port()64 int MiracGstSink::sink_udp_port() {
65     if (gst_elem == NULL)
66         return 0;
67 
68     GstElement* source = NULL;
69     g_object_get(gst_elem, "source", &source, NULL);
70 
71     if (source == NULL)
72         return 0;
73 
74     gint port = 0;
75     g_object_get(source, "port", &port, NULL);
76     return port;
77 }
78 
Play()79 void MiracGstSink::Play() {
80   assert(gst_elem);
81   if(!IsInState(GST_STATE_PLAYING)) {
82     gst_element_set_state(gst_elem, GST_STATE_PLAYING);
83     IsInState(GST_STATE_PLAYING);
84   }
85 }
86 
Pause()87 void MiracGstSink::Pause() {
88   assert(gst_elem);
89   if(!IsPaused()) {
90     gst_element_set_state(gst_elem, GST_STATE_PAUSED);
91     IsPaused();
92   }
93 }
94 
Teardown()95 void MiracGstSink::Teardown() {
96   assert(gst_elem);
97   if(!IsInState(GST_STATE_READY)) {
98     gst_element_set_state(gst_elem, GST_STATE_READY);
99     IsInState(GST_STATE_READY);
100   }
101 }
102 
IsPaused() const103 bool MiracGstSink::IsPaused() const {
104   return IsInState(GST_STATE_PAUSED);
105 }
106 
IsInState(GstState state) const107 bool MiracGstSink::IsInState(GstState state) const {
108   assert(gst_elem);
109   GstState current_state;
110   gst_element_get_state(gst_elem, &current_state, NULL, GST_CLOCK_TIME_NONE);
111   return current_state == state;
112 }
113 
~MiracGstSink()114 MiracGstSink::~MiracGstSink () {
115   if (gst_elem) {
116     gst_element_set_state (gst_elem, GST_STATE_NULL);
117     g_source_remove (bus_watch_id);
118     gst_object_unref (GST_OBJECT (gst_elem));
119   }
120 }
121