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 void Client::process_command(const string &cmd)
79 string::size_type space = cmd.find(' ');
80 string keyword = cmd.substr(0, space);
82 if(space!=string::npos)
83 args = cmd.substr(space+1);
85 if(keyword=="list_directory")
87 else if(keyword=="play_file")
88 xinema.play_file(args);
89 else if(keyword=="play")
91 else if(keyword=="seek")
92 get_stream().seek(lexical_cast<float>(args)*Time::sec);
93 else if(keyword=="pause")
95 else if(keyword=="stop")
97 else if(keyword=="select_audio")
98 set_audio_channel(args);
99 else if(keyword=="select_spu")
100 set_spu_channel(args);
102 throw runtime_error("Invalid command");
105 void Client::send_reply(const string &reply)
107 Msp::MutexLock lock(mutex);
110 socket->write(reply);
113 catch(const std::exception &)
119 void Client::list_directory(const FS::Path &dn)
121 list<string> files = FS::list_files(dn);
123 send_reply("directory "+dn.str());
124 for(list<string>::const_iterator i=files.begin(); i!=files.end(); ++i)
126 if(FS::is_dir(dn / *i))
127 send_reply("subdir "+*i);
129 send_reply("file "+*i);
133 void Client::set_audio_channel(const string &arg)
135 XineStream &stream = get_stream();
137 stream.set_audio_off();
139 stream.set_audio_channel(lexical_cast<unsigned>(arg));
142 void Client::set_spu_channel(const string &arg)
144 XineStream &stream = get_stream();
146 stream.set_spu_off();
148 stream.set_spu_channel(lexical_cast<unsigned>(arg));
151 void Client::stream_created(XineStream &stream)
153 stream.signal_state_changed.connect(sigc::mem_fun(this, &Client::stream_state_changed));
154 stream.signal_title_changed.connect(sigc::mem_fun(this, &Client::stream_title_changed));
155 stream.signal_duration_changed.connect(sigc::mem_fun(this, &Client::stream_duration_changed));
156 stream.signal_position_changed.connect(sigc::mem_fun(this, &Client::stream_position_changed));
157 stream.signal_channels_changed.connect(sigc::mem_fun(this, &Client::stream_channels_changed));
159 MutexLock lock(stream.get_mutex());
160 stream_state_changed(stream.get_state());
162 string title = stream.get_title();
164 send_reply("title "+title);
166 if(const Time::TimeDelta &dur = stream.get_duration())
167 stream_duration_changed(dur);
169 stream_channels_changed();
172 void Client::stream_destroyed()
174 send_reply("ejected");
177 void Client::stream_state_changed(XineStream::State state)
179 send_reply(format("state %s", state));
182 void Client::stream_title_changed(const string &title)
184 send_reply("title "+title);
187 void Client::stream_duration_changed(const Time::TimeDelta &dur)
189 send_reply(format("duration %.3f", dur/Time::sec));
192 void Client::stream_position_changed(const Time::TimeDelta &pos)
194 if(abs(pos-last_position)>=Time::sec)
196 send_reply(format("position %.3f", pos/Time::sec));
201 void Client::stream_channels_changed()
203 XineStream &stream = get_stream();
205 const vector<string> &audio_channels = stream.get_audio_channels();
206 send_reply(format("audio_count %d", audio_channels.size()));
207 for(unsigned i=0; i<audio_channels.size(); ++i)
208 send_reply(format("audio %d %s", i, audio_channels[i]));
210 const vector<string> &spu_channels = stream.get_spu_channels();
211 send_reply(format("spu_count %d", spu_channels.size()));
212 for(unsigned i=0; i<spu_channels.size(); ++i)
213 send_reply(format("spu %d %s", i, spu_channels[i]));
215 send_reply("channels_end");