]> git.tdb.fi Git - xinema.git/blob - source/client.cpp
Re-display the logo after the stream finishes
[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
98                 throw runtime_error("Invalid command");
99 }
100
101 void Client::send_reply(const string &reply)
102 {
103         Msp::MutexLock lock(mutex);
104         try
105         {
106                 socket->write(reply);
107                 socket->put('\n');
108         }
109         catch(const std::exception &)
110         {
111                 stale = true;
112         }
113 }
114
115 void Client::list_directory(const FS::Path &dn)
116 {
117         list<string> files = FS::list_files(dn);
118
119         send_reply("directory "+dn.str());
120         for(list<string>::const_iterator i=files.begin(); i!=files.end(); ++i)
121         {
122                 if(FS::is_dir(dn / *i))
123                         send_reply("subdir "+*i);
124                 else
125                         send_reply("file "+*i);
126         }
127 }
128
129 void Client::stream_created(XineStream &stream)
130 {
131         stream.signal_state_changed.connect(sigc::mem_fun(this, &Client::stream_state_changed));
132         stream.signal_title_changed.connect(sigc::mem_fun(this, &Client::stream_title_changed));
133         stream.signal_duration_changed.connect(sigc::mem_fun(this, &Client::stream_duration_changed));
134         stream.signal_position_changed.connect(sigc::mem_fun(this, &Client::stream_position_changed));
135
136         stream_state_changed(stream.get_state());
137
138         string title = stream.get_title();
139         if(!title.empty())
140                 send_reply("title "+title);
141
142         if(const Time::TimeDelta &dur = stream.get_duration())
143                 stream_duration_changed(dur);
144 }
145
146 void Client::stream_destroyed()
147 {
148         send_reply("ejected");
149 }
150
151 void Client::stream_state_changed(XineStream::State state)
152 {
153         send_reply(format("state %s", state));
154 }
155
156 void Client::stream_title_changed(const string &title)
157 {
158         send_reply("title "+title);
159 }
160
161 void Client::stream_duration_changed(const Time::TimeDelta &dur)
162 {
163         send_reply(format("duration %.3f", dur/Time::sec));
164 }
165
166 void Client::stream_position_changed(const Time::TimeDelta &pos)
167 {
168         if(abs(pos-last_position)>=Time::sec)
169         {
170                 send_reply(format("position %.3f", pos/Time::sec));
171                 last_position = pos;
172         }
173 }