vfs: check userland buffers before reading them.
[haiku.git] / src / kits / media / AddOnManager.cpp
blobd1add217c8443644270df462ee8181ff70e22246
1 /*
2 * Copyright 2004-2010, Haiku. All rights reserved.
3 * Distributed under the terms of the MIT license.
5 * Authors:
6 * Marcus Overhagen
7 * Axel Dörfler
8 * Stephan Aßmus <superstippi@gmx.de>
9 */
12 #include "AddOnManager.h"
14 #include <stdio.h>
15 #include <string.h>
17 #include <Architecture.h>
18 #include <AutoDeleter.h>
19 #include <Autolock.h>
20 #include <Directory.h>
21 #include <Entry.h>
22 #include <FindDirectory.h>
23 #include <image.h>
24 #include <Path.h>
26 #include "debug.h"
28 #include "FormatManager.h"
29 #include "MetaFormat.h"
32 namespace BPrivate {
33 namespace media {
36 // #pragma mark - ImageLoader
38 /*! The ImageLoader class is a convenience class to temporarily load
39 an image file, and unload it on deconstruction automatically.
41 class ImageLoader {
42 public:
43 ImageLoader(BPath& path)
45 fImage = load_add_on(path.Path());
48 ~ImageLoader()
50 if (fImage >= B_OK)
51 unload_add_on(fImage);
54 status_t InitCheck() const { return fImage >= 0 ? B_OK : fImage; }
55 image_id Image() const { return fImage; }
57 private:
58 image_id fImage;
62 // #pragma mark -
65 AddOnManager::AddOnManager()
67 fLock("add-on manager"),
68 fNextWriterFormatFamilyID(0),
69 fNextEncoderCodecInfoID(0)
74 AddOnManager::~AddOnManager()
79 AddOnManager AddOnManager::sInstance;
82 /* static */ AddOnManager*
83 AddOnManager::GetInstance()
85 return &sInstance;
89 status_t
90 AddOnManager::GetDecoderForFormat(entry_ref* _decoderRef,
91 const media_format& format)
93 if ((format.type == B_MEDIA_ENCODED_VIDEO
94 || format.type == B_MEDIA_ENCODED_AUDIO
95 || format.type == B_MEDIA_MULTISTREAM)
96 && format.Encoding() == 0) {
97 return B_MEDIA_BAD_FORMAT;
99 if (format.type == B_MEDIA_NO_TYPE || format.type == B_MEDIA_UNKNOWN_TYPE)
100 return B_MEDIA_BAD_FORMAT;
102 BAutolock locker(fLock);
103 RegisterAddOns();
105 // Since the list of decoders is unsorted, we need to search for
106 // a decoder by add-on directory, in order to maintain the shadowing
107 // of system add-ons by user add-ons, in case they offer decoders
108 // for the same format.
110 char** directories = NULL;
111 size_t directoryCount = 0;
113 if (find_paths_etc(get_architecture(), B_FIND_PATH_ADD_ONS_DIRECTORY,
114 "media/plugins", B_FIND_PATH_EXISTING_ONLY, &directories,
115 &directoryCount) != B_OK) {
116 printf("AddOnManager::GetDecoderForFormat: failed to locate plugins\n");
117 return B_ENTRY_NOT_FOUND;
120 MemoryDeleter directoriesDeleter(directories);
122 BPath path;
123 for (uint i = 0; i < directoryCount; i++) {
124 path.SetTo(directories[i]);
125 if (_FindDecoder(format, path, _decoderRef))
126 return B_OK;
129 return B_ENTRY_NOT_FOUND;
133 status_t
134 AddOnManager::GetEncoderForFormat(entry_ref* _encoderRef,
135 const media_format& outputFormat)
137 if ((outputFormat.type == B_MEDIA_RAW_VIDEO
138 || outputFormat.type == B_MEDIA_RAW_AUDIO)) {
139 return B_MEDIA_BAD_FORMAT;
142 if (outputFormat.type == B_MEDIA_NO_TYPE
143 || outputFormat.type == B_MEDIA_UNKNOWN_TYPE) {
144 return B_MEDIA_BAD_FORMAT;
147 BAutolock locker(fLock);
148 RegisterAddOns();
150 char** directories = NULL;
151 size_t directoryCount = 0;
153 if (find_paths_etc(get_architecture(), B_FIND_PATH_ADD_ONS_DIRECTORY,
154 "media/plugins", B_FIND_PATH_EXISTING_ONLY, &directories,
155 &directoryCount) != B_OK) {
156 printf("AddOnManager::GetDecoderForFormat: failed to locate plugins\n");
157 return B_ENTRY_NOT_FOUND;
160 MemoryDeleter directoriesDeleter(directories);
162 BPath path;
163 for (uint i = 0; i < directoryCount; i++) {
164 path.SetTo(directories[i]);
165 if (_FindEncoder(outputFormat, path, _encoderRef))
166 return B_OK;
169 return B_ENTRY_NOT_FOUND;
173 status_t
174 AddOnManager::GetReaders(entry_ref* outRefs, int32* outCount,
175 int32 maxCount)
177 BAutolock locker(fLock);
178 RegisterAddOns();
180 *outCount = 0;
182 // See GetDecoderForFormat() for why we need to scan the list by path.
184 char** directories = NULL;
185 size_t directoryCount = 0;
187 if (find_paths_etc(get_architecture(), B_FIND_PATH_ADD_ONS_DIRECTORY,
188 "media/plugins", B_FIND_PATH_EXISTING_ONLY, &directories,
189 &directoryCount) != B_OK) {
190 printf("AddOnManager::GetReaders: failed to locate plugins\n");
191 return B_ENTRY_NOT_FOUND;
194 MemoryDeleter directoriesDeleter(directories);
196 BPath path;
197 for (uint i = 0; i < directoryCount; i++) {
198 path.SetTo(directories[i]);
199 _GetReaders(path, outRefs, outCount, maxCount);
202 return B_OK;
206 status_t
207 AddOnManager::GetStreamers(entry_ref* outRefs, int32* outCount,
208 int32 maxCount)
210 BAutolock locker(fLock);
211 RegisterAddOns();
213 int32 count = 0;
214 streamer_info* info;
215 for (fStreamerList.Rewind(); fStreamerList.GetNext(&info);) {
216 if (count == maxCount)
217 break;
219 *outRefs = info->ref;
220 outRefs++;
221 count++;
224 *outCount = count;
225 return B_OK;
229 status_t
230 AddOnManager::GetEncoder(entry_ref* _encoderRef, int32 id)
232 BAutolock locker(fLock);
233 RegisterAddOns();
235 encoder_info* info;
236 for (fEncoderList.Rewind(); fEncoderList.GetNext(&info);) {
237 // check if the encoder matches the supplied format
238 if (info->internalID == (uint32)id) {
239 *_encoderRef = info->ref;
240 return B_OK;
244 return B_ENTRY_NOT_FOUND;
248 status_t
249 AddOnManager::GetWriter(entry_ref* _ref, uint32 internalID)
251 BAutolock locker(fLock);
252 RegisterAddOns();
254 writer_info* info;
255 for (fWriterList.Rewind(); fWriterList.GetNext(&info);) {
256 if (info->internalID == internalID) {
257 *_ref = info->ref;
258 return B_OK;
262 return B_ERROR;
266 status_t
267 AddOnManager::GetFileFormat(media_file_format* _fileFormat, int32 cookie)
269 BAutolock locker(fLock);
270 RegisterAddOns();
272 media_file_format* fileFormat;
273 if (fWriterFileFormats.Get(cookie, &fileFormat)) {
274 *_fileFormat = *fileFormat;
275 return B_OK;
278 return B_BAD_INDEX;
282 status_t
283 AddOnManager::GetCodecInfo(media_codec_info* _codecInfo,
284 media_format_family* _formatFamily,
285 media_format* _inputFormat, media_format* _outputFormat, int32 cookie)
287 BAutolock locker(fLock);
288 RegisterAddOns();
290 encoder_info* info;
291 if (fEncoderList.Get(cookie, &info)) {
292 *_codecInfo = info->codecInfo;
293 *_formatFamily = info->formatFamily;
294 *_inputFormat = info->intputFormat;
295 *_outputFormat = info->outputFormat;
296 return B_OK;
299 return B_BAD_INDEX;
303 // #pragma mark -
306 void
307 AddOnManager::RegisterAddOns()
309 // Check if add-ons are already registered.
310 if (!fReaderList.IsEmpty() || !fWriterList.IsEmpty()
311 || !fDecoderList.IsEmpty() || !fEncoderList.IsEmpty()) {
312 return;
315 char** directories = NULL;
316 size_t directoryCount = 0;
318 if (find_paths_etc(get_architecture(), B_FIND_PATH_ADD_ONS_DIRECTORY,
319 "media/plugins", B_FIND_PATH_EXISTING_ONLY, &directories,
320 &directoryCount) != B_OK) {
321 return;
324 MemoryDeleter directoriesDeleter(directories);
326 BPath path;
327 for (uint i = 0; i < directoryCount; i++) {
328 BDirectory directory;
329 if (directory.SetTo(directories[i]) == B_OK) {
330 entry_ref ref;
331 while(directory.GetNextRef(&ref) == B_OK)
332 _RegisterAddOn(ref);
338 status_t
339 AddOnManager::_RegisterAddOn(const entry_ref& ref)
341 BPath path(&ref);
343 ImageLoader loader(path);
344 status_t status = loader.InitCheck();
345 if (status != B_OK)
346 return status;
348 MediaPlugin* (*instantiate_plugin_func)();
350 if (get_image_symbol(loader.Image(), "instantiate_plugin",
351 B_SYMBOL_TYPE_TEXT, (void**)&instantiate_plugin_func) < B_OK) {
352 printf("AddOnManager::_RegisterAddOn(): can't find instantiate_plugin "
353 "in \"%s\"\n", path.Path());
354 return B_BAD_TYPE;
357 MediaPlugin* plugin = (*instantiate_plugin_func)();
358 if (plugin == NULL) {
359 printf("AddOnManager::_RegisterAddOn(): instantiate_plugin in \"%s\" "
360 "returned NULL\n", path.Path());
361 return B_ERROR;
364 ReaderPlugin* reader = dynamic_cast<ReaderPlugin*>(plugin);
365 if (reader != NULL)
366 _RegisterReader(reader, ref);
368 DecoderPlugin* decoder = dynamic_cast<DecoderPlugin*>(plugin);
369 if (decoder != NULL)
370 _RegisterDecoder(decoder, ref);
372 WriterPlugin* writer = dynamic_cast<WriterPlugin*>(plugin);
373 if (writer != NULL)
374 _RegisterWriter(writer, ref);
376 EncoderPlugin* encoder = dynamic_cast<EncoderPlugin*>(plugin);
377 if (encoder != NULL)
378 _RegisterEncoder(encoder, ref);
380 StreamerPlugin* streamer = dynamic_cast<StreamerPlugin*>(plugin);
381 if (streamer != NULL)
382 _RegisterStreamer(streamer, ref);
384 delete plugin;
386 return B_OK;
390 status_t
391 AddOnManager::_UnregisterAddOn(const entry_ref& ref)
393 BAutolock locker(fLock);
395 // Remove any Readers exported by this add-on
396 reader_info* readerInfo;
397 for (fReaderList.Rewind(); fReaderList.GetNext(&readerInfo);) {
398 if (readerInfo->ref == ref) {
399 fReaderList.RemoveCurrent();
400 break;
404 // Remove any Decoders exported by this add-on
405 decoder_info* decoderInfo;
406 for (fDecoderList.Rewind(); fDecoderList.GetNext(&decoderInfo);) {
407 if (decoderInfo->ref == ref) {
408 media_format* format;
409 for (decoderInfo->formats.Rewind();
410 decoderInfo->formats.GetNext(&format);) {
411 FormatManager::GetInstance()->RemoveFormat(*format);
413 fDecoderList.RemoveCurrent();
414 break;
418 // Remove any Writers exported by this add-on
419 writer_info* writerInfo;
420 for (fWriterList.Rewind(); fWriterList.GetNext(&writerInfo);) {
421 if (writerInfo->ref == ref) {
422 // Remove any formats from this writer
423 media_file_format* writerFormat;
424 for (fWriterFileFormats.Rewind();
425 fWriterFileFormats.GetNext(&writerFormat);) {
426 if (writerFormat->id.internal_id == writerInfo->internalID)
427 fWriterFileFormats.RemoveCurrent();
429 fWriterList.RemoveCurrent();
430 break;
434 encoder_info* encoderInfo;
435 for (fEncoderList.Rewind(); fEncoderList.GetNext(&encoderInfo);) {
436 if (encoderInfo->ref == ref) {
437 fEncoderList.RemoveCurrent();
438 // Keep going, since we add multiple encoder infos per add-on.
442 return B_OK;
446 void
447 AddOnManager::_RegisterReader(ReaderPlugin* reader, const entry_ref& ref)
449 BAutolock locker(fLock);
451 reader_info* pinfo;
452 for (fReaderList.Rewind(); fReaderList.GetNext(&pinfo);) {
453 if (!strcmp(pinfo->ref.name, ref.name)) {
454 // we already know this reader
455 return;
459 reader_info info;
460 info.ref = ref;
462 fReaderList.Insert(info);
466 void
467 AddOnManager::_RegisterDecoder(DecoderPlugin* plugin, const entry_ref& ref)
469 BAutolock locker(fLock);
471 decoder_info* pinfo;
472 for (fDecoderList.Rewind(); fDecoderList.GetNext(&pinfo);) {
473 if (!strcmp(pinfo->ref.name, ref.name)) {
474 // we already know this decoder
475 return;
479 decoder_info info;
480 info.ref = ref;
482 media_format* formats = 0;
483 size_t count = 0;
484 if (plugin->GetSupportedFormats(&formats, &count) != B_OK) {
485 printf("AddOnManager::_RegisterDecoder(): plugin->GetSupportedFormats"
486 "(...) failed!\n");
487 return;
489 for (uint i = 0 ; i < count ; i++)
490 info.formats.Insert(formats[i]);
492 fDecoderList.Insert(info);
496 void
497 AddOnManager::_RegisterWriter(WriterPlugin* writer, const entry_ref& ref)
499 BAutolock locker(fLock);
501 writer_info* pinfo;
502 for (fWriterList.Rewind(); fWriterList.GetNext(&pinfo);) {
503 if (!strcmp(pinfo->ref.name, ref.name)) {
504 // we already know this writer
505 return;
509 writer_info info;
510 info.ref = ref;
511 info.internalID = fNextWriterFormatFamilyID++;
513 // Get list of support media_file_formats...
514 const media_file_format* fileFormats = NULL;
515 size_t count = 0;
516 if (writer->GetSupportedFileFormats(&fileFormats, &count) != B_OK) {
517 printf("AddOnManager::_RegisterWriter(): "
518 "plugin->GetSupportedFileFormats(...) failed!\n");
519 return;
521 for (uint i = 0 ; i < count ; i++) {
522 // Generate a proper ID before inserting this format, this encodes
523 // the specific plugin in the media_file_format.
524 media_file_format fileFormat = fileFormats[i];
525 fileFormat.id.node = ref.directory;
526 fileFormat.id.device = ref.device;
527 fileFormat.id.internal_id = info.internalID;
529 fWriterFileFormats.Insert(fileFormat);
532 fWriterList.Insert(info);
536 void
537 AddOnManager::_RegisterEncoder(EncoderPlugin* plugin, const entry_ref& ref)
539 BAutolock locker(fLock);
541 encoder_info* pinfo;
542 for (fEncoderList.Rewind(); fEncoderList.GetNext(&pinfo);) {
543 if (!strcmp(pinfo->ref.name, ref.name)) {
544 // We already know this encoder. When we reject encoders with
545 // the same name, we allow the user to overwrite system encoders
546 // in her home folder.
547 return;
551 // Get list of supported encoders...
553 encoder_info info;
554 info.ref = ref;
555 info.internalID = fNextEncoderCodecInfoID++;
557 int32 cookie = 0;
559 while (true) {
560 memset(&info.codecInfo, 0, sizeof(media_codec_info));
561 memset(&info.intputFormat, 0, sizeof(media_format));
562 memset(&info.outputFormat, 0, sizeof(media_format));
563 if (plugin->RegisterNextEncoder(&cookie,
564 &info.codecInfo, &info.formatFamily, &info.intputFormat,
565 &info.outputFormat) != B_OK) {
566 break;
568 info.codecInfo.id = info.internalID;
569 // NOTE: info.codecInfo.sub_id is for private use by the Encoder,
570 // we don't touch it, but it is maintained and passed back to the
571 // EncoderPlugin in NewEncoder(media_codec_info).
573 if (!fEncoderList.Insert(info))
574 break;
579 void
580 AddOnManager::_RegisterStreamer(StreamerPlugin* streamer, const entry_ref& ref)
582 BAutolock locker(fLock);
584 streamer_info* pInfo;
585 for (fStreamerList.Rewind(); fStreamerList.GetNext(&pInfo);) {
586 if (!strcmp(pInfo->ref.name, ref.name)) {
587 // We already know this streamer
588 return;
592 streamer_info info;
593 info.ref = ref;
594 fStreamerList.Insert(info);
598 bool
599 AddOnManager::_FindDecoder(const media_format& format, const BPath& path,
600 entry_ref* _decoderRef)
602 node_ref nref;
603 BDirectory directory;
604 if (directory.SetTo(path.Path()) != B_OK
605 || directory.GetNodeRef(&nref) != B_OK) {
606 return false;
609 decoder_info* info;
610 for (fDecoderList.Rewind(); fDecoderList.GetNext(&info);) {
611 if (info->ref.directory != nref.node)
612 continue;
614 media_format* decoderFormat;
615 for (info->formats.Rewind(); info->formats.GetNext(&decoderFormat);) {
616 // check if the decoder matches the supplied format
617 if (!decoderFormat->Matches(&format))
618 continue;
620 *_decoderRef = info->ref;
621 return true;
624 return false;
628 bool
629 AddOnManager::_FindEncoder(const media_format& format, const BPath& path,
630 entry_ref* _encoderRef)
632 node_ref nref;
633 BDirectory directory;
634 if (directory.SetTo(path.Path()) != B_OK
635 || directory.GetNodeRef(&nref) != B_OK) {
636 return false;
639 encoder_info* info;
640 for (fEncoderList.Rewind(); fEncoderList.GetNext(&info);) {
641 if (info->ref.directory != nref.node)
642 continue;
644 // check if the encoder matches the supplied format
645 if (info->outputFormat.Matches(&format)) {
646 *_encoderRef = info->ref;
647 return true;
649 continue;
651 return false;
655 void
656 AddOnManager::_GetReaders(const BPath& path, entry_ref* outRefs,
657 int32* outCount, int32 maxCount)
659 node_ref nref;
660 BDirectory directory;
661 if (directory.SetTo(path.Path()) != B_OK
662 || directory.GetNodeRef(&nref) != B_OK) {
663 return;
666 reader_info* info;
667 for (fReaderList.Rewind(); fReaderList.GetNext(&info)
668 && *outCount < maxCount;) {
669 if (info->ref.directory != nref.node)
670 continue;
672 outRefs[*outCount] = info->ref;
673 (*outCount)++;
678 } // namespace media
679 } // namespace BPrivate