OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "base/logging.h" |
| 6 #include "base/strings/string_number_conversions.h" |
| 7 #include "media/base/video_frame_metadata.h" |
| 8 |
| 9 namespace media { |
| 10 |
| 11 namespace { |
| 12 |
| 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)); |
| 17 } |
| 18 |
| 19 } // namespace |
| 20 |
| 21 VideoFrameMetadata::VideoFrameMetadata() {} |
| 22 |
| 23 VideoFrameMetadata::~VideoFrameMetadata() {} |
| 24 |
| 25 bool VideoFrameMetadata::HasKey(Key key) const { |
| 26 return dictionary_.HasKey(ToInternalKey(key)); |
| 27 } |
| 28 |
| 29 void VideoFrameMetadata::SetBoolean(Key key, bool value) { |
| 30 dictionary_.SetBooleanWithoutPathExpansion(ToInternalKey(key), value); |
| 31 } |
| 32 |
| 33 void VideoFrameMetadata::SetInteger(Key key, int value) { |
| 34 dictionary_.SetIntegerWithoutPathExpansion(ToInternalKey(key), value); |
| 35 } |
| 36 |
| 37 void VideoFrameMetadata::SetDouble(Key key, double value) { |
| 38 dictionary_.SetDoubleWithoutPathExpansion(ToInternalKey(key), value); |
| 39 } |
| 40 |
| 41 void VideoFrameMetadata::SetString(Key key, const std::string& value) { |
| 42 dictionary_.SetWithoutPathExpansion( |
| 43 ToInternalKey(key), |
| 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())); |
| 48 } |
| 49 |
| 50 void VideoFrameMetadata::SetTimeTicks(Key key, const base::TimeTicks& value) { |
| 51 const int64 internal_value = value.ToInternalValue(); |
| 52 dictionary_.SetWithoutPathExpansion( |
| 53 ToInternalKey(key), |
| 54 base::BinaryValue::CreateWithCopiedBuffer( |
| 55 reinterpret_cast<const char*>(&internal_value), |
| 56 sizeof(internal_value))); |
| 57 } |
| 58 |
| 59 void VideoFrameMetadata::SetValue(Key key, scoped_ptr<base::Value> value) { |
| 60 dictionary_.SetWithoutPathExpansion(ToInternalKey(key), value.Pass()); |
| 61 } |
| 62 |
| 63 bool VideoFrameMetadata::GetBoolean(Key key, bool* value) const { |
| 64 DCHECK(value); |
| 65 return dictionary_.GetBooleanWithoutPathExpansion(ToInternalKey(key), value); |
| 66 } |
| 67 |
| 68 bool VideoFrameMetadata::GetInteger(Key key, int* value) const { |
| 69 DCHECK(value); |
| 70 return dictionary_.GetIntegerWithoutPathExpansion(ToInternalKey(key), value); |
| 71 } |
| 72 |
| 73 bool VideoFrameMetadata::GetDouble(Key key, double* value) const { |
| 74 DCHECK(value); |
| 75 return dictionary_.GetDoubleWithoutPathExpansion(ToInternalKey(key), value); |
| 76 } |
| 77 |
| 78 bool VideoFrameMetadata::GetString(Key key, std::string* value) const { |
| 79 DCHECK(value); |
| 80 const base::BinaryValue* const binary_value = GetBinaryValue(key); |
| 81 if (binary_value) |
| 82 value->assign(binary_value->GetBuffer(), binary_value->GetSize()); |
| 83 return !!binary_value; |
| 84 } |
| 85 |
| 86 bool VideoFrameMetadata::GetTimeTicks(Key key, base::TimeTicks* value) const { |
| 87 DCHECK(value); |
| 88 const base::BinaryValue* const binary_value = GetBinaryValue(key); |
| 89 if (binary_value && binary_value->GetSize() == sizeof(int64)) { |
| 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 } |
| 97 |
| 98 const base::Value* VideoFrameMetadata::GetValue(Key key) const { |
| 99 const base::Value* result = nullptr; |
| 100 if (!dictionary_.GetWithoutPathExpansion(ToInternalKey(key), &result)) |
| 101 return nullptr; |
| 102 return result; |
| 103 } |
| 104 |
| 105 void VideoFrameMetadata::MergeInternalValuesInto( |
| 106 base::DictionaryValue* out) const { |
| 107 out->MergeDictionary(&dictionary_); |
| 108 } |
| 109 |
| 110 void VideoFrameMetadata::MergeInternalValuesFrom( |
| 111 const base::DictionaryValue& in) { |
| 112 dictionary_.MergeDictionary(&in); |
| 113 } |
| 114 |
| 115 const base::BinaryValue* VideoFrameMetadata::GetBinaryValue(Key key) const { |
| 116 const base::Value* internal_value = nullptr; |
| 117 if (dictionary_.GetWithoutPathExpansion(ToInternalKey(key), |
| 118 &internal_value) && |
| 119 internal_value->GetType() == base::Value::TYPE_BINARY) { |
| 120 return static_cast<const base::BinaryValue*>(internal_value); |
| 121 } |
| 122 return nullptr; |
| 123 } |
| 124 |
| 125 } // namespace media |
OLD | NEW |