]> git.tdb.fi Git - xinema.git/blob - source/client.cpp
Support changing audio and SPU channels
[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 void Client::process_command(const string &cmd)
78 {
79         string::size_type space = cmd.find(' ');
80         string keyword = cmd.substr(0, space);
81         string args;
82         if(space!=string::npos)
83                 args = cmd.substr(space+1);
84
85         if(keyword=="list_directory")
86                 list_directory(args);
87         else if(keyword=="play_file")
88                 xinema.play_file(args);
89         else if(keyword=="play")
90                 get_stream().play();
91         else if(keyword=="seek")
92                 get_stream().seek(lexical_cast<float>(args)*Time::sec);
93         else if(keyword=="pause")
94                 get_stream().pause();
95         else if(keyword=="stop")
96                 get_stream().stop();
97         else if(keyword=="select_audio")
98                 set_audio_channel(args);
99         else if(keyword=="select_spu")
100                 set_spu_channel(args);
101         else
102                 throw runtime_error("Invalid command");
103 }
104
105 void Client::send_reply(const string &reply)
106 {
107         Msp::MutexLock lock(mutex);
108         try
109         {
110                 socket->write(reply);
111                 socket->put('\n');
112         }
113         catch(const std::exception &)
114         {
115                 stale = true;
116         }
117 }
118
119 void Client::list_directory(const FS::Path &dn)
120 {
121         list<string> files = FS::list_files(dn);
122
123         send_reply("directory "+dn.str());
124         for(list<string>::const_iterator i=files.begin(); i!=files.end(); ++i)
125         {
126                 if(FS::is_dir(dn / *i))
127                         send_reply("subdir "+*i);
128                 else
129                         send_reply("file "+*i);
130         }
131 }
132
133 void Client::set_audio_channel(const string &arg)
134 {
135         XineStream &stream = get_stream();
136         if(arg=="off")
137                 stream.set_audio_off();
138         else
139                 stream.set_audio_channel(lexical_cast<unsigned>(arg));
140 }
141
142 void Client::set_spu_channel(const string &arg)
143 {
144         XineStream &stream = get_stream();
145         if(arg=="off")
146                 stream.set_spu_off();
147         else
148                 stream.set_spu_channel(lexical_cast<unsigned>(arg));
149 }
150
151 void Client::stream_created(XineStream &stream)
152 {
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));
158
159         MutexLock lock(stream.get_mutex());
160         stream_state_changed(stream.get_state());
161
162         string title = stream.get_title();
163         if(!title.empty())
164                 send_reply("title "+title);
165
166         if(const Time::TimeDelta &dur = stream.get_duration())
167                 stream_duration_changed(dur);
168
169         stream_channels_changed();
170 }
171
172 void Client::stream_destroyed()
173 {
174         send_reply("ejected");
175 }
176
177 void Client::stream_state_changed(XineStream::State state)
178 {
179         send_reply(format("state %s", state));
180 }
181
182 void Client::stream_title_changed(const string &title)
183 {
184         send_reply("title "+title);
185 }
186
187 void Client::stream_duration_changed(const Time::TimeDelta &dur)
188 {
189         send_reply(format("duration %.3f", dur/Time::sec));
190 }
191
192 void Client::stream_position_changed(const Time::TimeDelta &pos)
193 {
194         if(abs(pos-last_position)>=Time::sec)
195         {
196                 send_reply(format("position %.3f", pos/Time::sec));
197                 last_position = pos;
198         }
199 }
200
201 void Client::stream_channels_changed()
202 {
203         XineStream &stream = get_stream();
204
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]));
209
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]));
214
215         send_reply("channels_end");
216 }