Skip to content

Commit c8a313d

Browse files
pesaderLorbusChris
andcommitted
nd-sink: Add URI helpers and to_uri method
Prep for turning GND into a daemon. Co-authored-by: Christian Glombek <[email protected]>
1 parent 09f6c45 commit c8a313d

16 files changed

+506
-10
lines changed

Diff for: src/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ gnome_nd_sources = [
2424
'nd-dummy-wfd-sink.c',
2525
'nd-dummy-cc-sink.c',
2626
'nd-pulseaudio.c',
27+
'nd-uri-helpers.c',
2728
]
2829

2930
enum_headers = files('nd-sink.h')

Diff for: src/nd-cc-sink.c

+77-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
*/
1919

20-
#include "gnome-network-displays-config.h"
21-
#include "nd-cc-sink.h"
2220
#include "cc/cc-ctrl.h"
2321
#include "cc/cc-common.h"
2422
#include "cc/cc-http-server.h"
23+
#include "gnome-network-displays-config.h"
24+
#include "nd-cc-sink.h"
25+
#include "nd-enum-types.h"
26+
#include "nd-uri-helpers.h"
2527

2628
struct _NdCCSink
2729
{
@@ -69,6 +71,7 @@ const static NdSinkProtocol protocol = ND_SINK_PROTOCOL_CC;
6971
static void nd_cc_sink_sink_iface_init (NdSinkIface *iface);
7072
static NdSink * nd_cc_sink_sink_start_stream (NdSink *sink);
7173
static void nd_cc_sink_sink_stop_stream (NdSink *sink);
74+
static gchar * nd_cc_sink_sink_to_uri (NdSink *sink);
7275

7376
static void nd_cc_sink_sink_stop_stream_int (NdCCSink *self);
7477

@@ -304,6 +307,24 @@ nd_cc_sink_init (NdCCSink *self)
304307
srand (time (NULL));
305308
}
306309

310+
static gchar *
311+
nd_cc_sink_sink_to_uri (NdSink *sink)
312+
{
313+
NdCCSink *self = ND_CC_SINK (sink);
314+
GHashTable *params = g_hash_table_new (g_str_hash, g_str_equal);
315+
316+
/* protocol */
317+
g_hash_table_insert (params, "protocol", (gpointer *) g_strdup_printf ("%d", protocol));
318+
319+
/* remote name */
320+
g_hash_table_insert (params, "name", (gpointer *) g_strdup (self->name));
321+
322+
/* remote address */
323+
g_hash_table_insert (params, "ip", (gpointer *) g_strdup (self->ip));
324+
325+
return nd_uri_helpers_generate_uri (params);
326+
}
327+
307328
/******************************************************************
308329
* NdSink interface implementation
309330
******************************************************************/
@@ -313,6 +334,7 @@ nd_cc_sink_sink_iface_init (NdSinkIface *iface)
313334
{
314335
iface->start_stream = nd_cc_sink_sink_start_stream;
315336
iface->stop_stream = nd_cc_sink_sink_stop_stream;
337+
iface->to_uri = nd_cc_sink_sink_to_uri;
316338
}
317339

318340
static GstElement *
@@ -436,6 +458,59 @@ nd_cc_sink_new (GSocketClient *client,
436458
NULL);
437459
}
438460

