summaryrefslogtreecommitdiffstats
path: root/remoting/protocol/rtp_utils.h
blob: 4441d78eb6b0610564678d6e6029f4f6f25f9eac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef REMOTING_PROTOCOL_RTP_UTILS_H_
#define REMOTING_PROTOCOL_RTP_UTILS_H_

#include "base/basictypes.h"

namespace remoting {
namespace protocol {

struct RtpHeader {
  RtpHeader();

  // RTP version is always set to 2.
  // version = 2
  bool padding;
  bool extension;
  uint8 sources;
  bool marker;
  uint8 payload_type;
  uint16 sequence_number;
  uint32 timestamp;
  uint32 sync_source_id;
  uint32 source_id[15];
};

struct RtcpReceiverReport {
  RtcpReceiverReport();

  uint32 receiver_ssrc;
  uint32 sender_ssrc;
  uint8 loss_fraction;  // 8bit fixed point value in the interval [0..1].
  uint32 total_lost_packets;
  uint32 last_sequence_number;
  uint32 jitter;
  uint32 last_sender_report_timestamp;
  uint32 last_sender_report_delay;
};

// Vp8Descriptor struct used to store values of the VP8 RTP descriptor
// fields. Meaning of each field is documented in the RTP Payload
// Format for VP8 spec: http://www.webmproject.org/code/specs/rtp/ .
struct Vp8Descriptor {
  enum FragmentationInfo {
    NOT_FRAGMENTED = 0,
    FIRST_FRAGMENT = 1,
    MIDDLE_FRAGMENT = 2,
    LAST_FRAGMENT = 3,
  };

  Vp8Descriptor()
      : non_reference_frame(false),
        fragmentation_info(NOT_FRAGMENTED),
        frame_beginning(false),
        picture_id(kuint32max) {
  }

  bool non_reference_frame;
  uint8 fragmentation_info;
  bool frame_beginning;

  // PictureID is considered to be absent if |picture_id| is set to kuint32max.
  uint32 picture_id;
};

// Returns size of RTP header for the specified number of sources.
int GetRtpHeaderSize(const RtpHeader& header);

// Packs RTP header into the buffer.
void PackRtpHeader(const RtpHeader& header, uint8* buffer, int buffer_size);

// Unpacks RTP header and stores unpacked values in |header|.
int UnpackRtpHeader(const uint8* buffer, int buffer_size, RtpHeader* header);

// Three following functions below are used to pack and unpack RTCP
// Receiver Report packets. They implement only subset of RTCP that is
// useful for chromoting.  Particularly there are following
// limitations:
//
//  1. Only one report per packet. There is always only one sender and
//     only one receiver in chromotocol session, so we never need to
//     have more than one report per packet.
//  2. No RTCP Sender Report. Sender Reports are useful for streams
//     synchronization (e.g. audio/video syncronization), but it is
//     not needed for screencasts.

// Returns size of RTCP Receiver Report packet.
int GetRtcpReceiverReportSize(const RtcpReceiverReport& report);

// Packs RTCP Receiver Report into the |buffer|.
void PackRtcpReceiverReport(const RtcpReceiverReport& report,
                            uint8* buffer, int buffer_size);

// Unpack RTCP Receiver Report packet. If the packet is invalid
// returns -1, othewise returns size of the data that was read from
// the packet.
int UnpackRtcpReceiverReport(const uint8* buffer, int buffer_size,
                             RtcpReceiverReport* report);

// Returns size of VP8 Payload Descriptor.
int GetVp8DescriptorSize(const Vp8Descriptor& descriptor);

// Packs VP8 Payload Descriptor into the |buffer|.
void PackVp8Descriptor(const Vp8Descriptor& descriptor, uint8* buffer,
                       int buffer_size);

// Unpacks VP8 Payload Descriptor. If the descriptor is not valid
// returns -1, otherwise returns size of the descriptor.
int UnpackVp8Descriptor(const uint8* buffer, int buffer_size,
                        Vp8Descriptor* descriptor);

}  // namespace protocol
}  // namespace remoting

#endif  // REMOTING_PROTOCOL_RTP_UTILS_H_