OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/logging.h" | 5 #include "base/logging.h" |
6 #include "base/strings/string_number_conversions.h" | 6 #include "base/strings/string_number_conversions.h" |
7 #include "media/base/video_frame_metadata.h" | 7 #include "media/base/video_frame_metadata.h" |
8 | 8 |
9 namespace media { | 9 namespace media { |
10 | 10 |
(...skipping 29 matching lines...) Expand all Loading... |
40 | 40 |
41 void VideoFrameMetadata::SetString(Key key, const std::string& value) { | 41 void VideoFrameMetadata::SetString(Key key, const std::string& value) { |
42 dictionary_.SetWithoutPathExpansion( | 42 dictionary_.SetWithoutPathExpansion( |
43 ToInternalKey(key), | 43 ToInternalKey(key), |
44 // Using BinaryValue since we don't want the |value| interpreted as having | 44 // Using BinaryValue since we don't want the |value| interpreted as having |
45 // any particular character encoding (e.g., UTF-8) by | 45 // any particular character encoding (e.g., UTF-8) by |
46 // base::DictionaryValue. | 46 // base::DictionaryValue. |
47 base::BinaryValue::CreateWithCopiedBuffer(value.data(), value.size())); | 47 base::BinaryValue::CreateWithCopiedBuffer(value.data(), value.size())); |
48 } | 48 } |
49 | 49 |
50 void VideoFrameMetadata::SetTimeTicks(Key key, const base::TimeTicks& value) { | 50 namespace { |
| 51 template<class TimeType> |
| 52 void SetTimeValue(VideoFrameMetadata::Key key, |
| 53 const TimeType& value, |
| 54 base::DictionaryValue* dictionary) { |
51 const int64 internal_value = value.ToInternalValue(); | 55 const int64 internal_value = value.ToInternalValue(); |
52 dictionary_.SetWithoutPathExpansion( | 56 dictionary->SetWithoutPathExpansion( |
53 ToInternalKey(key), | 57 ToInternalKey(key), |
54 base::BinaryValue::CreateWithCopiedBuffer( | 58 base::BinaryValue::CreateWithCopiedBuffer( |
55 reinterpret_cast<const char*>(&internal_value), | 59 reinterpret_cast<const char*>(&internal_value), |
56 sizeof(internal_value))); | 60 sizeof(internal_value))); |
57 } | 61 } |
| 62 } // namespace |
| 63 |
| 64 void VideoFrameMetadata::SetTimeDelta(Key key, const base::TimeDelta& value) { |
| 65 SetTimeValue(key, value, &dictionary_); |
| 66 } |
| 67 |
| 68 void VideoFrameMetadata::SetTimeTicks(Key key, const base::TimeTicks& value) { |
| 69 SetTimeValue(key, value, &dictionary_); |
| 70 } |
58 | 71 |
59 void VideoFrameMetadata::SetValue(Key key, scoped_ptr<base::Value> value) { | 72 void VideoFrameMetadata::SetValue(Key key, scoped_ptr<base::Value> value) { |
60 dictionary_.SetWithoutPathExpansion(ToInternalKey(key), value.Pass()); | 73 dictionary_.SetWithoutPathExpansion(ToInternalKey(key), value.Pass()); |
61 } | 74 } |
62 | 75 |
63 bool VideoFrameMetadata::GetBoolean(Key key, bool* value) const { | 76 bool VideoFrameMetadata::GetBoolean(Key key, bool* value) const { |
64 DCHECK(value); | 77 DCHECK(value); |
65 return dictionary_.GetBooleanWithoutPathExpansion(ToInternalKey(key), value); | 78 return dictionary_.GetBooleanWithoutPathExpansion(ToInternalKey(key), value); |
66 } | 79 } |
67 | 80 |
68 bool VideoFrameMetadata::GetInteger(Key key, int* value) const { | 81 bool VideoFrameMetadata::GetInteger(Key key, int* value) const { |
69 DCHECK(value); | 82 DCHECK(value); |
70 return dictionary_.GetIntegerWithoutPathExpansion(ToInternalKey(key), value); | 83 return dictionary_.GetIntegerWithoutPathExpansion(ToInternalKey(key), value); |
71 } | 84 } |
72 | 85 |
73 bool VideoFrameMetadata::GetDouble(Key key, double* value) const { | 86 bool VideoFrameMetadata::GetDouble(Key key, double* value) const { |
74 DCHECK(value); | 87 DCHECK(value); |
75 return dictionary_.GetDoubleWithoutPathExpansion(ToInternalKey(key), value); | 88 return dictionary_.GetDoubleWithoutPathExpansion(ToInternalKey(key), value); |
76 } | 89 } |
77 | 90 |
78 bool VideoFrameMetadata::GetString(Key key, std::string* value) const { | 91 bool VideoFrameMetadata::GetString(Key key, std::string* value) const { |
79 DCHECK(value); | 92 DCHECK(value); |
80 const base::BinaryValue* const binary_value = GetBinaryValue(key); | 93 const base::BinaryValue* const binary_value = GetBinaryValue(key); |
81 if (binary_value) | 94 if (binary_value) |
82 value->assign(binary_value->GetBuffer(), binary_value->GetSize()); | 95 value->assign(binary_value->GetBuffer(), binary_value->GetSize()); |
83 return !!binary_value; | 96 return !!binary_value; |
84 } | 97 } |
85 | 98 |
| 99 namespace { |
| 100 template<class TimeType> |
| 101 bool ToTimeValue(const base::BinaryValue& binary_value, TimeType* value) { |
| 102 DCHECK(value); |
| 103 int64 internal_value; |
| 104 if (binary_value.GetSize() != sizeof(internal_value)) |
| 105 return false; |
| 106 memcpy(&internal_value, binary_value.GetBuffer(), sizeof(internal_value)); |
| 107 *value = TimeType::FromInternalValue(internal_value); |
| 108 return true; |
| 109 } |
| 110 } // namespace |
| 111 |
| 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); |
| 115 } |
| 116 |
86 bool VideoFrameMetadata::GetTimeTicks(Key key, base::TimeTicks* value) const { | 117 bool VideoFrameMetadata::GetTimeTicks(Key key, base::TimeTicks* value) const { |
87 DCHECK(value); | |
88 const base::BinaryValue* const binary_value = GetBinaryValue(key); | 118 const base::BinaryValue* const binary_value = GetBinaryValue(key); |
89 if (binary_value && binary_value->GetSize() == sizeof(int64)) { | 119 return binary_value && ToTimeValue(*binary_value, value); |
90 int64 internal_value; | |
91 memcpy(&internal_value, binary_value->GetBuffer(), sizeof(internal_value)); | |
92 *value = base::TimeTicks::FromInternalValue(internal_value); | |
93 return true; | |
94 } | |
95 return false; | |
96 } | 120 } |
97 | 121 |
98 const base::Value* VideoFrameMetadata::GetValue(Key key) const { | 122 const base::Value* VideoFrameMetadata::GetValue(Key key) const { |
99 const base::Value* result = nullptr; | 123 const base::Value* result = nullptr; |
100 if (!dictionary_.GetWithoutPathExpansion(ToInternalKey(key), &result)) | 124 if (!dictionary_.GetWithoutPathExpansion(ToInternalKey(key), &result)) |
101 return nullptr; | 125 return nullptr; |
102 return result; | 126 return result; |
103 } | 127 } |
104 | 128 |
105 void VideoFrameMetadata::MergeInternalValuesInto( | 129 void VideoFrameMetadata::MergeInternalValuesInto( |
(...skipping 10 matching lines...) Expand all Loading... |
116 const base::Value* internal_value = nullptr; | 140 const base::Value* internal_value = nullptr; |
117 if (dictionary_.GetWithoutPathExpansion(ToInternalKey(key), | 141 if (dictionary_.GetWithoutPathExpansion(ToInternalKey(key), |
118 &internal_value) && | 142 &internal_value) && |
119 internal_value->GetType() == base::Value::TYPE_BINARY) { | 143 internal_value->GetType() == base::Value::TYPE_BINARY) { |
120 return static_cast<const base::BinaryValue*>(internal_value); | 144 return static_cast<const base::BinaryValue*>(internal_value); |
121 } | 145 } |
122 return nullptr; | 146 return nullptr; |
123 } | 147 } |
124 | 148 |
125 } // namespace media | 149 } // namespace media |
OLD | NEW |