chiark / gitweb /
plugins/tracklength-gstreamer.c: Rewrite to use `GstDiscoverer'.
[disorder] / disobedience / rtp.c
CommitLineData
a99c4e9a
RK
1/*
2 * This file is part of Disobedience
9efa107e 3 * Copyright (C) 2007-2010 Richard Kettlewell
a99c4e9a 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
a99c4e9a 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
a99c4e9a
RK
8 * (at your option) any later version.
9 *
e7eb3a27
RK
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
a99c4e9a 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
a99c4e9a
RK
17 */
18/** @file disobedience/rtp.c
19 * @brief RTP player support for Disobedience
20 */
21
22#include "disobedience.h"
9fbe0996 23#include <sys/types.h>
a99c4e9a
RK
24#include <sys/stat.h>
25#include <unistd.h>
26#include <fcntl.h>
27#include <sys/un.h>
c764e004 28#include <sys/utsname.h>
9fbe0996 29#include <sys/wait.h>
a99c4e9a
RK
30
31/** @brief Path to RTP player's control socket */
32static char *rtp_socket;
33
34/** @brief Path to RTP player's logfile */
35static char *rtp_log;
36
37/** @brief Initialize @ref rtp_socket and @ref rtp_log if necessary */
38static void rtp_init(void) {
39 if(!rtp_socket) {
40 const char *home = getenv("HOME");
c764e004
RK
41 char *dir, *base;
42 struct utsname uts;
a99c4e9a 43
c764e004 44 byte_xasprintf(&dir, "%s/.disorder/", home);
a99c4e9a 45 mkdir(dir, 02700);
c764e004
RK
46 if(uname(&uts) < 0)
47 byte_xasprintf(&base, "%s/", dir);
48 else
49 byte_xasprintf(&base, "%s/%s-", dir, uts.nodename);
50 byte_xasprintf(&rtp_socket, "%srtp", base);
51 byte_xasprintf(&rtp_log, "%srtp.log", base);
a99c4e9a
RK
52 }
53}
54
55/** @brief Return a connection to the RTP player's control socket */
56static FILE *rtp_connect(void) {
57 struct sockaddr_un un;
58 int fd;
59 FILE *fp;
60
61 rtp_init();
62 memset(&un, 0, sizeof un);
63 un.sun_family = AF_UNIX;
64 strcpy(un.sun_path, rtp_socket);
65 fd = xsocket(PF_UNIX, SOCK_STREAM, 0);
66 if(connect(fd, (const struct sockaddr *)&un, sizeof un) < 0) {
67 /* Connection refused just means that the player quit without deleting its
68 * socket */
69 switch(errno) {
70 case ECONNREFUSED:
71 case ENOENT:
72 break;
73 default:
74 /* Anything else may be a problem */
75 fpopup_msg(GTK_MESSAGE_ERROR, "connecting to %s: %s",
76 rtp_socket, strerror(errno));
77 break;
78 }
79 close(fd);
80 return 0;
81 }
82 if(!(fp = fdopen(fd, "r+"))) {
83 fpopup_msg(GTK_MESSAGE_ERROR, "error calling fdopen: %s", strerror(errno));
84 close(fd);
85 return 0;
86 }
87 return fp;
88}
89
90/** @brief Return non-0 iff the RTP player is running */
91int rtp_running(void) {
92 FILE *fp = rtp_connect();
93
94 if(!fp)
95 return 0; /* no connection -> not running */
96 fclose(fp); /* connection -> running */
97 return 1;
98}
99
100/** @brief Activate the RTP player if it is not running */
101void start_rtp(void) {
102 pid_t pid;
103 int w, fd;
104
105 if(rtp_running())
106 return; /* already running */
107 /* double-fork so we don't have to wait() later */
108 if(!(pid = xfork())) {
109 if(setsid() < 0)
2e9ba080 110 disorder_fatal(errno, "error calling setsid");
9efa107e 111 if(!xfork()) {
a99c4e9a
RK
112 /* grandchild */
113 exitfn = _exit;
114 /* log errors and output somewhere reasonably sane. rtp_running()
115 * will have made sure the directory exists. */
116 if((fd = open(rtp_log, O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0)
2e9ba080 117 disorder_fatal(errno, "creating %s", rtp_log);
a99c4e9a
RK
118 if(dup2(fd, 1) < 0
119 || dup2(fd, 2) < 0)
2e9ba080 120 disorder_fatal(errno, "dup2");
a99c4e9a 121 if(close(fd) < 0)
2e9ba080 122 disorder_fatal(errno, "close");
a99c4e9a
RK
123 /* We don't want to hang onto whatever stdin was */
124 if((fd = open("/dev/null", O_RDONLY)) < 0)
2e9ba080 125 disorder_fatal(errno, "opening /dev/null");
a99c4e9a 126 if(dup2(fd, 0) < 0)
2e9ba080 127 disorder_fatal(errno, "dup2");
a99c4e9a 128 if(close(fd) < 0)
2e9ba080 129 disorder_fatal(errno, "close");
a99c4e9a
RK
130 /* execute the player */
131 execlp("disorder-playrtp",
a2364abb
RK
132 "disorder-playrtp",
133 "--socket", rtp_socket,
134 "--api", rtp_api,
135 (char *)0);
2e9ba080 136 disorder_fatal(errno, "disorder-playrtp");
a99c4e9a
RK
137 } else {
138 /* child */
139 _exit(0);
140 }
141 } else {
142 /* parent */
143 while(waitpid(pid, &w, 0) < 0 && errno == EINTR)
144 ;
145 }
146}
147
148/** @brief Stop the RTP player if it is running */
149void stop_rtp(void) {
150 FILE *fp = rtp_connect();
151
152 if(!fp)
153 return; /* already stopped */
154 fprintf(fp, "stop\n");
155 fclose(fp);
156}
157
a2364abb
RK
158static char *rtp_config_file(void) {
159 static char *rtp_config;
160 const char *home = getenv("HOME");
161 if(!rtp_config)
162 byte_xasprintf(&rtp_config, "%s/.disorder/api", home);
163 return rtp_config;
164}
165
166const char *rtp_api;
167
168void load_rtp_config(void) {
169 char *rtp_config = rtp_config_file();
170 FILE *fp;
171 if((fp = fopen(rtp_config, "r"))) {
172 char *line;
173 if(inputline(rtp_config, fp, &line, '\n') == 0) {
174 for(int n = 0; uaudio_apis[n]; ++n)
175 if(!strcmp(uaudio_apis[n]->name, line))
176 rtp_api = line;
177 }
178 fclose(fp);
179 }
180 if(!rtp_api)
181 rtp_api = uaudio_default(uaudio_apis, UAUDIO_API_CLIENT)->name;
182}
183
184void save_rtp_config(void) {
185 if(rtp_api) {
186 char *rtp_config = rtp_config_file();
187 char *tmp;
188 byte_xasprintf(&tmp, "%s.tmp", rtp_config);
189 FILE *fp;
190 if(!(fp = fopen(tmp, "w"))){
191 fpopup_msg(GTK_MESSAGE_ERROR, "error opening %s: %s",
192 tmp, strerror(errno));
193 return;
194 }
195 if(fprintf(fp, "%s\n", rtp_api) < 0) {
196 fpopup_msg(GTK_MESSAGE_ERROR, "error writing to %s: %s",
197 tmp, strerror(errno));
198 fclose(fp);
199 return;
200 }
201 if(fclose(fp) < 0) {
202 fpopup_msg(GTK_MESSAGE_ERROR, "error closing %s: %s",
203 tmp, strerror(errno));
204 return;
205 }
206 if(rename(tmp, rtp_config) < 0) {
207 fpopup_msg(GTK_MESSAGE_ERROR, "error renaming %s: %s",
208 tmp, strerror(errno));
209 }
210 }
211}
212
213void change_rtp_api(const char *api) {
214 if(rtp_api && !strcmp(api, rtp_api))
215 return; /* no change */
216 int running = rtp_running();
217 if(running)
218 stop_rtp();
219 rtp_api = api;
220 save_rtp_config();
221 // TODO this is racy and does not work; the player doesn't shut down quickly
222 // enough.
223 if(running)
224 start_rtp();
225}
226
a99c4e9a
RK
227/*
228Local Variables:
229c-basic-offset:2
230comment-column:40
231fill-column:79
232indent-tabs-mode:nil
233End:
234*/