-
-
Notifications
You must be signed in to change notification settings - Fork 338
/
AddOnManager.cpp
679 lines (534 loc) · 14.7 KB
/
AddOnManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
/*
* Copyright 2004-2010, Haiku. All rights reserved.
* Distributed under the terms of the MIT license.
*
* Authors:
* Marcus Overhagen
* Axel Dörfler
* Stephan Aßmus <superstippi@gmx.de>
*/
#include "AddOnManager.h"
#include <stdio.h>
#include <string.h>
#include <Architecture.h>
#include <AutoDeleter.h>
#include <Autolock.h>
#include <Directory.h>
#include <Entry.h>
#include <FindDirectory.h>
#include <image.h>
#include <Path.h>
#include "debug.h"
#include "FormatManager.h"
#include "MetaFormat.h"
namespace BPrivate {
namespace media {
// #pragma mark - ImageLoader
/*! The ImageLoader class is a convenience class to temporarily load
an image file, and unload it on deconstruction automatically.
*/
class ImageLoader {
public:
ImageLoader(BPath& path)
{
fImage = load_add_on(path.Path());
}
~ImageLoader()
{
if (fImage >= B_OK)
unload_add_on(fImage);
}
status_t InitCheck() const { return fImage >= 0 ? B_OK : fImage; }
image_id Image() const { return fImage; }
private:
image_id fImage;
};
// #pragma mark -
AddOnManager::AddOnManager()
:
fLock("add-on manager"),
fNextWriterFormatFamilyID(0),
fNextEncoderCodecInfoID(0)
{
}
AddOnManager::~AddOnManager()
{
}
AddOnManager AddOnManager::sInstance;
/* static */ AddOnManager*
AddOnManager::GetInstance()
{
return &sInstance;
}
status_t
AddOnManager::GetDecoderForFormat(entry_ref* _decoderRef,
const media_format& format)
{
if ((format.type == B_MEDIA_ENCODED_VIDEO
|| format.type == B_MEDIA_ENCODED_AUDIO
|| format.type == B_MEDIA_MULTISTREAM)
&& format.Encoding() == 0) {
return B_MEDIA_BAD_FORMAT;
}
if (format.type == B_MEDIA_NO_TYPE || format.type == B_MEDIA_UNKNOWN_TYPE)
return B_MEDIA_BAD_FORMAT;
BAutolock locker(fLock);
RegisterAddOns();
// Since the list of decoders is unsorted, we need to search for
// a decoder by add-on directory, in order to maintain the shadowing
// of system add-ons by user add-ons, in case they offer decoders
// for the same format.
char** directories = NULL;
size_t directoryCount = 0;
if (find_paths_etc(get_architecture(), B_FIND_PATH_ADD_ONS_DIRECTORY,
"media/plugins", B_FIND_PATH_EXISTING_ONLY, &directories,
&directoryCount) != B_OK) {
printf("AddOnManager::GetDecoderForFormat: failed to locate plugins\n");
return B_ENTRY_NOT_FOUND;
}
MemoryDeleter directoriesDeleter(directories);
BPath path;
for (uint i = 0; i < directoryCount; i++) {
path.SetTo(directories[i]);
if (_FindDecoder(format, path, _decoderRef))
return B_OK;
}
return B_ENTRY_NOT_FOUND;
}
status_t
AddOnManager::GetEncoderForFormat(entry_ref* _encoderRef,
const media_format& outputFormat)
{
if ((outputFormat.type == B_MEDIA_RAW_VIDEO
|| outputFormat.type == B_MEDIA_RAW_AUDIO)) {
return B_MEDIA_BAD_FORMAT;
}
if (outputFormat.type == B_MEDIA_NO_TYPE
|| outputFormat.type == B_MEDIA_UNKNOWN_TYPE) {
return B_MEDIA_BAD_FORMAT;
}
BAutolock locker(fLock);
RegisterAddOns();
char** directories = NULL;
size_t directoryCount = 0;
if (find_paths_etc(get_architecture(), B_FIND_PATH_ADD_ONS_DIRECTORY,
"media/plugins", B_FIND_PATH_EXISTING_ONLY, &directories,
&directoryCount) != B_OK) {
printf("AddOnManager::GetDecoderForFormat: failed to locate plugins\n");
return B_ENTRY_NOT_FOUND;
}
MemoryDeleter directoriesDeleter(directories);
BPath path;
for (uint i = 0; i < directoryCount; i++) {
path.SetTo(directories[i]);
if (_FindEncoder(outputFormat, path, _encoderRef))
return B_OK;
}
return B_ENTRY_NOT_FOUND;
}
status_t
AddOnManager::GetReaders(entry_ref* outRefs, int32* outCount,
int32 maxCount)
{
BAutolock locker(fLock);
RegisterAddOns();
*outCount = 0;
// See GetDecoderForFormat() for why we need to scan the list by path.
char** directories = NULL;
size_t directoryCount = 0;
if (find_paths_etc(get_architecture(), B_FIND_PATH_ADD_ONS_DIRECTORY,
"media/plugins", B_FIND_PATH_EXISTING_ONLY, &directories,
&directoryCount) != B_OK) {
printf("AddOnManager::GetReaders: failed to locate plugins\n");
return B_ENTRY_NOT_FOUND;
}
MemoryDeleter directoriesDeleter(directories);
BPath path;
for (uint i = 0; i < directoryCount; i++) {
path.SetTo(directories[i]);
_GetReaders(path, outRefs, outCount, maxCount);
}
return B_OK;
}
status_t
AddOnManager::GetStreamers(entry_ref* outRefs, int32* outCount,
int32 maxCount)
{
BAutolock locker(fLock);
RegisterAddOns();
int32 count = 0;
streamer_info* info;
for (fStreamerList.Rewind(); fStreamerList.GetNext(&info);) {
if (count == maxCount)
break;
*outRefs = info->ref;
outRefs++;
count++;
}
*outCount = count;
return B_OK;
}
status_t
AddOnManager::GetEncoder(entry_ref* _encoderRef, int32 id)
{
BAutolock locker(fLock);
RegisterAddOns();
encoder_info* info;
for (fEncoderList.Rewind(); fEncoderList.GetNext(&info);) {
// check if the encoder matches the supplied format
if (info->internalID == (uint32)id) {
*_encoderRef = info->ref;
return B_OK;
}
}
return B_ENTRY_NOT_FOUND;
}
status_t
AddOnManager::GetWriter(entry_ref* _ref, uint32 internalID)
{
BAutolock locker(fLock);
RegisterAddOns();
writer_info* info;
for (fWriterList.Rewind(); fWriterList.GetNext(&info);) {
if (info->internalID == internalID) {
*_ref = info->ref;
return B_OK;
}
}
return B_ERROR;
}
status_t
AddOnManager::GetFileFormat(media_file_format* _fileFormat, int32 cookie)
{
BAutolock locker(fLock);
RegisterAddOns();
media_file_format* fileFormat;
if (fWriterFileFormats.Get(cookie, &fileFormat)) {
*_fileFormat = *fileFormat;
return B_OK;
}
return B_BAD_INDEX;
}
status_t
AddOnManager::GetCodecInfo(media_codec_info* _codecInfo,
media_format_family* _formatFamily,
media_format* _inputFormat, media_format* _outputFormat, int32 cookie)
{
BAutolock locker(fLock);
RegisterAddOns();
encoder_info* info;
if (fEncoderList.Get(cookie, &info)) {
*_codecInfo = info->codecInfo;
*_formatFamily = info->formatFamily;
*_inputFormat = info->intputFormat;
*_outputFormat = info->outputFormat;
return B_OK;
}
return B_BAD_INDEX;
}
// #pragma mark -
void
AddOnManager::RegisterAddOns()
{
// Check if add-ons are already registered.
if (!fReaderList.IsEmpty() || !fWriterList.IsEmpty()
|| !fDecoderList.IsEmpty() || !fEncoderList.IsEmpty()) {
return;
}
char** directories = NULL;
size_t directoryCount = 0;
if (find_paths_etc(get_architecture(), B_FIND_PATH_ADD_ONS_DIRECTORY,
"media/plugins", B_FIND_PATH_EXISTING_ONLY, &directories,
&directoryCount) != B_OK) {
return;
}
MemoryDeleter directoriesDeleter(directories);
BPath path;
for (uint i = 0; i < directoryCount; i++) {
BDirectory directory;
if (directory.SetTo(directories[i]) == B_OK) {
entry_ref ref;
while(directory.GetNextRef(&ref) == B_OK)
_RegisterAddOn(ref);
}
}
}
status_t
AddOnManager::_RegisterAddOn(const entry_ref& ref)
{
BPath path(&ref);
ImageLoader loader(path);
status_t status = loader.InitCheck();
if (status != B_OK)
return status;
MediaPlugin* (*instantiate_plugin_func)();
if (get_image_symbol(loader.Image(), "instantiate_plugin",
B_SYMBOL_TYPE_TEXT, (void**)&instantiate_plugin_func) < B_OK) {
printf("AddOnManager::_RegisterAddOn(): can't find instantiate_plugin "
"in \"%s\"\n", path.Path());
return B_BAD_TYPE;
}
MediaPlugin* plugin = (*instantiate_plugin_func)();
if (plugin == NULL) {
printf("AddOnManager::_RegisterAddOn(): instantiate_plugin in \"%s\" "
"returned NULL\n", path.Path());
return B_ERROR;
}
ReaderPlugin* reader = dynamic_cast<ReaderPlugin*>(plugin);
if (reader != NULL)
_RegisterReader(reader, ref);
DecoderPlugin* decoder = dynamic_cast<DecoderPlugin*>(plugin);
if (decoder != NULL)
_RegisterDecoder(decoder, ref);
WriterPlugin* writer = dynamic_cast<WriterPlugin*>(plugin);
if (writer != NULL)
_RegisterWriter(writer, ref);
EncoderPlugin* encoder = dynamic_cast<EncoderPlugin*>(plugin);
if (encoder != NULL)
_RegisterEncoder(encoder, ref);
StreamerPlugin* streamer = dynamic_cast<StreamerPlugin*>(plugin);
if (streamer != NULL)
_RegisterStreamer(streamer, ref);
delete plugin;
return B_OK;
}
status_t
AddOnManager::_UnregisterAddOn(const entry_ref& ref)
{
BAutolock locker(fLock);
// Remove any Readers exported by this add-on
reader_info* readerInfo;
for (fReaderList.Rewind(); fReaderList.GetNext(&readerInfo);) {
if (readerInfo->ref == ref) {
fReaderList.RemoveCurrent();
break;
}
}
// Remove any Decoders exported by this add-on
decoder_info* decoderInfo;
for (fDecoderList.Rewind(); fDecoderList.GetNext(&decoderInfo);) {
if (decoderInfo->ref == ref) {
media_format* format;
for (decoderInfo->formats.Rewind();
decoderInfo->formats.GetNext(&format);) {
FormatManager::GetInstance()->RemoveFormat(*format);
}
fDecoderList.RemoveCurrent();
break;
}
}
// Remove any Writers exported by this add-on
writer_info* writerInfo;
for (fWriterList.Rewind(); fWriterList.GetNext(&writerInfo);) {
if (writerInfo->ref == ref) {
// Remove any formats from this writer
media_file_format* writerFormat;
for (fWriterFileFormats.Rewind();
fWriterFileFormats.GetNext(&writerFormat);) {
if (writerFormat->id.internal_id == writerInfo->internalID)
fWriterFileFormats.RemoveCurrent();
}
fWriterList.RemoveCurrent();
break;
}
}
encoder_info* encoderInfo;
for (fEncoderList.Rewind(); fEncoderList.GetNext(&encoderInfo);) {
if (encoderInfo->ref == ref) {
fEncoderList.RemoveCurrent();
// Keep going, since we add multiple encoder infos per add-on.
}
}
return B_OK;
}
void
AddOnManager::_RegisterReader(ReaderPlugin* reader, const entry_ref& ref)
{
BAutolock locker(fLock);
reader_info* pinfo;
for (fReaderList.Rewind(); fReaderList.GetNext(&pinfo);) {
if (!strcmp(pinfo->ref.name, ref.name)) {
// we already know this reader
return;
}
}
reader_info info;
info.ref = ref;
fReaderList.Insert(info);
}
void
AddOnManager::_RegisterDecoder(DecoderPlugin* plugin, const entry_ref& ref)
{
BAutolock locker(fLock);
decoder_info* pinfo;
for (fDecoderList.Rewind(); fDecoderList.GetNext(&pinfo);) {
if (!strcmp(pinfo->ref.name, ref.name)) {
// we already know this decoder
return;
}
}
decoder_info info;
info.ref = ref;
media_format* formats = 0;
size_t count = 0;
if (plugin->GetSupportedFormats(&formats, &count) != B_OK) {
printf("AddOnManager::_RegisterDecoder(): plugin->GetSupportedFormats"
"(...) failed!\n");
return;
}
for (uint i = 0 ; i < count ; i++)
info.formats.Insert(formats[i]);
fDecoderList.Insert(info);
}
void
AddOnManager::_RegisterWriter(WriterPlugin* writer, const entry_ref& ref)
{
BAutolock locker(fLock);
writer_info* pinfo;
for (fWriterList.Rewind(); fWriterList.GetNext(&pinfo);) {
if (!strcmp(pinfo->ref.name, ref.name)) {
// we already know this writer
return;
}
}
writer_info info;
info.ref = ref;
info.internalID = fNextWriterFormatFamilyID++;
// Get list of support media_file_formats...
const media_file_format* fileFormats = NULL;
size_t count = 0;
if (writer->GetSupportedFileFormats(&fileFormats, &count) != B_OK) {
printf("AddOnManager::_RegisterWriter(): "
"plugin->GetSupportedFileFormats(...) failed!\n");
return;
}
for (uint i = 0 ; i < count ; i++) {
// Generate a proper ID before inserting this format, this encodes
// the specific plugin in the media_file_format.
media_file_format fileFormat = fileFormats[i];
fileFormat.id.node = ref.directory;
fileFormat.id.device = ref.device;
fileFormat.id.internal_id = info.internalID;
fWriterFileFormats.Insert(fileFormat);
}
fWriterList.Insert(info);
}
void
AddOnManager::_RegisterEncoder(EncoderPlugin* plugin, const entry_ref& ref)
{
BAutolock locker(fLock);
encoder_info* pinfo;
for (fEncoderList.Rewind(); fEncoderList.GetNext(&pinfo);) {
if (!strcmp(pinfo->ref.name, ref.name)) {
// We already know this encoder. When we reject encoders with
// the same name, we allow the user to overwrite system encoders
// in her home folder.
return;
}
}
// Get list of supported encoders...
encoder_info info;
info.ref = ref;
info.internalID = fNextEncoderCodecInfoID++;
int32 cookie = 0;
while (true) {
memset(&info.codecInfo, 0, sizeof(media_codec_info));
memset(&info.intputFormat, 0, sizeof(media_format));
memset(&info.outputFormat, 0, sizeof(media_format));
if (plugin->RegisterNextEncoder(&cookie,
&info.codecInfo, &info.formatFamily, &info.intputFormat,
&info.outputFormat) != B_OK) {
break;
}
info.codecInfo.id = info.internalID;
// NOTE: info.codecInfo.sub_id is for private use by the Encoder,
// we don't touch it, but it is maintained and passed back to the
// EncoderPlugin in NewEncoder(media_codec_info).
if (!fEncoderList.Insert(info))
break;
}
}
void
AddOnManager::_RegisterStreamer(StreamerPlugin* streamer, const entry_ref& ref)
{
BAutolock locker(fLock);
streamer_info* pInfo;
for (fStreamerList.Rewind(); fStreamerList.GetNext(&pInfo);) {
if (!strcmp(pInfo->ref.name, ref.name)) {
// We already know this streamer
return;
}
}
streamer_info info;
info.ref = ref;
fStreamerList.Insert(info);
}
bool
AddOnManager::_FindDecoder(const media_format& format, const BPath& path,
entry_ref* _decoderRef)
{
node_ref nref;
BDirectory directory;
if (directory.SetTo(path.Path()) != B_OK
|| directory.GetNodeRef(&nref) != B_OK) {
return false;
}
decoder_info* info;
for (fDecoderList.Rewind(); fDecoderList.GetNext(&info);) {
if (info->ref.directory != nref.node)
continue;
media_format* decoderFormat;
for (info->formats.Rewind(); info->formats.GetNext(&decoderFormat);) {
// check if the decoder matches the supplied format
if (!decoderFormat->Matches(&format))
continue;
*_decoderRef = info->ref;
return true;
}
}
return false;
}
bool
AddOnManager::_FindEncoder(const media_format& format, const BPath& path,
entry_ref* _encoderRef)
{
node_ref nref;
BDirectory directory;
if (directory.SetTo(path.Path()) != B_OK
|| directory.GetNodeRef(&nref) != B_OK) {
return false;
}
encoder_info* info;
for (fEncoderList.Rewind(); fEncoderList.GetNext(&info);) {
if (info->ref.directory != nref.node)
continue;
// check if the encoder matches the supplied format
if (info->outputFormat.Matches(&format)) {
*_encoderRef = info->ref;
return true;
}
continue;
}
return false;
}
void
AddOnManager::_GetReaders(const BPath& path, entry_ref* outRefs,
int32* outCount, int32 maxCount)
{
node_ref nref;
BDirectory directory;
if (directory.SetTo(path.Path()) != B_OK
|| directory.GetNodeRef(&nref) != B_OK) {
return;
}
reader_info* info;
for (fReaderList.Rewind(); fReaderList.GetNext(&info)
&& *outCount < maxCount;) {
if (info->ref.directory != nref.node)
continue;
outRefs[*outCount] = info->ref;
(*outCount)++;
}
}
} // namespace media
} // namespace BPrivate