]> git.tdb.fi Git - xinema.git/blob - source/client.cpp
Implement basic playback controls
[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         XineStream *stream = xinema.get_stream();
20         if(stream)
21                 stream_created(*stream);
22 }
23
24 void Client::data_available()
25 {
26         char rbuf[1024];
27         unsigned len = socket->read(rbuf, sizeof(rbuf));
28         buffer.append(rbuf, len);
29
30         string::size_type start = 0;
31         while(1)
32         {
33                 string::size_type newline = buffer.find('\n', start);
34                 if(newline==string::npos)
35                         break;
36
37                 try
38                 {
39                         process_command(buffer.substr(start, newline-start));
40                 }
41                 catch(const exception &e)
42                 {
43                         send_reply(string("error ")+e.what());
44                 }
45
46                 start = newline+1;
47         }
48
49         buffer.erase(0, start);
50 }
51
52 void Client::end_of_stream()
53 {
54         stale = true;
55 }
56
57 XineStream &Client::get_stream() const
58 {
59         XineStream *stream = xinema.get_stream();
60         if(stream)
61                 return *stream;
62
63         throw runtime_error("No stream");
64 }
65
66 void Client::process_command(const string &cmd)
67 {
68         string::size_type space = cmd.find(' ');
69         string keyword = cmd.substr(0, space);
70         string args;
71         if(space!=string::npos)
72                 args = cmd.substr(space+1);
73
74         if(keyword=="list_directory")
75                 list_directory(args);
76         else if(keyword=="play_file")
77                 xinema.play_file(args);
78         else if(keyword=="play")
79                 get_stream().play();
80         else if(keyword=="seek")
81                 get_stream().seek(lexical_cast<float>(args)*Time::sec);
82         else if(keyword=="pause")
83                 get_stream().pause();
84         else if(keyword=="stop")
85                 get_stream().stop();
86         else
87                 throw runtime_error("Invalid command");
88 }
89
90 void Client::send_reply(const string &reply)
91 {
92         socket->write(reply);
93         socket->put('\n');
94 }
95
96 void Client::list_directory(const FS::Path &dn)
97 {
98         list<string> files = FS::list_files(dn);
99
100         send_reply("directory "+dn.str());
101         for(list<string>::const_iterator i=files.begin(); i!=files.end(); ++i)
102         {
103                 if(FS::is_dir(dn / *i))
104                         send_reply("subdir "+*i);
105                 else
106                         send_reply("file "+*i);
107         }
108 }
109
110 void Client::stream_created(XineStream &stream)
111 {
112         stream.signal_state_changed.connect(sigc::mem_fun(this, &Client::stream_state_changed));
113         stream.signal_title_changed.connect(sigc::mem_fun(this, &Client::stream_title_changed));
114         stream.signal_duration_changed.connect(sigc::mem_fun(this, &Client::stream_duration_changed));
115         stream.signal_position_changed.connect(sigc::mem_fun(this, &Client::stream_position_changed));
116
117         stream_state_changed(stream.get_state());
118
119         string title = stream.get_title();
120         if(!title.empty())
121                 send_reply("title "+title);
122
123         if(const Time::TimeDelta &dur = stream.get_duration())
124                 stream_duration_changed(dur);
125 }
126
127 void Client::stream_state_changed(XineStream::State state)
128 {
129         send_reply(format("state %s", state));
130 }
131
132 void Client::stream_title_changed(const string &title)
133 {
134         send_reply("title "+title);
135 }
136
137 void Client::stream_duration_changed(const Time::TimeDelta &dur)
138 {
139         send_reply(format("duration %.3f", dur/Time::sec));
140 }
141
142 void Client::stream_position_changed(const Time::TimeDelta &pos)
143 {
144         if(abs(pos-last_position)>=Time::sec)
145         {
146                 send_reply(format("position %.3f", pos/Time::sec));
147                 last_position = pos;
148         }
149 }