summaryrefslogtreecommitdiff
path: root/media_resource_manager/arbitrator/MediaResourceArbitrator.h
blob: abb7b1a80cc50ba6223217829925a446aa899b72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* Copyright (c) 2015 Intel Corporation.  All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


#ifndef MEDIA_RESOURCE_ARBITRATOR_H_
#define MEDIA_RESOURCE_ARBITRATOR_H_

#include <unistd.h>
#include <string.h>
#include <utils/KeyedVector.h>
#include <utils/Vector.h>

#define MAX_BUFFER_SIZE (20 * 1024)

using namespace android;

// This error type keeps align with the OMX error type
typedef enum _ArbitratorErrorType {
    ArbitratorErrorNone = 0,

    /** There were insufficient resources to perform the requested operation */
    ArbitratorErrorInsufficientResources = 0x80001000,

    /** There was an error, but the cause of the error could not be determined */
    ArbitratorErrorUndefined = 0x80001001
} ArbitratorErrorType;


typedef enum _ResolutionType {
    Resolution_CIF = 0,
    Resolution_480,
    Resolution_720,
    Resolution_1080,
    Resolution_2K,
    Resolution_4K,
    Resolution_MAX
} ResolutionType;


typedef enum _CodecType {
    CODEC_TYPE_AVC = 0,
    CODEC_TYPE_HEVC,
    CODEC_TYPE_VP8,
    CODEC_TYPE_VP9,
    CODEC_TYPE_MPEG4,
    CODEC_TYPE_MPEG2,
    CODEC_TYPE_H263,
    CODEC_TYPE_VC1,
    CODEC_TYPE_WMV,
    CODEC_TYPE_MAX
} CodecType;


typedef struct _CodecInfo {
    CodecType codecType;
    bool isEncoder;
    bool isSecured;
    ResolutionType resolution;
    uint frameRate;
} CodecInfo;


typedef struct _CodecLimitInfo {
    CodecInfo codecInfo;
    int instanceLimit;
} CodecLimitInfo;


typedef struct _LivingDecodersTable {
    Vector<CodecInfo> livingDecoders;
    uint maxResolution;
    uint maxFrameRate;
} LivingDecodersTable;


typedef struct _LivingEncodersTable {
    Vector<CodecInfo> livingEncoders;
    uint maxResolution;
    uint maxFrameRate;
} LivingEncodersTable;


class MediaResourceArbitrator {
public:
    MediaResourceArbitrator ();
    ~MediaResourceArbitrator ();

    /* Initialize the arbitrator.
       Parse the config XML file if given. */
    ArbitratorErrorType Config(const char* configFilePath);

    /* Check if the resource limitation is hit and
       it is under full load status. In such status, there
       is no room to instantiate codec anymore. */
    bool CheckIfFullLoad(bool isEncoder);

    /* Add codec in the pool.
       Resolution and frame rate must be provided.
       This is not necessarily be called when codec instance
       is constructed when the resolution and frame rate are
       not known yet.
       This may be called when codec is configured,
       for example in OMX set parameter, etc.
       Return value is expected to be as one of:
           ArbitratorErrorNone,
           ArbitratorErrorInsufficientResources
     */
    ArbitratorErrorType AddResource(/* in */  CodecType codecType,
                                    /* in */  bool isEncoder,
                                    /* in */  bool isSecured,
                                    /* in */  ResolutionType resolution,
                                    /* in */  uint frameRate);

    /* Remove codec in the pool.*/
    ArbitratorErrorType RemoveResource(CodecType codecType,
                                       bool isEncoder,
                                       bool isSecured,
                                       ResolutionType resolution,
                                       uint frameRate);

    uint GetLivingCodecsNum(void);

    // XML function
    void ParseXMLFile(FILE* fp);
    static void startElement(void *userData, const char *name, const char **atts);
    static void endElement(void *userData, const char *name);
    void getConfigData(const char *name, const char **atts);

private:
    // a global table stores all codec limit info
    Vector<CodecLimitInfo> mDecoderLimitInfos;
    Vector<CodecLimitInfo> mEncoderLimitInfos;

    // a global talbe stores all living codec info
    LivingDecodersTable mLivingDecodersTable;
    LivingEncodersTable mLivingEncodersTable;

    // arbitrator lock
    pthread_mutex_t mArbitratorLock;

    // indicate whether it is under full load status
    bool mIsEncoderUnderFullLoad;
    bool mIsDecoderUnderFullLoad;

    KeyedVector <const char*, CodecType> mCodecNameTypeMap;
    KeyedVector <const char*, ResolutionType> mResolutionNameTypeMap;

    static const int mBufSize = MAX_BUFFER_SIZE;
    // indicate XML parser is parsing a codec tag
    bool mIfParsingCodec;
    CodecLimitInfo mParsingCodecLimitInfo;

    ArbitratorErrorType ArbitrateFullLoad(CodecInfo& codec);

    bool CheckCodecMatched(const CodecInfo& sourceCodec,
                           const CodecInfo& targetCodec);

    void SetupDefaultCodecLimitation(void);
    void InitializeCodecNameTypeMap();
    void InitializeResolutionNameTypeMap();
    void DumpCodecTypeFromVector(void);
    CodecType MapCodecTypeFromName(const char* name);
    ResolutionType MapResolutionTypeFromName(const char* name);
};

#endif /* MEDIA_RESOURCE_ARBITRATOR_H_ */