]> git.tdb.fi Git - xinema.git/blob - source/client.cpp
Create an icon for the remote
[xinema.git] / source / client.cpp
1 #include <msp/fs/dir.h>
2 #include <msp/fs/stat.h>
3 #include <msp/strings/format.h>
4 #include "client.h"
5 #include "xinema.h"
6
7 using namespace std;
8 using namespace Msp;
9
10 Client::Client(Xinema &x, Net::StreamSocket *s):
11         xinema(x),
12         socket(s),
13         stale(false)
14 {
15         socket->signal_data_available.connect(sigc::mem_fun(this, &Client::data_available));
16         socket->signal_end_of_file.connect(sigc::mem_fun(this, &Client::end_of_stream));
17
18         xinema.signal_stream_created.connect(sigc::mem_fun(this, &Client::stream_created));
19         xinema.signal_stream_destroyed.connect(sigc::mem_fun(this, &Client::stream_destroyed));
20         XineStream *stream = xinema.get_stream();
21         if(stream)
22                 stream_created(*stream);
23 }
24
25 void Client::data_available()
26 {
27         char rbuf[1024];
28         unsigned len;
29         try
30         {
31                 len = socket->read(rbuf, sizeof(rbuf));
32         }
33         catch(const std::exception &)
34         {
35                 stale = true;
36                 return;
37         }
38
39         buffer.append(rbuf, len);
40
41         string::size_type start = 0;
42         while(1)
43         {
44                 string::size_type newline = buffer.find('\n', start);
45                 if(newline==string::npos)
46                         break;
47
48                 try
49                 {
50                         process_command(buffer.substr(start, newline-start));
51                 }
52                 catch(const exception &e)
53                 {
54                         send_reply(string("error ")+e.what());
55                 }
56
57                 start = newline+1;
58         }
59
60         buffer.erase(0, start);
61 }
62
63 void Client::end_of_stream()
64 {
65         stale = true;
66 }
67
68 XineStream &Client::get_stream() const
69 {
70         XineStream *stream = xinema.get_stream();
71         if(stream)
72                 return *stream;
73
74         throw runtime_error("No stream");
75 }
76
77 int Client::convert_channel(const string &arg)
78 {
79         if(arg=="off")
80                 return XineStream::OFF;
81         else
82                 return lexical_cast<unsigned>(arg);
83 }
84
85 void Client::process_command(const string &cmd)
86 {
87         string::size_type space = cmd.find(' ');
88         string keyword = cmd.substr(0, space);
89         string args;
90         if(space!=string::npos)
91                 args = cmd.substr(space+1);
92
93         if(keyword=="list_directory")
94                 list_directory(args);
95         else if(keyword=="play_file")
96                 xinema.play_file(args);
97         else if(keyword=="play")
98                 get_stream().play();
99         else if(keyword=="seek")
100                 get_stream().seek(lexical_cast<float>(args)*Time::sec);
101         else if(keyword=="pause")
102                 get_stream().pause();
103         else if(keyword=="stop")
104                 get_stream().stop();
105         else if(keyword=="select_audio")
106                 get_stream().select_audio_channel(convert_channel(args));
107         else if(keyword=="select_spu")
108                 get_stream().select_spu_channel(convert_channel(args));
109         else
110                 throw runtime_error("Invalid command");
111 }
112
113 void Client::send_reply(const string &reply)
114 {
115         Msp::MutexLock lock(mutex);
116         try
117         {
118                 socket->write(reply);
119                 socket->put('\n');
120         }
121         catch(const std::exception &)
122         {
123                 stale = true;
124         }
125 }
126
127 void Client::list_directory(const FS::Path &dn)
128 {
129         list<string> files = FS::list_files(dn);
130
131         send_reply("directory "+dn.str());
132         for(list<string>::const_iterator i=files.begin(); i!=files.end(); ++i)
133         {
134                 if(FS::is_dir(dn / *i))
135                         send_reply("subdir "+*i);
136                 else
137                         send_reply("file "+*i);
138         }
139
140         send_reply("directory_end");
141 }
142
143 void Client::stream_created(XineStream &stream)
144 {
145         stream.signal_state_changed.connect(sigc::mem_fun(this, &Client::stream_state_changed));
146         stream.signal_title_changed.connect(sigc::mem_fun(this, &Client::stream_title_changed));
147         stream.signal_duration_changed.connect(sigc::mem_fun(this, &Client::stream_duration_changed));
148         stream.signal_position_changed.connect(sigc::mem_fun(this, &Client::stream_position_changed));
149         stream.signal_video_size_changed.connect(sigc::mem_fun(this, &Client::stream_size_changed));
150         stream.signal_framerate_changed.connect(sigc::mem_fun(this, &Client::stream_framerate_changed));
151         stream.signal_codecs_changed.connect(sigc::mem_fun(this, &Client::stream_codecs_changed));
152         stream.signal_channels_changed.connect(sigc::mem_fun(this, &Client::stream_channels_changed));
153         stream.signal_current_audio_channel_changed.connect(sigc::mem_fun(this, &Client::stream_audio_channel_changed));
154         stream.signal_current_spu_channel_changed.connect(sigc::mem_fun(this, &Client::stream_spu_channel_changed));
155
156         MutexLock lock(stream.get_mutex());
157         stream_state_changed(stream.get_state());
158
159         string title = stream.get_title();
160         if(!title.empty())
161                 send_reply("title "+title);
162
163         if(const Time::TimeDelta &dur = stream.get_duration())
164         {
165                 stream_duration_changed(dur);
166                 stream_position_changed(stream.get_position());
167         }
168
169         stream_size_changed(stream.get_video_width(), stream.get_video_height());
170         stream_framerate_changed(stream.get_framerate());
171         stream_codecs_changed(stream.get_video_codec(), stream.get_audio_codec());
172
173         stream_channels_changed();
174         stream_audio_channel_changed(stream.get_current_audio_channel());
175         stream_spu_channel_changed(stream.get_current_spu_channel());
176 }
177
178 void Client::stream_destroyed()
179 {
180         send_reply("ejected");
181 }
182
183 void Client::stream_state_changed(XineStream::State state)
184 {
185         send_reply(format("state %s", state));
186 }
187
188 void Client::stream_title_changed(const string &title)
189 {
190         send_reply("title "+title);
191 }
192
193 void Client::stream_duration_changed(const Time::TimeDelta &dur)
194 {
195         send_reply(format("duration %.3f", dur/Time::sec));
196 }
197
198 void Client::stream_position_changed(const Time::TimeDelta &pos)
199 {
200         if(abs(pos-last_position)>=Time::sec)
201         {
202                 send_reply(format("position %.3f", pos/Time::sec));
203                 last_position = pos;
204         }
205 }
206
207 void Client::stream_size_changed(unsigned w, unsigned h)
208 {
209         send_reply(format("video_size %d %d", w, h));
210 }
211
212 void Client::stream_framerate_changed(float fps)
213 {
214         send_reply(format("framerate %.2f", fps));
215 }
216
217 void Client::stream_codecs_changed(const string &vc, const string &ac)
218 {
219         send_reply("video_codec "+vc);
220         send_reply("audio_codec "+ac);
221 }
222
223 void Client::stream_channels_changed()
224 {
225         XineStream &stream = get_stream();
226
227         const vector<string> &audio_channels = stream.get_audio_channels();
228         send_reply(format("audio_count %d", audio_channels.size()));
229         for(unsigned i=0; i<audio_channels.size(); ++i)
230                 send_reply(format("audio %d %s", i, audio_channels[i]));
231
232         const vector<string> &spu_channels = stream.get_spu_channels();
233         send_reply(format("spu_count %d", spu_channels.size()));
234         for(unsigned i=0; i<spu_channels.size(); ++i)
235                 send_reply(format("spu %d %s", i, spu_channels[i]));
236
237         send_reply("channels_end");
238 }
239
240 void Client::stream_audio_channel_changed(int chan)
241 {
242         if(chan==XineStream::OFF)
243                 send_reply("current_audio off");
244         else
245                 send_reply(format("current_audio %d", chan));
246 }
247
248 void Client::stream_spu_channel_changed(int chan)
249 {
250         if(chan==XineStream::OFF)
251                 send_reply("current_spu off");
252         else
253                 send_reply(format("current_spu %d", chan));
254 }