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