Gnash  0.8.11dev
RTMP.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012
3 // Free Software Foundation, Inc.
4 //
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.
9 //
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.
14 //
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
18 
19 #ifndef GNASH_RTMP_H
20 #define GNASH_RTMP_H
21 
22 #include <cstdint>
23 #include <memory>
24 #include <deque>
25 #include <map>
26 
27 #include "SimpleBuffer.h"
28 #include "Socket.h"
29 #include "dsodefs.h"
30 
31 #define RTMP_DEFAULT_CHUNKSIZE 128
32 
33 // Forward declarations.
34 namespace gnash {
35  namespace rtmp {
36  class HandShaker;
37  }
38  class URL;
39 }
40 
41 namespace gnash {
42 namespace rtmp {
43 
45 //
47 //
70 {
76  CONTROL_PING = 0x06,
77  CONTROL_PONG = 0x07,
82 };
83 
85 //
90 //
92 //
100 //
103 {
106  CHANNEL_VIDEO = 0x08
107 };
108 
111 {
126  PACKET_TYPE_FLV = 0x16
127 };
128 
130 //
136 //
142 //
149 };
150 
153 {
155  static const size_t headerSize = 18;
156 
158  :
161  _timestamp(0),
162  _streamID(0),
163  channel(0),
164  dataSize(0)
165  {}
166 
169 
171  //
174  std::uint32_t _timestamp;
175 
177  std::uint32_t _streamID;
178 
179  size_t channel;
180 
181  // The size of the data.
182  size_t dataSize;
183 
184 };
185 
187 //
191 //
195 {
197  //
203  explicit RTMPPacket(size_t reserve = 0);
204 
206  //
209  RTMPPacket(const RTMPPacket& other);
210 
212 
214 
216  //
219  std::shared_ptr<SimpleBuffer> buffer;
220 
221  size_t bytesRead;
222 };
223 
224 
226 //
229 inline bool
231 {
232  return (p.buffer.get());
233 }
234 
236 //
240 inline void
242 {
243  p.buffer.reset();
244  p.bytesRead = 0;
245 }
246 
248 //
251 inline size_t
253 {
254  assert(hasPayload(p));
255  const SimpleBuffer& buf = *p.buffer;
256  assert(buf.size() >= RTMPHeader::headerSize);
257  return buf.size() - RTMPHeader::headerSize;
258 }
259 
261 inline std::uint8_t*
263 {
264  assert(hasPayload(p));
265  SimpleBuffer& buf = *p.buffer;
266  return buf.data() + RTMPHeader::headerSize;
267 }
268 
270 inline const std::uint8_t*
272 {
273  assert(hasPayload(p));
274  const SimpleBuffer& buf = *p.buffer;
275  return buf.data() + RTMPHeader::headerSize;
276 }
277 
279 //
283 inline const std::uint8_t*
285 {
286  assert(hasPayload(p));
287  SimpleBuffer& buf = *p.buffer;
288  return buf.data() + buf.size();
289 }
290 
292 //
296 inline bool
298  return p.bytesRead == p.header.dataSize;
299 }
300 
301 
303 //
306 //
309 //
319 //
327 {
328 
330  RTMP();
331 
332  ~RTMP();
333 
335  //
339  //
343  bool connect(const URL& url);
344 
346  //
353  void call(const SimpleBuffer& amf);
354 
356  //
359  //
362  void play(const SimpleBuffer& amf, int id);
363 
365  //
368  void setBufferTime(size_t time, int streamID);
369 
371  //
375  //
378  bool connected() const {
379  return _connected;
380  }
381 
383  //
385  bool error() const {
386  return _error;
387  }
388 
390  //
393  //
399  //
403  void update();
404 
406  //
408  void close();
409 
411  //
415  std::shared_ptr<SimpleBuffer> getMessage() {
416  if (_messageQueue.empty()) return std::shared_ptr<SimpleBuffer>();
417  std::shared_ptr<SimpleBuffer> b = _messageQueue.front();
418  _messageQueue.pop_front();
419  return b;
420  }
421 
423  //
427  std::shared_ptr<SimpleBuffer> getFLVFrame() {
428  if (_flvQueue.empty()) return std::shared_ptr<SimpleBuffer>();
429  std::shared_ptr<SimpleBuffer> b = _flvQueue.front();
430  _flvQueue.pop_front();
431  return b;
432  }
433 
435  void handlePacket(const RTMPPacket& packet);
436 
438  int readSocket(std::uint8_t* dst, int num);
439 
441  bool sendPacket(RTMPPacket& packet);
442 
444  //
446  void setServerBandwidth(std::uint32_t bw) {
447  _serverBandwidth = bw;
448  }
449 
451  std::uint32_t serverBandwidth() const {
452  return _serverBandwidth;
453  }
454 
456  void setBandwidth(std::uint32_t bw) {
457  _bandwidth = bw;
458  }
459 
461  std::uint32_t bandwidth() const {
462  return _bandwidth;
463  }
464 
467  std::uint8_t m_nClientBW2;
468  size_t _bytesIn;
469  size_t _bytesInSent;
470 
471 private:
472 
473  enum ChannelType {
474  CHANNELS_IN,
475  CHANNELS_OUT
476  };
477 
479  bool readPacketHeader(RTMPPacket& packet);
480 
481  bool readPacketPayload(RTMPPacket& packet);
482 
484  bool hasPacket(ChannelType t, size_t channel) const;
485 
487  //
490  RTMPPacket& getPacket(ChannelType t, size_t channel);
491 
493  //
496  RTMPPacket& storePacket(ChannelType t, size_t channel, const RTMPPacket& p);
497 
499  //
503  //
505  typedef std::map<size_t, RTMPPacket> ChannelSet;
506 
507  Socket _socket;
508 
510  ChannelSet _inChannels;
511 
513  ChannelSet _outChannels;
514 
515  std::deque<std::shared_ptr<SimpleBuffer> > _messageQueue;
516  std::deque<std::shared_ptr<SimpleBuffer> > _flvQueue;
517 
519  std::uint32_t _serverBandwidth;
520 
522  std::uint32_t _bandwidth;
523 
525  size_t _outChunkSize;
526 
527  std::unique_ptr<HandShaker> _handShaker;
528 
529  bool _connected;
530 
531  bool _error;
532 
534  //
537  std::unique_ptr<RTMPPacket> _incompletePacket;
538 
539 };
540 
542 DSOEXPORT bool sendServerBW(RTMP& r);
543 
545 bool sendCtrl(RTMP& r, ControlType, unsigned int nObject, unsigned int nTime);
546 
548 std::ostream& operator<<(std::ostream& o, PacketType p);
549 
551 std::ostream& operator<<(std::ostream& o, ControlType t);
552 
553 } // namespace rtmp
554 
555 } // namespace gnash
556 #endif
AMF.h
gnash::rtmp::RTMP::bandwidth
std::uint32_t bandwidth() const
Get our bandwidth.
Definition: RTMP.h:461
gnash::rtmp::CONTROL_PONG
@ CONTROL_PONG
Definition: RTMP.h:77
gnash::rtmp::CONTROL_CLEAR_BUFFER
@ CONTROL_CLEAR_BUFFER
Definition: RTMP.h:72
gnash::rtmp::PACKET_TYPE_SHARED_OBJECT
@ PACKET_TYPE_SHARED_OBJECT
Definition: RTMP.h:124
gnash::rtmp::RTMP::_inChunkSize
int _inChunkSize
Definition: RTMP.h:465
gnash::rtmp::RTMP::update
void update()
This function handles reading incoming data and filling data queues.
Definition: RTMP.cpp:231
gnash::rtmp::RTMP::_bytesInSent
size_t _bytesInSent
Definition: RTMP.h:469
gnash::rtmp::RTMP_PACKET_SIZE_MINIMUM
@ RTMP_PACKET_SIZE_MINIMUM
Definition: RTMP.h:148
gnash::rtmp::RTMP::play
void play(const SimpleBuffer &amf, int id)
This is used for sending NetStream requests.
Definition: RTMP.cpp:385
gnash::rtmp::RTMP::sendPacket
bool sendPacket(RTMPPacket &packet)
Send an RTMPPacket on the connection.
Definition: RTMP.cpp:582
gnash::rtmp::CONTROL_REQUEST_VERIFY
@ CONTROL_REQUEST_VERIFY
Definition: RTMP.h:78
gnash::rtmp::RTMP::m_nClientBW2
std::uint8_t m_nClientBW2
Definition: RTMP.h:467
gnash::SimpleBuffer::appendNetworkLong
void appendNetworkLong(const std::uint32_t l)
Append 4 bytes to the buffer.
Definition: SimpleBuffer.h:169
gnash::rtmp::CONTROL_BUFFER_TIME
@ CONTROL_BUFFER_TIME
Definition: RTMP.h:74
SimpleBuffer.h
dsodefs.h
gnash::rtmp::RTMPHeader::channel
size_t channel
Definition: RTMP.h:179
gnash::log_debug
void log_debug(StringType msg, Args... args)
Definition: log.h:301
gnash::key::i
@ i
Definition: GnashKey.h:155
LOG_ONCE
#define LOG_ONCE(x)
Definition: log.h:49
gnash::rtmp::CONTROL_BUFFER_EMPTY
@ CONTROL_BUFFER_EMPTY
Definition: RTMP.h:80
gnash::rtmp::RTMP::setBufferTime
void setBufferTime(size_t time, int streamID)
Instruct server to buffer this much data.
Definition: RTMP.cpp:183
gnash::rtmp::isReady
bool isReady(const RTMPPacket &p)
Check if a packet is ready for processing.
Definition: RTMP.h:297
gnash::rtmp::PACKET_TYPE_AUDIO
@ PACKET_TYPE_AUDIO
Definition: RTMP.h:118
_
#define _(String)
Definition: log.h:44
gnash::rtmp::RTMP::_bytesIn
size_t _bytesIn
Definition: RTMP.h:468
gnash
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:41
gnash::rtmp::payloadSize
size_t payloadSize(const RTMPPacket &p)
The current size of the space allocated for the message payload.
Definition: RTMP.h:252
gnash::rtmp::PACKET_TYPE_METADATA
@ PACKET_TYPE_METADATA
Definition: RTMP.h:123
gnash::rtmp::RTMPPacket::~RTMPPacket
~RTMPPacket()
Definition: RTMP.h:211
RTMP.h
gnash::rtmp::sendServerBW
bool sendServerBW(RTMP &r)
Send the server bandwidth.
Definition: RTMP.cpp:402
gnash::rtmp::RTMPHeader::packetType
PacketType packetType
Definition: RTMP.h:168
gnash::key::s
@ s
Definition: GnashKey.h:165
gnash::rtmp::RTMPPacket
An RTMPPacket class contains a full description of an RTMP packet.
Definition: RTMP.h:195
gnash::Socket::eof
virtual bool eof() const
Return true if the end of the stream has been reached.
Definition: Socket.cpp:415
gnash::key::n
@ n
Definition: GnashKey.h:160
gnash::rtmp::RTMP_PACKET_SIZE_LARGE
@ RTMP_PACKET_SIZE_LARGE
Definition: RTMP.h:145
gnash::amf::readNetworkShort
std::uint16_t readNetworkShort(const std::uint8_t *buf)
Read an unsigned 16-bit value in network byte order.
Definition: AMF.h:119
__FUNCTION__
#define __FUNCTION__
Definition: log.h:437
gnash::rtmp::CONTROL_CLEAR_STREAM
@ CONTROL_CLEAR_STREAM
Definition: RTMP.h:71
gnash::Socket::connected
bool connected() const
Whether a connection attempt is complete.
Definition: Socket.cpp:50
gnash::rtmp::payloadEnd
const std::uint8_t * payloadEnd(const RTMPPacket &p)
Get the end of the allocated payload data section of the buffer.
Definition: RTMP.h:284
gnash::rtmp::RTMPHeader::_timestamp
std::uint32_t _timestamp
The timestamp.
Definition: RTMP.h:174
gnash::rtmp::PACKET_TYPE_CONTROL
@ PACKET_TYPE_CONTROL
Definition: RTMP.h:115
gnash::log_error
void log_error(StringType msg, Args... args)
Definition: log.h:283
gnash::rtmp::PACKET_TYPE_FLEX_STREAM_SEND
@ PACKET_TYPE_FLEX_STREAM_SEND
Definition: RTMP.h:120
gnash::rtmp::PACKET_TYPE_CLIENTBW
@ PACKET_TYPE_CLIENTBW
Definition: RTMP.h:117
gnash::rtmp::Channels
Channels
The known channels.
Definition: RTMP.h:103
gnash::rtmp::RTMP::setServerBandwidth
void setServerBandwidth(std::uint32_t bw)
Store the server bandwidth.
Definition: RTMP.h:446
gnash::rtmp::RTMPPacket::buffer
std::shared_ptr< SimpleBuffer > buffer
A buffer with enough storage to write the entire message.
Definition: RTMP.h:219
gnash::amf::readNetworkLong
std::uint32_t readNetworkLong(const std::uint8_t *buf)
Read an unsigned 32-bit value in network byte order.
Definition: AMF.h:129
gnash::rtmp::PACKET_TYPE_FLEX_MESSAGE
@ PACKET_TYPE_FLEX_MESSAGE
Definition: RTMP.h:122
gnash::key::t
@ t
Definition: GnashKey.h:166
ClockTime.h
gnash::rtmp::PACKET_TYPE_FLV
@ PACKET_TYPE_FLV
Definition: RTMP.h:126
gnash::key::r
@ r
Definition: GnashKey.h:164
gnash::rtmp::RTMP::error
bool error() const
Whether the RTMP connection is in error condition.
Definition: RTMP.h:385
Socket.h
GnashSystemNetHeaders.h
gnash::rtmp::PacketSize
PacketSize
The PacketSize specifies the number of fields contained in the header.
Definition: RTMP.h:144
gnash::rtmp::ControlType
ControlType
Known control / ping codes.
Definition: RTMP.h:70
gnash::rtmp::RTMPHeader::headerSize
static const size_t headerSize
The maximum header size of an RTMP packet.
Definition: RTMP.h:155
GnashAlgorithm.h
gnash::rtmp::RTMPPacket::header
RTMPHeader header
Definition: RTMP.h:213
gnash::amf::readString
std::string readString(const std::uint8_t *&pos, const std::uint8_t *end)
Read a string value from the buffer.
Definition: AMF.cpp:87
play
@ play
Definition: klash_part.cpp:330
URL.h
gnash::SimpleBuffer::size
size_t size() const
Return size of the buffer.
Definition: SimpleBuffer.h:75
gnash::SimpleBuffer::data
std::uint8_t * data()
Get a pointer to start of data. May be NULL if size==0.
Definition: SimpleBuffer.h:81
gnash::Socket
A simple IOChannel subclass for reading and writing sockets.
Definition: Socket.h:42
gnash::key::p
@ p
Definition: GnashKey.h:162
gnash::rtmp::RTMP::getFLVFrame
std::shared_ptr< SimpleBuffer > getFLVFrame()
Get an FLV packet received from the server.
Definition: RTMP.h:427
gnash::rtmp::CHANNEL_CONTROL1
@ CHANNEL_CONTROL1
Definition: RTMP.h:104
gnash::rtmp::PACKET_TYPE_INVOKE
@ PACKET_TYPE_INVOKE
Definition: RTMP.h:125
gnash::rtmp::clearPayload
void clearPayload(RTMPPacket &p)
Clear the message body and the bytes read of an RTMPPacket.
Definition: RTMP.h:241
gnash::Socket::read
virtual std::streamsize read(void *dst, std::streamsize num)
Read exactly the given number of bytes from the Socket or none at all.
Definition: Socket.cpp:302
gnash::rtmp::RTMP::connect
bool connect(const URL &url)
Initiate a network connection.
Definition: RTMP.cpp:200
gnash::rtmp::PACKET_TYPE_NONE
@ PACKET_TYPE_NONE
Definition: RTMP.h:112
gnash::Socket::write
virtual std::streamsize write(const void *src, std::streamsize num)
Write the given number of bytes to the stream.
Definition: Socket.cpp:355
gnash::rtmp::CONTROL_BUFFER_READY
@ CONTROL_BUFFER_READY
Definition: RTMP.h:81
gnash::rtmp::RTMP
This class is for handling the RTMP protocol.
Definition: RTMP.h:327
gnash::rtmp::CONTROL_RESET_STREAM
@ CONTROL_RESET_STREAM
Definition: RTMP.h:75
gnash::rtmp::sendCtrl
bool sendCtrl(RTMP &r, ControlType t, unsigned int nObject, unsigned int nTime)
Send a control packet.
Definition: RTMP.cpp:964
gnash::rtmp::RTMP_PACKET_SIZE_SMALL
@ RTMP_PACKET_SIZE_SMALL
Definition: RTMP.h:147
gnash::rtmp::RTMP::close
void close()
Close the connection.
Definition: RTMP.cpp:785
log.h
gnash::rtmp::CHANNEL_VIDEO
@ CHANNEL_VIDEO
Definition: RTMP.h:106
gnash::rtmp::RTMPPacket::bytesRead
size_t bytesRead
Definition: RTMP.h:221
gnash::Socket::close
void close()
Close the Socket.
Definition: Socket.cpp:114
url
std::string url
Definition: gnash.cpp:59
gnash::rtmp::RTMP::getMessage
std::shared_ptr< SimpleBuffer > getMessage()
Get an AMF message received from the server.
Definition: RTMP.h:415
gnash::rtmp::RTMPHeader::headerType
PacketSize headerType
Definition: RTMP.h:167
gnash::image::end
pixel_iterator< T > end(GnashImage &im)
Definition: ImageIterators.h:198
gnash::rtmp::RTMP::call
void call(const SimpleBuffer &amf)
This is used for sending call requests from the core.
Definition: RTMP.cpp:189
RTMP_DEFAULT_CHUNKSIZE
#define RTMP_DEFAULT_CHUNKSIZE
Definition: RTMP.h:31
gnash::rtmp::CHANNEL_CONTROL2
@ CHANNEL_CONTROL2
Definition: RTMP.h:105
gnash::rtmp::operator<<
std::ostream & operator<<(std::ostream &o, PacketType p)
Logging assistance for PacketType.
Definition: RTMP.cpp:1180
gnash::rtmp::PACKET_TYPE_CHUNK_SIZE
@ PACKET_TYPE_CHUNK_SIZE
Definition: RTMP.h:113
gnash::key::c
@ c
Definition: GnashKey.h:149
gnash::rtmp::RTMP::readSocket
int readSocket(std::uint8_t *dst, int num)
Read from the socket.
Definition: RTMP.cpp:359
gnash::rtmp::PACKET_TYPE_FLEX_SHARED_OBJECT
@ PACKET_TYPE_FLEX_SHARED_OBJECT
Definition: RTMP.h:121
gnash::rtmp::hasPayload
bool hasPayload(const RTMPPacket &p)
Check whether an RTMPPacket has a payload.
Definition: RTMP.h:230
gnash::rtmp::RTMP::~RTMP
~RTMP()
Definition: RTMP.cpp:155
gnash::rtmp::PACKET_TYPE_BYTES_READ
@ PACKET_TYPE_BYTES_READ
Definition: RTMP.h:114
DSOEXPORT
#define DSOEXPORT
Definition: dsodefs.h:55
gnash::amf::STRING_AMF0
@ STRING_AMF0
Definition: AMF.h:49
gnash::rtmp::RTMPHeader::dataSize
size_t dataSize
Definition: RTMP.h:182
gnash::rtmp::RTMPHeader
The RTMPHeader contains all the fields for the packet header.
Definition: RTMP.h:153
gnash::equal
bool equal(string_table &st, string_table::key a, string_table::key b, bool caseless)
Check whether two keys are equivalent.
Definition: string_table.cpp:174
gnash::rtmp::PACKET_TYPE_SERVERBW
@ PACKET_TYPE_SERVERBW
Definition: RTMP.h:116
gnash::rtmp::RTMPPacket::RTMPPacket
RTMPPacket(size_t reserve=0)
Construct a packet with an optional reserved memory allocation.
Definition: RTMP.cpp:121
gnash::Socket::connect
bool connect(const std::string &hostname, std::uint16_t port)
Initiate a connection.
Definition: Socket.cpp:147
gnash::rtmp::RTMP_PACKET_SIZE_MEDIUM
@ RTMP_PACKET_SIZE_MEDIUM
Definition: RTMP.h:146
gnash::rtmp::PACKET_TYPE_VIDEO
@ PACKET_TYPE_VIDEO
Definition: RTMP.h:119
gnash::URL
Uniform Resource Locator.
Definition: URL.h:35
gnash::rtmp::RTMP::RTMP
RTMP()
Construct a non-connected RTMP handler.
Definition: RTMP.cpp:140
gnash::rtmp::RTMPHeader::RTMPHeader
RTMPHeader()
Definition: RTMP.h:157
gnash::key::o
@ o
Definition: GnashKey.h:161
gnash::rtmp::RTMP::connected
bool connected() const
Whether we have a basic connection to a server.
Definition: RTMP.h:378
gnash::log_unimpl
void log_unimpl(StringType msg, Args... args)
Definition: log.h:289
gnash::hexify
std::string hexify(const unsigned char *p, size_t length, bool ascii)
Convert a sequence of bytes to hex or ascii format.
Definition: log.cpp:48
gnash::rtmp::RTMP::m_mediaChannel
int m_mediaChannel
Definition: RTMP.h:466
gnash::rtmp::CONTROL_PING
@ CONTROL_PING
Definition: RTMP.h:76
gnash::rtmp::PacketType
PacketType
The known packet types.
Definition: RTMP.h:111
gnash::rtmp::RTMPHeader::_streamID
std::uint32_t _streamID
This seems to be used for NetStream.play.
Definition: RTMP.h:177
gnash::SimpleBuffer::appendNetworkShort
void appendNetworkShort(const std::uint16_t s)
Append 2 bytes to the buffer.
Definition: SimpleBuffer.h:154
gnash::key::b
@ b
Definition: GnashKey.h:148
gnash::rtmp::CONTROL_RESPOND_VERIFY
@ CONTROL_RESPOND_VERIFY
Definition: RTMP.h:79
gnash::Socket::bad
virtual bool bad() const
True if the Socket is in an error condition.
Definition: Socket.h:79
gnash::rtmp::CONTROL_STREAM_DRY
@ CONTROL_STREAM_DRY
Definition: RTMP.h:73
gnash::rtmp::RTMP::handlePacket
void handlePacket(const RTMPPacket &packet)
Handle an RTMPPacket.
Definition: RTMP.cpp:285
gnash::rtmp::RTMP::serverBandwidth
std::uint32_t serverBandwidth() const
Get the stored server bandwidth.
Definition: RTMP.h:451
gnash::rtmp::payloadData
std::uint8_t * payloadData(RTMPPacket &p)
Access the payload data section of the buffer.
Definition: RTMP.h:262
gnash::SimpleBuffer
A simple buffer of bytes.
Definition: SimpleBuffer.h:38
gnash::rtmp::RTMP::setBandwidth
void setBandwidth(std::uint32_t bw)
Store our bandwidth.
Definition: RTMP.h:456