| 1 | /* |
| 2 | * Copyright (C) 2017 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "RTCPacketOptions.h" |
| 28 | |
| 29 | #if USE(LIBWEBRTC) |
| 30 | |
| 31 | #include "DataReference.h" |
| 32 | #include "WebCoreArgumentCoders.h" |
| 33 | #include <wtf/Optional.h> |
| 34 | |
| 35 | namespace WebKit { |
| 36 | |
| 37 | void RTCPacketOptions::encode(IPC::Encoder& encoder) const |
| 38 | { |
| 39 | encoder.encodeEnum(options.dscp); |
| 40 | encoder << safeCast<int32_t>(options.packet_id); |
| 41 | encoder << options.packet_time_params.rtp_sendtime_extension_id; |
| 42 | |
| 43 | encoder << static_cast<int64_t>(options.packet_time_params.srtp_auth_tag_len); |
| 44 | if (options.packet_time_params.srtp_auth_tag_len > 0) |
| 45 | encoder << IPC::DataReference(reinterpret_cast<const uint8_t*>(options.packet_time_params.srtp_auth_key.data()), options.packet_time_params.srtp_auth_key.size()); |
| 46 | |
| 47 | encoder << options.packet_time_params.srtp_packet_index; |
| 48 | } |
| 49 | |
| 50 | Optional<RTCPacketOptions> RTCPacketOptions::decode(IPC::Decoder& decoder) |
| 51 | { |
| 52 | rtc::PacketTimeUpdateParams params; |
| 53 | rtc::PacketOptions options; |
| 54 | |
| 55 | rtc::DiffServCodePoint dscp; |
| 56 | if (!decoder.decodeEnum(dscp)) |
| 57 | return WTF::nullopt; |
| 58 | options.dscp = dscp; |
| 59 | |
| 60 | Optional<int32_t> packetId; |
| 61 | decoder >> packetId; |
| 62 | if (!packetId) |
| 63 | return WTF::nullopt; |
| 64 | options.packet_id = packetId.value(); |
| 65 | |
| 66 | Optional<int> rtpSendtimeExtensionId; |
| 67 | decoder >> rtpSendtimeExtensionId; |
| 68 | if (!rtpSendtimeExtensionId) |
| 69 | return WTF::nullopt; |
| 70 | params.rtp_sendtime_extension_id = rtpSendtimeExtensionId.value(); |
| 71 | |
| 72 | Optional<int64_t> srtpAuthTagLength; |
| 73 | decoder >> srtpAuthTagLength; |
| 74 | if (!srtpAuthTagLength) |
| 75 | return WTF::nullopt; |
| 76 | params.srtp_auth_tag_len = srtpAuthTagLength.value(); |
| 77 | |
| 78 | if (params.srtp_auth_tag_len > 0) { |
| 79 | IPC::DataReference srtpAuthKey; |
| 80 | if (!decoder.decode(srtpAuthKey)) |
| 81 | return WTF::nullopt; |
| 82 | |
| 83 | params.srtp_auth_key = std::vector<char>(static_cast<size_t>(srtpAuthKey.size())); |
| 84 | memcpy(params.srtp_auth_key.data(), reinterpret_cast<const char*>(srtpAuthKey.data()), srtpAuthKey.size() * sizeof(char)); |
| 85 | } |
| 86 | |
| 87 | Optional<int64_t> srtpPacketIndex; |
| 88 | decoder >> srtpPacketIndex; |
| 89 | if (!srtpPacketIndex) |
| 90 | return WTF::nullopt; |
| 91 | params.srtp_packet_index = srtpPacketIndex.value(); |
| 92 | |
| 93 | options.packet_time_params = WTFMove(params); |
| 94 | return RTCPacketOptions { WTFMove(options) }; |
| 95 | } |
| 96 | |
| 97 | } |
| 98 | |
| 99 | #endif // USE(LIBWEBRTC) |
| 100 | |