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#include "chromecast/media/cma/ipc_streamer/audio_decoder_config_marshaller.h"
6
7#include "base/basictypes.h"
8#include "base/logging.h"
9#include "chromecast/media/cma/ipc/media_message.h"
10#include "media/base/audio_decoder_config.h"
11
12namespace chromecast {
13namespace media {
14
15namespace {
16const size_t kMaxExtraDataSize = 16 * 1024;
17}
18
19// static
20void AudioDecoderConfigMarshaller::Write(
21    const ::media::AudioDecoderConfig& config, MediaMessage* msg) {
22  CHECK(msg->WritePod(config.codec()));
23  CHECK(msg->WritePod(config.channel_layout()));
24  CHECK(msg->WritePod(config.samples_per_second()));
25  CHECK(msg->WritePod(config.sample_format()));
26  CHECK(msg->WritePod(config.is_encrypted()));
27  CHECK(msg->WritePod(config.extra_data_size()));
28  if (config.extra_data_size() > 0)
29    CHECK(msg->WriteBuffer(config.extra_data(), config.extra_data_size()));
30}
31
32// static
33::media::AudioDecoderConfig AudioDecoderConfigMarshaller::Read(
34    MediaMessage* msg) {
35  ::media::AudioCodec codec;
36  ::media::SampleFormat sample_format;
37  ::media::ChannelLayout channel_layout;
38  int samples_per_second;
39  bool is_encrypted;
40  size_t extra_data_size;
41  scoped_ptr<uint8[]> extra_data;
42
43  CHECK(msg->ReadPod(&codec));
44  CHECK(msg->ReadPod(&channel_layout));
45  CHECK(msg->ReadPod(&samples_per_second));
46  CHECK(msg->ReadPod(&sample_format));
47  CHECK(msg->ReadPod(&is_encrypted));
48  CHECK(msg->ReadPod(&extra_data_size));
49
50  CHECK_GE(codec, ::media::kUnknownAudioCodec);
51  CHECK_LE(codec, ::media::kAudioCodecMax);
52  CHECK_GE(channel_layout, ::media::CHANNEL_LAYOUT_NONE);
53  CHECK_LE(channel_layout, ::media::CHANNEL_LAYOUT_MAX);
54  CHECK_GE(sample_format, ::media::kUnknownSampleFormat);
55  CHECK_LE(sample_format, ::media::kSampleFormatMax);
56  CHECK_LT(extra_data_size, kMaxExtraDataSize);
57  if (extra_data_size > 0) {
58    extra_data.reset(new uint8[extra_data_size]);
59    CHECK(msg->ReadBuffer(extra_data.get(), extra_data_size));
60  }
61
62  return ::media::AudioDecoderConfig(
63      codec, sample_format,
64      channel_layout, samples_per_second,
65      extra_data.get(), extra_data_size,
66      is_encrypted);
67}
68
69}  // namespace media
70}  // namespace chromecast
71