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 {
107 };
108 
111 {
127 };
128 
130 //
136 //
142 //
149 };
150 
153 {
155  static const size_t headerSize = 18;
156 
158  :
159  headerType(RTMP_PACKET_SIZE_LARGE),
160  packetType(PACKET_TYPE_NONE),
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 
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
Definition: RTMP.h:105
An RTMPPacket class contains a full description of an RTMP packet.
Definition: RTMP.h:194
bool isReady(const RTMPPacket &p)
Check if a packet is ready for processing.
Definition: RTMP.h:297
Definition: RTMP.h:76
Definition: RTMP.h:112
std::uint8_t m_nClientBW2
Definition: RTMP.h:467
PacketType packetType
Definition: RTMP.h:168
const std::uint8_t * payloadEnd(const RTMPPacket &p)
Get the end of the allocated payload data section of the buffer.
Definition: RTMP.h:284
Definition: RTMP.h:115
size_t payloadSize(const RTMPPacket &p)
The current size of the space allocated for the message payload.
Definition: RTMP.h:252
Definition: RTMP.h:106
std::uint32_t serverBandwidth() const
Get the stored server bandwidth.
Definition: RTMP.h:451
std::uint32_t _timestamp
The timestamp.
Definition: RTMP.h:174
void setServerBandwidth(std::uint32_t bw)
Store the server bandwidth.
Definition: RTMP.h:446
int m_mediaChannel
Definition: RTMP.h:466
~RTMPPacket()
Definition: RTMP.h:211
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
std::uint32_t _streamID
This seems to be used for NetStream.play.
Definition: RTMP.h:177
PacketSize
The PacketSize specifies the number of fields contained in the header.
Definition: RTMP.h:144
bool sendServerBW(RTMP &r)
Send the server bandwidth.
Definition: RTMP.cpp:402
size_t _bytesIn
Definition: RTMP.h:468
std::shared_ptr< SimpleBuffer > buffer
A buffer with enough storage to write the entire message.
Definition: RTMP.h:219
size_t size() const
Return size of the buffer.
Definition: SimpleBuffer.h:75
Definition: GnashKey.h:161
Definition: RTMP.h:73
ControlType
Known control / ping codes.
Definition: RTMP.h:69
A simple IOChannel subclass for reading and writing sockets.
Definition: Socket.h:41
The RTMPHeader contains all the fields for the packet header.
Definition: RTMP.h:152
RTMPHeader header
Definition: RTMP.h:213
Definition: klash_part.cpp:330
Definition: GnashKey.h:164
Definition: RTMP.h:118
Definition: GnashKey.h:166
std::uint8_t * data()
Get a pointer to start of data. May be NULL if size==0.
Definition: SimpleBuffer.h:81
std::shared_ptr< SimpleBuffer > getMessage()
Get an AMF message received from the server.
Definition: RTMP.h:415
PacketSize headerType
Definition: RTMP.h:167
static const size_t headerSize
The maximum header size of an RTMP packet.
Definition: RTMP.h:155
Channels
The known channels.
Definition: RTMP.h:102
bool connected() const
Whether we have a basic connection to a server.
Definition: RTMP.h:378
void setBandwidth(std::uint32_t bw)
Store our bandwidth.
Definition: RTMP.h:456
std::uint32_t bandwidth() const
Get our bandwidth.
Definition: RTMP.h:461
Definition: GnashKey.h:148
#define DSOEXPORT
Definition: dsodefs.h:55
Definition: RTMP.h:77
This class is for handling the RTMP protocol.
Definition: RTMP.h:326
bool error() const
Whether the RTMP connection is in error condition.
Definition: RTMP.h:385
Definition: RTMP.h:117
Definition: RTMP.h:104
void clearPayload(RTMPPacket &p)
Clear the message body and the bytes read of an RTMPPacket.
Definition: RTMP.h:241
Definition: RTMP.h:116
Definition: GnashKey.h:162
std::string url
Definition: gnash.cpp:59
PacketType
The known packet types.
Definition: RTMP.h:110
Definition: RTMP.h:125
size_t channel
Definition: RTMP.h:179
A simple buffer of bytes.
Definition: SimpleBuffer.h:38
size_t bytesRead
Definition: RTMP.h:221
std::ostream & operator<<(std::ostream &o, PacketType p)
Logging assistance for PacketType.
Definition: RTMP.cpp:1180
bool hasPayload(const RTMPPacket &p)
Check whether an RTMPPacket has a payload.
Definition: RTMP.h:230
bool sendCtrl(RTMP &r, ControlType t, unsigned int nObject, unsigned int nTime)
Send a control packet.
Definition: RTMP.cpp:964
size_t dataSize
Definition: RTMP.h:182
std::shared_ptr< SimpleBuffer > getFLVFrame()
Get an FLV packet received from the server.
Definition: RTMP.h:427
Definition: RTMP.h:123
RTMPHeader()
Definition: RTMP.h:157
std::uint8_t * payloadData(RTMPPacket &p)
Access the payload data section of the buffer.
Definition: RTMP.h:262
size_t _bytesInSent
Definition: RTMP.h:469
Definition: RTMP.h:119
Definition: RTMP.h:126
Uniform Resource Locator.
Definition: URL.h:34
int _inChunkSize
Definition: RTMP.h:465