]> git.tdb.fi Git - xinema.git/blob - source/client.cpp
Send an end message after directory listing as well
[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_channels_changed.connect(sigc::mem_fun(this, &Client::stream_channels_changed));
150         stream.signal_current_audio_channel_changed.connect(sigc::mem_fun(this, &Client::stream_audio_channel_changed));
151         stream.signal_current_spu_channel_changed.connect(sigc::mem_fun(this, &Client::stream_spu_channel_changed));
152
153         MutexLock lock(stream.get_mutex());
154         stream_state_changed(stream.get_state());
155
156         string title = stream.get_title();
157         if(!title.empty())
158                 send_reply("title "+title);
159
160         if(const Time::TimeDelta &dur = stream.get_duration())
161                 stream_duration_changed(dur);
162
163         stream_channels_changed();
164         stream_audio_channel_changed(stream.get_current_audio_channel());
165         stream_spu_channel_changed(stream.get_current_spu_channel());
166 }
167
168 void Client::stream_destroyed()
169 {
170         send_reply("ejected");
171 }
172
173 void Client::stream_state_changed(XineStream::State state)
174 {
175         send_reply(format("state %s", state));
176 }
177
178 void Client::stream_title_changed(const string &title)
179 {
180         send_reply("title "+title);
181 }
182
183 void Client::stream_duration_changed(const Time::TimeDelta &dur)
184 {
185         send_reply(format("duration %.3f", dur/Time::sec));
186 }
187
188 void Client::stream_position_changed(const Time::TimeDelta &pos)
189 {
190         if(abs(pos-last_position)>=Time::sec)
191         {
192                 send_reply(format("position %.3f", pos/Time::sec));
193                 last_position = pos;
194         }
195 }
196
197 void Client::stream_channels_changed()
198 {
199         XineStream &stream = get_stream();
200
201         const vector<string> &audio_channels = stream.get_audio_channels();
202         send_reply(format("audio_count %d", audio_channels.size()));
203         for(unsigned i=0; i<audio_channels.size(); ++i)
204                 send_reply(format("audio %d %s", i, audio_channels[i]));
205
206         const vector<string> &spu_channels = stream.get_spu_channels();
207         send_reply(format("spu_count %d", spu_channels.size()));
208         for(unsigned i=0; i<spu_channels.size(); ++i)
209                 send_reply(format("spu %d %s", i, spu_channels[i]));
210
211         send_reply("channels_end");
212 }
213
214 void Client::stream_audio_channel_changed(int chan)
215 {
216         if(chan==XineStream::OFF)
217                 send_reply("current_audio off");
218         else
219                 send_reply(format("current_audio %d", chan));
220 }
221
222 void Client::stream_spu_channel_changed(int chan)
223 {
224         if(chan==XineStream::OFF)
225                 send_reply("current_spu off");
226         else
227                 send_reply(format("current_spu %d", chan));
228 }