/* * * 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. * */ //#define LOG_NDEBUG 0 #define LOG_TAG "MRM_Arbitrator" #include #include #include #include #include #include "MediaResourceArbitrator.h" using namespace android; MediaResourceArbitrator::MediaResourceArbitrator() : mIsEncoderUnderFullLoad (false), mIsDecoderUnderFullLoad (false) { ALOGV("construct MediaResourceArbitrator"); pthread_mutex_init(&mArbitratorLock, NULL); //InitializeCodecNameTypeMap(); //InitializeResolutionNameTypeMap(); } MediaResourceArbitrator::~MediaResourceArbitrator() {} ArbitratorErrorType MediaResourceArbitrator::Config(const char* configFilePath) { FILE *fp = NULL; fp = ::fopen(configFilePath, "r"); if (fp == NULL) { ALOGV("%s: can not open config xml file.\ try to set up default codec limitation"); SetupDefaultCodecLimitation(); return ArbitratorErrorNone; } ParseXMLFile(fp); return ArbitratorErrorNone; } bool MediaResourceArbitrator::CheckIfFullLoad(bool isEncoder) { if (isEncoder) { return mIsEncoderUnderFullLoad; } else { return mIsDecoderUnderFullLoad; } } ArbitratorErrorType MediaResourceArbitrator::AddResource( /* in */ CodecType codecType, /* in */ bool isEncoder, /* in */ bool isSecured, /* in */ ResolutionType resolution, /* in */ uint frameRate) { ALOGV("MediaResourceArbitrator::AddResource ++"); pthread_mutex_lock(&mArbitratorLock); ArbitratorErrorType err = ArbitratorErrorNone; if (CheckIfFullLoad(isEncoder) == true) { pthread_mutex_unlock(&mArbitratorLock); return ArbitratorErrorInsufficientResources; } CodecInfo resource; resource.codecType = codecType; resource.isEncoder = isEncoder; resource.isSecured = isSecured; resource.resolution = resolution; resource.frameRate = frameRate; ALOGV("Adding resource: codecType = %d, isEncoder = %d, isSecured = %d, resolution = %d, frameRate = %d", codecType, isEncoder, isSecured, resolution, frameRate); if (isEncoder) { mLivingEncodersTable.livingEncoders.push_back(resource); if (resolution > mLivingEncodersTable.maxResolution) { mLivingEncodersTable.maxResolution = resolution; } if (frameRate > mLivingEncodersTable.maxFrameRate) { mLivingEncodersTable.maxFrameRate = frameRate; } } else { // decoder mLivingDecodersTable.livingDecoders.push_back(resource); if (resolution > mLivingDecodersTable.maxResolution) { mLivingDecodersTable.maxResolution = resolution; } if (frameRate > mLivingDecodersTable.maxFrameRate) { mLivingDecodersTable.maxFrameRate = frameRate; } } err = ArbitrateFullLoad(resource); pthread_mutex_unlock(&mArbitratorLock); ALOGV("AddResource --"); return err; } uint MediaResourceArbitrator::GetLivingCodecsNum(void) { return mLivingDecodersTable.livingDecoders.size() + mLivingEncodersTable.livingEncoders.size(); } ArbitratorErrorType MediaResourceArbitrator::RemoveResource( CodecType codecType, bool isEncoder, bool isSecured, ResolutionType resolution, uint frameRate) { ALOGV("MediaResourceArbitrator::RemoveResource"); uint i; ArbitratorErrorType err = ArbitratorErrorNone; pthread_mutex_lock(&mArbitratorLock); if (isEncoder) { for(i=0; i mDecoderLimitInfos[j].instanceLimit) { targetInstanceLimit = mDecoderLimitInfos[j].instanceLimit; break; } } } } ALOGV("Go through decoder limit table and get current instance limit = %d", targetInstanceLimit); if (livingInstanceNum >= targetInstanceLimit) { ALOGV("setting full load flag to true."); mIsDecoderUnderFullLoad = true; } else { ALOGV("setting full load flag to false."); mIsDecoderUnderFullLoad = false; } } else { // encoder for(i=0; i mEncoderLimitInfos[j].instanceLimit) { targetInstanceLimit = mEncoderLimitInfos[j].instanceLimit; break; } } } } ALOGV("Go through encoder limit table and get current instance limit = %d", targetInstanceLimit); if (livingInstanceNum >= targetInstanceLimit) { ALOGV("setting full load flag to true."); mIsEncoderUnderFullLoad = true; } else { ALOGV("setting full load flag to false."); mIsEncoderUnderFullLoad = false; } } return err; } bool MediaResourceArbitrator::CheckCodecMatched( const CodecInfo& sourceCodec, const CodecInfo& targetCodec) { ALOGV("CheckCodecMatched"); return ((sourceCodec.codecType == targetCodec.codecType) && (sourceCodec.isSecured == targetCodec.isSecured) && (sourceCodec.resolution == targetCodec.resolution) && (sourceCodec.frameRate == targetCodec.frameRate)); } void MediaResourceArbitrator::DumpCodecTypeFromVector(void) { unsigned int i; ALOGV("MediaResourceArbitrator::DumpCodecTypeFromVector"); for (i=0; igetConfigData(name, atts); } // End tag void MediaResourceArbitrator::endElement(void *userData, const char *name) { MediaResourceArbitrator* arbitrator = (MediaResourceArbitrator*)userData; if (strcmp(name, "Codec") == 0) { if (arbitrator->mParsingCodecLimitInfo.codecInfo.isEncoder == true) { arbitrator->mEncoderLimitInfos.push_back(arbitrator->mParsingCodecLimitInfo); } else { arbitrator->mDecoderLimitInfos.push_back(arbitrator->mParsingCodecLimitInfo); } arbitrator->mIfParsingCodec = false; } }