2 // Copyright (C) 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #define __HANDLER_H__ 1
31 # include <sys/poll.h>
34 # include <sys/epoll.h>
44 #include "dsodefs.h" //For DSOEXPORT.
47 #include "diskstream.h"
48 #include "sharedlib.h"
49 #include "extension.h"
50 #include "diskstream.h"
57 // _definst_ is the default instance name
61 // The number of disk streams in the array.
62 const size_t STREAMS_BLOCK
= 1000;
68 class Handler
: public gnash::Extension
, gnash::Network
72 /// The Admin command sent by the client
81 /// This enum contains the possible values for streaming video
88 /// This typedef is only used for the io function that must be
89 /// supported by the plugin.
90 typedef size_t (*cygnal_io_write_t
)(std::uint8_t *data
, size_t size
);
91 typedef std::shared_ptr
<cygnal::Buffer
> (*cygnal_io_read_t
)();
94 std::string description
;
97 cygnal_io_read_t read_func
;
98 cygnal_io_write_t write_func
;
99 gnash::Network::protocols_supported_e protocol
;
102 /// This typedef is only used for the init function optionally
103 /// supported by the plugin.
104 typedef std::shared_ptr
<cygnal_init_t
>(*cygnal_io_init_t
)(std::shared_ptr
<gnash::RTMPMsg
> &msg
);
110 /// Send the onSync message to all connected clients
111 bool sync() { return sync(_in_fd
); };
112 bool sync(int in_fd
);
114 // Access the name field
115 void setName(const std::string
&x
) { _name
= x
; };
116 std::string
&getName() { return _name
; }
118 // Check the status of active disk streams, which is one less than
119 // default as the Streams IDs start at 1.
120 int getActiveDiskStreams() { return _diskstreams
.size(); }
121 // int removeDiskStream(std::shared_ptr<DiskStream> x);
123 // Operate on a disk streaming inprogress
124 std::shared_ptr
<gnash::DiskStream
> getDiskStream(int x
) { return _diskstreams
[x
]; }
125 void setDiskStream(int x
, std::shared_ptr
<gnash::DiskStream
> y
) { _diskstreams
[x
] = y
; }
127 /// Add a SharedObject
128 void addSOL(std::shared_ptr
<cygnal::Element
> x
) {
132 /// \method addClient
133 /// Add a client to the list for output messages for a
134 /// resource. This also specifies the protocol handler
135 /// required for data on this file descriptor.
136 size_t addClient(int fd
, gnash::Network::protocols_supported_e proto
);
137 /// \method removeClient
138 /// Remove a client from the list for messages.
139 void removeClient(int fd
);
141 /// Get the vector of file descriptors for this handler.
142 std::vector
<int> &getClients() { return _clients
; };
144 /// Get a client from the list of clients, we have too many
145 /// arrays so using an operator isn't useful.
146 int getClient(int x
) { return _clients
[x
]; };
148 /// \brief Receive a message from the other end of the network connection.
150 /// @param fd The file descriptor to read from
152 /// @return The number of bytes sent
155 gnash::Network::protocols_supported_e
getProtocol(int x
) { return _protocol
[x
]; };
156 void setProtocol(int fd
, gnash::Network::protocols_supported_e x
) { _protocol
[fd
] = x
; };
158 /// \method addRemote
159 /// Add a remote machine to the list for input messages.
160 size_t addRemote(int x
) { _remote
.push_back(x
); return _remote
.size(); };
162 void setPlugin(std::shared_ptr
<Handler::cygnal_init_t
> &init
);
163 void setPlugin(Handler::cygnal_io_read_t read_ptr
, Handler::cygnal_io_write_t write_ptr
);
165 /// Initialize the named module within Cygnal
167 std::shared_ptr
<cygnal_init_t
> initModule(const std::string
& module
);
169 /// \method initialized
170 /// See if any of the cgi-bins has been loaded.
173 /// This method reads raw data from a plugin.
174 std::shared_ptr
<cygnal::Buffer
> readFromPlugin();
176 /// This method writes raw data to a plugin.
177 size_t writeToPlugin(cygnal::Buffer
&buf
) {
178 return writeToPlugin(buf
.begin(), buf
.allocated()); };
179 size_t writeToPlugin(std::uint8_t *data
, size_t size
);
181 // These methods handle control of the file streaming, and are
182 // used by both HTTP and RTMP*
184 /// \fn int createStream()
185 double createStream(double transid
);
186 /// \overload int createStream(const std::string &filespec)
187 /// @param filespec The spec of the file to stream
188 double createStream(double transid
, const std::string
&filespec
);
191 /// Play the specified file as a stream
193 /// \overload int playStream(const std::string &filespec)
194 bool playStream(const std::string
&filespec
);
196 // Publish a live RTMP stream
198 int publishStream(const std::string
&filespec
, pub_stream_e op
);
200 // Seek within the RTMP stream
202 int seekStream(int offset
);
204 // Pause the RTMP stream
205 int pauseStream(double transid
);
207 // Find a stream in the vector or Disk Streams
208 std::shared_ptr
<gnash::DiskStream
> findStream(const std::string
&filespec
);
210 // Pause the RTMP stream
211 int togglePause(double);
213 // Resume the paused RTMP stream
214 double resumeStream(double transid
);
216 // Close the RTMP stream
217 double closeStream(double transid
);
219 // Delete the RTMP stream
220 double deleteStream(double transid
);
222 // This is a site specific identifier of some kind.
223 void setFCSubscribe(const std::string
&x
) { _fcsubscribe
= x
; };
224 std::string
&getFCSubscribe() { return _fcsubscribe
; }
227 // FIXME: This holds the data from the first NetConnection packet,
228 // and shouldn't really be done here, but we're trying not to
229 // break things while refactoring.
230 void setNetConnection(gnash::RTMPMsg
*msg
) { _netconnect
.reset(msg
); };
231 void setNetConnection(std::shared_ptr
<gnash::RTMPMsg
> msg
) { _netconnect
= msg
; };
232 std::shared_ptr
<gnash::RTMPMsg
> getNetConnection() { return _netconnect
;};
236 std::shared_ptr
<HTTPServer
> &getHTTPHandler(int fd
) { return _http
[fd
]; };
237 std::shared_ptr
<RTMPServer
> getRTMPHandler(int fd
) { return _rtmp
[fd
]; };
240 // Parse the first nessages when starting a new message handler,
241 // which is used to determine the name of the resource to
242 // initialize, or load from the cache.
243 cygnal::Buffer
*parseFirstRequest(int fd
, gnash::Network::protocols_supported_e proto
);
245 std::string
&getKey(int x
) { return _keys
[x
]; };
246 void setKey(int fd
, std::string x
) { _keys
[fd
] = x
; };
248 // Dump internal data.
253 /// The name of the path this handler is supporting.
255 /// Each incoming request has one of 4 states the server has
256 /// to handle to send a response.
258 /// This is a counter of how many streams have been allocated
261 /// \var _diskstreams
262 /// This is all the opened disk based files that are currently
263 /// being streamed by the server.
264 std::map
<int, std::shared_ptr
<gnash::DiskStream
> > _diskstreams
;
266 /// this is the map of which protocol is being used by which
268 std::map
<int, gnash::Network::protocols_supported_e
> _protocol
;
270 std::map
<int, std::shared_ptr
<HTTPServer
> > _http
;
271 std::map
<int, std::shared_ptr
<RTMPServer
> > _rtmp
;
274 /// is the array of all clients connected to this server for
275 /// this application. This is where all the output goes.
276 std::vector
<int> _clients
;
278 /// This is network connections to other processes,
279 /// on other computers.
280 std::vector
<int> _remote
;
283 /// These are local process we're responsible for
284 /// starting and stopping.
285 std::shared_ptr
<cygnal::Proc
> _local
;
287 /// is for the dynamically loaded applications
288 std::shared_ptr
<cygnal_init_t
> _plugin
;
290 /// is for disk based files
291 std::vector
<std::shared_ptr
<gnash::DiskStream
> > _files
;
293 /// is for remote SharedObjects
294 std::vector
<std::shared_ptr
<cygnal::Element
> > _sol
;
296 /// is to store the body size of the previous packet for this
297 /// channel. 4 and 1 byte heades don't use the length field,
298 /// they just use the previous vaue for this field.
299 std::map
<int, size_t> _bodysize
;
301 /// The file descriptor of the incoming data for an
305 /// \var _fcssubscribe
306 /// This is a string sometimes sent by the client with what
307 /// appears to be a unique ID number.
308 std::string _fcsubscribe
;
312 /// This store the data from the NetConnection ActionScript
313 /// object we get as the final part of the handshake process
314 /// that is used to set up the connection. This has all the
315 /// file paths and other information needed by the server.
316 std::shared_ptr
<gnash::RTMPMsg
> _netconnect
;
319 std::map
<int, std::string
> _keys
;
323 // Remote Shared Objects. References are an index into this vector.
324 // std::map<std::string, std::shared_ptr<handler_t> > _handlers;
327 } // end of gnash namespace
329 #endif // end of __HANDLER_H__
333 // indent-tabs-mode: t