xref: /haiku/src/kits/media/MediaFile.cpp (revision cfc3fa87da824bdf593eb8b817a83b6376e77935)
1 /*
2  * Copyright (c) 2002-2004, Marcus Overhagen <marcus@overhagen.de>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  *  * Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright notice,
11  *    this list of conditions and the following disclaimer in the documentation
12  *    and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include <MediaFile.h>
27 #include <MediaTrack.h>
28 #include <File.h>
29 #include <string.h>
30 #include "MediaExtractor.h"
31 #include "debug.h"
32 
33 
34 /*************************************************************
35  * public BMediaFile
36  *************************************************************/
37 
38 
39 BMediaFile::BMediaFile(const entry_ref *ref)
40 {
41 	CALLED();
42 	Init();
43 	fDeleteSource = true;
44 	InitReader(new BFile(ref, O_RDONLY));
45 }
46 
47 
48 BMediaFile::BMediaFile(BDataIO * source)
49 {
50 	CALLED();
51 	Init();
52 	InitReader(source);
53 }
54 
55 
56 BMediaFile::BMediaFile(const entry_ref * ref,
57 					   int32 flags)
58 {
59 	CALLED();
60 	Init();
61 	fDeleteSource = true;
62 	InitReader(new BFile(ref, O_RDONLY), flags);
63 }
64 
65 
66 BMediaFile::BMediaFile(BDataIO * source,
67 					   int32 flags)
68 {
69 	CALLED();
70 	Init();
71 	InitReader(source, flags);
72 }
73 
74 
75 BMediaFile::BMediaFile(const entry_ref *ref,
76 					   const media_file_format * mfi,
77 					   int32 flags)
78 {
79 	CALLED();
80 	Init();
81 	fDeleteSource = true;
82 	InitWriter(new BFile(ref, O_WRONLY), mfi, flags);
83 }
84 
85 
86 BMediaFile::BMediaFile(BDataIO	*destination,
87 					   const media_file_format * mfi,
88 					   int32 flags)
89 {
90 	CALLED();
91 	Init();
92 	InitWriter(destination, mfi, flags);
93 }
94 
95 
96 // File will be set later by SetTo()
97 BMediaFile::BMediaFile(const media_file_format * mfi,
98 					   int32 flags)
99 {
100 	debugger("BMediaFile::BMediaFile not implemented");
101 }
102 
103 
104 status_t
105 BMediaFile::SetTo(const entry_ref *ref)
106 {
107 	debugger("BMediaFile::SetTo not implemented");
108 	return B_OK;
109 }
110 
111 
112 status_t
113 BMediaFile::SetTo(BDataIO *destination)
114 {
115 	debugger("BMediaFile::SetTo not implemented");
116 	return B_OK;
117 }
118 
119 
120 /* virtual */
121 BMediaFile::~BMediaFile()
122 {
123 	CALLED();
124 
125 	ReleaseAllTracks();
126 	delete[] fTrackList;
127 	delete fExtractor;
128 	if (fDeleteSource)
129 		delete fSource;
130 }
131 
132 
133 status_t
134 BMediaFile::InitCheck() const
135 {
136 	CALLED();
137 	return fErr;
138 }
139 
140 
141 // Get info about the underlying file format.
142 status_t
143 BMediaFile::GetFileFormatInfo(media_file_format *mfi) const
144 {
145 	CALLED();
146 	if (fErr)
147 		return B_ERROR;
148 	*mfi = fMFI;
149 	return B_OK;
150 }
151 
152 
153 const char *
154 BMediaFile::Copyright(void) const
155 {
156 	UNIMPLEMENTED();
157 	return "";
158 }
159 
160 
161 int32
162 BMediaFile::CountTracks() const
163 {
164 	return fTrackNum;
165 }
166 
167 
168 // Can be called multiple times with the same index.  You must call
169 // ReleaseTrack() when you're done with a track.
170 BMediaTrack	*
171 BMediaFile::TrackAt(int32 index)
172 {
173 	CALLED();
174 	if (!fTrackList || !fExtractor || index < 0 || index >= fTrackNum)
175 		return 0;
176 	if (!fTrackList[index]) {
177 		TRACE("BMediaFile::TrackAt, creating new track for index %ld\n", index);
178 		fTrackList[index] = new BMediaTrack(fExtractor, index);
179 		TRACE("BMediaFile::TrackAt, new track is %p\n", fTrackList[index]);
180 	}
181 	return fTrackList[index];
182 }
183 
184 
185 // Release the resource used by a given BMediaTrack object, to reduce
186 // the memory usage of your application. The specific 'track' object
187 // can no longer be used, but you can create another one by calling
188 // TrackAt() with the same track index.
189 status_t
190 BMediaFile::ReleaseTrack(BMediaTrack *track)
191 {
192 	CALLED();
193 	if (!fTrackList || !track)
194 		return B_ERROR;
195 	for (int32 i = 0; i < fTrackNum; i++) {
196 		if (fTrackList[i] == track) {
197 			TRACE("BMediaFile::ReleaseTrack, releasing track %p with index %ld\n", track, i);
198 			delete track;
199 			fTrackList[i] = 0;
200 			return B_OK;
201 		}
202 	}
203 	fprintf(stderr, "BMediaFile::ReleaseTrack track %p not found\n", track);
204 	return B_ERROR;
205 }
206 
207 
208 status_t
209 BMediaFile::ReleaseAllTracks(void)
210 {
211 	CALLED();
212 	if (!fTrackList)
213 		return B_ERROR;
214 	for (int32 i = 0; i < fTrackNum; i++) {
215 		if (fTrackList[i]) {
216 			TRACE("BMediaFile::ReleaseAllTracks, releasing track %p with index %ld\n", fTrackList[i], i);
217 			delete fTrackList[i];
218 			fTrackList[i] = 0;
219 		}
220 	}
221 	return B_OK;
222 }
223 
224 
225 // Create and add a track to the media file
226 BMediaTrack	*
227 BMediaFile::CreateTrack(media_format *mf,
228 						const media_codec_info *mci,
229 						uint32 flags)
230 {
231 	UNIMPLEMENTED();
232 	return 0;
233 }
234 
235 
236 // Create and add a raw track to the media file (it has no encoder)
237 BMediaTrack *
238 BMediaFile::CreateTrack(media_format *mf, uint32 flags)
239 {
240 	return CreateTrack(mf, NULL, flags);
241 }
242 
243 
244 // For BeOS R5 compatibility
245 extern "C" BMediaTrack * CreateTrack__10BMediaFileP12media_formatPC16media_codec_info(
246 	BMediaFile *self, media_format *mf, const media_codec_info *mci);
247 BMediaTrack *
248 CreateTrack__10BMediaFileP12media_formatPC16media_codec_info(
249 		BMediaFile *self,
250 		media_format *mf,
251 		const media_codec_info *mci)
252 {
253 	return self->CreateTrack(mf, mci, 0);
254 }
255 
256 
257 // For BeOS R5 compatibility
258 extern "C" BMediaTrack * CreateTrack__10BMediaFileP12media_format(
259 	BMediaFile *self, media_format *mf);
260 BMediaTrack *
261 CreateTrack__10BMediaFileP12media_format(
262 		BMediaFile *self,
263 		media_format *mf)
264 {
265 	return self->CreateTrack(mf, NULL, 0);
266 }
267 
268 
269 // Lets you set the copyright info for the entire file
270 status_t
271 BMediaFile::AddCopyright(const char *data)
272 {
273 	UNIMPLEMENTED();
274 	return B_OK;
275 }
276 
277 
278 // Call this to add user-defined chunks to a file (if they're supported)
279 status_t
280 BMediaFile::AddChunk(int32 type, const void *data, size_t size)
281 {
282 	UNIMPLEMENTED();
283 	return B_OK;
284 }
285 
286 
287 // After you have added all the tracks you want, call this
288 status_t
289 BMediaFile::CommitHeader(void)
290 {
291 	UNIMPLEMENTED();
292 	return B_OK;
293 }
294 
295 
296 // After you have written all the data to the track objects, call this
297 status_t
298 BMediaFile::CloseFile(void)
299 {
300 	UNIMPLEMENTED();
301 	return B_OK;
302 }
303 
304 // This is for controlling file format parameters
305 
306 // returns a copy of the parameter web
307 status_t
308 BMediaFile::GetParameterWeb(BParameterWeb** outWeb)
309 {
310 	UNIMPLEMENTED();
311 	return B_ERROR;
312 }
313 
314 
315 // deprecated BeOS R5 API
316 BParameterWeb *
317 BMediaFile::Web()
318 {
319 	UNIMPLEMENTED();
320 	return 0;
321 }
322 
323 
324 status_t
325 BMediaFile::GetParameterValue(int32 id,	void *valu, size_t *size)
326 {
327 	UNIMPLEMENTED();
328 	return B_OK;
329 }
330 
331 
332 status_t
333 BMediaFile::SetParameterValue(int32 id,	const void *valu, size_t size)
334 {
335 	UNIMPLEMENTED();
336 	return B_OK;
337 }
338 
339 
340 BView *
341 BMediaFile::GetParameterView()
342 {
343 	UNIMPLEMENTED();
344 	return 0;
345 }
346 
347 
348 /* virtual */ status_t
349 BMediaFile::Perform(int32 selector, void * data)
350 {
351 	UNIMPLEMENTED();
352 	return B_OK;
353 }
354 
355 
356 status_t
357 BMediaFile::ControlFile(int32 selector, void * io_data, size_t size)
358 {
359 	UNIMPLEMENTED();
360 	return B_ERROR;
361 }
362 
363 
364 /*************************************************************
365  * private BMediaFile
366  *************************************************************/
367 
368 
369 void
370 BMediaFile::Init()
371 {
372 	CALLED();
373 	fSource = 0;
374 	fTrackNum = 0;
375 	fTrackList = 0;
376 	fExtractor = 0;
377 	fErr = B_OK;
378 	fDeleteSource = false;
379 
380 	// not used so far:
381 	fEncoderMgr = 0;
382 	fWriterMgr = 0;
383 	fWriter = 0;
384 	fWriterID = 0;
385 	fFileClosed = 0;
386 }
387 
388 
389 void
390 BMediaFile::InitReader(BDataIO *source, int32 flags)
391 {
392 	CALLED();
393 
394 	fSource = source;
395 
396 	fExtractor = new MediaExtractor(source, flags);
397 	fErr = fExtractor->InitCheck();
398 	if (fErr)
399 		return;
400 
401 	fExtractor->GetFileFormatInfo(&fMFI);
402 	fTrackNum = fExtractor->StreamCount();
403 	fTrackList = new BMediaTrack *[fTrackNum];
404 	memset(fTrackList, 0, fTrackNum * sizeof(BMediaTrack *));
405 }
406 
407 
408 void
409 BMediaFile::InitWriter(BDataIO *source, const media_file_format * mfi,  int32 flags)
410 {
411 	UNIMPLEMENTED();
412 	fSource = source;
413 	fErr = B_NOT_ALLOWED;
414 }
415 
416 
417 /*
418 //unimplemented
419 BMediaFile::BMediaFile();
420 BMediaFile::BMediaFile(const BMediaFile&);
421  BMediaFile::BMediaFile& operator=(const BMediaFile&);
422 */
423 
424 status_t BMediaFile::_Reserved_BMediaFile_0(int32 arg, ...) { return B_ERROR; }
425 status_t BMediaFile::_Reserved_BMediaFile_1(int32 arg, ...) { return B_ERROR; }
426 status_t BMediaFile::_Reserved_BMediaFile_2(int32 arg, ...) { return B_ERROR; }
427 status_t BMediaFile::_Reserved_BMediaFile_3(int32 arg, ...) { return B_ERROR; }
428 status_t BMediaFile::_Reserved_BMediaFile_4(int32 arg, ...) { return B_ERROR; }
429 status_t BMediaFile::_Reserved_BMediaFile_5(int32 arg, ...) { return B_ERROR; }
430 status_t BMediaFile::_Reserved_BMediaFile_6(int32 arg, ...) { return B_ERROR; }
431 status_t BMediaFile::_Reserved_BMediaFile_7(int32 arg, ...) { return B_ERROR; }
432 status_t BMediaFile::_Reserved_BMediaFile_8(int32 arg, ...) { return B_ERROR; }
433 status_t BMediaFile::_Reserved_BMediaFile_9(int32 arg, ...) { return B_ERROR; }
434 status_t BMediaFile::_Reserved_BMediaFile_10(int32 arg, ...) { return B_ERROR; }
435 status_t BMediaFile::_Reserved_BMediaFile_11(int32 arg, ...) { return B_ERROR; }
436 status_t BMediaFile::_Reserved_BMediaFile_12(int32 arg, ...) { return B_ERROR; }
437 status_t BMediaFile::_Reserved_BMediaFile_13(int32 arg, ...) { return B_ERROR; }
438 status_t BMediaFile::_Reserved_BMediaFile_14(int32 arg, ...) { return B_ERROR; }
439 status_t BMediaFile::_Reserved_BMediaFile_15(int32 arg, ...) { return B_ERROR; }
440 status_t BMediaFile::_Reserved_BMediaFile_16(int32 arg, ...) { return B_ERROR; }
441 status_t BMediaFile::_Reserved_BMediaFile_17(int32 arg, ...) { return B_ERROR; }
442 status_t BMediaFile::_Reserved_BMediaFile_18(int32 arg, ...) { return B_ERROR; }
443 status_t BMediaFile::_Reserved_BMediaFile_19(int32 arg, ...) { return B_ERROR; }
444 status_t BMediaFile::_Reserved_BMediaFile_20(int32 arg, ...) { return B_ERROR; }
445 status_t BMediaFile::_Reserved_BMediaFile_21(int32 arg, ...) { return B_ERROR; }
446 status_t BMediaFile::_Reserved_BMediaFile_22(int32 arg, ...) { return B_ERROR; }
447 status_t BMediaFile::_Reserved_BMediaFile_23(int32 arg, ...) { return B_ERROR; }
448 status_t BMediaFile::_Reserved_BMediaFile_24(int32 arg, ...) { return B_ERROR; }
449 status_t BMediaFile::_Reserved_BMediaFile_25(int32 arg, ...) { return B_ERROR; }
450 status_t BMediaFile::_Reserved_BMediaFile_26(int32 arg, ...) { return B_ERROR; }
451 status_t BMediaFile::_Reserved_BMediaFile_27(int32 arg, ...) { return B_ERROR; }
452 status_t BMediaFile::_Reserved_BMediaFile_28(int32 arg, ...) { return B_ERROR; }
453 status_t BMediaFile::_Reserved_BMediaFile_29(int32 arg, ...) { return B_ERROR; }
454 status_t BMediaFile::_Reserved_BMediaFile_30(int32 arg, ...) { return B_ERROR; }
455 status_t BMediaFile::_Reserved_BMediaFile_31(int32 arg, ...) { return B_ERROR; }
456 status_t BMediaFile::_Reserved_BMediaFile_32(int32 arg, ...) { return B_ERROR; }
457 status_t BMediaFile::_Reserved_BMediaFile_33(int32 arg, ...) { return B_ERROR; }
458 status_t BMediaFile::_Reserved_BMediaFile_34(int32 arg, ...) { return B_ERROR; }
459 status_t BMediaFile::_Reserved_BMediaFile_35(int32 arg, ...) { return B_ERROR; }
460 status_t BMediaFile::_Reserved_BMediaFile_36(int32 arg, ...) { return B_ERROR; }
461 status_t BMediaFile::_Reserved_BMediaFile_37(int32 arg, ...) { return B_ERROR; }
462 status_t BMediaFile::_Reserved_BMediaFile_38(int32 arg, ...) { return B_ERROR; }
463 status_t BMediaFile::_Reserved_BMediaFile_39(int32 arg, ...) { return B_ERROR; }
464 status_t BMediaFile::_Reserved_BMediaFile_40(int32 arg, ...) { return B_ERROR; }
465 status_t BMediaFile::_Reserved_BMediaFile_41(int32 arg, ...) { return B_ERROR; }
466 status_t BMediaFile::_Reserved_BMediaFile_42(int32 arg, ...) { return B_ERROR; }
467 status_t BMediaFile::_Reserved_BMediaFile_43(int32 arg, ...) { return B_ERROR; }
468 status_t BMediaFile::_Reserved_BMediaFile_44(int32 arg, ...) { return B_ERROR; }
469 status_t BMediaFile::_Reserved_BMediaFile_45(int32 arg, ...) { return B_ERROR; }
470 status_t BMediaFile::_Reserved_BMediaFile_46(int32 arg, ...) { return B_ERROR; }
471 status_t BMediaFile::_Reserved_BMediaFile_47(int32 arg, ...) { return B_ERROR; }
472 
473