461+
/**
462+
* nd_cc_sink_from_uri
463+
* @uri: a URI string
464+
*
465+
* Construct a #NdCCSink using the information encoded in the URI string
466+
*
467+
* Returns: The newly constructed #NdCCSink or #NULL if failed
468+
*/
469+
NdCCSink *
470+
nd_cc_sink_from_uri (gchar *uri)
471+
{
472+
GHashTable *params = nd_uri_helpers_parse_uri (uri);
473+
474+
/* protocol */
475+
const gchar *protocol_in_uri_str = g_hash_table_lookup (params, "protocol");
476+
477+
;
478+
NdSinkProtocol protocol_in_uri = g_ascii_strtoll (protocol_in_uri_str, NULL, 10);
479+
if (protocol != protocol_in_uri)
480+
{
481+
g_warning ("NdCCSink: Attempted to create sink whose protocol (%s) doesn't match the URI (%s)",
482+
g_enum_to_string (ND_TYPE_SINK_PROTOCOL, protocol),
483+
g_enum_to_string (ND_TYPE_SINK_PROTOCOL, protocol_in_uri));
484+
return NULL;
485+
}
486+
487+
/* client */
488+
GSocketClient *client = g_socket_client_new ();
489+
if (!client)
490+
{
491+
g_warning ("NdCCSink: Failed to instantiate GSocketClient");
492+
return NULL;
493+
}
494+
495+
/* remote name */
496+
gchar *name = g_hash_table_lookup (params, "name");
497+
if (!name)
498+
{
499+
g_warning ("NdCCSink: Failed to find remote name in the URI %s", uri);
500+
return NULL;
501+
}
502+
503+
/* remote ip */
504+
gchar *ip = g_hash_table_lookup (params, "ip");
505+
if (!ip)
506+
{
507+
g_warning ("NdCCSink: Failed to find remote IP address in the URI %s", uri);
508+
return NULL;
509+
}
510+
511+
return nd_cc_sink_new (client, name, ip);
512+
}
513+
439514
NdSinkState
440515
nd_cc_sink_get_state (NdCCSink *self)
441516
{

Diff for: src/nd-cc-sink.h

+1
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@ NdCCSink * nd_cc_sink_new (GSocketClient * client,
3232
gchar * ip);
3333

3434
NdSinkState nd_cc_sink_get_state (NdCCSink *sink);
35+
NdCCSink * nd_cc_sink_from_uri (gchar *uri);
3536

3637
G_END_DECLS

Diff for: src/nd-dummy-cc-sink.c

+45-1
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
*/
1919

20+
#include "cc/cc-http-server.h"
2021
#include "gnome-network-displays-config.h"
2122
#include "nd-dummy-cc-sink.h"
22-
#include "cc/cc-http-server.h"
23+
#include "nd-enum-types.h"
24+
#include "nd-uri-helpers.h"
2325

2426
struct _NdDummyCCSink
2527
{
@@ -59,6 +61,7 @@ const static NdSinkProtocol protocol = ND_SINK_PROTOCOL_DUMMY_CC;
5961
static void nd_dummy_cc_sink_sink_iface_init (NdSinkIface *iface);
6062
static NdSink * nd_dummy_cc_sink_sink_start_stream (NdSink *sink);
6163
static void nd_dummy_cc_sink_sink_stop_stream (NdSink *sink);
64+
static gchar * nd_dummy_cc_sink_sink_to_uri (NdSink *sink);
6265

6366
G_DEFINE_TYPE_EXTENDED (NdDummyCCSink, nd_dummy_cc_sink, G_TYPE_OBJECT, 0,
6467
G_IMPLEMENT_INTERFACE (ND_TYPE_SINK,
@@ -165,6 +168,17 @@ nd_dummy_cc_sink_init (NdDummyCCSink *sink)
165168
sink->state = ND_SINK_STATE_DISCONNECTED;
166169
}
167170

171+
static gchar *
172+
nd_dummy_cc_sink_sink_to_uri (NdSink *sink)
173+
{
174+
GHashTable *params = g_hash_table_new (g_str_hash, g_str_equal);
175+
176+
/* protocol */
177+
g_hash_table_insert (params, "protocol", (gpointer *) g_strdup_printf ("%d", protocol));
178+
179+
return nd_uri_helpers_generate_uri (params);
180+
}
181+
168182
/******************************************************************
169183
* NdSink interface implementation
170184
******************************************************************/
@@ -174,6 +188,7 @@ nd_dummy_cc_sink_sink_iface_init (NdSinkIface *iface)
174188
{
175189
iface->start_stream = nd_dummy_cc_sink_sink_start_stream;
176190
iface->stop_stream = nd_dummy_cc_sink_sink_stop_stream;
191+
iface->to_uri = nd_dummy_cc_sink_sink_to_uri;
177192
}
178193

179194
static void
@@ -322,3 +337,32 @@ nd_dummy_cc_sink_new (void)
322337
return g_object_new (ND_TYPE_DUMMY_CC_SINK,
323338
NULL);
324339
}
340+
341+
/**
342+
* nd_dummy_cc_sink_from_uri
343+
* @uri: a URI string
344+
*
345+
* Construct a #NdDummyCCSink using the information encoded in the URI string
346+
*
347+
* Returns: The newly constructed #NdDummyCCSink
348+
*/
349+
NdDummyCCSink *
350+
nd_dummy_cc_sink_from_uri (gchar *uri)
351+
{
352+
GHashTable *params = nd_uri_helpers_parse_uri (uri);
353+
354+
/* protocol */
355+
const gchar *protocol_in_uri_str = g_hash_table_lookup (params, "protocol");
356+
357+
;
358+
NdSinkProtocol protocol_in_uri = g_ascii_strtoll (protocol_in_uri_str, NULL, 10);
359+
if (protocol != protocol_in_uri)
360+
{
361+
g_warning ("NdDummyCCSink: Attempted to create sink whose protocol (%s) doesn't match the URI (%s)",
362+
g_enum_to_string (ND_TYPE_SINK_PROTOCOL, protocol),
363+
g_enum_to_string (ND_TYPE_SINK_PROTOCOL, protocol_in_uri));
364+
return NULL;
365+
}
366+
367+
return nd_dummy_cc_sink_new ();
368+
}

Diff for: src/nd-dummy-cc-sink.h

+1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ G_BEGIN_DECLS
99
G_DECLARE_FINAL_TYPE (NdDummyCCSink, nd_dummy_cc_sink, ND, DUMMY_CC_SINK, GObject)
1010

1111
NdDummyCCSink * nd_dummy_cc_sink_new (void);
12+
NdDummyCCSink * nd_dummy_cc_sink_from_uri (gchar *uri);
1213

1314
G_END_DECLS

Diff for: src/nd-dummy-wfd-sink.c

+47-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19-
#include "wfd/wfd-client.h"
20-
#include "wfd/wfd-server.h"
2119
#include "gnome-network-displays-config.h"
2220
#include "nd-dummy-wfd-sink.h"
21+
#include "nd-enum-types.h"
22+
#include "nd-uri-helpers.h"
23+
#include "wfd/wfd-client.h"
24+
#include "wfd/wfd-media-factory.h"
25+
#include "wfd/wfd-server.h"
2326

2427
struct _NdDummyWFDSink
2528
{
@@ -59,6 +62,7 @@ const static NdSinkProtocol protocol = ND_SINK_PROTOCOL_DUMMY_WFD_P2P;
5962
static void nd_dummy_wfd_sink_sink_iface_init (NdSinkIface *iface);
6063
static NdSink * nd_dummy_wfd_sink_sink_start_stream (NdSink *sink);
6164
static void nd_dummy_wfd_sink_sink_stop_stream (NdSink *sink);
65+
static gchar * nd_dummy_wfd_sink_sink_to_uri (NdSink *sink);
6266

6367
G_DEFINE_TYPE_EXTENDED (NdDummyWFDSink, nd_dummy_wfd_sink, G_TYPE_OBJECT, 0,
6468
G_IMPLEMENT_INTERFACE (ND_TYPE_SINK,
@@ -164,6 +168,17 @@ nd_dummy_wfd_sink_init (NdDummyWFDSink *sink)
164168
sink->state = ND_SINK_STATE_DISCONNECTED;
165169
}
166170

171+
static gchar *
172+
nd_dummy_wfd_sink_sink_to_uri (NdSink *sink)
173+
{
174+
GHashTable *params = g_hash_table_new (g_str_hash, g_str_equal);
175+
176+
/* protocol */
177+
g_hash_table_insert (params, "protocol", (gpointer *) g_strdup_printf ("%d", protocol));
178+
179+
return nd_uri_helpers_generate_uri (params);
180+
}
181+
167182
/******************************************************************
168183
* NdSink interface implementation
169184
******************************************************************/
@@ -173,6 +188,7 @@ nd_dummy_wfd_sink_sink_iface_init (NdSinkIface *iface)
173188
{
174189
iface->start_stream = nd_dummy_wfd_sink_sink_start_stream;
175190
iface->stop_stream = nd_dummy_wfd_sink_sink_stop_stream;
191+
iface->to_uri = nd_dummy_wfd_sink_sink_to_uri;
176192
}
177193

178194
static void
@@ -337,3 +353,32 @@ nd_dummy_wfd_sink_new (void)
337353
return g_object_new (ND_TYPE_DUMMY_WFD_SINK,
338354
NULL);
339355
}
356+
357+
/**
358+
* nd_dummy_wfd_sink_from_uri
359+
* @uri: a URI string
360+
*
361+
* Construct a #NdDummyWFDSink using the information encoded in the URI string
362+
*
363+
* Returns: The newly constructed #NdDummyWFDSink
364+
*/
365+
NdDummyWFDSink *
366+
nd_dummy_wfd_sink_from_uri (gchar *uri)
367+
{
368+
GHashTable *params = nd_uri_helpers_parse_uri (uri);
369+
370+
/* protocol */
371+
const gchar *protocol_in_uri_str = g_hash_table_lookup (params, "protocol");
372+
373+
;
374+
NdSinkProtocol protocol_in_uri = g_ascii_strtoll (protocol_in_uri_str, NULL, 10);
375+
if (protocol != protocol_in_uri)
376+
{
377+
g_warning ("NdDummyWFDSink: Attempted to create sink whose protocol (%s) doesn't match the URI (%s)",
378+
g_enum_to_string (ND_TYPE_SINK_PROTOCOL, protocol),
379+
g_enum_to_string (ND_TYPE_SINK_PROTOCOL, protocol_in_uri));
380+
return NULL;
381+
}
382+
383+
return nd_dummy_wfd_sink_new ();
384+
}

Diff for: src/nd-dummy-wfd-sink.h

+1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ G_BEGIN_DECLS
99
G_DECLARE_FINAL_TYPE (NdDummyWFDSink, nd_dummy_wfd_sink, ND, DUMMY_WFD_SINK, GObject)
1010

1111
NdDummyWFDSink * nd_dummy_wfd_sink_new (void);
12+
NdDummyWFDSink * nd_dummy_wfd_sink_from_uri (gchar *uri);
1213

1314
G_END_DECLS

Diff for: src/nd-meta-sink.c

+13
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "gnome-network-displays-config.h"
2020
#include "nd-meta-sink.h"
21+
#include "nd-sink.h"
2122

2223
struct _NdMetaSink
2324
{
@@ -50,6 +51,7 @@ const static NdSinkProtocol protocol = ND_SINK_PROTOCOL_META;
5051
static void nd_meta_sink_sink_iface_init (NdSinkIface *iface);
5152
static NdSink * nd_meta_sink_sink_start_stream (NdSink *sink);
5253
static void nd_meta_sink_sink_stop_stream (NdSink *sink);
54+
static gchar * nd_meta_sink_sink_to_uri (NdSink *sink);
5355

5456
G_DEFINE_TYPE_EXTENDED (NdMetaSink, nd_meta_sink, G_TYPE_OBJECT, 0,
5557
G_IMPLEMENT_INTERFACE (ND_TYPE_SINK,
@@ -287,6 +289,16 @@ nd_meta_sink_init (NdMetaSink *meta_sink)
287289
meta_sink->sinks = g_ptr_array_new_with_free_func (g_object_unref);
288290
}
289291

292+
static gchar *
293+
nd_meta_sink_sink_to_uri (NdSink *sink)
294+
{
295+
NdMetaSink *meta_sink = ND_META_SINK (sink);
296+
297+
g_assert (meta_sink->current_sink);
298+
299+
return nd_sink_to_uri (meta_sink->current_sink);
300+
}
301+
290302
/******************************************************************
291303
* NdSink interface implementation
292304
******************************************************************/
@@ -296,6 +308,7 @@ nd_meta_sink_sink_iface_init (NdSinkIface *iface)
296308
{
297309
iface->start_stream = nd_meta_sink_sink_start_stream;
298310
iface->stop_stream = nd_meta_sink_sink_stop_stream;
311+
iface->to_uri = nd_meta_sink_sink_to_uri;
299312
}
300313

301314
static NdSink *

Diff for: src/nd-sink.c

+16-1
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818

1919
#include <gio/gio.h>
2020
#include <gst/gst.h>
21-
#include "nd-sink.h"
2221
#include "gnome-network-displays-config.h"
22+
#include "nd-sink.h"
23+
#include "nd-enum-types.h"
2324

2425
typedef NdSinkIface NdSinkInterface;
2526
G_DEFINE_INTERFACE (NdSink, nd_sink, G_TYPE_OBJECT);
@@ -141,3 +142,17 @@ nd_sink_stop_stream (NdSink *sink)
141142

142143
iface->stop_stream (sink);
143144
}
145+
146+
/**
147+
* nd_sink_to_uri
148+
* @sink: the #NdSink
149+
*
150+
* Create a URI string for the given sink
151+
*/
152+
gchar *
153+
nd_sink_to_uri (NdSink *sink)
154+
{
155+
NdSinkIface *iface = ND_SINK_GET_IFACE (sink);
156+
157+
return iface->to_uri (sink);
158+
}

Diff for: src/nd-sink.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,14 @@ struct _NdSinkIface
6868
/*< public >*/
6969
NdSink * (* start_stream) (NdSink *sink);
7070
void (* stop_stream) (NdSink *sink);
71+
gchar * (* to_uri) (NdSink *sink);
7172
};
7273

7374
GType nd_sink_get_type (void) G_GNUC_CONST;
7475

7576
NdSink *nd_sink_start_stream (NdSink *sink);
76-
void nd_sink_stop_stream (NdSink *sink);
77+
void nd_sink_stop_stream (NdSink *sink);
78+
gchar *nd_sink_to_uri (NdSink *sink);
7779

7880
G_DEFINE_AUTOPTR_CLEANUP_FUNC (NdSink, g_object_unref)
7981

0 commit comments

Comments
 (0)