]> git.tdb.fi Git - xinema.git/blob - remote/source/xinemacontrol.cpp
Display stream size and codecs on the playback page
[xinema.git] / remote / source / xinemacontrol.cpp
1 #include "xinemacontrol.h"
2
3 XinemaControl::XinemaControl():
4         playback_state(STOPPED),
5         duration(0.0f),
6         position(0.0f),
7         framerate(0.0f),
8         current_audio_channel(-1),
9         current_spu_channel(-1)
10 {
11         QObject::connect(&socket, &QAbstractSocket::connected, this, &XinemaControl::connected);
12         QObject::connect(&socket, &QAbstractSocket::disconnected, this, &XinemaControl::disconnected);
13         QObject::connect(&socket, &QIODevice::readyRead, this, &XinemaControl::data_available);
14 }
15
16 void XinemaControl::connect(const QHostAddress &addr)
17 {
18         socket.connectToHost(addr, 34588);
19 }
20
21 bool XinemaControl::is_connected() const
22 {
23         return socket.state()==QAbstractSocket::ConnectedState;
24 }
25
26 void XinemaControl::list_directory(const QString &dir)
27 {
28         if(!is_connected())
29                 return;
30
31         send_request("list_directory "+dir);
32 }
33
34 void XinemaControl::play_file(const QString &fn)
35 {
36         if(!is_connected())
37                 return;
38
39         send_request("play_file "+fn);
40 }
41
42 void XinemaControl::play()
43 {
44         send_request("play");
45 }
46
47 void XinemaControl::seek(float time)
48 {
49         send_request(QString("seek %1").arg(time));
50 }
51
52 void XinemaControl::pause()
53 {
54         send_request("pause");
55 }
56
57 void XinemaControl::stop()
58 {
59         send_request("stop");
60 }
61
62 void XinemaControl::select_audio_channel(int chan)
63 {
64         if(chan<0)
65                 send_request("select_audio off");
66         else
67                 send_request(QString("select_audio %1").arg(chan));
68 }
69
70 void XinemaControl::select_spu_channel(int chan)
71 {
72         if(chan<0)
73                 send_request("select_spu off");
74         else
75                 send_request(QString("select_spu %1").arg(chan));
76 }
77
78 void XinemaControl::send_request(const QString &req)
79 {
80         socket.write(req.toUtf8());
81         socket.write("\n", 1);
82 }
83
84 void XinemaControl::data_available()
85 {
86         while(socket.bytesAvailable())
87         {
88                 char rbuf[1024];
89                 int len = socket.read(rbuf, sizeof(rbuf));
90                 if(len<0)
91                         break;
92
93                 buffer.append(rbuf, len);
94         }
95
96         unsigned start = 0;
97         while(1)
98         {
99                 int newline = buffer.indexOf('\n', start);
100                 if(newline<0)
101                         break;
102
103                 QString reply = QString::fromUtf8(buffer.mid(start, newline-start));
104                 process_reply(reply);
105
106                 start = newline+1;
107         }
108
109         buffer.remove(0, start);
110 }
111
112 void XinemaControl::process_reply(const QString &reply)
113 {
114         int space = reply.indexOf(' ');
115         QString keyword = reply.mid(0, space);
116         QString args;
117         if(space>=0)
118                 args = reply.mid(space+1);
119
120         if(keyword=="directory")
121                 emit directory_started(args);
122         else if(keyword=="subdir")
123                 emit subdirectory_added(args);
124         else if(keyword=="file")
125                 emit file_added(args);
126         else if(keyword=="state")
127         {
128                 if(args=="STOPPED")
129                         playback_state = STOPPED;
130                 else if(args=="PAUSED")
131                         playback_state = PAUSED;
132                 else if(args=="PLAYING")
133                         playback_state = PLAYING;
134                 emit playback_state_changed(playback_state);
135         }
136         else if(keyword=="title")
137         {
138                 title = args;
139                 emit title_changed(title);
140         }
141         else if(keyword=="duration")
142         {
143                 duration = args.toFloat();
144                 emit duration_changed(duration);
145         }
146         else if(keyword=="position")
147         {
148                 position = args.toFloat();
149                 emit position_changed(position);
150         }
151         else if(keyword=="video_size")
152         {
153                 space = args.indexOf(' ');
154                 video_size = QSize(args.mid(0, space).toInt(), args.mid(space+1).toInt());
155                 emit size_changed(video_size);
156         }
157         else if(keyword=="framerate")
158         {
159                 framerate = args.toFloat();
160                 emit framerate_changed(framerate);
161         }
162         else if(keyword=="video_codec")
163         {
164                 video_codec = args;
165                 emit codecs_changed(video_codec, audio_codec);
166         }
167         else if(keyword=="audio_codec")
168         {
169                 audio_codec = args;
170                 emit codecs_changed(video_codec, audio_codec);
171         }
172         else if(keyword=="audio_count")
173                 resize_list(audio_channels, args.toInt());
174         else if(keyword=="audio")
175         {
176                 space = args.indexOf(' ');
177                 audio_channels[args.mid(0, space).toInt()] = args.mid(space+1);
178         }
179         else if(keyword=="spu_count")
180                 resize_list(spu_channels, args.toInt());
181         else if(keyword=="spu")
182         {
183                 space = args.indexOf(' ');
184                 spu_channels[args.mid(0, space).toInt()] = args.mid(space+1);
185         }
186         else if(keyword=="channels_end")
187                 emit channels_changed();
188         else if(keyword=="current_audio")
189         {
190                 current_audio_channel = convert_channel(args);
191                 emit current_audio_channel_changed(current_audio_channel);
192         }
193         else if(keyword=="current_spu")
194         {
195                 current_spu_channel = convert_channel(args);
196                 emit current_spu_channel_changed(current_spu_channel);
197         }
198 }
199
200 int XinemaControl::convert_channel(const QString &arg)
201 {
202         if(arg=="off")
203                 return OFF;
204         else
205                 return arg.toInt();
206 }
207
208 void XinemaControl::resize_list(QStringList &list, int size)
209 {
210         while(list.size()>size)
211                 list.removeLast();
212         while(list.size()<size)
213                 list.append(QString());
214 }