xref: /haiku/headers/private/media/DormantNodeManager.h (revision db10640de90f7f9519ba2da9577b7c1af3c64f6b)
1 /*
2  * Copyright 2002, Marcus Overhagen. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #ifndef _DORMANT_NODE_MANAGER_H
7 #define _DORMANT_NODE_MANAGER_H
8 
9 #include "TMap.h"
10 
11 namespace BPrivate {
12 namespace media {
13 
14 // To instantiate a dormant node in the current address space, we need to
15 // either load the add-on from file and create a new BMediaAddOn class and
16 // cache the BMediaAddOn after that for later reuse, or reuse the cached
17 // BMediaAddOn if we already have one.
18 // This is handled by the DormantNodeManager.
19 //
20 // GetAddon() will provide a ready to use BMediaAddOn, while
21 // PutAddonDelayed() will unload it when it's no longer needed,
22 // but will delay the unloading slightly, because it is called
23 // from a node destructor of the loaded add-on.
24 
25 class DormantNodeManager
26 {
27 public:
28 	DormantNodeManager();
29 	~DormantNodeManager();
30 
31 	// Be careful, GetAddon and PutAddon[Delayed] must be balanced.
32 	BMediaAddOn *GetAddon(media_addon_id id);
33 	void PutAddon(media_addon_id id);
34 	void PutAddonDelayed(media_addon_id id);
35 
36 	// For use by media_addon_server only
37 	media_addon_id RegisterAddon(const char *path);
38 	void UnregisterAddon(media_addon_id id);
39 
40 	// query the server for the path
41 	status_t FindAddonPath(BPath *path, media_addon_id id);
42 
43 private:
44 	struct loaded_addon_info
45 	{
46 		BMediaAddOn *addon;
47 		image_id image;
48 		int32 usecount;
49 	};
50 
51 	// returns the addon or NULL if it needs to be loaded
52 	BMediaAddOn *TryGetAddon(media_addon_id id);
53 
54 	// manage loading and unloading add-ons from images
55 	status_t LoadAddon(BMediaAddOn **newaddon, image_id *newimage, const char *path, media_addon_id id);
56 	void UnloadAddon(BMediaAddOn *addon, image_id image);
57 
58 private:
59 
60 	Map<media_addon_id,loaded_addon_info> *fAddonmap;
61 	BLocker *fLock;
62 };
63 
64 }; // namespace media
65 }; // namespace BPrivate
66 
67 extern BPrivate::media::DormantNodeManager *_DormantNodeManager;
68 
69 #endif /* _DORMANT_NODE_MANAGER_H */
70