media/mtransport/mediapacket.h
author Sven Sauleau <ssauleau@igalia.com>
Tue, 05 Mar 2019 12:19:31 +0000
changeset 522212 64cc60d52a04d0c97162b18b4c3c744a11684e41
parent 518880 f18e61be455d493f10a2cc651cd254859894e02e
permissions -rw-r--r--
Bug 1526694 [wpt PR 15276] - [wasm] update js-api test runner and expectations, a=testonly Automatic update from web-platform-tests [wasm] update js-api test runner and expectations wasm-module-builder: - consistently adds the end opcode in function bodies (using addBody). - add missing setTableLength method table/get-set: - Avoid unrelated errors like: Cannot destructure property `fn` of 'undefined' or 'null'. Bug: 926311, v8:8319 Change-Id: Ia7db1ee5c3a9b1c1d6092cdce8885ddee4dd9b0e Reviewed-on: https://chromium-review.googlesource.com/c/1458099 Reviewed-by: Michael Lippautz <mlippautz@chromium.org> Commit-Queue: Sven Sauleau <ssauleau@igalia.com> Cr-Commit-Position: refs/heads/master@{#633121} -- wpt-commits: 22400f9755a49a50632a8f2067cb06bca74efff8 wpt-pr: 15276

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mediapacket_h__
#define mediapacket_h__

#include <cstddef>
#include <cstdint>
#include "mozilla/UniquePtr.h"
#include "mozilla/Maybe.h"

class PickleIterator;

namespace IPC {
class Message;
}

namespace mozilla {

// TODO: It might be worthwhile to teach this class how to "borrow" a buffer.
// That would make it easier to misuse, however, so maybe not worth it.
class MediaPacket {
 public:
  MediaPacket() = default;
  MediaPacket(MediaPacket&& orig) = default;
  MediaPacket(const MediaPacket& orig);

  // Takes ownership of the passed-in data
  void Take(UniquePtr<uint8_t[]>&& data, size_t len, size_t capacity = 0) {
    data_ = std::move(data);
    len_ = len;
    if (capacity < len) {
      capacity = len;
    }
    capacity_ = capacity;
  }

  void Reset() {
    data_.reset();
    len_ = 0;
    capacity_ = 0;
    encrypted_data_.reset();
    encrypted_len_ = 0;
    sdp_level_.reset();
  }

  // Copies the passed-in data
  void Copy(const uint8_t* data, size_t len, size_t capacity = 0);

  uint8_t* data() const { return data_.get(); }

  size_t len() const { return len_; }

  void SetLength(size_t length) { len_ = length; }

  size_t capacity() const { return capacity_; }

  Maybe<size_t>& sdp_level() { return sdp_level_; }

  void CopyDataToEncrypted() {
    encrypted_data_ = std::move(data_);
    encrypted_len_ = len_;
    Copy(encrypted_data_.get(), len_);
  }

  const uint8_t* encrypted_data() const { return encrypted_data_.get(); }

  size_t encrypted_len() const { return encrypted_len_; }

  enum Type { UNCLASSIFIED, SRTP, SRTCP, DTLS, RTP, RTCP, SCTP };

  void Categorize();

  void SetType(Type type) { type_ = type; }

  Type type() const { return type_; }

  void Serialize(IPC::Message* aMsg) const;
  bool Deserialize(const IPC::Message* aMsg, PickleIterator* aIter);

 private:
  UniquePtr<uint8_t[]> data_;
  size_t len_ = 0;
  size_t capacity_ = 0;
  // Encrypted form of the data, if there is one.
  UniquePtr<uint8_t[]> encrypted_data_;
  size_t encrypted_len_ = 0;
  // SDP level that this packet belongs to, if known.
  Maybe<size_t> sdp_level_;
  Type type_ = UNCLASSIFIED;
};
}  // namespace mozilla

namespace IPC {
template <typename>
struct ParamTraits;

template <>
struct ParamTraits<mozilla::MediaPacket> {
  static void Write(Message* aMsg, const mozilla::MediaPacket& aParam) {
    aParam.Serialize(aMsg);
  }

  static bool Read(const Message* aMsg, PickleIterator* aIter,
                   mozilla::MediaPacket* aResult) {
    return aResult->Deserialize(aMsg, aIter);
  }
};
}  // namespace IPC
#endif  // mediapacket_h__