xref: /haiku/src/apps/mediaplayer/supplier/MediaFileTrackSupplier.cpp (revision c90684742e7361651849be4116d0e5de3a817194)
1 /*
2  * Copyright 2010, Stephan Aßmus <superstippi@gmx.de>. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "MediaFileTrackSupplier.h"
8 
9 #include <new>
10 
11 #include <stdio.h>
12 #include <string.h>
13 
14 #include <MediaFile.h>
15 #include <MediaTrack.h>
16 #include <String.h>
17 
18 #include "MediaTrackAudioSupplier.h"
19 #include "MediaTrackVideoSupplier.h"
20 
21 
22 MediaFileTrackSupplier::MediaFileTrackSupplier(BMediaFile* mediaFile)
23 	:
24 	TrackSupplier(),
25 	fMediaFile(mediaFile)
26 {
27 	if (fMediaFile->InitCheck() != B_OK)
28 		return;
29 	int trackCount = fMediaFile->CountTracks();
30 	if (trackCount <= 0)
31 		return;
32 
33 	for (int i = 0; i < trackCount; i++) {
34 		BMediaTrack* track = fMediaFile->TrackAt(i);
35 		media_format format;
36 		status_t status = track->EncodedFormat(&format);
37 		if (status != B_OK) {
38 			fprintf(stderr, "MediaFileTrackSupplier: EncodedFormat failed for "
39 				"track index %d, error: %s\n", i, strerror(status));
40 			fMediaFile->ReleaseTrack(track);
41 			continue;
42 		}
43 
44 		if (track->Duration() <= 0) {
45 			fprintf(stderr, "MediaFileTrackSupplier: warning! track index %d "
46 				"has no duration\n", i);
47 		}
48 
49 		if (format.IsAudio()) {
50 			if (!fAudioTracks.AddItem(track)) {
51 				fMediaFile->ReleaseTrack(track);
52 				return;
53 			}
54 		} else if (format.IsVideo()) {
55 			if (!fVideoTracks.AddItem(track)) {
56 				fMediaFile->ReleaseTrack(track);
57 				return;
58 			}
59 		} else {
60 			printf("MediaFileTrackSupplier: track index %d has unknown "
61 				"type\n", i);
62 			fMediaFile->ReleaseTrack(track);
63 		}
64 	}
65 }
66 
67 
68 MediaFileTrackSupplier::~MediaFileTrackSupplier()
69 {
70 	delete fMediaFile;
71 		// BMediaFile destructor will call ReleaseAllTracks()
72  	for (int32 i = fSubTitleTracks.CountItems() - 1; i >= 0; i--)
73  		delete reinterpret_cast<SubTitles*>(fSubTitleTracks.ItemAtFast(i));
74  }
75 
76 
77 status_t
78 MediaFileTrackSupplier::InitCheck()
79 {
80 	return fMediaFile->InitCheck();
81 }
82 
83 
84 status_t
85 MediaFileTrackSupplier::GetFileFormatInfo(media_file_format* fileFormat)
86 {
87 	return fMediaFile->GetFileFormatInfo(fileFormat);
88 }
89 
90 
91 status_t
92 MediaFileTrackSupplier::GetCopyright(BString* copyright)
93 {
94 	*copyright = fMediaFile->Copyright();
95 	return B_OK;
96 }
97 
98 
99 status_t
100 MediaFileTrackSupplier::GetMetaData(BMessage* metaData)
101 {
102 	return fMediaFile->GetMetaData(metaData);
103 }
104 
105 
106 int32
107 MediaFileTrackSupplier::CountAudioTracks()
108 {
109 	return fAudioTracks.CountItems();
110 }
111 
112 
113 int32
114 MediaFileTrackSupplier::CountVideoTracks()
115 {
116 	return fVideoTracks.CountItems();
117 }
118 
119 
120 int32
121 MediaFileTrackSupplier::CountSubTitleTracks()
122 {
123 	return fSubTitleTracks.CountItems();
124 }
125 
126 
127 status_t
128 MediaFileTrackSupplier::GetAudioMetaData(int32 index, BMessage* metaData)
129 {
130 	BMediaTrack* track = (BMediaTrack*)fAudioTracks.ItemAt(index);
131 	if (track == NULL)
132 		return B_BAD_INDEX;
133 
134 	return track->GetMetaData(metaData);
135 }
136 
137 
138 status_t
139 MediaFileTrackSupplier::GetVideoMetaData(int32 index, BMessage* metaData)
140 {
141 	BMediaTrack* track = (BMediaTrack*)fVideoTracks.ItemAt(index);
142 	if (track == NULL)
143 		return B_BAD_INDEX;
144 
145 	return track->GetMetaData(metaData);
146 }
147 
148 
149 AudioTrackSupplier*
150 MediaFileTrackSupplier::CreateAudioTrackForIndex(int32 index)
151 {
152 	BMediaTrack* track = (BMediaTrack*)fAudioTracks.ItemAt(index);
153 	if (track == NULL)
154 		return NULL;
155 
156 	return new(std::nothrow) MediaTrackAudioSupplier(track, index);
157 }
158 
159 
160 VideoTrackSupplier*
161 MediaFileTrackSupplier::CreateVideoTrackForIndex(int32 index)
162 {
163 	BMediaTrack* track = (BMediaTrack*)fVideoTracks.ItemAt(index);
164 	if (track == NULL)
165 		return NULL;
166 
167 	status_t initStatus;
168 	MediaTrackVideoSupplier* supplier
169 		= new(std::nothrow) MediaTrackVideoSupplier(track, index, initStatus);
170 	if (initStatus != B_OK) {
171 		delete supplier;
172 		return NULL;
173 	}
174 
175 	return supplier;
176 }
177 
178 
179 const SubTitles*
180 MediaFileTrackSupplier::SubTitleTrackForIndex(int32 index)
181 {
182 	return reinterpret_cast<SubTitles*>(fSubTitleTracks.ItemAt(index));
183 }
184 
185 
186 bool
187 MediaFileTrackSupplier::AddSubTitles(SubTitles* subTitles)
188 {
189 	return fSubTitleTracks.AddItem(subTitles);
190 }
191 
192