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/video_decoder_config_marshaller.h"
7 #include "base/basictypes.h"
8 #include "base/logging.h"
9 #include "chromecast/media/cma/ipc/media_message.h"
10 #include "media/base/video_decoder_config.h"
11 #include "ui/gfx/geometry/rect.h"
12 #include "ui/gfx/geometry/size.h"
14 namespace chromecast
{
18 const size_t kMaxExtraDataSize
= 16 * 1024;
20 class SizeMarshaller
{
22 static void Write(const gfx::Size
& size
, MediaMessage
* msg
) {
23 CHECK(msg
->WritePod(size
.width()));
24 CHECK(msg
->WritePod(size
.height()));
27 static gfx::Size
Read(MediaMessage
* msg
) {
29 CHECK(msg
->ReadPod(&w
));
30 CHECK(msg
->ReadPod(&h
));
31 return gfx::Size(w
, h
);
35 class RectMarshaller
{
37 static void Write(const gfx::Rect
& rect
, MediaMessage
* msg
) {
38 CHECK(msg
->WritePod(rect
.x()));
39 CHECK(msg
->WritePod(rect
.y()));
40 CHECK(msg
->WritePod(rect
.width()));
41 CHECK(msg
->WritePod(rect
.height()));
44 static gfx::Rect
Read(MediaMessage
* msg
) {
46 CHECK(msg
->ReadPod(&x
));
47 CHECK(msg
->ReadPod(&y
));
48 CHECK(msg
->ReadPod(&w
));
49 CHECK(msg
->ReadPod(&h
));
50 return gfx::Rect(x
, y
, w
, h
);
57 void VideoDecoderConfigMarshaller::Write(
58 const ::media::VideoDecoderConfig
& config
, MediaMessage
* msg
) {
59 CHECK(msg
->WritePod(config
.codec()));
60 CHECK(msg
->WritePod(config
.profile()));
61 CHECK(msg
->WritePod(config
.format()));
62 CHECK(msg
->WritePod(config
.color_space()));
63 SizeMarshaller::Write(config
.coded_size(), msg
);
64 RectMarshaller::Write(config
.visible_rect(), msg
);
65 SizeMarshaller::Write(config
.natural_size(), msg
);
66 CHECK(msg
->WritePod(config
.is_encrypted()));
67 CHECK(msg
->WritePod(config
.extra_data_size()));
68 if (config
.extra_data_size() > 0)
69 CHECK(msg
->WriteBuffer(config
.extra_data(), config
.extra_data_size()));
73 ::media::VideoDecoderConfig
VideoDecoderConfigMarshaller::Read(
75 ::media::VideoCodec codec
;
76 ::media::VideoCodecProfile profile
;
77 ::media::VideoPixelFormat format
;
78 ::media::ColorSpace color_space
;
80 gfx::Rect visible_rect
;
81 gfx::Size natural_size
;
83 size_t extra_data_size
;
84 scoped_ptr
<uint8
[]> extra_data
;
86 CHECK(msg
->ReadPod(&codec
));
87 CHECK(msg
->ReadPod(&profile
));
88 CHECK(msg
->ReadPod(&format
));
89 CHECK(msg
->ReadPod(&color_space
));
90 coded_size
= SizeMarshaller::Read(msg
);
91 visible_rect
= RectMarshaller::Read(msg
);
92 natural_size
= SizeMarshaller::Read(msg
);
93 CHECK(msg
->ReadPod(&is_encrypted
));
94 CHECK(msg
->ReadPod(&extra_data_size
));
96 CHECK_GE(codec
, ::media::kUnknownVideoCodec
);
97 CHECK_LE(codec
, ::media::kVideoCodecMax
);
98 CHECK_GE(profile
, ::media::VIDEO_CODEC_PROFILE_UNKNOWN
);
99 CHECK_LE(profile
, ::media::VIDEO_CODEC_PROFILE_MAX
);
100 CHECK_GE(format
, ::media::PIXEL_FORMAT_UNKNOWN
);
101 CHECK_LE(format
, ::media::PIXEL_FORMAT_MAX
);
102 CHECK_GE(color_space
, ::media::COLOR_SPACE_UNSPECIFIED
);
103 CHECK_LE(color_space
, ::media::COLOR_SPACE_MAX
);
104 CHECK_LT(extra_data_size
, kMaxExtraDataSize
);
105 if (extra_data_size
> 0) {
106 extra_data
.reset(new uint8
[extra_data_size
]);
107 CHECK(msg
->ReadBuffer(extra_data
.get(), extra_data_size
));
110 return ::media::VideoDecoderConfig(
111 codec
, profile
, format
, color_space
,
112 coded_size
, visible_rect
, natural_size
,
113 extra_data
.get(), extra_data_size
,
118 } // namespace chromecast