Wed, 31 Dec 2014 13:27:57 +0100
Ignore runtime configuration files generated during quality assurance.
1 /*
2 ** Copyright 2010, The Android Open Source Project
3 ** Copyright 2013, Mozilla Foundation
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 ** http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
18 //#define LOG_NDEBUG 0
19 #define LOG_TAG "IMediaResourceManagerDeathNotifier"
20 #include <utils/Log.h>
22 #include <binder/IServiceManager.h>
23 #include <binder/IPCThreadState.h>
25 #include "IMediaResourceManagerDeathNotifier.h"
27 #define DN_LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)
28 #define DN_LOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)
29 #define DN_LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
30 #define DN_LOGE_IF(cond, ...) \
31 ( (CONDITION(cond)) \
32 ? ((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
33 : (void)0 )
35 namespace android {
37 // client singleton for binder interface to services
38 Mutex IMediaResourceManagerDeathNotifier::sServiceLock;
39 sp<IMediaResourceManagerService> IMediaResourceManagerDeathNotifier::sMediaResourceManagerService;
40 sp<IMediaResourceManagerDeathNotifier::DeathNotifier> IMediaResourceManagerDeathNotifier::sDeathNotifier;
41 SortedVector< wp<IMediaResourceManagerDeathNotifier> > IMediaResourceManagerDeathNotifier::sObitRecipients;
43 // establish binder interface to MediaResourceManagerService
44 /*static*/const sp<IMediaResourceManagerService>&
45 IMediaResourceManagerDeathNotifier::getMediaResourceManagerService()
46 {
47 DN_LOGV("getMediaResourceManagerService");
48 Mutex::Autolock _l(sServiceLock);
49 if (sMediaResourceManagerService.get() == 0) {
50 sp<IServiceManager> sm = defaultServiceManager();
51 sp<IBinder> binder;
52 do {
53 binder = sm->getService(String16("media.resource_manager"));
54 if (binder != 0) {
55 break;
56 }
57 DN_LOGW("Media resource manager service not published, waiting...");
58 usleep(500000); // 0.5 s
59 } while(true);
61 if (sDeathNotifier == NULL) {
62 sDeathNotifier = new DeathNotifier();
63 }
64 binder->linkToDeath(sDeathNotifier);
65 sMediaResourceManagerService = interface_cast<IMediaResourceManagerService>(binder);
66 }
67 DN_LOGE_IF(sMediaResourceManagerService == 0, "no media player service!?");
68 return sMediaResourceManagerService;
69 }
71 /*static*/ void
72 IMediaResourceManagerDeathNotifier::addObitRecipient(const wp<IMediaResourceManagerDeathNotifier>& recipient)
73 {
74 Mutex::Autolock _l(sServiceLock);
75 sObitRecipients.add(recipient);
76 }
78 /*static*/ void
79 IMediaResourceManagerDeathNotifier::removeObitRecipient(const wp<IMediaResourceManagerDeathNotifier>& recipient)
80 {
81 Mutex::Autolock _l(sServiceLock);
82 sObitRecipients.remove(recipient);
83 }
85 void
86 IMediaResourceManagerDeathNotifier::DeathNotifier::binderDied(const wp<IBinder>& who)
87 {
88 DN_LOGW("media resource manager service died");
89 // Need to do this with the lock held
90 SortedVector< wp<IMediaResourceManagerDeathNotifier> > list;
91 {
92 Mutex::Autolock _l(sServiceLock);
93 sMediaResourceManagerService.clear();
94 list = sObitRecipients;
95 }
97 // Notify application when media server dies.
98 // Don't hold the static lock during callback in case app
99 // makes a call that needs the lock.
100 size_t count = list.size();
101 for (size_t iter = 0; iter < count; ++iter) {
102 sp<IMediaResourceManagerDeathNotifier> notifier = list[iter].promote();
103 if (notifier != 0) {
104 notifier->died();
105 }
106 }
107 }
109 IMediaResourceManagerDeathNotifier::DeathNotifier::~DeathNotifier()
110 {
111 Mutex::Autolock _l(sServiceLock);
112 sObitRecipients.clear();
113 if (sMediaResourceManagerService != 0) {
114 sMediaResourceManagerService->asBinder()->unlinkToDeath(this);
115 }
116 }
118 }; // namespace android