Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / media / cast / test / utility / barcode.h
blob6dbd08596b7dc01cb28b6714e3bdf680ac2b2588
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.
5 #ifndef MEDIA_CAST_TEST_UTILITY_BARCODE_H_
6 #define MEDIA_CAST_TEST_UTILITY_BARCODE_H_
8 #include <vector>
10 #include "base/memory/ref_counted.h"
12 namespace media {
13 class VideoFrame;
15 namespace cast {
16 namespace test {
17 // Encode a resilient barcode into |frame| containing all the bits
18 // from |bits|.
19 bool 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.
24 bool DecodeBarcode(const scoped_refptr<media::VideoFrame>& frame,
25 std::vector<bool>* output);
27 // Convenience templates that allows you to encode/decode numeric
28 // types directly.
29 template<class T>
30 bool 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;
35 return EncodeBarcode(bits, output_frame);
38 template<class T>
39 bool 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;
49 return true;
52 } // namespace test
53 } // namespace cast
54 } // namespace media
56 #endif // MEDIA_CAST_TEST_UTILITY_BARCODE_H_