xref: /haiku/src/add-ons/media/media-add-ons/writer/MediaWriterAddOn.cpp (revision 1e36cfc2721ef13a187c6f7354dc9cbc485e89d3)
1 // MediaWriterAddOn.cpp
2 //
3 // Andrew Bachmann, 2002
4 //
5 // A MediaWriterAddOn is an add-on
6 // that can make MediaWriter nodes
7 //
8 // MediaWriter nodes write a multistream into a file
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 "MediaWriter.h"
19 #include "MediaWriterAddOn.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 MediaWriterAddOn(image);
28 }
29 
30 // -------------------------------------------------------- //
31 // ctor/dtor
32 // -------------------------------------------------------- //
33 
34 MediaWriterAddOn::~MediaWriterAddOn()
35 {
36 }
37 
38 MediaWriterAddOn::MediaWriterAddOn(image_id image) :
39 	AbstractFileInterfaceAddOn(image)
40 {
41 	fprintf(stderr,"MediaWriterAddOn::MediaWriterAddOn\n");
42 }
43 
44 // -------------------------------------------------------- //
45 // BMediaAddOn impl
46 // -------------------------------------------------------- //
47 
48 status_t MediaWriterAddOn::GetFlavorAt(
49 	int32 n,
50 	const flavor_info ** out_info)
51 {
52 	fprintf(stderr,"MediaWriterAddOn::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 	MediaWriter::GetFlavor(&infos[0],n);
63 	(*out_info) = infos;
64 	return B_OK;
65 }
66 
67 BMediaNode * MediaWriterAddOn::InstantiateNodeFor(
68 				const flavor_info * info,
69 				BMessage * config,
70 				status_t * out_error)
71 {
72 	fprintf(stderr,"MediaWriterAddOn::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 	size_t defaultChunkSize = size_t(8192); // XXX: read from add-on's attributes
78 	float defaultBitRate = 800000;
79 	MediaWriter * node
80 		= new MediaWriter(defaultChunkSize,
81 						  defaultBitRate,
82 						  info,config,this);
83 	if (node == 0) {
84 		*out_error = B_NO_MEMORY;
85 		fprintf(stderr,"<- B_NO_MEMORY\n");
86 	} else {
87 		*out_error = node->InitCheck();
88 	}
89 	return node;
90 }
91 
92 status_t MediaWriterAddOn::GetConfigurationFor(
93 				BMediaNode * your_node,
94 				BMessage * into_message)
95 {
96 	fprintf(stderr,"MediaWriterAddOn::GetConfigurationFor\n");
97 	if (into_message == 0) {
98 		fprintf(stderr,"<- B_BAD_VALUE\n");
99 		return B_BAD_VALUE; // we refuse to crash because you were stupid
100 	}
101 	MediaWriter * node
102 		= dynamic_cast<MediaWriter*>(your_node);
103 	if (node == 0) {
104 		fprintf(stderr,"<- B_BAD_TYPE\n");
105 		return B_BAD_TYPE;
106 	}
107 	return node->GetConfigurationFor(into_message);
108 }
109 
110 // -------------------------------------------------------- //
111 // BMediaAddOn impl for B_FILE_INTERFACE nodes
112 // -------------------------------------------------------- //
113 
114 // This function treats null pointers slightly differently than the others.
115 // This is because a program could reasonably call this function with just
116 // about any junk, get the out_read_items or out_write_items and then use
117 // that to create an array of sufficient size to hold the result, and then
118 // call us again.  So we won't punish them if they supply us with null
119 // pointers the first time around.
120 //
121 // A stupid program might not supply an out_read_items, but actually supply
122 // an out_readable_formats and then try to do something useful with it. As
123 // an extreme gesture of nicety we will fill the out_readable_formats with
124 // a valid entry, although they could easily read into garbage after that...
125 status_t MediaWriterAddOn::GetFileFormatList(
126 				int32 flavor_id,
127 				media_file_format * out_writable_formats,
128 				int32 in_write_items,
129 				int32 * out_write_items,
130 				media_file_format * out_readable_formats,
131 				int32 in_read_items,
132 				int32 * out_read_items,
133 				void * _reserved)
134 {
135 	fprintf(stderr,"MediaWriterAddOn::GetFileFormatList\n");
136 	if (flavor_id != 0) {
137 		// this is a sanity check for now
138 		fprintf(stderr,"<- B_BAD_INDEX\n");
139 		return B_BAD_INDEX;
140 	}
141 	// see null check comment above
142 	if (out_write_items != 0) {
143 		*out_write_items = 0;
144 	}
145 	// see null check comment above
146 	if (out_read_items != 0) {
147 		*out_read_items = 1;
148 	}
149 	if (out_writable_formats != 0) {
150 		// don't go off the end
151 		if (in_write_items > 0) {
152 			MediaWriter::GetFileFormat(&out_writable_formats[0]);
153 		}
154 	}
155 	return B_OK;
156 }
157 
158 status_t MediaWriterAddOn::SniffTypeKind(
159 				const BMimeType & type,
160 				uint64 in_kinds,
161 				float * out_quality,
162 				int32 * out_internal_id,
163 				void * _reserved)
164 {
165 	fprintf(stderr,"MediaWriterAddOn::SniffTypeKind\n");
166 	return AbstractFileInterfaceAddOn::SniffTypeKind(type,in_kinds,
167 													 B_BUFFER_CONSUMER,
168 													 out_quality,out_internal_id,
169 													 _reserved);
170 }
171 
172 // -------------------------------------------------------- //
173 // main
174 // -------------------------------------------------------- //
175 
176 int main(int argc, char *argv[])
177 {
178 	fprintf(stderr,"main called for MediaWriterAddOn\n");
179 }
180 
181 // -------------------------------------------------------- //
182 // stuffing
183 // -------------------------------------------------------- //
184 
185 status_t MediaWriterAddOn::_Reserved_MediaWriterAddOn_0(void *) {};
186 status_t MediaWriterAddOn::_Reserved_MediaWriterAddOn_1(void *) {};
187 status_t MediaWriterAddOn::_Reserved_MediaWriterAddOn_2(void *) {};
188 status_t MediaWriterAddOn::_Reserved_MediaWriterAddOn_3(void *) {};
189 status_t MediaWriterAddOn::_Reserved_MediaWriterAddOn_4(void *) {};
190 status_t MediaWriterAddOn::_Reserved_MediaWriterAddOn_5(void *) {};
191 status_t MediaWriterAddOn::_Reserved_MediaWriterAddOn_6(void *) {};
192 status_t MediaWriterAddOn::_Reserved_MediaWriterAddOn_7(void *) {};
193 status_t MediaWriterAddOn::_Reserved_MediaWriterAddOn_8(void *) {};
194 status_t MediaWriterAddOn::_Reserved_MediaWriterAddOn_9(void *) {};
195 status_t MediaWriterAddOn::_Reserved_MediaWriterAddOn_10(void *) {};
196 status_t MediaWriterAddOn::_Reserved_MediaWriterAddOn_11(void *) {};
197 status_t MediaWriterAddOn::_Reserved_MediaWriterAddOn_12(void *) {};
198 status_t MediaWriterAddOn::_Reserved_MediaWriterAddOn_13(void *) {};
199 status_t MediaWriterAddOn::_Reserved_MediaWriterAddOn_14(void *) {};
200 status_t MediaWriterAddOn::_Reserved_MediaWriterAddOn_15(void *) {};
201