Line data Source code
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 : #include <string>
12 :
13 : #include "webrtc/base/logging.h"
14 : #include "webrtc/modules/include/module_common_types.h"
15 : #include "webrtc/modules/rtp_rtcp/source/rtp_format_video_generic.h"
16 : #include "webrtc/modules/rtp_rtcp/source/rtp_packet_to_send.h"
17 :
18 : namespace webrtc {
19 :
20 : static const size_t kGenericHeaderLength = 1;
21 :
22 0 : RtpPacketizerGeneric::RtpPacketizerGeneric(FrameType frame_type,
23 0 : size_t max_payload_len)
24 : : payload_data_(NULL),
25 : payload_size_(0),
26 0 : max_payload_len_(max_payload_len - kGenericHeaderLength),
27 0 : frame_type_(frame_type) {
28 0 : }
29 :
30 0 : RtpPacketizerGeneric::~RtpPacketizerGeneric() {
31 0 : }
32 :
33 0 : void RtpPacketizerGeneric::SetPayloadData(
34 : const uint8_t* payload_data,
35 : size_t payload_size,
36 : const RTPFragmentationHeader* fragmentation) {
37 0 : payload_data_ = payload_data;
38 0 : payload_size_ = payload_size;
39 :
40 : // Fragment packets more evenly by splitting the payload up evenly.
41 : size_t num_packets =
42 0 : (payload_size_ + max_payload_len_ - 1) / max_payload_len_;
43 0 : payload_length_ = (payload_size_ + num_packets - 1) / num_packets;
44 0 : assert(payload_length_ <= max_payload_len_);
45 :
46 0 : generic_header_ = RtpFormatVideoGeneric::kFirstPacketBit;
47 0 : }
48 :
49 0 : bool RtpPacketizerGeneric::NextPacket(RtpPacketToSend* packet,
50 : bool* last_packet) {
51 0 : RTC_DCHECK(packet);
52 0 : RTC_DCHECK(last_packet);
53 0 : if (payload_size_ < payload_length_) {
54 0 : payload_length_ = payload_size_;
55 : }
56 :
57 0 : payload_size_ -= payload_length_;
58 0 : RTC_DCHECK_LE(payload_length_, max_payload_len_);
59 :
60 : uint8_t* out_ptr =
61 0 : packet->AllocatePayload(kGenericHeaderLength + payload_length_);
62 : // Put generic header in packet
63 0 : if (frame_type_ == kVideoFrameKey) {
64 0 : generic_header_ |= RtpFormatVideoGeneric::kKeyFrameBit;
65 : }
66 0 : out_ptr[0] = generic_header_;
67 : // Remove first-packet bit, following packets are intermediate
68 0 : generic_header_ &= ~RtpFormatVideoGeneric::kFirstPacketBit;
69 :
70 : // Put payload in packet
71 0 : memcpy(out_ptr + kGenericHeaderLength, payload_data_, payload_length_);
72 0 : payload_data_ += payload_length_;
73 :
74 0 : *last_packet = payload_size_ <= 0;
75 0 : packet->SetMarker(*last_packet);
76 0 : return true;
77 : }
78 :
79 0 : ProtectionType RtpPacketizerGeneric::GetProtectionType() {
80 0 : return kProtectedPacket;
81 : }
82 :
83 0 : StorageType RtpPacketizerGeneric::GetStorageType(
84 : uint32_t retransmission_settings) {
85 0 : return kAllowRetransmission;
86 : }
87 :
88 0 : std::string RtpPacketizerGeneric::ToString() {
89 0 : return "RtpPacketizerGeneric";
90 : }
91 :
92 0 : bool RtpDepacketizerGeneric::Parse(ParsedPayload* parsed_payload,
93 : const uint8_t* payload_data,
94 : size_t payload_data_length) {
95 0 : assert(parsed_payload != NULL);
96 0 : if (payload_data_length == 0) {
97 0 : LOG(LS_ERROR) << "Empty payload.";
98 0 : return false;
99 : }
100 :
101 0 : uint8_t generic_header = *payload_data++;
102 0 : --payload_data_length;
103 :
104 0 : parsed_payload->frame_type =
105 0 : ((generic_header & RtpFormatVideoGeneric::kKeyFrameBit) != 0)
106 0 : ? kVideoFrameKey
107 : : kVideoFrameDelta;
108 0 : parsed_payload->type.Video.is_first_packet_in_frame =
109 0 : (generic_header & RtpFormatVideoGeneric::kFirstPacketBit) != 0;
110 0 : parsed_payload->type.Video.codec = kRtpVideoGeneric;
111 0 : parsed_payload->type.Video.width = 0;
112 0 : parsed_payload->type.Video.height = 0;
113 :
114 0 : parsed_payload->payload = payload_data;
115 0 : parsed_payload->payload_length = payload_data_length;
116 0 : return true;
117 : }
118 : } // namespace webrtc
|