1 // Copyright 2015 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 "base/logging.h"
6 #include "base/strings/string_number_conversions.h"
7 #include "media/base/video_frame_metadata.h"
13 // Map enum key to internal std::string key used by base::DictionaryValue.
14 inline std::string
ToInternalKey(VideoFrameMetadata::Key key
) {
15 DCHECK_LT(key
, VideoFrameMetadata::NUM_KEYS
);
16 return base::IntToString(static_cast<int>(key
));
21 VideoFrameMetadata::VideoFrameMetadata() {}
23 VideoFrameMetadata::~VideoFrameMetadata() {}
25 bool VideoFrameMetadata::HasKey(Key key
) const {
26 return dictionary_
.HasKey(ToInternalKey(key
));
29 void VideoFrameMetadata::SetBoolean(Key key
, bool value
) {
30 dictionary_
.SetBooleanWithoutPathExpansion(ToInternalKey(key
), value
);
33 void VideoFrameMetadata::SetInteger(Key key
, int value
) {
34 dictionary_
.SetIntegerWithoutPathExpansion(ToInternalKey(key
), value
);
37 void VideoFrameMetadata::SetDouble(Key key
, double value
) {
38 dictionary_
.SetDoubleWithoutPathExpansion(ToInternalKey(key
), value
);
41 void VideoFrameMetadata::SetString(Key key
, const std::string
& value
) {
42 dictionary_
.SetWithoutPathExpansion(
44 // Using BinaryValue since we don't want the |value| interpreted as having
45 // any particular character encoding (e.g., UTF-8) by
46 // base::DictionaryValue.
47 base::BinaryValue::CreateWithCopiedBuffer(value
.data(), value
.size()));
51 template<class TimeType
>
52 void SetTimeValue(VideoFrameMetadata::Key key
,
53 const TimeType
& value
,
54 base::DictionaryValue
* dictionary
) {
55 const int64 internal_value
= value
.ToInternalValue();
56 dictionary
->SetWithoutPathExpansion(
58 base::BinaryValue::CreateWithCopiedBuffer(
59 reinterpret_cast<const char*>(&internal_value
),
60 sizeof(internal_value
)));
64 void VideoFrameMetadata::SetTimeDelta(Key key
, const base::TimeDelta
& value
) {
65 SetTimeValue(key
, value
, &dictionary_
);
68 void VideoFrameMetadata::SetTimeTicks(Key key
, const base::TimeTicks
& value
) {
69 SetTimeValue(key
, value
, &dictionary_
);
72 void VideoFrameMetadata::SetValue(Key key
, scoped_ptr
<base::Value
> value
) {
73 dictionary_
.SetWithoutPathExpansion(ToInternalKey(key
), value
.Pass());
76 bool VideoFrameMetadata::GetBoolean(Key key
, bool* value
) const {
78 return dictionary_
.GetBooleanWithoutPathExpansion(ToInternalKey(key
), value
);
81 bool VideoFrameMetadata::GetInteger(Key key
, int* value
) const {
83 return dictionary_
.GetIntegerWithoutPathExpansion(ToInternalKey(key
), value
);
86 bool VideoFrameMetadata::GetDouble(Key key
, double* value
) const {
88 return dictionary_
.GetDoubleWithoutPathExpansion(ToInternalKey(key
), value
);
91 bool VideoFrameMetadata::GetString(Key key
, std::string
* value
) const {
93 const base::BinaryValue
* const binary_value
= GetBinaryValue(key
);
95 value
->assign(binary_value
->GetBuffer(), binary_value
->GetSize());
96 return !!binary_value
;
100 template<class TimeType
>
101 bool ToTimeValue(const base::BinaryValue
& binary_value
, TimeType
* value
) {
103 int64 internal_value
;
104 if (binary_value
.GetSize() != sizeof(internal_value
))
106 memcpy(&internal_value
, binary_value
.GetBuffer(), sizeof(internal_value
));
107 *value
= TimeType::FromInternalValue(internal_value
);
112 bool VideoFrameMetadata::GetTimeDelta(Key key
, base::TimeDelta
* value
) const {
113 const base::BinaryValue
* const binary_value
= GetBinaryValue(key
);
114 return binary_value
&& ToTimeValue(*binary_value
, value
);
117 bool VideoFrameMetadata::GetTimeTicks(Key key
, base::TimeTicks
* value
) const {
118 const base::BinaryValue
* const binary_value
= GetBinaryValue(key
);
119 return binary_value
&& ToTimeValue(*binary_value
, value
);
122 const base::Value
* VideoFrameMetadata::GetValue(Key key
) const {
123 const base::Value
* result
= nullptr;
124 if (!dictionary_
.GetWithoutPathExpansion(ToInternalKey(key
), &result
))
129 bool VideoFrameMetadata::IsTrue(Key key
) const {
131 return GetBoolean(key
, &value
) && value
;
134 void VideoFrameMetadata::MergeInternalValuesInto(
135 base::DictionaryValue
* out
) const {
136 out
->MergeDictionary(&dictionary_
);
139 void VideoFrameMetadata::MergeInternalValuesFrom(
140 const base::DictionaryValue
& in
) {
141 dictionary_
.MergeDictionary(&in
);
144 const base::BinaryValue
* VideoFrameMetadata::GetBinaryValue(Key key
) const {
145 const base::Value
* internal_value
= nullptr;
146 if (dictionary_
.GetWithoutPathExpansion(ToInternalKey(key
),
148 internal_value
->GetType() == base::Value::TYPE_BINARY
) {
149 return static_cast<const base::BinaryValue
*>(internal_value
);