1 /*
2 * camswclient.c - GStreamer softcam client
3 * Copyright (C) 2007 Alessandro Decina
4 *
5 * Authors:
6 * Alessandro Decina <alessandro.d@gmail.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 */
23
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <sys/un.h>
27 #include <unistd.h>
28 #include <stdio.h>
29 #include <string.h>
30
31 #include <gst/gst.h>
32
33 #include "camswclient.h"
34 #include "cam.h"
35
36 #define GST_CAT_DEFAULT cam_debug_cat
37 #define UNIX_PATH_MAX 108
38
39 CamSwClient *
cam_sw_client_new(void)40 cam_sw_client_new (void)
41 {
42 CamSwClient *client = g_new0 (CamSwClient, 1);
43
44 client->state = CAM_SW_CLIENT_STATE_CLOSED;
45
46 return client;
47 }
48
49 static void
reset_state(CamSwClient * client)50 reset_state (CamSwClient * client)
51 {
52 if (client->sock)
53 close (client->sock);
54
55 g_free (client->sock_path);
56 }
57
58 void
cam_sw_client_free(CamSwClient * client)59 cam_sw_client_free (CamSwClient * client)
60 {
61 g_return_if_fail (client != NULL);
62
63 if (client->state != CAM_SW_CLIENT_STATE_CLOSED)
64 GST_WARNING ("client not in CLOSED state when free'd");
65
66 reset_state (client);
67 g_free (client);
68 }
69
70 gboolean
cam_sw_client_open(CamSwClient * client,const char * sock_path)71 cam_sw_client_open (CamSwClient * client, const char *sock_path)
72 {
73 struct sockaddr_un addr;
74 int ret;
75
76 g_return_val_if_fail (client != NULL, FALSE);
77 g_return_val_if_fail (client->state == CAM_SW_CLIENT_STATE_CLOSED, FALSE);
78 g_return_val_if_fail (sock_path != NULL, FALSE);
79
80 /* sun.path needs to end up NULL-terminated */
81 if (strlen (sock_path) >= (sizeof (addr.sun_path) - 1)) {
82 GST_ERROR ("sock_path is too long");
83 return FALSE;
84 }
85
86 addr.sun_family = AF_UNIX;
87 memcpy (addr.sun_path, sock_path, strlen (sock_path) + 1);
88
89 GST_INFO ("connecting to softcam socket: %s", sock_path);
90 if ((client->sock = socket (PF_UNIX, SOCK_STREAM, 0)) < 0) {
91 GST_ERROR ("Failed to create a socket, error: %s", g_strerror (errno));
92 return FALSE;
93 }
94 ret =
95 connect (client->sock, (struct sockaddr *) &addr,
96 sizeof (struct sockaddr_un));
97 if (ret != 0) {
98 GST_ERROR ("error opening softcam socket %s, error: %s",
99 sock_path, g_strerror (errno));
100
101 return FALSE;
102 }
103
104 client->sock_path = g_strdup (sock_path);
105 client->state = CAM_SW_CLIENT_STATE_OPEN;
106
107 return TRUE;
108 }
109
110 void
cam_sw_client_close(CamSwClient * client)111 cam_sw_client_close (CamSwClient * client)
112 {
113 g_return_if_fail (client != NULL);
114 g_return_if_fail (client->state == CAM_SW_CLIENT_STATE_OPEN);
115
116 reset_state (client);
117 client->state = CAM_SW_CLIENT_STATE_CLOSED;
118 }
119
120 static void
send_ca_pmt(CamSwClient * client,GstMpegtsPMT * pmt,guint8 list_management,guint8 cmd_id)121 send_ca_pmt (CamSwClient * client, GstMpegtsPMT * pmt,
122 guint8 list_management, guint8 cmd_id)
123 {
124 guint8 *buffer;
125 guint buffer_size;
126 guint8 *ca_pmt;
127 guint ca_pmt_size;
128 guint length_field_len;
129 guint header_len;
130
131 ca_pmt = cam_build_ca_pmt (pmt, list_management, cmd_id, &ca_pmt_size);
132
133 length_field_len = cam_calc_length_field_size (ca_pmt_size);
134 header_len = 3 + length_field_len;
135 buffer_size = header_len + ca_pmt_size;
136
137 buffer = g_malloc0 (buffer_size);
138 memcpy (buffer + header_len, ca_pmt, ca_pmt_size);
139
140 /* ca_pmt resource_id */
141 buffer[0] = 0x9F;
142 buffer[1] = 0x80;
143 buffer[2] = 0x32;
144
145 cam_write_length_field (&buffer[3], ca_pmt_size);
146
147 if (write (client->sock, buffer, buffer_size) == -1) {
148 GST_WARNING ("write failed when sending PMT with error: %s (%d)",
149 g_strerror (errno), errno);
150 }
151
152 g_free (ca_pmt);
153 g_free (buffer);
154 }
155
156 void
cam_sw_client_set_pmt(CamSwClient * client,GstMpegtsPMT * pmt)157 cam_sw_client_set_pmt (CamSwClient * client, GstMpegtsPMT * pmt)
158 {
159 g_return_if_fail (client != NULL);
160 g_return_if_fail (pmt != NULL);
161
162 return send_ca_pmt (client, pmt, 0x03 /* only */ ,
163 0x01 /* ok_descrambling */ );
164 }
165
166 void
cam_sw_client_update_pmt(CamSwClient * client,GstMpegtsPMT * pmt)167 cam_sw_client_update_pmt (CamSwClient * client, GstMpegtsPMT * pmt)
168 {
169 g_return_if_fail (client != NULL);
170 g_return_if_fail (pmt != NULL);
171
172 return send_ca_pmt (client, pmt, 0x05 /* update */ ,
173 0x01 /* ok_descrambling */ );
174 }
175