2 * Copyright 2009, Stephan Aßmus <superstippi@gmx.de>
3 * Copyright 2002-2004, Marcus Overhagen <marcus@overhagen.de>
4 * All rights reserved. Distributed under the terms of the MIT license.
15 #include <MediaTrack.h>
20 #include "MediaExtractor.h"
21 #include "MediaStreamer.h"
22 #include "MediaWriter.h"
25 BMediaFile::BMediaFile(const entry_ref
* ref
)
30 _InitReader(new(std::nothrow
) BFile(ref
, O_RDONLY
));
34 BMediaFile::BMediaFile(BDataIO
* source
)
42 BMediaFile::BMediaFile(const entry_ref
* ref
, int32 flags
)
47 _InitReader(new(std::nothrow
) BFile(ref
, O_RDONLY
), NULL
, flags
);
51 BMediaFile::BMediaFile(BDataIO
* source
, int32 flags
)
55 _InitReader(source
, NULL
, flags
);
59 BMediaFile::BMediaFile(const entry_ref
* ref
, const media_file_format
* mfi
,
65 _InitWriter(new(std::nothrow
) BFile(ref
, B_CREATE_FILE
| B_ERASE_FILE
66 | B_WRITE_ONLY
), NULL
, mfi
, flags
);
70 BMediaFile::BMediaFile(BDataIO
* destination
, const media_file_format
* mfi
,
75 _InitWriter(destination
, NULL
, mfi
, flags
);
79 // File will be set later by SetTo()
80 BMediaFile::BMediaFile(const media_file_format
* mfi
, int32 flags
)
82 debugger("BMediaFile::BMediaFile not implemented");
86 BMediaFile::BMediaFile(const BUrl
& url
)
91 _InitReader(NULL
, &url
);
95 BMediaFile::BMediaFile(const BUrl
& url
, int32 flags
)
100 _InitReader(NULL
, &url
, flags
);
104 BMediaFile::BMediaFile(const BUrl
& destination
, const media_file_format
* mfi
,
109 fDeleteSource
= true;
110 _InitWriter(NULL
, &destination
, mfi
, flags
);
111 // TODO: Implement streaming server support, it's
112 // a pretty complex thing compared to client mode
113 // and will require to expand the current BMediaFile
114 // design to be aware of it.
119 BMediaFile::SetTo(const entry_ref
* ref
)
127 fDeleteSource
= true;
128 _InitReader(new(std::nothrow
) BFile(ref
, O_RDONLY
));
135 BMediaFile::SetTo(BDataIO
* destination
)
139 if (destination
== NULL
)
143 _InitReader(destination
);
150 BMediaFile::SetTo(const BUrl
& url
)
155 _InitReader(NULL
, &url
);
161 BMediaFile::~BMediaFile()
170 BMediaFile::InitCheck() const
178 BMediaFile::GetFileFormatInfo(media_file_format
* mfi
) const
191 BMediaFile::GetMetaData(BMessage
* _data
) const
193 if (fExtractor
== NULL
)
200 return fExtractor
->GetMetaData(_data
);
205 BMediaFile::Copyright() const
207 return fExtractor
->Copyright();
212 BMediaFile::CountTracks() const
218 // Can be called multiple times with the same index. You must call
219 // ReleaseTrack() when you're done with a track.
221 BMediaFile::TrackAt(int32 index
)
224 if (fTrackList
== NULL
|| fExtractor
== NULL
225 || index
< 0 || index
>= fTrackNum
) {
228 if (fTrackList
[index
] == NULL
) {
229 TRACE("BMediaFile::TrackAt, creating new track for index %"
230 B_PRId32
"\n", index
);
231 fTrackList
[index
] = new(std::nothrow
) BMediaTrack(fExtractor
, index
);
232 TRACE("BMediaFile::TrackAt, new track is %p\n", fTrackList
[index
]);
234 return fTrackList
[index
];
238 // Release the resource used by a given BMediaTrack object, to reduce
239 // the memory usage of your application. The specific 'track' object
240 // can no longer be used, but you can create another one by calling
241 // TrackAt() with the same track index.
243 BMediaFile::ReleaseTrack(BMediaTrack
* track
)
246 if (!fTrackList
|| !track
)
248 for (int32 i
= 0; i
< fTrackNum
; i
++) {
249 if (fTrackList
[i
] == track
) {
250 TRACE("BMediaFile::ReleaseTrack, releasing track %p with index "
251 "%" B_PRId32
"\n", track
, i
);
253 fTrackList
[i
] = NULL
;
257 fprintf(stderr
, "BMediaFile::ReleaseTrack track %p not found\n", track
);
263 BMediaFile::ReleaseAllTracks()
268 for (int32 i
= 0; i
< fTrackNum
; i
++) {
270 TRACE("BMediaFile::ReleaseAllTracks, releasing track %p with "
271 "index %" B_PRId32
"\n", fTrackList
[i
], i
);
272 delete fTrackList
[i
];
273 fTrackList
[i
] = NULL
;
280 // Create and add a track to the media file
282 BMediaFile::CreateTrack(media_format
* mediaFormat
,
283 const media_codec_info
* codecInfo
, uint32 flags
)
285 if (mediaFormat
== NULL
)
288 // NOTE: It is allowed to pass NULL for codecInfo. In that case, the
289 // track won't have an Encoder and you can only use WriteChunk() with
290 // already encoded data.
292 // Make room for the new track.
293 BMediaTrack
** trackList
= (BMediaTrack
**)realloc(fTrackList
,
294 (fTrackNum
+ 1) * sizeof(BMediaTrack
*));
295 if (trackList
== NULL
)
298 int32 streamIndex
= fTrackNum
;
299 fTrackList
= trackList
;
302 BMediaTrack
* track
= new(std::nothrow
) BMediaTrack(fWriter
, streamIndex
,
303 mediaFormat
, codecInfo
);
305 fTrackList
[streamIndex
] = track
;
311 // Create and add a raw track to the media file (it has no encoder)
313 BMediaFile::CreateTrack(media_format
* mf
, uint32 flags
)
315 return CreateTrack(mf
, NULL
, flags
);
319 // For BeOS R5 compatibility
320 extern "C" BMediaTrack
*
321 CreateTrack__10BMediaFileP12media_formatPC16media_codec_info(
322 BMediaFile
* self
, media_format
* mf
, const media_codec_info
* mci
);
324 CreateTrack__10BMediaFileP12media_formatPC16media_codec_info(BMediaFile
* self
,
325 media_format
* mf
, const media_codec_info
* mci
)
327 return self
->CreateTrack(mf
, mci
, 0);
331 // For BeOS R5 compatibility
332 extern "C" BMediaTrack
* CreateTrack__10BMediaFileP12media_format(
333 BMediaFile
* self
, media_format
* mf
);
335 CreateTrack__10BMediaFileP12media_format(BMediaFile
* self
, media_format
* mf
)
337 return self
->CreateTrack(mf
, NULL
, 0);
341 // Lets you set the copyright info for the entire file
343 BMediaFile::AddCopyright(const char* copyright
)
348 return fWriter
->SetCopyright(copyright
);
352 // Call this to add user-defined chunks to a file (if they're supported)
354 BMediaFile::AddChunk(int32 type
, const void* data
, size_t size
)
361 // After you have added all the tracks you want, call this
363 BMediaFile::CommitHeader()
368 return fWriter
->CommitHeader();
372 // After you have written all the data to the track objects, call this
374 BMediaFile::CloseFile()
379 return fWriter
->Close();
382 // This is for controlling file format parameters
384 // returns a copy of the parameter web
386 BMediaFile::GetParameterWeb(BParameterWeb
** outWeb
)
393 // deprecated BeOS R5 API
403 BMediaFile::GetParameterValue(int32 id
, void* value
, size_t* size
)
411 BMediaFile::SetParameterValue(int32 id
, const void* value
, size_t size
)
419 BMediaFile::GetParameterView()
427 BMediaFile::Perform(int32 selector
, void* data
)
435 BMediaFile::ControlFile(int32 selector
, void* ioData
, size_t size
)
442 // #pragma mark - private
458 fDeleteSource
= false;
468 BMediaFile::_UnInit()
475 // Tells the extractor to stop its asynchronous processing
476 // before deleting its source
477 if (fExtractor
!= NULL
)
478 fExtractor
->StopProcessing();
483 fDeleteSource
= false;
486 // Deleting the extractor or writer can cause unloading of the plugins.
487 // The source must be deleted before that, because it can come from a
488 // plugin (for example the http_streamer)
499 BMediaFile::_InitReader(BDataIO
* source
, const BUrl
* url
, int32 flags
)
503 if (source
== NULL
&& url
== NULL
) {
509 _InitStreamer(*url
, &source
);
510 else if (BFile
* file
= dynamic_cast<BFile
*>(source
))
511 fErr
= file
->InitCheck();
516 fExtractor
= new(std::nothrow
) MediaExtractor(source
, flags
);
518 if (fExtractor
== NULL
)
521 fErr
= fExtractor
->InitCheck();
526 // Get the actual source from the extractor
527 fSource
= fExtractor
->Source();
529 fExtractor
->GetFileFormatInfo(&fMFI
);
530 fTrackNum
= fExtractor
->StreamCount();
531 fTrackList
= (BMediaTrack
**)malloc(fTrackNum
* sizeof(BMediaTrack
*));
532 if (fTrackList
== NULL
) {
536 memset(fTrackList
, 0, fTrackNum
* sizeof(BMediaTrack
*));
541 BMediaFile::_InitWriter(BDataIO
* target
, const BUrl
* url
,
542 const media_file_format
* fileFormat
, int32 flags
)
546 if (fileFormat
== NULL
) {
551 if (target
== NULL
&& url
== NULL
) {
558 if (target
== NULL
) {
559 _InitStreamer(*url
, &target
);
564 fWriter
= new(std::nothrow
) MediaWriter(target
, fMFI
);
569 fErr
= fWriter
->InitCheck();
573 // Get the actual source from the writer
574 fSource
= fWriter
->Target();
580 BMediaFile::_InitStreamer(const BUrl
& url
, BDataIO
** adapter
)
582 if (fStreamer
!= NULL
)
585 TRACE(url
.UrlString());
587 fStreamer
= new(std::nothrow
) MediaStreamer(url
);
588 if (fStreamer
== NULL
) {
593 fErr
= fStreamer
->CreateAdapter(adapter
);
598 BMediaFile::BMediaFile();
599 BMediaFile::BMediaFile(const BMediaFile&);
600 BMediaFile::BMediaFile& operator=(const BMediaFile&);
603 status_t
BMediaFile::_Reserved_BMediaFile_0(int32 arg
, ...) { return B_ERROR
; }
604 status_t
BMediaFile::_Reserved_BMediaFile_1(int32 arg
, ...) { return B_ERROR
; }
605 status_t
BMediaFile::_Reserved_BMediaFile_2(int32 arg
, ...) { return B_ERROR
; }
606 status_t
BMediaFile::_Reserved_BMediaFile_3(int32 arg
, ...) { return B_ERROR
; }
607 status_t
BMediaFile::_Reserved_BMediaFile_4(int32 arg
, ...) { return B_ERROR
; }
608 status_t
BMediaFile::_Reserved_BMediaFile_5(int32 arg
, ...) { return B_ERROR
; }
609 status_t
BMediaFile::_Reserved_BMediaFile_6(int32 arg
, ...) { return B_ERROR
; }
610 status_t
BMediaFile::_Reserved_BMediaFile_7(int32 arg
, ...) { return B_ERROR
; }
611 status_t
BMediaFile::_Reserved_BMediaFile_8(int32 arg
, ...) { return B_ERROR
; }
612 status_t
BMediaFile::_Reserved_BMediaFile_9(int32 arg
, ...) { return B_ERROR
; }
613 status_t
BMediaFile::_Reserved_BMediaFile_10(int32 arg
, ...) { return B_ERROR
; }
614 status_t
BMediaFile::_Reserved_BMediaFile_11(int32 arg
, ...) { return B_ERROR
; }
615 status_t
BMediaFile::_Reserved_BMediaFile_12(int32 arg
, ...) { return B_ERROR
; }
616 status_t
BMediaFile::_Reserved_BMediaFile_13(int32 arg
, ...) { return B_ERROR
; }
617 status_t
BMediaFile::_Reserved_BMediaFile_14(int32 arg
, ...) { return B_ERROR
; }
618 status_t
BMediaFile::_Reserved_BMediaFile_15(int32 arg
, ...) { return B_ERROR
; }
619 status_t
BMediaFile::_Reserved_BMediaFile_16(int32 arg
, ...) { return B_ERROR
; }
620 status_t
BMediaFile::_Reserved_BMediaFile_17(int32 arg
, ...) { return B_ERROR
; }
621 status_t
BMediaFile::_Reserved_BMediaFile_18(int32 arg
, ...) { return B_ERROR
; }
622 status_t
BMediaFile::_Reserved_BMediaFile_19(int32 arg
, ...) { return B_ERROR
; }
623 status_t
BMediaFile::_Reserved_BMediaFile_20(int32 arg
, ...) { return B_ERROR
; }
624 status_t
BMediaFile::_Reserved_BMediaFile_21(int32 arg
, ...) { return B_ERROR
; }
625 status_t
BMediaFile::_Reserved_BMediaFile_22(int32 arg
, ...) { return B_ERROR
; }
626 status_t
BMediaFile::_Reserved_BMediaFile_23(int32 arg
, ...) { return B_ERROR
; }
627 status_t
BMediaFile::_Reserved_BMediaFile_24(int32 arg
, ...) { return B_ERROR
; }
628 status_t
BMediaFile::_Reserved_BMediaFile_25(int32 arg
, ...) { return B_ERROR
; }
629 status_t
BMediaFile::_Reserved_BMediaFile_26(int32 arg
, ...) { return B_ERROR
; }
630 status_t
BMediaFile::_Reserved_BMediaFile_27(int32 arg
, ...) { return B_ERROR
; }
631 status_t
BMediaFile::_Reserved_BMediaFile_28(int32 arg
, ...) { return B_ERROR
; }
632 status_t
BMediaFile::_Reserved_BMediaFile_29(int32 arg
, ...) { return B_ERROR
; }
633 status_t
BMediaFile::_Reserved_BMediaFile_30(int32 arg
, ...) { return B_ERROR
; }
634 status_t
BMediaFile::_Reserved_BMediaFile_31(int32 arg
, ...) { return B_ERROR
; }
635 status_t
BMediaFile::_Reserved_BMediaFile_32(int32 arg
, ...) { return B_ERROR
; }
636 status_t
BMediaFile::_Reserved_BMediaFile_33(int32 arg
, ...) { return B_ERROR
; }
637 status_t
BMediaFile::_Reserved_BMediaFile_34(int32 arg
, ...) { return B_ERROR
; }
638 status_t
BMediaFile::_Reserved_BMediaFile_35(int32 arg
, ...) { return B_ERROR
; }
639 status_t
BMediaFile::_Reserved_BMediaFile_36(int32 arg
, ...) { return B_ERROR
; }
640 status_t
BMediaFile::_Reserved_BMediaFile_37(int32 arg
, ...) { return B_ERROR
; }
641 status_t
BMediaFile::_Reserved_BMediaFile_38(int32 arg
, ...) { return B_ERROR
; }
642 status_t
BMediaFile::_Reserved_BMediaFile_39(int32 arg
, ...) { return B_ERROR
; }
643 status_t
BMediaFile::_Reserved_BMediaFile_40(int32 arg
, ...) { return B_ERROR
; }
644 status_t
BMediaFile::_Reserved_BMediaFile_41(int32 arg
, ...) { return B_ERROR
; }
645 status_t
BMediaFile::_Reserved_BMediaFile_42(int32 arg
, ...) { return B_ERROR
; }
646 status_t
BMediaFile::_Reserved_BMediaFile_43(int32 arg
, ...) { return B_ERROR
; }
647 status_t
BMediaFile::_Reserved_BMediaFile_44(int32 arg
, ...) { return B_ERROR
; }
648 status_t
BMediaFile::_Reserved_BMediaFile_45(int32 arg
, ...) { return B_ERROR
; }
649 status_t
BMediaFile::_Reserved_BMediaFile_46(int32 arg
, ...) { return B_ERROR
; }
650 status_t
BMediaFile::_Reserved_BMediaFile_47(int32 arg
, ...) { return B_ERROR
; }