xref: /haiku/src/kits/media/DormantNodeManager.cpp (revision 99d027cd0238c1d86da86d7c3f4200509ccc61a6)
1 /*
2  * Copyright (c) 2002, 2003 Marcus Overhagen <Marcus@Overhagen.de>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files or portions
6  * thereof (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify, merge,
8  * publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so, subject
10  * to the following conditions:
11  *
12  *  * Redistributions of source code must retain the above copyright notice,
13  *    this list of conditions and the following disclaimer.
14  *
15  *  * Redistributions in binary form must reproduce the above copyright notice
16  *    in the  binary, as well as this list of conditions and the following
17  *    disclaimer in the documentation and/or other materials provided with
18  *    the distribution.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26  * THE SOFTWARE.
27  *
28  */
29 
30 
31 /*!	This is a management class for dormant media nodes.
32 	It is private to the media kit and only accessed by the BMediaRoster class
33 	and the media_addon_server.
34 	It handles loading/unloading of dormant nodes.
35 
36 	Dormant media nodes can be instantiated on demand. The reside on harddisk in
37 	the directories /boot/beos/system/add-ons/media, /boot/common/add-ons/media,
38 	and /boot/home/config/add-ons/media.
39 	Multiple media nodes can be included in one file, they can be accessed using
40 	the BMediaAddOn that each file implements.
41 	The BMediaAddOn allows getting a list of supported flavors. Each flavor
42 	represents a media node.
43 	The media_addon_server does the initial scanning of files and getting the
44 	list of supported flavors. It uses the flavor_info to do this, and reports
45 	the list of flavors to the media_server packed into individual
46 	dormant_media_node structures.
47 */
48 
49 
50 #include "DormantNodeManager.h"
51 
52 #include <stdio.h>
53 #include <string.h>
54 
55 #include <Autolock.h>
56 #include <Entry.h>
57 #include <Path.h>
58 
59 #include <debug.h>
60 #include <MediaMisc.h>
61 #include <ServerInterface.h>
62 #include <DataExchange.h>
63 
64 
65 namespace BPrivate {
66 namespace media {
67 
68 
69 DormantNodeManager* gDormantNodeManager;
70 	// initialized by BMediaRoster.
71 
72 
73 DormantNodeManager::DormantNodeManager()
74 	:
75 	fLock("dormant node manager locker")
76 {
77 }
78 
79 
80 DormantNodeManager::~DormantNodeManager()
81 {
82 	// force unloading all currently loaded images
83 
84 	AddOnMap::iterator iterator = fAddOnMap.begin();
85 	for (; iterator != fAddOnMap.end(); iterator++) {
86 		loaded_add_on_info& info = iterator->second;
87 
88 		ERROR("Forcing unload of add-on id %ld with usecount %ld\n",
89 			info.add_on->AddonID(), info.use_count);
90 		_UnloadAddOn(info.add_on, info.image);
91 	}
92 }
93 
94 
95 BMediaAddOn*
96 DormantNodeManager::GetAddOn(media_addon_id id)
97 {
98 	TRACE("DormantNodeManager::GetAddon, id %ld\n", id);
99 
100 	// first try to use a already loaded add-on
101 	BMediaAddOn* addOn = _LookupAddOn(id);
102 	if (addOn != NULL)
103 		return addOn;
104 
105 	// Be careful, we avoid locking here!
106 
107 	// ok, it's not loaded, try to get the path
108 	BPath path;
109 	if (FindAddOnPath(&path, id) != B_OK) {
110 		ERROR("DormantNodeManager::GetAddon: can't find path for add-on "
111 			"%ld\n", id);
112 		return NULL;
113 	}
114 
115 	// try to load it
116 	BMediaAddOn* newAddOn;
117 	image_id image;
118 	if (_LoadAddOn(path.Path(), id, &newAddOn, &image) != B_OK) {
119 		ERROR("DormantNodeManager::GetAddon: can't load add-on %ld from path "
120 			"%s\n",id, path.Path());
121 		return NULL;
122 	}
123 
124 	// ok, we successfully loaded it. Now lock and insert it into the map,
125 	// or unload it if the map already contains one that was loaded by another
126 	// thread at the same time
127 
128 	BAutolock _(fLock);
129 
130 	addOn = _LookupAddOn(id);
131 	if (addOn == NULL) {
132 		// we use the loaded one
133 		addOn = newAddOn;
134 
135 		// and save it into the list
136 		loaded_add_on_info info;
137 		info.add_on = newAddOn;
138 		info.image = image;
139 		info.use_count = 1;
140 		try {
141 			fAddOnMap.insert(std::make_pair(id, info));
142 		} catch (std::bad_alloc& exception) {
143 			_UnloadAddOn(newAddOn, image);
144 			return NULL;
145 		}
146 	} else
147 		_UnloadAddOn(newAddOn, image);
148 
149 	ASSERT(addOn->AddonID() == id);
150 	return addOn;
151 }
152 
153 
154 void
155 DormantNodeManager::PutAddOn(media_addon_id id)
156 {
157 	TRACE("DormantNodeManager::PutAddon, id %ld\n", id);
158 
159 	BAutolock locker(fLock);
160 
161 	AddOnMap::iterator found = fAddOnMap.find(id);
162 	if (found == fAddOnMap.end()) {
163 		ERROR("DormantNodeManager::PutAddon: failed to find add-on %ld\n", id);
164 		return;
165 	}
166 
167 	loaded_add_on_info& info = found->second;
168 
169 	if (--info.use_count == 0) {
170 		// unload add-on
171 
172 		BMediaAddOn* addOn = info.add_on;
173 		image_id image = info.image;
174 		fAddOnMap.erase(found);
175 
176 		locker.Unlock();
177 		_UnloadAddOn(addOn, image);
178 	}
179 }
180 
181 
182 void
183 DormantNodeManager::PutAddOnDelayed(media_addon_id id)
184 {
185 	// Called from a node destructor of the loaded media-add-on.
186 	// We must make sure that the media-add-on stays in memory
187 	// a couple of seconds longer.
188 
189 	UNIMPLEMENTED();
190 }
191 
192 
193 //!	For use by media_addon_server only
194 media_addon_id
195 DormantNodeManager::RegisterAddOn(const char* path)
196 {
197 	TRACE("DormantNodeManager::RegisterAddon, path %s\n",path);
198 
199 	entry_ref ref;
200 	status_t status = get_ref_for_path(path, &ref);
201 	if (status != B_OK) {
202 		ERROR("DormantNodeManager::RegisterAddon failed, couldn't get ref "
203 			"for path %s: %s\n", path, strerror(status));
204 		return 0;
205 	}
206 
207 	server_register_add_on_request request;
208 	request.ref = ref;
209 
210 	server_register_add_on_reply reply;
211 	status = QueryServer(SERVER_REGISTER_ADD_ON, &request, sizeof(request),
212 		&reply, sizeof(reply));
213 	if (status != B_OK) {
214 		ERROR("DormantNodeManager::RegisterAddon failed, couldn't talk to "
215 			"media server\n");
216 		return 0;
217 	}
218 
219 	if (status < B_OK) {
220 		ERROR("DormantNodeManager::RegisterAddon failed, couldn't talk to "
221 			"media server: %s\n", strerror(status));
222 		return 0;
223 	}
224 
225 	TRACE("DormantNodeManager::RegisterAddon finished with id %ld\n",
226 		reply.add_on_id);
227 
228 	return reply.add_on_id;
229 }
230 
231 
232 //!	For use by media_addon_server only
233 void
234 DormantNodeManager::UnregisterAddOn(media_addon_id id)
235 {
236 	TRACE("DormantNodeManager::UnregisterAddon id %ld\n",id);
237 	ASSERT(id > 0);
238 
239 	port_id port = find_port(MEDIA_SERVER_PORT_NAME);
240 	if (port < 0)
241 		return;
242 
243 	server_unregister_add_on_command msg;
244 	msg.add_on_id = id;
245 	write_port(port, SERVER_UNREGISTER_ADD_ON, &msg, sizeof(msg));
246 }
247 
248 
249 status_t
250 DormantNodeManager::FindAddOnPath(BPath* path, media_addon_id id)
251 {
252 	server_get_add_on_ref_request request;
253 	request.add_on_id = id;
254 
255 	server_get_add_on_ref_reply reply;
256 	status_t status = QueryServer(SERVER_GET_ADD_ON_REF, &request,
257 		sizeof(request), &reply, sizeof(reply));
258 	if (status != B_OK)
259 		return status;
260 
261 	entry_ref ref = reply.ref;
262 	return path->SetTo(&ref);
263 }
264 
265 
266 BMediaAddOn*
267 DormantNodeManager::_LookupAddOn(media_addon_id id)
268 {
269 	BAutolock _(fLock);
270 
271 	AddOnMap::iterator found = fAddOnMap.find(id);
272 	if (found == fAddOnMap.end())
273 		return NULL;
274 
275 	loaded_add_on_info& info = found->second;
276 
277 	ASSERT(id == info.add_on->AddonID());
278 	info.use_count++;
279 
280 	return info.add_on;
281 }
282 
283 
284 status_t
285 DormantNodeManager::_LoadAddOn(const char* path, media_addon_id id,
286 	BMediaAddOn** _newAddOn, image_id* _newImage)
287 {
288 	image_id image = load_add_on(path);
289 	if (image < 0) {
290 		ERROR("DormantNodeManager::LoadAddon: loading \"%s\" failed: %s\n",
291 			path, strerror(image));
292 		return image;
293 	}
294 
295 	BMediaAddOn* (*makeAddOn)(image_id);
296 	status_t status = get_image_symbol(image, "make_media_addon",
297 		B_SYMBOL_TYPE_TEXT, (void**)&makeAddOn);
298 	if (status != B_OK) {
299 		ERROR("DormantNodeManager::LoadAddon: loading failed, function not "
300 			"found: %s\n", strerror(status));
301 		unload_add_on(image);
302 		return status;
303 	}
304 
305 	BMediaAddOn* addOn = makeAddOn(image);
306 	if (addOn == NULL) {
307 		ERROR("DormantNodeManager::LoadAddon: creating BMediaAddOn failed\n");
308 		unload_add_on(image);
309 		return B_ERROR;
310 	}
311 
312 	ASSERT(addOn->ImageID() == image);
313 		// this should be true for a well behaving add-ons
314 
315 	// We are a friend class of BMediaAddOn and initialize these member
316 	// variables
317 	addOn->fAddon = id;
318 	addOn->fImage = image;
319 
320 	// everything ok
321 	*_newAddOn = addOn;
322 	*_newImage = image;
323 
324 	return B_OK;
325 }
326 
327 
328 void
329 DormantNodeManager::_UnloadAddOn(BMediaAddOn* addOn, image_id image)
330 {
331 	ASSERT(addOn != NULL);
332 	ASSERT(addOn->ImageID() == image);
333 		// if this fails, something bad happened to the add-on
334 
335 	delete addOn;
336 	unload_add_on(image);
337 }
338 
339 
340 }	// namespace media
341 }	// namespace BPrivate
342