2 * Copyright 2010-2011, Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Stephan Aßmus <superstippi@gmx.de>
9 #include "MediaFileTrackSupplier.h"
16 #include <MediaFile.h>
17 #include <MediaTrack.h>
20 #include "MediaTrackAudioSupplier.h"
21 #include "MediaTrackVideoSupplier.h"
22 #include "ImageTrackVideoSupplier.h"
25 MediaFileTrackSupplier::MediaFileTrackSupplier()
32 MediaFileTrackSupplier::~MediaFileTrackSupplier()
34 for (vector
<BMediaFile
*>::size_type i
= fMediaFiles
.size() - 1;
35 static_cast<int32
>(i
) >= 0; i
--) {
36 delete fMediaFiles
[i
];
37 // BMediaFile destructor will call ReleaseAllTracks()
40 for (vector
<BBitmap
*>::size_type i
= fBitmaps
.size() - 1;
41 static_cast<int32
>(i
) >= 0; i
--) {
45 for (int32 i
= fSubTitleTracks
.CountItems() - 1; i
>= 0; i
--)
46 delete reinterpret_cast<SubTitles
*>(fSubTitleTracks
.ItemAtFast(i
));
51 MediaFileTrackSupplier::InitCheck()
53 if (fMediaFiles
.empty())
56 return fMediaFiles
[0]->InitCheck();
61 MediaFileTrackSupplier::GetFileFormatInfo(media_file_format
* fileFormat
)
63 if (fMediaFiles
.empty())
66 return fMediaFiles
[0]->GetFileFormatInfo(fileFormat
);
71 MediaFileTrackSupplier::GetCopyright(BString
* copyright
)
73 if (fMediaFiles
.empty())
76 *copyright
= fMediaFiles
[0]->Copyright();
82 MediaFileTrackSupplier::GetMetaData(BMessage
* metaData
)
84 if (fMediaFiles
.empty())
87 return fMediaFiles
[0]->GetMetaData(metaData
);
92 MediaFileTrackSupplier::CountAudioTracks()
94 return fAudioTracks
.CountItems();
99 MediaFileTrackSupplier::CountVideoTracks()
101 return fVideoTracks
.CountItems() + fBitmaps
.size();
106 MediaFileTrackSupplier::CountSubTitleTracks()
108 return fSubTitleTracks
.CountItems();
113 MediaFileTrackSupplier::GetAudioMetaData(int32 index
, BMessage
* metaData
)
115 BMediaTrack
* track
= (BMediaTrack
*)fAudioTracks
.ItemAt(index
);
119 return track
->GetMetaData(metaData
);
124 MediaFileTrackSupplier::GetVideoMetaData(int32 index
, BMessage
* metaData
)
126 BMediaTrack
* track
= (BMediaTrack
*)fVideoTracks
.ItemAt(index
);
130 return track
->GetMetaData(metaData
);
135 MediaFileTrackSupplier::CreateAudioTrackForIndex(int32 index
)
137 BMediaTrack
* track
= (BMediaTrack
*)fAudioTracks
.ItemAt(index
);
141 return new(std::nothrow
) MediaTrackAudioSupplier(track
, index
);
146 MediaFileTrackSupplier::CreateVideoTrackForIndex(int32 index
)
148 VideoTrackSupplier
* supplier
;
151 if (fVideoTracks
.CountItems() <= index
152 && index
< fVideoTracks
.CountItems() + static_cast<int32
>(fBitmaps
.size())) {
153 supplier
= new(std::nothrow
) ImageTrackVideoSupplier(
154 fBitmaps
[index
- fVideoTracks
.CountItems()], index
, initStatus
);
156 BMediaTrack
* track
= (BMediaTrack
*)fVideoTracks
.ItemAt(index
);
160 supplier
= new(std::nothrow
) MediaTrackVideoSupplier(track
, index
,
164 if (initStatus
!= B_OK
) {
173 MediaFileTrackSupplier::SubTitleTrackForIndex(int32 index
)
175 return reinterpret_cast<SubTitles
*>(fSubTitleTracks
.ItemAt(index
));
180 MediaFileTrackSupplier::AddSubTitles(SubTitles
* subTitles
)
182 return fSubTitleTracks
.AddItem(subTitles
);
187 MediaFileTrackSupplier::AddMediaFile(BMediaFile
* mediaFile
)
189 if (mediaFile
->InitCheck() != B_OK
)
191 int trackCount
= mediaFile
->CountTracks();
195 status_t funcStatus
= B_ERROR
;
196 for (int i
= 0; i
< trackCount
; i
++) {
197 BMediaTrack
* track
= mediaFile
->TrackAt(i
);
199 status_t status
= track
->EncodedFormat(&format
);
200 if (status
!= B_OK
) {
201 fprintf(stderr
, "MediaFileTrackSupplier: EncodedFormat failed for "
202 "track index %d, error: %s\n", i
, strerror(status
));
203 mediaFile
->ReleaseTrack(track
);
207 if (track
->Duration() <= 0) {
208 fprintf(stderr
, "MediaFileTrackSupplier: warning! track index %d "
209 "has no duration\n", i
);
212 if (format
.IsAudio()) {
213 if (fAudioTracks
.AddItem(track
)) {
216 mediaFile
->ReleaseTrack(track
);
218 } else if (format
.IsVideo()) {
219 if (fVideoTracks
.AddItem(track
)) {
222 mediaFile
->ReleaseTrack(track
);
225 printf("MediaFileTrackSupplier: track index %d has unknown "
227 mediaFile
->ReleaseTrack(track
);
230 if (funcStatus
== B_OK
)
231 fMediaFiles
.push_back(mediaFile
);
237 MediaFileTrackSupplier::AddBitmap(BBitmap
* bitmap
)
239 fBitmaps
.push_back(bitmap
);