1 #include <msp/fs/dir.h>
2 #include <msp/fs/stat.h>
3 #include <msp/strings/format.h>
10 Client::Client(Xinema &x, Net::StreamSocket *s):
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));
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();
22 stream_created(*stream);
25 void Client::data_available()
31 len = socket->read(rbuf, sizeof(rbuf));
33 catch(const std::exception &)
39 buffer.append(rbuf, len);
41 string::size_type start = 0;
44 string::size_type newline = buffer.find('\n', start);
45 if(newline==string::npos)
50 process_command(buffer.substr(start, newline-start));
52 catch(const exception &e)
54 send_reply(string("error ")+e.what());
60 buffer.erase(0, start);
63 void Client::end_of_stream()
68 XineStream &Client::get_stream() const
70 XineStream *stream = xinema.get_stream();
74 throw runtime_error("No stream");
77 int Client::convert_channel(const string &arg)
80 return XineStream::OFF;
82 return lexical_cast<unsigned>(arg);
85 void Client::process_command(const string &cmd)
87 string::size_type space = cmd.find(' ');
88 string keyword = cmd.substr(0, space);
90 if(space!=string::npos)
91 args = cmd.substr(space+1);
93 if(keyword=="list_directory")
95 else if(keyword=="play_file")
96 xinema.play_file(args);
97 else if(keyword=="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")
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));
110 throw runtime_error("Invalid command");
113 void Client::send_reply(const string &reply)
115 Msp::MutexLock lock(mutex);
118 socket->write(reply);
121 catch(const std::exception &)
127 void Client::list_directory(const FS::Path &dn)
129 list<string> files = FS::list_files(dn);
131 send_reply("directory "+dn.str());
132 for(list<string>::const_iterator i=files.begin(); i!=files.end(); ++i)
134 if(FS::is_dir(dn / *i))
135 send_reply("subdir "+*i);
137 send_reply("file "+*i);
140 send_reply("directory_end");
143 void Client::stream_created(XineStream &stream)
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));
153 MutexLock lock(stream.get_mutex());
154 stream_state_changed(stream.get_state());
156 string title = stream.get_title();
158 send_reply("title "+title);
160 if(const Time::TimeDelta &dur = stream.get_duration())
162 stream_duration_changed(dur);
163 stream_position_changed(stream.get_position());
166 stream_channels_changed();
167 stream_audio_channel_changed(stream.get_current_audio_channel());
168 stream_spu_channel_changed(stream.get_current_spu_channel());
171 void Client::stream_destroyed()
173 send_reply("ejected");
176 void Client::stream_state_changed(XineStream::State state)
178 send_reply(format("state %s", state));
181 void Client::stream_title_changed(const string &title)
183 send_reply("title "+title);
186 void Client::stream_duration_changed(const Time::TimeDelta &dur)
188 send_reply(format("duration %.3f", dur/Time::sec));
191 void Client::stream_position_changed(const Time::TimeDelta &pos)
193 if(abs(pos-last_position)>=Time::sec)
195 send_reply(format("position %.3f", pos/Time::sec));
200 void Client::stream_channels_changed()
202 XineStream &stream = get_stream();
204 const vector<string> &audio_channels = stream.get_audio_channels();
205 send_reply(format("audio_count %d", audio_channels.size()));
206 for(unsigned i=0; i<audio_channels.size(); ++i)
207 send_reply(format("audio %d %s", i, audio_channels[i]));
209 const vector<string> &spu_channels = stream.get_spu_channels();
210 send_reply(format("spu_count %d", spu_channels.size()));
211 for(unsigned i=0; i<spu_channels.size(); ++i)
212 send_reply(format("spu %d %s", i, spu_channels[i]));
214 send_reply("channels_end");
217 void Client::stream_audio_channel_changed(int chan)
219 if(chan==XineStream::OFF)
220 send_reply("current_audio off");
222 send_reply(format("current_audio %d", chan));
225 void Client::stream_spu_channel_changed(int chan)
227 if(chan==XineStream::OFF)
228 send_reply("current_spu off");
230 send_reply(format("current_spu %d", chan));