Wed, 31 Dec 2014 13:27:57 +0100
Ignore runtime configuration files generated during quality assurance.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim:set ts=2 sw=2 sts=2 et cindent: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 5 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | //#define LOG_NDEBUG 0 |
michael@0 | 8 | #define LOG_TAG "MediaResourceManagerService" |
michael@0 | 9 | |
michael@0 | 10 | #include <binder/IServiceManager.h> |
michael@0 | 11 | #include <media/stagefright/foundation/AMessage.h> |
michael@0 | 12 | #include <utils/Log.h> |
michael@0 | 13 | |
michael@0 | 14 | #include "MediaResourceManagerClient.h" |
michael@0 | 15 | #include "MediaResourceManagerService.h" |
michael@0 | 16 | |
michael@0 | 17 | namespace android { |
michael@0 | 18 | |
michael@0 | 19 | /* static */ |
michael@0 | 20 | void MediaResourceManagerService::instantiate() { |
michael@0 | 21 | defaultServiceManager()->addService( |
michael@0 | 22 | String16("media.resource_manager"), new MediaResourceManagerService()); |
michael@0 | 23 | } |
michael@0 | 24 | |
michael@0 | 25 | MediaResourceManagerService::MediaResourceManagerService() |
michael@0 | 26 | : mVideoDecoderCount(VIDEO_DECODER_COUNT) |
michael@0 | 27 | { |
michael@0 | 28 | mLooper = new ALooper; |
michael@0 | 29 | mLooper->setName("MediaResourceManagerService"); |
michael@0 | 30 | |
michael@0 | 31 | mReflector = new AHandlerReflector<MediaResourceManagerService>(this); |
michael@0 | 32 | // Register AMessage handler to ALooper. |
michael@0 | 33 | mLooper->registerHandler(mReflector); |
michael@0 | 34 | // Start ALooper thread. |
michael@0 | 35 | mLooper->start(); |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | MediaResourceManagerService::~MediaResourceManagerService() |
michael@0 | 39 | { |
michael@0 | 40 | // Unregister AMessage handler from ALooper. |
michael@0 | 41 | mLooper->unregisterHandler(mReflector->id()); |
michael@0 | 42 | // Stop ALooper thread. |
michael@0 | 43 | mLooper->stop(); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | void MediaResourceManagerService::binderDied(const wp<IBinder>& who) |
michael@0 | 47 | { |
michael@0 | 48 | if (who != NULL) { |
michael@0 | 49 | Mutex::Autolock autoLock(mLock); |
michael@0 | 50 | sp<IBinder> binder = who.promote(); |
michael@0 | 51 | if (binder != NULL) { |
michael@0 | 52 | cancelClientLocked(binder); |
michael@0 | 53 | } |
michael@0 | 54 | } |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | void MediaResourceManagerService::requestMediaResource(const sp<IMediaResourceManagerClient>& client, int resourceType) |
michael@0 | 58 | { |
michael@0 | 59 | if (resourceType != MediaResourceManagerClient::HW_VIDEO_DECODER) { |
michael@0 | 60 | // Support only HW_VIDEO_DECODER |
michael@0 | 61 | return; |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | { |
michael@0 | 65 | Mutex::Autolock autoLock(mLock); |
michael@0 | 66 | sp<IBinder> binder = client->asBinder(); |
michael@0 | 67 | mVideoCodecRequestQueue.push_back(binder); |
michael@0 | 68 | binder->linkToDeath(this); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | sp<AMessage> notify = |
michael@0 | 72 | new AMessage(kNotifyRequest, mReflector->id()); |
michael@0 | 73 | // Post AMessage to MediaResourceManagerService via ALooper. |
michael@0 | 74 | notify->post(); |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | status_t MediaResourceManagerService::cancelClient(const sp<IMediaResourceManagerClient>& client) |
michael@0 | 78 | { |
michael@0 | 79 | { |
michael@0 | 80 | Mutex::Autolock autoLock(mLock); |
michael@0 | 81 | sp<IBinder> binder = client->asBinder(); |
michael@0 | 82 | cancelClientLocked(binder); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | sp<AMessage> notify = |
michael@0 | 86 | new AMessage(kNotifyRequest, mReflector->id()); |
michael@0 | 87 | // Post AMessage to MediaResourceManagerService via ALooper. |
michael@0 | 88 | notify->post(); |
michael@0 | 89 | |
michael@0 | 90 | return NO_ERROR; |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | // Called on ALooper thread. |
michael@0 | 94 | void MediaResourceManagerService::onMessageReceived(const sp<AMessage> &msg) |
michael@0 | 95 | { |
michael@0 | 96 | Mutex::Autolock autoLock(mLock); |
michael@0 | 97 | |
michael@0 | 98 | // Exit if no request. |
michael@0 | 99 | if (mVideoCodecRequestQueue.empty()) { |
michael@0 | 100 | return; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | // Check if resource is available |
michael@0 | 104 | int found = -1; |
michael@0 | 105 | for (int i=0 ; i<mVideoDecoderCount ; i++) { |
michael@0 | 106 | if (!mVideoDecoderSlots[i].mClient.get()) { |
michael@0 | 107 | found = i; |
michael@0 | 108 | } |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | // Exit if no resource is available. |
michael@0 | 112 | if (found == -1) { |
michael@0 | 113 | return; |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | // Assign resource to IMediaResourceManagerClient |
michael@0 | 117 | Fifo::iterator front(mVideoCodecRequestQueue.begin()); |
michael@0 | 118 | mVideoDecoderSlots[found].mClient = *front; |
michael@0 | 119 | mVideoCodecRequestQueue.erase(front); |
michael@0 | 120 | // Notify resource assignment to the client. |
michael@0 | 121 | sp<IMediaResourceManagerClient> client = interface_cast<IMediaResourceManagerClient>(mVideoDecoderSlots[found].mClient); |
michael@0 | 122 | client->statusChanged(MediaResourceManagerClient::CLIENT_STATE_RESOURCE_ASSIGNED); |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | void MediaResourceManagerService::cancelClientLocked(const sp<IBinder>& binder) |
michael@0 | 126 | { |
michael@0 | 127 | // Clear the request from request queue. |
michael@0 | 128 | Fifo::iterator it(mVideoCodecRequestQueue.begin()); |
michael@0 | 129 | while (it != mVideoCodecRequestQueue.end()) { |
michael@0 | 130 | if ((*it).get() == binder.get()) { |
michael@0 | 131 | mVideoCodecRequestQueue.erase(it); |
michael@0 | 132 | break; |
michael@0 | 133 | } |
michael@0 | 134 | it++; |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | // Clear the client from the resource |
michael@0 | 138 | for (int i=0 ; i<mVideoDecoderCount ; i++) { |
michael@0 | 139 | if (mVideoDecoderSlots[i].mClient.get() == binder.get()) { |
michael@0 | 140 | mVideoDecoderSlots[i].mClient = NULL; |
michael@0 | 141 | } |
michael@0 | 142 | } |
michael@0 | 143 | binder->unlinkToDeath(this); |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | }; // namespace android |
michael@0 | 147 |