X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fclient.cpp;h=604779662b5f0fc6944bf07542bd686ce390bdfb;hb=a4982bb4d3fd9908aa01c824ac6202ac8618ac24;hp=dbe6091df0f6232c9bfb642663921d7505471297;hpb=8c7e5bd0d1f966af2b22293a3a0780c419fb9c95;p=xinema.git diff --git a/source/client.cpp b/source/client.cpp index dbe6091..6047796 100644 --- a/source/client.cpp +++ b/source/client.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "client.h" #include "xinema.h" @@ -13,12 +14,28 @@ Client::Client(Xinema &x, Net::StreamSocket *s): { socket->signal_data_available.connect(sigc::mem_fun(this, &Client::data_available)); socket->signal_end_of_file.connect(sigc::mem_fun(this, &Client::end_of_stream)); + + xinema.signal_stream_created.connect(sigc::mem_fun(this, &Client::stream_created)); + xinema.signal_stream_destroyed.connect(sigc::mem_fun(this, &Client::stream_destroyed)); + XineStream *stream = xinema.get_stream(); + if(stream) + stream_created(*stream); } void Client::data_available() { char rbuf[1024]; - unsigned len = socket->read(rbuf, sizeof(rbuf)); + unsigned len; + try + { + len = socket->read(rbuf, sizeof(rbuf)); + } + catch(const std::exception &) + { + stale = true; + return; + } + buffer.append(rbuf, len); string::size_type start = 0; @@ -35,7 +52,6 @@ void Client::data_available() catch(const exception &e) { send_reply(string("error ")+e.what()); - return; } start = newline+1; @@ -49,6 +65,15 @@ void Client::end_of_stream() stale = true; } +XineStream &Client::get_stream() const +{ + XineStream *stream = xinema.get_stream(); + if(stream) + return *stream; + + throw runtime_error("No stream"); +} + void Client::process_command(const string &cmd) { string::size_type space = cmd.find(' '); @@ -56,18 +81,39 @@ void Client::process_command(const string &cmd) string args; if(space!=string::npos) args = cmd.substr(space+1); + if(keyword=="list_directory") list_directory(args); else if(keyword=="play_file") xinema.play_file(args); + else if(keyword=="play") + get_stream().play(); + else if(keyword=="seek") + get_stream().seek(lexical_cast(args)*Time::sec); + else if(keyword=="pause") + get_stream().pause(); + else if(keyword=="stop") + get_stream().stop(); + else if(keyword=="select_audio") + set_audio_channel(args); + else if(keyword=="select_spu") + set_spu_channel(args); else - send_reply("error Invalid command"); + throw runtime_error("Invalid command"); } void Client::send_reply(const string &reply) { - socket->write(reply); - socket->put('\n'); + Msp::MutexLock lock(mutex); + try + { + socket->write(reply); + socket->put('\n'); + } + catch(const std::exception &) + { + stale = true; + } } void Client::list_directory(const FS::Path &dn) @@ -83,3 +129,88 @@ void Client::list_directory(const FS::Path &dn) send_reply("file "+*i); } } + +void Client::set_audio_channel(const string &arg) +{ + XineStream &stream = get_stream(); + if(arg=="off") + stream.set_audio_off(); + else + stream.set_audio_channel(lexical_cast(arg)); +} + +void Client::set_spu_channel(const string &arg) +{ + XineStream &stream = get_stream(); + if(arg=="off") + stream.set_spu_off(); + else + stream.set_spu_channel(lexical_cast(arg)); +} + +void Client::stream_created(XineStream &stream) +{ + stream.signal_state_changed.connect(sigc::mem_fun(this, &Client::stream_state_changed)); + stream.signal_title_changed.connect(sigc::mem_fun(this, &Client::stream_title_changed)); + stream.signal_duration_changed.connect(sigc::mem_fun(this, &Client::stream_duration_changed)); + stream.signal_position_changed.connect(sigc::mem_fun(this, &Client::stream_position_changed)); + stream.signal_channels_changed.connect(sigc::mem_fun(this, &Client::stream_channels_changed)); + + MutexLock lock(stream.get_mutex()); + stream_state_changed(stream.get_state()); + + string title = stream.get_title(); + if(!title.empty()) + send_reply("title "+title); + + if(const Time::TimeDelta &dur = stream.get_duration()) + stream_duration_changed(dur); + + stream_channels_changed(); +} + +void Client::stream_destroyed() +{ + send_reply("ejected"); +} + +void Client::stream_state_changed(XineStream::State state) +{ + send_reply(format("state %s", state)); +} + +void Client::stream_title_changed(const string &title) +{ + send_reply("title "+title); +} + +void Client::stream_duration_changed(const Time::TimeDelta &dur) +{ + send_reply(format("duration %.3f", dur/Time::sec)); +} + +void Client::stream_position_changed(const Time::TimeDelta &pos) +{ + if(abs(pos-last_position)>=Time::sec) + { + send_reply(format("position %.3f", pos/Time::sec)); + last_position = pos; + } +} + +void Client::stream_channels_changed() +{ + XineStream &stream = get_stream(); + + const vector &audio_channels = stream.get_audio_channels(); + send_reply(format("audio_count %d", audio_channels.size())); + for(unsigned i=0; i &spu_channels = stream.get_spu_channels(); + send_reply(format("spu_count %d", spu_channels.size())); + for(unsigned i=0; i