2 * camswclient.c - GStreamer softcam client
3 * Copyright (C) 2007 Alessandro Decina
6 * Alessandro Decina <alessandro@nnva.org>
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.
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.
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., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
24 #include <sys/types.h>
25 #include <sys/socket.h>
33 #include "camswclient.h"
37 #define GST_CAT_DEFAULT cam_debug_cat
38 #define UNIX_PATH_MAX 108
41 cam_sw_client_new (void)
43 CamSwClient *client = g_new0 (CamSwClient, 1);
45 client->state = CAM_SW_CLIENT_STATE_CLOSED;
51 reset_state (CamSwClient * client)
56 if (client->sock_path)
57 g_free (client->sock_path);
61 cam_sw_client_free (CamSwClient * client)
63 g_return_if_fail (client != NULL);
65 if (client->state != CAM_SW_CLIENT_STATE_CLOSED)
66 GST_WARNING ("client not in CLOSED state when free'd");
73 cam_sw_client_open (CamSwClient * client, const char *sock_path)
75 struct sockaddr_un addr;
78 g_return_val_if_fail (client != NULL, FALSE);
79 g_return_val_if_fail (client->state == CAM_SW_CLIENT_STATE_CLOSED, FALSE);
80 g_return_val_if_fail (sock_path != NULL, FALSE);
82 addr.sun_family = AF_UNIX;
83 strncpy (addr.sun_path, sock_path, sizeof (addr.sun_path));
85 GST_INFO ("connecting to softcam socket: %s", sock_path);
86 client->sock = socket (PF_UNIX, SOCK_STREAM, 0);
88 connect (client->sock, (struct sockaddr *) &addr,
89 sizeof (struct sockaddr_un));
91 GST_ERROR ("error opening softcam socket %s, error: %s",
92 sock_path, strerror (errno));
97 client->sock_path = g_strdup (sock_path);
98 client->state = CAM_SW_CLIENT_STATE_OPEN;
104 cam_sw_client_close (CamSwClient * client)
106 g_return_if_fail (client != NULL);
107 g_return_if_fail (client->state == CAM_SW_CLIENT_STATE_OPEN);
109 reset_state (client);
110 client->state = CAM_SW_CLIENT_STATE_CLOSED;
114 send_ca_pmt (CamSwClient * client, GstStructure * pmt,
115 guint8 list_management, guint8 cmd_id)
121 guint length_field_len;
124 ca_pmt = cam_build_ca_pmt (pmt, list_management, cmd_id, &ca_pmt_size);
126 length_field_len = cam_calc_length_field_size (ca_pmt_size);
127 header_len = 3 + length_field_len;
128 buffer_size = header_len + ca_pmt_size;
130 buffer = g_malloc0 (buffer_size);
131 memcpy (buffer + header_len, ca_pmt, ca_pmt_size);
133 /* ca_pmt resource_id */
138 cam_write_length_field (&buffer[3], ca_pmt_size);
140 if (write (client->sock, buffer, buffer_size) == -1) {
141 GST_WARNING ("write failed when sending pmt with errno: %d", errno);
149 cam_sw_client_set_pmt (CamSwClient * client, GstStructure * pmt)
151 g_return_if_fail (client != NULL);
152 g_return_if_fail (pmt != NULL);
154 return send_ca_pmt (client, pmt, 0x03 /* only */ ,
155 0x01 /* ok_descrambling */ );
159 cam_sw_client_update_pmt (CamSwClient * client, GstStructure * pmt)
161 g_return_if_fail (client != NULL);
162 g_return_if_fail (pmt != NULL);
164 return send_ca_pmt (client, pmt, 0x05 /* update */ ,
165 0x01 /* ok_descrambling */ );