xref: /haiku/src/apps/cortex/RouteApp/route_app_io.cpp (revision ba499cdc3336fb89429027418871bf263f1f5e14)
1 // route_app_io.cpp
2 
3 #include "route_app_io.h"
4 
5 #include <MediaDefs.h>
6 #include <MediaNode.h>
7 #include <MediaRoster.h>
8 
9 #include "NodeManager.h"
10 #include "NodeRef.h"
11 #include "NodeSetIOContext.h"
12 
13 __BEGIN_CORTEX_NAMESPACE
14 
15 // -------------------------------------------------------- //
16 
17 const char* const _NODE_SET_ELEMENT = "cortex_node_set";
18 const char* const _UI_STATE_ELEMENT = "cortex_ui_state";
19 
20 const char* const _DORMANT_NODE_ELEMENT = "dormant_node";
21 const char* const _KIND_ELEMENT = "kind";
22 const char* const _FLAVOR_ID_ELEMENT = "flavor_id";
23 const char* const _CYCLE_ELEMENT = "cycle";
24 const char* const _FLAG_ELEMENT = "flag";
25 const char* const _RUN_MODE_ELEMENT = "run_mode";
26 const char* const _TIME_SOURCE_ELEMENT = "time_source";
27 const char* const _RECORDING_DELAY_ELEMENT = "recording_delay";
28 const char* const _CONNECTION_ELEMENT = "connection";
29 const char* const _OUTPUT_ELEMENT = "output";
30 const char* const _INPUT_ELEMENT = "input";
31 const char* const _NODE_GROUP_ELEMENT = "node_group";
32 const char* const _NAME_ELEMENT = "name";
33 const char* const _REF_ELEMENT = "ref";
34 const char* const _LIVE_NODE_ELEMENT = "live_node";
35 
36 const char* const _AUDIO_INPUT_KEY = "AUDIO_INPUT";
37 const char* const _AUDIO_OUTPUT_KEY = "AUDIO_OUTPUT";
38 const char* const _AUDIO_MIXER_KEY = "AUDIO_MIXER";
39 const char* const _VIDEO_INPUT_KEY = "VIDEO_INPUT";
40 const char* const _VIDEO_OUTPUT_KEY = "VIDEO_OUTPUT";
41 
42 __END_CORTEX_NAMESPACE
43 
44 // -------------------------------------------------------- //
45 __USE_CORTEX_NAMESPACE
46 // -------------------------------------------------------- //
47 
48 void __CORTEX_NAMESPACE__ _write_simple(
49 	const char* element,
50 	const char* value,
51 	ExportContext& context) {
52 
53 	context.beginElement(element);
54 	context.beginContent();
55 	context.writeString(value);
56 	context.endElement();
57 }
58 
59 void __CORTEX_NAMESPACE__ _write_node_kinds(
60 	int64 kinds,
61 	ExportContext& context) {
62 
63 	if(kinds & B_BUFFER_PRODUCER)
64 		_write_simple(_KIND_ELEMENT, "B_BUFFER_PRODUCER", context);
65 	if(kinds & B_BUFFER_CONSUMER)
66 		_write_simple(_KIND_ELEMENT, "B_BUFFER_CONSUMER", context);
67 	if(kinds & B_TIME_SOURCE)
68 		_write_simple(_KIND_ELEMENT, "B_TIME_SOURCE", context);
69 	if(kinds & B_CONTROLLABLE)
70 		_write_simple(_KIND_ELEMENT, "B_CONTROLLABLE", context);
71 	if(kinds & B_FILE_INTERFACE)
72 		_write_simple(_KIND_ELEMENT, "B_FILE_INTERFACE", context);
73 	if(kinds & B_ENTITY_INTERFACE)
74 		_write_simple(_KIND_ELEMENT, "B_ENTITY_INTERFACE", context);
75 	if(kinds & B_PHYSICAL_INPUT)
76 		_write_simple(_KIND_ELEMENT, "B_PHYSICAL_INPUT", context);
77 	if(kinds & B_PHYSICAL_OUTPUT)
78 		_write_simple(_KIND_ELEMENT, "B_PHYSICAL_OUTPUT", context);
79 	if(kinds & B_SYSTEM_MIXER)
80 		_write_simple(_KIND_ELEMENT, "B_SYSTEM_MIXER", context);
81 }
82 
83 void __CORTEX_NAMESPACE__ _read_node_kind(
84 	int64& ioKind,
85 	const char* data,
86 	ImportContext& context) {
87 
88 	if(!strcmp(data, "B_BUFFER_PRODUCER"))
89 		ioKind |= B_BUFFER_PRODUCER;
90 	else if(!strcmp(data, "B_BUFFER_CONSUMER"))
91 		ioKind |= B_BUFFER_CONSUMER;
92 	else if(!strcmp(data, "B_TIME_SOURCE"))
93 		ioKind |= B_TIME_SOURCE;
94 	else if(!strcmp(data, "B_CONTROLLABLE"))
95 		ioKind |= B_CONTROLLABLE;
96 	else if(!strcmp(data, "B_FILE_INTERFACE"))
97 		ioKind |= B_FILE_INTERFACE;
98 	else if(!strcmp(data, "B_ENTITY_INTERFACE"))
99 		ioKind |= B_ENTITY_INTERFACE;
100 	else if(!strcmp(data, "B_PHYSICAL_INPUT"))
101 		ioKind |= B_PHYSICAL_INPUT;
102 	else if(!strcmp(data, "B_PHYSICAL_OUTPUT"))
103 		ioKind |= B_PHYSICAL_OUTPUT;
104 	else if(!strcmp(data, "B_SYSTEM_MIXER"))
105 		ioKind |= B_SYSTEM_MIXER;
106 	else {
107 		BString err;
108 		err << "_read_noderef_kind(): unknown node kind '" << data << "'\n";
109 		context.reportWarning(err.String());
110 	}
111 }
112 
113 // fills in either key or outName/kind for the provided
114 // node.  If the given node is one of the default system nodes,
115 // an appropriate 'preset' key value will be returned.
116 
117 status_t __CORTEX_NAMESPACE__ _get_node_signature(
118 	const NodeManager*			manager,
119 	const NodeSetIOContext*	context,
120 	media_node_id						node,
121 	BString&								outKey,
122 	BString&								outName,
123 	int64&									outKind) {
124 
125 	ASSERT(manager);
126 	ASSERT(context);
127 
128 	// get ref
129 	NodeRef* ref;
130 	status_t err = manager->getNodeRef(node, &ref);
131 	if(err < B_OK) {
132 		PRINT((
133 			"!!! ConnectionIO::_getNodeSignature(): node %ld not found\n",
134 			node));
135 		return err;
136 	}
137 
138 	// check against system-default nodes
139 	if(ref == manager->audioInputNode()) {
140 		outKey = _AUDIO_INPUT_KEY;
141 		return B_OK;
142 	}
143 	else if(ref == manager->audioOutputNode()) {
144 		outKey = _AUDIO_OUTPUT_KEY;
145 		return B_OK;
146 	}
147 	else if(ref == manager->audioMixerNode()) {
148 		outKey = _AUDIO_MIXER_KEY;
149 		return B_OK;
150 	}
151 	else if(ref == manager->videoInputNode()) {
152 		outKey = _VIDEO_INPUT_KEY;
153 		return B_OK;
154 	}
155 	else if(ref == manager->videoOutputNode()) {
156 		outKey = _VIDEO_OUTPUT_KEY;
157 		return B_OK;
158 	}
159 
160 	// check context for a key (found if the node has already
161 	// been exported)
162 	const char* key;
163 	err = context->getKeyFor(node, &key);
164 	if(err == B_OK) {
165 		outKey = key;
166 		return B_OK;
167 	}
168 
169 	// return name/kind signature
170 	outName = ref->name();
171 	outKind = ref->kind();
172 	return B_OK;
173 }
174 
175 
176 // given a name and kind, looks for a matching node
177 
178 status_t __CORTEX_NAMESPACE__ _match_node_signature(
179 	const char*							name,
180 	int64										kind,
181 	media_node_id*					outNode) {
182 
183 	// fetch matching nodes
184 	BMediaRoster* roster = BMediaRoster::Roster();
185 	const int32 bufferSize = 64;
186 	live_node_info buffer[bufferSize];
187 	int32 count = bufferSize;
188 	status_t err = roster->GetLiveNodes(
189 		buffer,
190 		&count,
191 		0,
192 		0,
193 		name,
194 		kind); // is this argument supported yet? +++++
195 
196 	if(err < B_OK)
197 		return err;
198 
199 	for(int32 n = 0; n < count; ++n) {
200 		PRINT(("# %ld\n", buffer[n].node.node));
201 		if((buffer[n].node.kind & kind) != kind) {
202 			PRINT(("# - kind mismatch\n"));
203 			continue;
204 		}
205 
206 		*outNode = buffer[n].node.node;
207 		return B_OK;
208 	}
209 
210 	return B_NAME_NOT_FOUND;
211 }
212 
213 // given a key, looks for a system-default node
214 
215 status_t __CORTEX_NAMESPACE__ _match_system_node_key(
216 	const char*							key,
217 	const NodeManager*			manager,
218 	media_node_id*					outNode) {
219 
220 	if(!strcmp(key, _AUDIO_INPUT_KEY)) {
221 		if(manager->audioInputNode()) {
222 			*outNode = manager->audioInputNode()->id();
223 			return B_OK;
224 		}
225 		return B_NAME_NOT_FOUND;
226 	}
227 	else if(!strcmp(key, _AUDIO_OUTPUT_KEY)) {
228 		if(manager->audioOutputNode()) {
229 			*outNode = manager->audioOutputNode()->id();
230 			return B_OK;
231 		}
232 		return B_NAME_NOT_FOUND;
233 	}
234 	else if(!strcmp(key, _AUDIO_MIXER_KEY)) {
235 		if(manager->audioMixerNode()) {
236 			*outNode = manager->audioMixerNode()->id();
237 			return B_OK;
238 		}
239 		return B_NAME_NOT_FOUND;
240 	}
241 	else if(!strcmp(key, _VIDEO_INPUT_KEY)) {
242 		if(manager->videoInputNode()) {
243 			*outNode = manager->videoInputNode()->id();
244 			return B_OK;
245 		}
246 		return B_NAME_NOT_FOUND;
247 	}
248 	else if(!strcmp(key, _VIDEO_OUTPUT_KEY)) {
249 		if(manager->videoOutputNode()) {
250 			*outNode = manager->videoOutputNode()->id();
251 			return B_OK;
252 		}
253 		return B_NAME_NOT_FOUND;
254 	}
255 	return B_NAME_NOT_FOUND;
256 }
257 
258 // adds mappings for the simple string-content elements to the
259 // given document type
260 
261 void __CORTEX_NAMESPACE__ _add_string_elements(
262 	XML::DocumentType*			docType) {
263 
264 	docType->addMapping(new Mapping<StringContent>(_NAME_ELEMENT));
265 	docType->addMapping(new Mapping<StringContent>(_KIND_ELEMENT));
266 	docType->addMapping(new Mapping<StringContent>(_FLAG_ELEMENT));
267 	docType->addMapping(new Mapping<StringContent>(_FLAVOR_ID_ELEMENT));
268 	docType->addMapping(new Mapping<StringContent>(_CYCLE_ELEMENT));
269 	docType->addMapping(new Mapping<StringContent>(_RUN_MODE_ELEMENT));
270 	docType->addMapping(new Mapping<StringContent>(_TIME_SOURCE_ELEMENT));
271 	docType->addMapping(new Mapping<StringContent>(_RECORDING_DELAY_ELEMENT));
272 	docType->addMapping(new Mapping<StringContent>(_REF_ELEMENT));
273 }
274 
275 // END -- route_app_io.cpp --
276 
277