xref: /haiku/src/tests/kits/media/wav_reader_test/main.cpp (revision e1c4049fed1047bdb957b0529e1921e97ef94770)
1 #include <OS.h>
2 #include <File.h>
3 #include <stdio.h>
4 #include <DataIO.h>
5 #include <string.h>
6 #include "MediaPlugin.h"
7 #include "ReaderPlugin.h"
8 #include "FileDataIO.h"
9 
10 const bool		SEEKABLE = 1;
11 const char *	FILENAME = "/boot/home/Desktop/mediastuff/test1.wav";
12 
13 #define TRACE_THIS 1
14 #if TRACE_THIS
15   #define TRACE printf
16 #else
17   #define TRACE ((void)0)
18 #endif
19 
20 void *cookies[100];
21 
22 int main(int argc, char *argv[])
23 {
24 	TRACE("\n\n");
25 
26 	TRACE("main: creating plugin...\n");
27 
28 	MediaPlugin *plugin = instantiate_plugin();
29 
30 	TRACE("main: creating reader...\n");
31 
32 	ReaderPlugin *readerplugin;
33 	readerplugin = dynamic_cast<ReaderPlugin *>(plugin);
34 
35 	Reader *reader = readerplugin->NewReader();
36 
37 	TRACE("main: opening source file...\n");
38 
39 	BDataIO *source;
40 	if (SEEKABLE)
41 		source = new BFile(argc == 2 ? argv[1] : FILENAME, O_RDWR);
42 	else
43 		source = new FileDataIO(argc == 2 ? argv[1] : FILENAME, O_RDWR);
44 
45 	TRACE("main: calling setup...\n");
46 
47 	reader->Setup(source);
48 
49 	TRACE("main: creating ...\n");
50 
51 	TRACE("main: copyright: \"%s\"\n", reader->Copyright());
52 
53 	status_t s;
54 	int32 streamCount;
55 
56 	s = reader->Sniff(&streamCount);
57 	if (s != B_OK) {
58 		TRACE("main: Sniff() failed with error %lx (%s)\n", s, strerror(s));
59 		goto err;
60 	}
61 
62 	TRACE("main: Sniff() found %ld streams\n", streamCount);
63 
64 
65 	for (int i = 0; i < streamCount; i++) {
66 		TRACE("main: calling AllocateCookie(stream = %d)\n", i);
67 		s = reader->AllocateCookie(i, &cookies[i]);
68 		if (s != B_OK) {
69 			TRACE("main: AllocateCookie(stream = %d) failed with error %lx (%s)\n", i,  s, strerror(s));
70 			goto err;
71 		}
72 	}
73 
74 	for (int i = 0; i < streamCount; i++) {
75 		TRACE("main: calling GetStreamInfo(stream = %d)\n", i);
76 		int64 frameCount;
77 		bigtime_t duration;
78 		media_format format;
79 		const void *infoBuffer;
80 		size_t infoSize;
81 		s = reader->GetStreamInfo(cookies[i], &frameCount, &duration, &format, &infoBuffer, &infoSize);
82 		if (s != B_OK) {
83 			TRACE("main: GetStreamInfo(stream = %d) failed with error %lx (%s)\n", i,  s, strerror(s));
84 			goto err;
85 		}
86 		TRACE("main: GetStreamInfo(stream = %d) result: %lld frames, %.6f sec\n", i, frameCount, duration / 1000000.0);
87 	}
88 
89 	for (int i = 0; i < streamCount; i++) {
90 		const void *chunkBuffer;
91 		size_t chunkSize;
92 		media_header mediaHeader;
93 		for (int j = 0; j < 5; j++) {
94 			s = reader->GetNextChunk(cookies[i], &chunkBuffer, &chunkSize, &mediaHeader);
95 			if (s != B_OK) {
96 				TRACE("main: GetNextChunk(stream = %d, chunk = %d) failed with error %lx (%s)\n", i, j,  s, strerror(s));
97 				break;
98 			}
99 		}
100 
101 		int64 frame;
102 		bigtime_t time;
103 
104 		time = 1000000; // 1 sec
105 		TRACE("main: calling Seek(stream = %d, time %.6f forward)\n", i, time / 1000000.0);
106 		s = reader->Seek(cookies[i], B_MEDIA_SEEK_TO_TIME | B_MEDIA_SEEK_CLOSEST_FORWARD, &frame, &time);
107 		TRACE("main: Seek result: time %.6f, frame %lld\n", time / 1000000.0, frame);
108 
109 		frame = 1000;
110 		TRACE("main: calling Seek(stream = %d, frame %lld forward)\n", i, frame);
111 		s = reader->Seek(cookies[i], B_MEDIA_SEEK_TO_FRAME | B_MEDIA_SEEK_CLOSEST_FORWARD, &frame, &time);
112 		TRACE("main: Seek result: time %.6f, frame %lld\n", time / 1000000.0, frame);
113 
114 		time = 1000000; // 1 sec
115 		TRACE("main: calling Seek(stream = %d, time %.6f backward)\n", i, time / 1000000.0);
116 		s = reader->Seek(cookies[i], B_MEDIA_SEEK_TO_TIME | B_MEDIA_SEEK_CLOSEST_BACKWARD, &frame, &time);
117 		TRACE("main: Seek result: time %.6f, frame %lld\n", time / 1000000.0, frame);
118 
119 		frame = 1000;
120 		TRACE("main: calling Seek(stream = %d, frame %lld backward)\n", i, frame);
121 		s = reader->Seek(cookies[i], B_MEDIA_SEEK_TO_FRAME | B_MEDIA_SEEK_CLOSEST_BACKWARD, &frame, &time);
122 		TRACE("main: Seek result: time %.6f, frame %lld\n", time / 1000000.0, frame);
123 	}
124 
125 	for (int i = 0; i < streamCount; i++) {
126 		TRACE("main: calling FreeCookie(stream = %d)\n", i);
127 		s = reader->FreeCookie(cookies[i]);
128 		if (s != B_OK) {
129 			TRACE("main: FreeCookie(stream = %d) failed with error %lx (%s)\n", i,  s, strerror(s));
130 			goto err;
131 		}
132 	}
133 
134 err:
135 	delete reader;
136 	delete plugin;
137 	delete source;
138 	return 0;
139 }
140 
141 status_t
142 _get_format_for_description(media_format *out_format, const media_format_description &in_desc)
143 {
144 	return B_OK;
145 }
146