1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef MEDIA_CAST_TEST_UTILITY_BARCODE_H_
6#define MEDIA_CAST_TEST_UTILITY_BARCODE_H_
7
8#include <vector>
9
10#include "base/memory/ref_counted.h"
11
12namespace media {
13class VideoFrame;
14
15namespace cast {
16namespace test {
17// Encode a resilient barcode into |frame| containing all the bits
18// from |bits|.
19bool EncodeBarcode(const std::vector<bool>& bits,
20                   scoped_refptr<media::VideoFrame> output_frame);
21// Decode a barcode (encoded by EncodeBarCode) into |output|.
22// |output| should already be sized to contain the right number
23// of bits.
24bool DecodeBarcode(const scoped_refptr<media::VideoFrame>& frame,
25                   std::vector<bool>* output);
26
27// Convenience templates that allows you to encode/decode numeric
28// types directly.
29template<class T>
30bool EncodeBarcode(T data, scoped_refptr<media::VideoFrame> output_frame) {
31  std::vector<bool> bits(sizeof(T) * 8);
32  for (size_t i = 0; i < bits.size(); i++) {
33    bits[i] = ((data >> i) & 1) == 1;
34  }
35  return EncodeBarcode(bits, output_frame);
36}
37
38template<class T>
39bool DecodeBarcode(scoped_refptr<media::VideoFrame> output_frame, T* data) {
40  std::vector<bool> bits(sizeof(T) * 8);
41  bool ret = DecodeBarcode(output_frame, &bits);
42  if (!ret) return false;
43  *data = 0;
44  for (size_t i = 0; i < bits.size(); i++) {
45    if (bits[i]) {
46      *data |= 1UL << i;
47    }
48  }
49  return true;
50}
51
52}  // namespace test
53}  // namespace cast
54}  // namespace media
55
56#endif  // MEDIA_CAST_TEST_UTILITY_BARCODE_H_
57