1/*
2 *  Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_FORMAT_H264_H_
12#define WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_FORMAT_H264_H_
13
14#include <queue>
15
16#include "webrtc/modules/rtp_rtcp/source/rtp_format.h"
17
18namespace webrtc {
19
20class RtpPacketizerH264 : public RtpPacketizer {
21 public:
22  // Initialize with payload from encoder.
23  // The payload_data must be exactly one encoded H264 frame.
24  RtpPacketizerH264(FrameType frame_type, size_t max_payload_len);
25
26  virtual ~RtpPacketizerH264();
27
28  virtual void SetPayloadData(
29      const uint8_t* payload_data,
30      size_t payload_size,
31      const RTPFragmentationHeader* fragmentation) OVERRIDE;
32
33  // Get the next payload with H264 payload header.
34  // buffer is a pointer to where the output will be written.
35  // bytes_to_send is an output variable that will contain number of bytes
36  // written to buffer. The parameter last_packet is true for the last packet of
37  // the frame, false otherwise (i.e., call the function again to get the
38  // next packet).
39  // Returns true on success or false if there was no payload to packetize.
40  virtual bool NextPacket(uint8_t* buffer,
41                          size_t* bytes_to_send,
42                          bool* last_packet) OVERRIDE;
43
44  virtual ProtectionType GetProtectionType() OVERRIDE;
45
46  virtual StorageType GetStorageType(uint32_t retransmission_settings) OVERRIDE;
47
48  virtual std::string ToString() OVERRIDE;
49
50 private:
51  struct Packet {
52    Packet(size_t offset,
53           size_t size,
54           bool first_fragment,
55           bool last_fragment,
56           bool aggregated,
57           uint8_t header)
58        : offset(offset),
59          size(size),
60          first_fragment(first_fragment),
61          last_fragment(last_fragment),
62          aggregated(aggregated),
63          header(header) {}
64
65    size_t offset;
66    size_t size;
67    bool first_fragment;
68    bool last_fragment;
69    bool aggregated;
70    uint8_t header;
71  };
72  typedef std::queue<Packet> PacketQueue;
73
74  void GeneratePackets();
75  void PacketizeFuA(size_t fragment_offset, size_t fragment_length);
76  int PacketizeStapA(size_t fragment_index,
77                     size_t fragment_offset,
78                     size_t fragment_length);
79  void NextAggregatePacket(uint8_t* buffer, size_t* bytes_to_send);
80  void NextFragmentPacket(uint8_t* buffer, size_t* bytes_to_send);
81
82  const uint8_t* payload_data_;
83  size_t payload_size_;
84  const size_t max_payload_len_;
85  RTPFragmentationHeader fragmentation_;
86  PacketQueue packets_;
87  FrameType frame_type_;
88
89  DISALLOW_COPY_AND_ASSIGN(RtpPacketizerH264);
90};
91
92// Depacketizer for H264.
93class RtpDepacketizerH264 : public RtpDepacketizer {
94 public:
95  explicit RtpDepacketizerH264(RtpData* const callback);
96
97  virtual ~RtpDepacketizerH264() {}
98
99  virtual bool Parse(WebRtcRTPHeader* rtp_header,
100                     const uint8_t* payload_data,
101                     size_t payload_data_length) OVERRIDE;
102
103 private:
104  RtpData* const callback_;
105
106  DISALLOW_COPY_AND_ASSIGN(RtpDepacketizerH264);
107};
108}  // namespace webrtc
109#endif  // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_FORMAT_H264_H_
110