danilchap | 1edb7ab | 2016-04-20 12:25:10 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 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 "webrtc/modules/rtp_rtcp/source/rtp_header_extensions.h" |
| 12 | |
| 13 | #include "webrtc/base/checks.h" |
| 14 | #include "webrtc/base/logging.h" |
| 15 | #include "webrtc/modules/rtp_rtcp/include/rtp_cvo.h" |
| 16 | #include "webrtc/modules/rtp_rtcp/source/byte_io.h" |
| 17 | |
| 18 | namespace webrtc { |
| 19 | // Absolute send time in RTP streams. |
| 20 | // |
| 21 | // The absolute send time is signaled to the receiver in-band using the |
| 22 | // general mechanism for RTP header extensions [RFC5285]. The payload |
| 23 | // of this extension (the transmitted value) is a 24-bit unsigned integer |
| 24 | // containing the sender's current time in seconds as a fixed point number |
| 25 | // with 18 bits fractional part. |
| 26 | // |
| 27 | // The form of the absolute send time extension block: |
| 28 | // |
| 29 | // 0 1 2 3 |
| 30 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 31 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 32 | // | ID | len=2 | absolute send time | |
| 33 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 34 | const char* AbsoluteSendTime::kName = |
| 35 | "http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time"; |
| 36 | bool AbsoluteSendTime::IsSupportedFor(MediaType type) { |
| 37 | return true; |
| 38 | } |
| 39 | |
| 40 | bool AbsoluteSendTime::Parse(const uint8_t* data, uint32_t* value) { |
| 41 | *value = ByteReader<uint32_t, 3>::ReadBigEndian(data); |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | bool AbsoluteSendTime::Write(uint8_t* data, int64_t time_ms) { |
| 46 | const uint32_t kAbsSendTimeFraction = 18; |
| 47 | uint32_t time_24_bits = |
| 48 | static_cast<uint32_t>(((time_ms << kAbsSendTimeFraction) + 500) / 1000) & |
| 49 | 0x00FFFFFF; |
| 50 | |
| 51 | ByteWriter<uint32_t, 3>::WriteBigEndian(data, time_24_bits); |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | // An RTP Header Extension for Client-to-Mixer Audio Level Indication |
| 56 | // |
| 57 | // https://datatracker.ietf.org/doc/draft-lennox-avt-rtp-audio-level-exthdr/ |
| 58 | // |
| 59 | // The form of the audio level extension block: |
| 60 | // |
| 61 | // 0 1 |
| 62 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 |
| 63 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 64 | // | ID | len=0 |V| level | |
| 65 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 66 | // |
| 67 | const char* AudioLevel::kName = "urn:ietf:params:rtp-hdrext:ssrc-audio-level"; |
| 68 | bool AudioLevel::IsSupportedFor(MediaType type) { |
| 69 | switch (type) { |
| 70 | case MediaType::ANY: |
| 71 | case MediaType::AUDIO: |
| 72 | return true; |
| 73 | case MediaType::VIDEO: |
| 74 | case MediaType::DATA: |
| 75 | return false; |
| 76 | } |
| 77 | RTC_NOTREACHED(); |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | bool AudioLevel::Parse(const uint8_t* data, |
| 82 | bool* voice_activity, |
| 83 | uint8_t* audio_level) { |
| 84 | *voice_activity = (data[0] & 0x80) != 0; |
| 85 | *audio_level = data[0] & 0x7F; |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | bool AudioLevel::Write(uint8_t* data, |
| 90 | bool voice_activity, |
| 91 | uint8_t audio_level) { |
| 92 | RTC_CHECK_LE(audio_level, 0x7f); |
| 93 | data[0] = (voice_activity ? 0x80 : 0x00) | audio_level; |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | // From RFC 5450: Transmission Time Offsets in RTP Streams. |
| 98 | // |
| 99 | // The transmission time is signaled to the receiver in-band using the |
| 100 | // general mechanism for RTP header extensions [RFC5285]. The payload |
| 101 | // of this extension (the transmitted value) is a 24-bit signed integer. |
| 102 | // When added to the RTP timestamp of the packet, it represents the |
| 103 | // "effective" RTP transmission time of the packet, on the RTP |
| 104 | // timescale. |
| 105 | // |
| 106 | // The form of the transmission offset extension block: |
| 107 | // |
| 108 | // 0 1 2 3 |
| 109 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 110 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 111 | // | ID | len=2 | transmission offset | |
| 112 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 113 | const char* TransmissionOffset::kName = "urn:ietf:params:rtp-hdrext:toffset"; |
| 114 | bool TransmissionOffset::IsSupportedFor(MediaType type) { |
| 115 | switch (type) { |
| 116 | case MediaType::ANY: |
| 117 | case MediaType::VIDEO: |
| 118 | return true; |
| 119 | case MediaType::AUDIO: |
| 120 | case MediaType::DATA: |
| 121 | return false; |
| 122 | } |
| 123 | RTC_NOTREACHED(); |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | bool TransmissionOffset::Parse(const uint8_t* data, int32_t* value) { |
| 128 | *value = ByteReader<int32_t, 3>::ReadBigEndian(data); |
| 129 | return true; |
| 130 | } |
| 131 | |
| 132 | bool TransmissionOffset::Write(uint8_t* data, int64_t value) { |
| 133 | RTC_CHECK_LE(value, 0x00ffffff); |
| 134 | ByteWriter<int32_t, 3>::WriteBigEndian(data, value); |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | // 0 1 2 |
| 139 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 |
| 140 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 141 | // | ID | L=1 |transport wide sequence number | |
| 142 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 143 | const char* TransportSequenceNumber::kName = |
| 144 | "http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions"; |
| 145 | bool TransportSequenceNumber::IsSupportedFor(MediaType type) { |
| 146 | return true; |
| 147 | } |
| 148 | |
| 149 | bool TransportSequenceNumber::Parse(const uint8_t* data, uint16_t* value) { |
| 150 | *value = ByteReader<uint16_t>::ReadBigEndian(data); |
| 151 | return true; |
| 152 | } |
| 153 | |
| 154 | bool TransportSequenceNumber::Write(uint8_t* data, uint16_t value) { |
| 155 | ByteWriter<uint16_t>::WriteBigEndian(data, value); |
| 156 | return true; |
| 157 | } |
| 158 | |
| 159 | // Coordination of Video Orientation in RTP streams. |
| 160 | // |
| 161 | // Coordination of Video Orientation consists in signaling of the current |
| 162 | // orientation of the image captured on the sender side to the receiver for |
| 163 | // appropriate rendering and displaying. |
| 164 | // |
| 165 | // 0 1 |
| 166 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 |
| 167 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 168 | // | ID | len=0 |0 0 0 0 C F R R| |
| 169 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 170 | const char* VideoOrientation::kName = "urn:3gpp:video-orientation"; |
| 171 | bool VideoOrientation::IsSupportedFor(MediaType type) { |
| 172 | switch (type) { |
| 173 | case MediaType::ANY: |
| 174 | case MediaType::VIDEO: |
| 175 | return true; |
| 176 | case MediaType::AUDIO: |
| 177 | case MediaType::DATA: |
| 178 | return false; |
| 179 | } |
| 180 | RTC_NOTREACHED(); |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | bool VideoOrientation::Parse(const uint8_t* data, VideoRotation* rotation) { |
| 185 | *rotation = ConvertCVOByteToVideoRotation(data[0] & 0x03); |
| 186 | return true; |
| 187 | } |
| 188 | |
| 189 | bool VideoOrientation::Write(uint8_t* data, VideoRotation rotation) { |
| 190 | data[0] = ConvertVideoRotationToCVOByte(rotation); |
| 191 | return true; |
| 192 | } |
| 193 | |
| 194 | bool VideoOrientation::Parse(const uint8_t* data, uint8_t* value) { |
| 195 | *value = data[0]; |
| 196 | return true; |
| 197 | } |
| 198 | |
| 199 | bool VideoOrientation::Write(uint8_t* data, uint8_t value) { |
| 200 | data[0] = value; |
| 201 | return true; |
| 202 | } |
| 203 | } // namespace webrtc |