Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / chromecast / media / cma / ipc_streamer / decrypt_config_marshaller.cc
blobbc373f6a2bac91b6b5a1658bf9e219595d35b8a8
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 #include "chromecast/media/cma/ipc_streamer/decrypt_config_marshaller.h"
7 #include "base/logging.h"
8 #include "chromecast/media/cma/base/cast_decrypt_config_impl.h"
9 #include "chromecast/media/cma/ipc/media_message.h"
10 #include "chromecast/public/media/cast_decrypt_config.h"
12 namespace chromecast {
13 namespace media {
15 namespace {
16 const size_t kMaxKeyIdSize = 256;
17 const size_t kMaxIvSize = 256;
18 const size_t kMaxSubsampleCount = 1024;
21 // static
22 void DecryptConfigMarshaller::Write(const CastDecryptConfig& config,
23 MediaMessage* msg) {
24 CHECK_GT(config.key_id().size(), 0u);
25 CHECK_GT(config.iv().size(), 0u);
26 CHECK_GT(config.subsamples().size(), 0u);
28 CHECK(msg->WritePod(config.key_id().size()));
29 CHECK(msg->WriteBuffer(config.key_id().data(), config.key_id().size()));
30 CHECK(msg->WritePod(config.iv().size()));
31 CHECK(msg->WriteBuffer(config.iv().data(), config.iv().size()));
32 CHECK(msg->WritePod(config.subsamples().size()));
33 for (size_t k = 0; k < config.subsamples().size(); k++) {
34 CHECK(msg->WritePod(config.subsamples()[k].clear_bytes));
35 CHECK(msg->WritePod(config.subsamples()[k].cypher_bytes));
39 // static
40 scoped_ptr<CastDecryptConfig> DecryptConfigMarshaller::Read(MediaMessage* msg) {
41 size_t key_id_size = 0;
42 CHECK(msg->ReadPod(&key_id_size));
43 CHECK_GT(key_id_size, 0u);
44 CHECK_LT(key_id_size, kMaxKeyIdSize);
45 scoped_ptr<char[]> key_id(new char[key_id_size]);
46 CHECK(msg->ReadBuffer(key_id.get(), key_id_size));
48 size_t iv_size = 0;
49 CHECK(msg->ReadPod(&iv_size));
50 CHECK_GT(iv_size, 0u);
51 CHECK_LT(iv_size, kMaxIvSize);
52 scoped_ptr<char[]> iv(new char[iv_size]);
53 CHECK(msg->ReadBuffer(iv.get(), iv_size));
55 size_t subsample_count = 0;
56 CHECK(msg->ReadPod(&subsample_count));
57 CHECK_GT(subsample_count, 0u);
58 CHECK_LT(subsample_count, kMaxSubsampleCount);
59 std::vector<SubsampleEntry> subsamples(subsample_count);
60 for (size_t k = 0; k < subsample_count; k++) {
61 subsamples[k].clear_bytes = 0;
62 subsamples[k].cypher_bytes = 0;
63 CHECK(msg->ReadPod(&subsamples[k].clear_bytes));
64 CHECK(msg->ReadPod(&subsamples[k].cypher_bytes));
67 return scoped_ptr<CastDecryptConfig>(
68 new CastDecryptConfigImpl(std::string(key_id.get(), key_id_size),
69 std::string(iv.get(), iv_size), subsamples));
72 } // namespace media
73 } // namespace chromecast