1 /*
2  * Copyright 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "MediaResource"
19 #include <utils/Log.h>
20 #include <media/MediaResource.h>
21 
22 #include <vector>
23 
24 namespace android {
25 
MediaResource(Type type,int64_t value)26 MediaResource::MediaResource(Type type, int64_t value) {
27     this->type = type;
28     this->subType = SubType::kUnspecifiedSubType;
29     this->value = value;
30 }
31 
MediaResource(Type type,SubType subType,int64_t value)32 MediaResource::MediaResource(Type type, SubType subType, int64_t value) {
33     this->type = type;
34     this->subType = subType;
35     this->value = value;
36 }
37 
MediaResource(Type type,const std::vector<uint8_t> & id,int64_t value)38 MediaResource::MediaResource(Type type, const std::vector<uint8_t> &id, int64_t value) {
39     this->type = type;
40     this->subType = SubType::kUnspecifiedSubType;
41     this->id = id;
42     this->value = value;
43 }
44 
45 //static
CodecResource(bool secure,SubType subType,int64_t instanceCount)46 MediaResource MediaResource::CodecResource(bool secure, SubType subType, int64_t instanceCount) {
47     return MediaResource(
48             secure ? Type::kSecureCodec : Type::kNonSecureCodec,
49             subType,
50             instanceCount);
51 }
52 
53 //static
GraphicMemoryResource(int64_t value)54 MediaResource MediaResource::GraphicMemoryResource(int64_t value) {
55     return MediaResource(Type::kGraphicMemory, value);
56 }
57 
58 //static
CpuBoostResource()59 MediaResource MediaResource::CpuBoostResource() {
60     return MediaResource(Type::kCpuBoost, 1);
61 }
62 
63 //static
VideoBatteryResource(bool isHardware)64 MediaResource MediaResource::VideoBatteryResource(bool isHardware) {
65     SubType subType = isHardware ? SubType::kHwVideoCodec : SubType::kSwVideoCodec;
66     return MediaResource(Type::kBattery, subType, 1);
67 }
68 
69 //static
DrmSessionResource(const std::vector<uint8_t> & id,int64_t value)70 MediaResource MediaResource::DrmSessionResource(const std::vector<uint8_t> &id, int64_t value) {
71     return MediaResource(Type::kDrmSession, id, value);
72 }
73 
bytesToHexString(const std::vector<uint8_t> & bytes)74 static String8 bytesToHexString(const std::vector<uint8_t> &bytes) {
75     String8 str;
76     for (auto &b : bytes) {
77         str.appendFormat("%02x", b);
78     }
79     return str;
80 }
81 
toString(const MediaResourceParcel & resource)82 String8 toString(const MediaResourceParcel& resource) {
83     String8 str;
84 
85     str.appendFormat("%s/%s:[%s]:%lld",
86             asString(resource.type), asString(resource.subType),
87             bytesToHexString(resource.id).c_str(),
88             (long long)resource.value);
89     return str;
90 }
91 
92 }; // namespace android
93