]> git.tdb.fi Git - xinema.git/blob - source/client.cpp
Also send the current position when a client connects
[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         {
162                 stream_duration_changed(dur);
163                 stream_position_changed(stream.get_position());
164         }
165
166         stream_channels_changed();
167         stream_audio_channel_changed(stream.get_current_audio_channel());
168         stream_spu_channel_changed(stream.get_current_spu_channel());
169 }
170
171 void Client::stream_destroyed()
172 {
173         send_reply("ejected");
174 }
175
176 void Client::stream_state_changed(XineStream::State state)
177 {
178         send_reply(format("state %s", state));
179 }
180
181 void Client::stream_title_changed(const string &title)
182 {
183         send_reply("title "+title);
184 }
185
186 void Client::stream_duration_changed(const Time::TimeDelta &dur)
187 {
188         send_reply(format("duration %.3f", dur/Time::sec));
189 }
190
191 void Client::stream_position_changed(const Time::TimeDelta &pos)
192 {
193         if(abs(pos-last_position)>=Time::sec)
194         {
195                 send_reply(format("position %.3f", pos/Time::sec));
196                 last_position = pos;
197         }
198 }
199
200 void Client::stream_channels_changed()
201 {
202         XineStream &stream = get_stream();
203
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]));
208
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]));
213
214         send_reply("channels_end");
215 }
216
217 void Client::stream_audio_channel_changed(int chan)
218 {
219         if(chan==XineStream::OFF)
220                 send_reply("current_audio off");
221         else
222                 send_reply(format("current_audio %d", chan));
223 }
224
225 void Client::stream_spu_channel_changed(int chan)
226 {
227         if(chan==XineStream::OFF)
228                 send_reply("current_spu off");
229         else
230                 send_reply(format("current_spu %d", chan));
231 }