2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010,
3 // 2011 Free Software Foundation, Inc
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #ifndef _RTMP_SERVER_H_
20 #define _RTMP_SERVER_H_
23 #include <boost/cstdint.hpp>
24 #include <boost/shared_ptr.hpp>
25 #include <boost/array.hpp>
34 #include "diskstream.h"
41 // Define this if you want to use numeric client ID. Undefining this
42 // create ASCII based client IDs instead.
43 // #define CLIENT_ID_NUMERIC 1
45 class RTMPServer
: public gnash::RTMP
52 /// This method is called after the initial network connection
53 /// is established. It reads in the handshake from the client,
54 /// responds appropriately, and then extracts the initial AMF
55 /// object, which is always of type NetConnection, doing an
56 /// INVOKE operation of ::connect(). serverFinish() is
57 /// actually used to extract the AMF data from the packet, and
58 /// handShakeResponse() is used to construct the response packet.
59 boost::shared_ptr
<cygnal::Element
> processClientHandShake(int fd
);
61 bool packetSend(cygnal::Buffer
&buf
);
62 bool packetRead(cygnal::Buffer
&buf
);
64 // These are handlers for the various types
65 boost::shared_ptr
<cygnal::Buffer
> encodeResult(gnash::RTMPMsg::rtmp_status_e status
);
66 boost::shared_ptr
<cygnal::Buffer
> encodeResult(gnash::RTMPMsg::rtmp_status_e status
, const std::string
&filename
);
67 boost::shared_ptr
<cygnal::Buffer
> encodeResult(gnash::RTMPMsg::rtmp_status_e status
, const std::string
&filename
, double &transid
);
68 boost::shared_ptr
<cygnal::Buffer
> encodeResult(gnash::RTMPMsg::rtmp_status_e status
, double &transid
);
69 boost::shared_ptr
<cygnal::Buffer
> encodeResult(gnash::RTMPMsg::rtmp_status_e status
, const std::string
&filename
, double &transid
, double &clientid
);
71 // Encode a Ping for the client
72 boost::shared_ptr
<cygnal::Buffer
> encodePing(rtmp_ping_e type
, boost::uint32_t milliseconds
);
73 boost::shared_ptr
<cygnal::Buffer
> encodePing(rtmp_ping_e type
);
74 // boost::shared_ptr<cygnal::Buffer> encodeUser(user_control_e type, boost::uint32_t milliseconds);
75 boost::shared_ptr
<cygnal::Buffer
> encodeAudio(boost::uint8_t *data
, size_t size
);
76 boost::shared_ptr
<cygnal::Buffer
> encodeVideo(boost::uint8_t *data
, size_t size
);
78 // Encode a onBWDone message for the client
79 boost::shared_ptr
<cygnal::Buffer
> encodeBWDone(double id
);
81 // Parse an Echo Request message coming from the Red5 echo_test.
82 std::vector
<boost::shared_ptr
<cygnal::Element
> > parseEchoRequest(cygnal::Buffer
&buf
) { return parseEchoRequest(buf
.reference(), buf
.size()); };
83 std::vector
<boost::shared_ptr
<cygnal::Element
> > parseEchoRequest(boost::uint8_t *buf
, size_t size
);
84 // format a response to the 'echo' test used for testing Gnash.
85 boost::shared_ptr
<cygnal::Buffer
> formatEchoResponse(double num
, cygnal::Element
&el
);
86 boost::shared_ptr
<cygnal::Buffer
> formatEchoResponse(double num
, cygnal::Buffer
&data
);
87 boost::shared_ptr
<cygnal::Buffer
> formatEchoResponse(double num
, boost::uint8_t *data
, size_t size
);
88 void addReference(boost::uint16_t index
, cygnal::Element
&el
) { _references
[index
] = el
; };
89 cygnal::Element
&getReference(boost::uint16_t index
) { return _references
[index
]; };
91 bool sendFile(int fd
, const std::string
&filespec
);
93 // Create a new client ID
94 #ifdef CLIENT_ID_NUMERIC
95 double createClientID();
97 std::string
createClientID();
100 // Create a new stream ID, which is simply an incrementing counter.
101 double createStreamID();
103 void setStreamID(double id
) { _streamid
= id
; };
104 double getStreamID() { return _streamid
; };
106 size_t sendToClient(std::vector
<int> &fds
, boost::uint8_t *data
,
108 size_t sendToClient(std::vector
<int> &fds
,cygnal::Buffer
&data
);
110 void setNetConnection(gnash::RTMPMsg
*msg
) { _netconnect
.reset(msg
); };
111 void setNetConnection(boost::shared_ptr
<gnash::RTMPMsg
> msg
) { _netconnect
= msg
; };
112 boost::shared_ptr
<gnash::RTMPMsg
> getNetConnection() { return _netconnect
;};
116 /// \method serverFinish
117 /// This is only called by processClientHandshake() to compare
118 /// the handshakes to make sure they match, and to extract the
119 /// initial AMF data from packet.
120 boost::shared_ptr
<cygnal::Buffer
> serverFinish(int fd
,
121 cygnal::Buffer
&handshake1
, cygnal::Buffer
&handshake2
);
122 /// \method handShakeResponse
123 /// This is only called by processClientHandshake() to
124 /// construct the handshake response to the client.
125 bool handShakeResponse(int fd
, cygnal::Buffer
&buf
);
127 /// This is used by the boost tokenizer functions, and is defined
128 /// here purely for convienience.
129 typedef boost::char_separator
<char> Sep
;
130 typedef boost::tokenizer
<Sep
> Tok
;
133 gnash::DiskStream::filetype_e _filetype
;
134 std::string _docroot
;
135 std::string _filespec
;
136 boost::uint32_t _filesize
;
137 std::map
<boost::uint16_t, cygnal::Element
> _references
;
138 #ifdef CLIENT_ID_NUMERIC
139 std::array
<double> _clientids
;
141 boost::array
<std::string
, 1000> _clientids
;
145 /// This store the data from the NetConnection ActionScript
146 /// object we get as the final part of the handshake process
147 /// that is used to set up the connection. This has all the
148 /// file paths and other information needed by the server.
149 boost::shared_ptr
<gnash::RTMPMsg
> _netconnect
;
152 // This is the thread for all incoming RTMP connections
153 bool DSOEXPORT
rtmp_handler(gnash::Network::thread_params_t
*args
);
155 } // end of gnash namespace
156 // end of _RTMP_SERVER_H_
161 // indent-tabs-mode: t