xref: /haiku/src/add-ons/media/media-add-ons/reader/MediaReaderAddOn.cpp (revision e79e4e7c9e432c90415f79809b7160e864f79001)
1 // MediaReaderAddOn.cpp
2 //
3 // Andrew Bachmann, 2002
4 //
5 // A MediaReaderAddOn is an add-on
6 // that can make MediaReader nodes
7 //
8 // MediaReader nodes read a file into a multistream
9 
10 
11 #include <MediaDefs.h>
12 #include <MediaAddOn.h>
13 #include <Errors.h>
14 #include <Node.h>
15 #include <Mime.h>
16 #include <StorageDefs.h>
17 
18 #include "MediaReader.h"
19 #include "MediaReaderAddOn.h"
20 
21 #include <limits.h>
22 #include <stdio.h>
23 #include <string.h>
24 
25 // instantiation function
26 extern "C" _EXPORT BMediaAddOn * make_media_addon(image_id image) {
27 	return new MediaReaderAddOn(image);
28 }
29 
30 // -------------------------------------------------------- //
31 // ctor/dtor
32 // -------------------------------------------------------- //
33 
34 MediaReaderAddOn::~MediaReaderAddOn()
35 {
36 }
37 
38 MediaReaderAddOn::MediaReaderAddOn(image_id image) :
39 	AbstractFileInterfaceAddOn(image)
40 {
41 	fprintf(stderr,"MediaReaderAddOn::MediaReaderAddOn\n");
42 }
43 
44 // -------------------------------------------------------- //
45 // BMediaAddOn impl
46 // -------------------------------------------------------- //
47 
48 status_t MediaReaderAddOn::GetFlavorAt(
49 	int32 n,
50 	const flavor_info ** out_info)
51 {
52 	fprintf(stderr,"MediaReaderAddOn::GetFlavorAt\n");
53 	if (out_info == 0) {
54 		fprintf(stderr,"<- B_BAD_VALUE\n");
55 		return B_BAD_VALUE; // we refuse to crash because you were stupid
56 	}
57 	if (n != 0) {
58 		fprintf(stderr,"<- B_BAD_INDEX\n");
59 		return B_BAD_INDEX;
60 	}
61 	flavor_info * infos = new flavor_info[1];
62 	MediaReader::GetFlavor(&infos[0],n);
63 	(*out_info) = infos;
64 	return B_OK;
65 }
66 
67 BMediaNode * MediaReaderAddOn::InstantiateNodeFor(
68 				const flavor_info * info,
69 				BMessage * config,
70 				status_t * out_error)
71 {
72 	fprintf(stderr,"MediaReaderAddOn::InstantiateNodeFor\n");
73 	if (out_error == 0) {
74 		fprintf(stderr,"<- NULL\n");
75 		return 0; // we refuse to crash because you were stupid
76 	}
77 	// XXX: read from add-on's attributes
78 	size_t defaultChunkSize = size_t(8192); // 8192 bytes = 8 Kilobytes
79 	float defaultBitRate = 2048; // = 2048 kilobits/millisec = 256000 Kilobytes/sec
80 	MediaReader * node
81 		= new MediaReader(defaultChunkSize,
82 						  defaultBitRate,
83 						  info,config,this);
84 	if (node == 0) {
85 		*out_error = B_NO_MEMORY;
86 		fprintf(stderr,"<- B_NO_MEMORY\n");
87 	} else {
88 		*out_error = node->InitCheck();
89 	}
90 	return node;
91 }
92 
93 status_t MediaReaderAddOn::GetConfigurationFor(
94 				BMediaNode * your_node,
95 				BMessage * into_message)
96 {
97 	fprintf(stderr,"MediaReaderAddOn::GetConfigurationFor\n");
98 	if (into_message == 0) {
99 		fprintf(stderr,"<- B_BAD_VALUE\n");
100 		return B_BAD_VALUE; // we refuse to crash because you were stupid
101 	}
102 	MediaReader * node
103 		= dynamic_cast<MediaReader*>(your_node);
104 	if (node == 0) {
105 		fprintf(stderr,"<- B_BAD_TYPE\n");
106 		return B_BAD_TYPE;
107 	}
108 	return node->GetConfigurationFor(into_message);
109 }
110 
111 // -------------------------------------------------------- //
112 // BMediaAddOn impl for B_FILE_INTERFACE nodes
113 // -------------------------------------------------------- //
114 
115 // This function treats null pointers slightly differently than the others.
116 // This is because a program could reasonably call this function with just
117 // about any junk, get the out_read_items or out_write_items and then use
118 // that to create an array of sufficient size to hold the result, and then
119 // call us again.  So we won't punish them if they supply us with null
120 // pointers the first time around.
121 //
122 // A stupid program might not supply an out_read_items, but actually supply
123 // an out_readable_formats and then try to do something useful with it. As
124 // an extreme gesture of nicety we will fill the out_readable_formats with
125 // a valid entry, although they could easily read into garbage after that...
126 status_t MediaReaderAddOn::GetFileFormatList(
127 				int32 flavor_id,
128 				media_file_format * out_writable_formats,
129 				int32 in_write_items,
130 				int32 * out_write_items,
131 				media_file_format * out_readable_formats,
132 				int32 in_read_items,
133 				int32 * out_read_items,
134 				void * _reserved)
135 {
136 	fprintf(stderr,"MediaReaderAddOn::GetFileFormatList\n");
137 	if (flavor_id != 0) {
138 		// this is a sanity check for now
139 		fprintf(stderr,"<- B_BAD_INDEX\n");
140 		return B_BAD_INDEX;
141 	}
142 	// see null check comment above
143 	if (out_write_items != 0) {
144 		*out_write_items = 0;
145 	}
146 	// see null check comment above
147 	if (out_read_items != 0) {
148 		*out_read_items = 1;
149 	}
150 	if (out_readable_formats != 0) {
151 		// don't go off the end
152 		if (in_read_items > 0) {
153 			MediaReader::GetFileFormat(&out_readable_formats[0]);
154 		}
155 	}
156 	return B_OK;
157 }
158 
159 status_t MediaReaderAddOn::SniffTypeKind(
160 				const BMimeType & type,
161 				uint64 in_kinds,
162 				float * out_quality,
163 				int32 * out_internal_id,
164 				void * _reserved)
165 {
166 	fprintf(stderr,"MediaReaderAddOn::SniffTypeKind\n");
167 	return AbstractFileInterfaceAddOn::SniffTypeKind(type,in_kinds,
168 													 B_BUFFER_PRODUCER,
169 													 out_quality,out_internal_id,
170 													 _reserved);
171 }
172 
173 // -------------------------------------------------------- //
174 // main
175 // -------------------------------------------------------- //
176 
177 int main(int argc, char *argv[])
178 {
179 	fprintf(stderr,"main called for MediaReaderAddOn\n");
180 }
181 
182 // -------------------------------------------------------- //
183 // stuffing
184 // -------------------------------------------------------- //
185 
186 status_t MediaReaderAddOn::_Reserved_MediaReaderAddOn_0(void *) {};
187 status_t MediaReaderAddOn::_Reserved_MediaReaderAddOn_1(void *) {};
188 status_t MediaReaderAddOn::_Reserved_MediaReaderAddOn_2(void *) {};
189 status_t MediaReaderAddOn::_Reserved_MediaReaderAddOn_3(void *) {};
190 status_t MediaReaderAddOn::_Reserved_MediaReaderAddOn_4(void *) {};
191 status_t MediaReaderAddOn::_Reserved_MediaReaderAddOn_5(void *) {};
192 status_t MediaReaderAddOn::_Reserved_MediaReaderAddOn_6(void *) {};
193 status_t MediaReaderAddOn::_Reserved_MediaReaderAddOn_7(void *) {};
194 status_t MediaReaderAddOn::_Reserved_MediaReaderAddOn_8(void *) {};
195 status_t MediaReaderAddOn::_Reserved_MediaReaderAddOn_9(void *) {};
196 status_t MediaReaderAddOn::_Reserved_MediaReaderAddOn_10(void *) {};
197 status_t MediaReaderAddOn::_Reserved_MediaReaderAddOn_11(void *) {};
198 status_t MediaReaderAddOn::_Reserved_MediaReaderAddOn_12(void *) {};
199 status_t MediaReaderAddOn::_Reserved_MediaReaderAddOn_13(void *) {};
200 status_t MediaReaderAddOn::_Reserved_MediaReaderAddOn_14(void *) {};
201 status_t MediaReaderAddOn::_Reserved_MediaReaderAddOn_15(void *) {};
202