2003-11-24 01:19:21 +03:00
|
|
|
#include "MediaExtractor.h"
|
2003-12-02 02:33:35 +03:00
|
|
|
#include "PluginManager.h"
|
2003-12-05 04:18:51 +03:00
|
|
|
#include "debug.h"
|
2004-01-23 10:11:15 +03:00
|
|
|
|
|
|
|
#include <Autolock.h>
|
|
|
|
|
2003-11-24 02:50:27 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2004-10-24 22:46:07 +04:00
|
|
|
// should be 0, to disable the chunk cache set it to 1
|
|
|
|
#define DISABLE_CHUNK_CACHE 0
|
2004-01-23 10:11:15 +03:00
|
|
|
|
|
|
|
MediaExtractor::MediaExtractor(BDataIO *source, int32 flags)
|
2003-11-24 01:19:21 +03:00
|
|
|
{
|
2003-12-05 04:18:51 +03:00
|
|
|
CALLED();
|
2003-11-24 02:50:27 +03:00
|
|
|
fSource = source;
|
|
|
|
fStreamInfo = 0;
|
2004-10-24 18:20:01 +04:00
|
|
|
fExtractorThread = -1;
|
|
|
|
fExtractorWaitSem = -1;
|
|
|
|
fTerminateExtractor = false;
|
2004-01-23 10:11:15 +03:00
|
|
|
|
2003-11-24 02:50:27 +03:00
|
|
|
fErr = _CreateReader(&fReader, &fStreamCount, &fMff, source);
|
2003-12-09 01:16:03 +03:00
|
|
|
if (fErr) {
|
|
|
|
fStreamCount = 0;
|
|
|
|
fReader = 0;
|
2003-11-24 02:50:27 +03:00
|
|
|
return;
|
2003-12-09 01:16:03 +03:00
|
|
|
}
|
2004-01-23 10:11:15 +03:00
|
|
|
|
2003-11-24 02:50:27 +03:00
|
|
|
fStreamInfo = new stream_info[fStreamCount];
|
2004-01-23 10:11:15 +03:00
|
|
|
|
2003-11-24 02:50:27 +03:00
|
|
|
// initialize stream infos
|
|
|
|
for (int32 i = 0; i < fStreamCount; i++) {
|
|
|
|
fStreamInfo[i].status = B_OK;
|
|
|
|
fStreamInfo[i].cookie = 0;
|
2004-02-29 21:50:23 +03:00
|
|
|
fStreamInfo[i].hasCookie = true;
|
2003-11-24 02:50:27 +03:00
|
|
|
fStreamInfo[i].infoBuffer = 0;
|
|
|
|
fStreamInfo[i].infoBufferSize = 0;
|
2004-10-24 18:20:01 +04:00
|
|
|
fStreamInfo[i].chunkCache = new ChunkCache;
|
2003-11-24 02:50:27 +03:00
|
|
|
memset(&fStreamInfo[i].encodedFormat, 0, sizeof(fStreamInfo[i].encodedFormat));
|
|
|
|
}
|
2004-01-23 10:11:15 +03:00
|
|
|
|
2003-11-24 02:50:27 +03:00
|
|
|
// create all stream cookies
|
|
|
|
for (int32 i = 0; i < fStreamCount; i++) {
|
|
|
|
if (B_OK != fReader->AllocateCookie(i, &fStreamInfo[i].cookie)) {
|
|
|
|
fStreamInfo[i].cookie = 0;
|
2004-02-29 21:50:23 +03:00
|
|
|
fStreamInfo[i].hasCookie = false;
|
2003-11-24 02:50:27 +03:00
|
|
|
fStreamInfo[i].status = B_ERROR;
|
|
|
|
printf("MediaExtractor::MediaExtractor: AllocateCookie for stream %ld failed\n", i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get info for all streams
|
|
|
|
for (int32 i = 0; i < fStreamCount; i++) {
|
|
|
|
if (fStreamInfo[i].status != B_OK)
|
|
|
|
continue;
|
|
|
|
int64 frameCount;
|
|
|
|
bigtime_t duration;
|
|
|
|
if (B_OK != fReader->GetStreamInfo(fStreamInfo[i].cookie, &frameCount, &duration,
|
|
|
|
&fStreamInfo[i].encodedFormat,
|
|
|
|
&fStreamInfo[i].infoBuffer,
|
|
|
|
&fStreamInfo[i].infoBufferSize)) {
|
|
|
|
fStreamInfo[i].status = B_ERROR;
|
|
|
|
printf("MediaExtractor::MediaExtractor: GetStreamInfo for stream %ld failed\n", i);
|
|
|
|
}
|
|
|
|
}
|
2004-10-24 18:20:01 +04:00
|
|
|
|
2004-10-24 22:46:07 +04:00
|
|
|
#if DISABLE_CHUNK_CACHE == 0
|
2004-10-24 18:20:01 +04:00
|
|
|
// start extractor thread
|
2004-10-24 21:15:50 +04:00
|
|
|
fExtractorWaitSem = create_sem(1, "media extractor thread sem");
|
2004-10-24 18:20:01 +04:00
|
|
|
fExtractorThread = spawn_thread(extractor_thread, "media extractor thread", 10, this);
|
2004-10-24 22:46:07 +04:00
|
|
|
resume_thread(fExtractorThread);
|
|
|
|
#endif
|
2003-11-24 01:19:21 +03:00
|
|
|
}
|
|
|
|
|
2004-10-24 18:20:01 +04:00
|
|
|
|
2003-11-24 01:19:21 +03:00
|
|
|
MediaExtractor::~MediaExtractor()
|
|
|
|
{
|
2003-12-05 04:18:51 +03:00
|
|
|
CALLED();
|
2004-10-24 18:20:01 +04:00
|
|
|
|
|
|
|
// terminate extractor thread
|
|
|
|
fTerminateExtractor = true;
|
|
|
|
release_sem(fExtractorWaitSem);
|
|
|
|
status_t err;
|
|
|
|
wait_for_thread(fExtractorThread, &err);
|
|
|
|
delete_sem(fExtractorWaitSem);
|
2003-12-06 01:11:52 +03:00
|
|
|
|
2003-11-24 02:50:27 +03:00
|
|
|
// free all stream cookies
|
2004-10-24 18:20:01 +04:00
|
|
|
// and chunk caches
|
2003-11-24 02:50:27 +03:00
|
|
|
for (int32 i = 0; i < fStreamCount; i++) {
|
2004-02-29 21:50:23 +03:00
|
|
|
if (fStreamInfo[i].hasCookie)
|
2003-11-24 02:50:27 +03:00
|
|
|
fReader->FreeCookie(fStreamInfo[i].cookie);
|
2004-10-24 18:20:01 +04:00
|
|
|
delete fStreamInfo[i].chunkCache;
|
2003-11-24 02:50:27 +03:00
|
|
|
}
|
|
|
|
|
2003-12-02 02:33:35 +03:00
|
|
|
if (fReader)
|
|
|
|
_DestroyReader(fReader);
|
|
|
|
|
2003-12-06 01:11:52 +03:00
|
|
|
delete [] fStreamInfo;
|
2003-12-06 01:27:17 +03:00
|
|
|
// fSource is owned by the BMediaFile
|
2003-11-24 01:19:21 +03:00
|
|
|
}
|
|
|
|
|
2004-10-24 18:20:01 +04:00
|
|
|
|
2003-11-24 01:19:21 +03:00
|
|
|
status_t
|
|
|
|
MediaExtractor::InitCheck()
|
|
|
|
{
|
2003-12-05 04:18:51 +03:00
|
|
|
CALLED();
|
2003-11-24 02:50:27 +03:00
|
|
|
return fErr;
|
2003-11-24 01:19:21 +03:00
|
|
|
}
|
|
|
|
|
2004-10-24 18:20:01 +04:00
|
|
|
|
2003-11-24 01:19:21 +03:00
|
|
|
void
|
|
|
|
MediaExtractor::GetFileFormatInfo(media_file_format *mfi) const
|
|
|
|
{
|
2003-12-05 04:18:51 +03:00
|
|
|
CALLED();
|
2003-11-24 02:50:27 +03:00
|
|
|
*mfi = fMff;
|
2003-11-24 01:19:21 +03:00
|
|
|
}
|
|
|
|
|
2004-10-24 18:20:01 +04:00
|
|
|
|
2003-11-24 01:19:21 +03:00
|
|
|
int32
|
|
|
|
MediaExtractor::StreamCount()
|
|
|
|
{
|
2003-12-05 04:18:51 +03:00
|
|
|
CALLED();
|
2003-11-24 02:50:27 +03:00
|
|
|
return fStreamCount;
|
2003-11-24 01:19:21 +03:00
|
|
|
}
|
|
|
|
|
2004-10-24 18:20:01 +04:00
|
|
|
|
2003-12-07 02:21:23 +03:00
|
|
|
const media_format *
|
2003-11-24 01:19:21 +03:00
|
|
|
MediaExtractor::EncodedFormat(int32 stream)
|
|
|
|
{
|
2003-11-24 02:50:27 +03:00
|
|
|
return &fStreamInfo[stream].encodedFormat;
|
2003-11-24 01:19:21 +03:00
|
|
|
}
|
|
|
|
|
2004-10-24 18:20:01 +04:00
|
|
|
|
2003-11-24 01:19:21 +03:00
|
|
|
int64
|
|
|
|
MediaExtractor::CountFrames(int32 stream) const
|
|
|
|
{
|
2003-12-05 04:18:51 +03:00
|
|
|
CALLED();
|
2003-11-24 02:50:27 +03:00
|
|
|
int64 frameCount;
|
|
|
|
bigtime_t duration;
|
|
|
|
media_format format;
|
|
|
|
void *infoBuffer;
|
|
|
|
int32 infoSize;
|
|
|
|
|
|
|
|
fReader->GetStreamInfo(fStreamInfo[stream].cookie, &frameCount, &duration, &format, &infoBuffer, &infoSize);
|
|
|
|
|
|
|
|
return frameCount;
|
2003-11-24 01:19:21 +03:00
|
|
|
}
|
|
|
|
|
2004-10-24 18:20:01 +04:00
|
|
|
|
2003-11-24 01:19:21 +03:00
|
|
|
bigtime_t
|
|
|
|
MediaExtractor::Duration(int32 stream) const
|
|
|
|
{
|
2003-12-05 04:18:51 +03:00
|
|
|
CALLED();
|
2003-11-24 02:50:27 +03:00
|
|
|
int64 frameCount;
|
|
|
|
bigtime_t duration;
|
|
|
|
media_format format;
|
|
|
|
void *infoBuffer;
|
|
|
|
int32 infoSize;
|
|
|
|
|
|
|
|
fReader->GetStreamInfo(fStreamInfo[stream].cookie, &frameCount, &duration, &format, &infoBuffer, &infoSize);
|
|
|
|
|
|
|
|
return duration;
|
2003-11-24 01:19:21 +03:00
|
|
|
}
|
|
|
|
|
2004-10-24 18:20:01 +04:00
|
|
|
|
2003-11-24 01:19:21 +03:00
|
|
|
status_t
|
|
|
|
MediaExtractor::Seek(int32 stream, uint32 seekTo,
|
|
|
|
int64 *frame, bigtime_t *time)
|
|
|
|
{
|
2003-12-05 04:18:51 +03:00
|
|
|
CALLED();
|
2004-11-07 20:04:53 +03:00
|
|
|
if (fStreamInfo[stream].status != B_OK)
|
|
|
|
return fStreamInfo[stream].status;
|
|
|
|
|
2003-11-24 02:50:27 +03:00
|
|
|
status_t result;
|
|
|
|
result = fReader->Seek(fStreamInfo[stream].cookie, seekTo, frame, time);
|
|
|
|
if (result != B_OK)
|
|
|
|
return result;
|
|
|
|
|
2004-10-24 18:20:01 +04:00
|
|
|
// clear buffered chunks
|
|
|
|
fStreamInfo[stream].chunkCache->MakeEmpty();
|
|
|
|
release_sem(fExtractorWaitSem);
|
|
|
|
|
2003-12-05 04:18:51 +03:00
|
|
|
return B_OK;
|
2003-11-24 01:19:21 +03:00
|
|
|
}
|
|
|
|
|
2004-10-24 18:20:01 +04:00
|
|
|
|
2003-11-24 01:19:21 +03:00
|
|
|
status_t
|
|
|
|
MediaExtractor::GetNextChunk(int32 stream,
|
|
|
|
void **chunkBuffer, int32 *chunkSize,
|
|
|
|
media_header *mediaHeader)
|
|
|
|
{
|
2004-11-07 20:04:53 +03:00
|
|
|
if (fStreamInfo[stream].status != B_OK)
|
|
|
|
return fStreamInfo[stream].status;
|
|
|
|
|
2004-10-24 22:46:07 +04:00
|
|
|
#if DISABLE_CHUNK_CACHE > 0
|
|
|
|
static BLocker locker;
|
|
|
|
BAutolock lock(locker);
|
|
|
|
return fReader->GetNextChunk(fStreamInfo[stream].cookie, chunkBuffer, chunkSize, mediaHeader);
|
|
|
|
#endif
|
|
|
|
|
2004-10-24 18:20:01 +04:00
|
|
|
status_t err;
|
|
|
|
err = fStreamInfo[stream].chunkCache->GetNextChunk(chunkBuffer, chunkSize, mediaHeader);
|
|
|
|
release_sem(fExtractorWaitSem);
|
|
|
|
return err;
|
2003-11-24 01:19:21 +03:00
|
|
|
}
|
|
|
|
|
2004-10-24 18:20:01 +04:00
|
|
|
|
2004-10-23 22:04:50 +04:00
|
|
|
class MediaExtractorChunkProvider : public ChunkProvider
|
|
|
|
{
|
2004-01-18 10:37:52 +03:00
|
|
|
private:
|
|
|
|
MediaExtractor * fExtractor;
|
|
|
|
int32 fStream;
|
|
|
|
public:
|
2004-10-23 22:04:50 +04:00
|
|
|
MediaExtractorChunkProvider(MediaExtractor * extractor, int32 stream)
|
|
|
|
{
|
2004-01-18 10:37:52 +03:00
|
|
|
fExtractor = extractor;
|
|
|
|
fStream = stream;
|
|
|
|
}
|
2004-10-23 22:04:50 +04:00
|
|
|
|
2004-01-18 10:37:52 +03:00
|
|
|
virtual status_t GetNextChunk(void **chunkBuffer, int32 *chunkSize,
|
2004-10-23 22:04:50 +04:00
|
|
|
media_header *mediaHeader)
|
|
|
|
{
|
|
|
|
return fExtractor->GetNextChunk(fStream, chunkBuffer, chunkSize, mediaHeader);
|
2004-01-18 10:37:52 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2004-10-24 18:20:01 +04:00
|
|
|
|
2003-11-24 01:19:21 +03:00
|
|
|
status_t
|
|
|
|
MediaExtractor::CreateDecoder(int32 stream, Decoder **decoder, media_codec_info *mci)
|
2003-11-24 02:50:27 +03:00
|
|
|
{
|
2003-12-05 04:18:51 +03:00
|
|
|
CALLED();
|
2003-12-09 01:16:03 +03:00
|
|
|
status_t res;
|
|
|
|
|
|
|
|
res = fStreamInfo[stream].status;
|
|
|
|
if (res != B_OK) {
|
2003-11-24 02:50:27 +03:00
|
|
|
printf("MediaExtractor::CreateDecoder can't create decoder for stream %ld\n", stream);
|
2003-12-09 01:16:03 +03:00
|
|
|
return res;
|
2003-11-24 02:50:27 +03:00
|
|
|
}
|
|
|
|
|
2004-02-29 21:50:23 +03:00
|
|
|
res = _CreateDecoder(decoder, fStreamInfo[stream].encodedFormat);
|
2003-12-09 01:16:03 +03:00
|
|
|
if (res != B_OK) {
|
2003-11-24 02:50:27 +03:00
|
|
|
printf("MediaExtractor::CreateDecoder failed for stream %ld\n", stream);
|
2003-12-09 01:16:03 +03:00
|
|
|
return res;
|
2003-11-24 02:50:27 +03:00
|
|
|
}
|
|
|
|
|
2004-01-23 10:11:15 +03:00
|
|
|
(*decoder)->Setup(new MediaExtractorChunkProvider(this, stream));
|
2003-11-24 02:50:27 +03:00
|
|
|
|
2003-12-07 02:21:23 +03:00
|
|
|
res = (*decoder)->Setup(&fStreamInfo[stream].encodedFormat, fStreamInfo[stream].infoBuffer , fStreamInfo[stream].infoBufferSize);
|
|
|
|
if (res != B_OK) {
|
2004-01-23 10:11:15 +03:00
|
|
|
printf("MediaExtractor::CreateDecoder Setup failed for stream %ld: %ld (%s)\n",
|
|
|
|
stream, res, strerror(res));
|
2004-02-16 23:54:35 +03:00
|
|
|
return res;
|
2003-12-07 02:21:23 +03:00
|
|
|
}
|
2004-02-16 23:54:35 +03:00
|
|
|
|
2004-01-31 04:00:11 +03:00
|
|
|
(*decoder)->GetCodecInfo(mci);
|
2004-10-24 18:20:01 +04:00
|
|
|
|
2004-02-16 23:54:35 +03:00
|
|
|
return B_OK;
|
2003-11-24 02:50:27 +03:00
|
|
|
}
|
|
|
|
|
2004-10-24 18:20:01 +04:00
|
|
|
|
|
|
|
int32
|
|
|
|
MediaExtractor::extractor_thread(void *arg)
|
|
|
|
{
|
|
|
|
static_cast<MediaExtractor *>(arg)->ExtractorThread();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaExtractor::ExtractorThread()
|
|
|
|
{
|
|
|
|
for (;;) {
|
|
|
|
acquire_sem(fExtractorWaitSem);
|
|
|
|
if (fTerminateExtractor)
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool refill_done;
|
|
|
|
do {
|
|
|
|
refill_done = false;
|
|
|
|
for (int32 stream = 0; stream < fStreamCount; stream++) {
|
2004-11-07 20:04:53 +03:00
|
|
|
if (fStreamInfo[stream].status != B_OK)
|
|
|
|
continue;
|
2004-10-24 18:20:01 +04:00
|
|
|
if (fStreamInfo[stream].chunkCache->NeedsRefill()) {
|
|
|
|
media_header mediaHeader;
|
|
|
|
void *chunkBuffer;
|
|
|
|
int32 chunkSize;
|
|
|
|
status_t err;
|
|
|
|
err = fReader->GetNextChunk(fStreamInfo[stream].cookie, &chunkBuffer, &chunkSize, &mediaHeader);
|
|
|
|
fStreamInfo[stream].chunkCache->PutNextChunk(chunkBuffer, chunkSize, mediaHeader, err);
|
|
|
|
refill_done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fTerminateExtractor)
|
|
|
|
return;
|
|
|
|
} while (refill_done);
|
|
|
|
}
|
|
|
|
}
|