Projektarbeit Line Following Robot bei Prof. Chowanetz im WS22/23
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mmal_graph.c 47KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479
  1. /*
  2. Copyright (c) 2012, Broadcom Europe Ltd
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of the copyright holder nor the
  12. names of its contributors may be used to endorse or promote products
  13. derived from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
  18. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "mmal.h"
  26. #include "util/mmal_util.h"
  27. #include "util/mmal_graph.h"
  28. #include "core/mmal_component_private.h"
  29. #include "core/mmal_port_private.h"
  30. #include "mmal_logging.h"
  31. #define GRAPH_CONNECTIONS_MAX 16
  32. #define PROCESSING_TIME_MAX 20000
  33. /*****************************************************************************/
  34. /** Private context for our graph.
  35. * This also acts as a MMAL_COMPONENT_MODULE_T for when components are instantiated from graphs */
  36. typedef struct MMAL_COMPONENT_MODULE_T
  37. {
  38. MMAL_GRAPH_T graph; /**< Must be the first member! */
  39. MMAL_COMPONENT_T *component[GRAPH_CONNECTIONS_MAX];
  40. MMAL_GRAPH_TOPOLOGY_T topology[GRAPH_CONNECTIONS_MAX];
  41. unsigned int component_num;
  42. MMAL_CONNECTION_T *connection[GRAPH_CONNECTIONS_MAX];
  43. unsigned int connection_num;
  44. unsigned int connection_current;
  45. MMAL_PORT_T *input[GRAPH_CONNECTIONS_MAX];
  46. unsigned int input_num;
  47. MMAL_PORT_T *output[GRAPH_CONNECTIONS_MAX];
  48. unsigned int output_num;
  49. MMAL_COMPONENT_T *graph_component;
  50. MMAL_BOOL_T stop_thread; /**< informs the worker thread to exit */
  51. VCOS_THREAD_T thread; /**< worker thread which processes all internal connections */
  52. VCOS_SEMAPHORE_T sema; /**< informs the worker thread that buffers are available */
  53. MMAL_GRAPH_EVENT_CB event_cb; /**< callback for sending control port events to the client */
  54. void *event_cb_data; /**< callback data supplied by the client */
  55. } MMAL_GRAPH_PRIVATE_T;
  56. typedef MMAL_GRAPH_PRIVATE_T MMAL_COMPONENT_MODULE_T;
  57. /*****************************************************************************/
  58. static MMAL_STATUS_T mmal_component_create_from_graph(const char *name, MMAL_COMPONENT_T *component);
  59. static MMAL_BOOL_T graph_do_processing(MMAL_GRAPH_PRIVATE_T *graph);
  60. static void graph_process_buffer(MMAL_GRAPH_PRIVATE_T *graph_private,
  61. MMAL_CONNECTION_T *connection, MMAL_BUFFER_HEADER_T *buffer);
  62. /*****************************************************************************/
  63. static void graph_control_cb(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
  64. {
  65. MMAL_GRAPH_PRIVATE_T *graph = (MMAL_GRAPH_PRIVATE_T *)port->userdata;
  66. LOG_TRACE("port: %s(%p), buffer: %p, event: %4.4s", port->name, port,
  67. buffer, (char *)&buffer->cmd);
  68. if (graph->event_cb)
  69. {
  70. graph->event_cb((MMAL_GRAPH_T *)graph, port, buffer, graph->event_cb_data);
  71. }
  72. else
  73. {
  74. LOG_ERROR("event lost on port %i,%i (event callback not defined)",
  75. (int)port->type, (int)port->index);
  76. mmal_buffer_header_release(buffer);
  77. }
  78. }
  79. /*****************************************************************************/
  80. static void graph_connection_cb(MMAL_CONNECTION_T *connection)
  81. {
  82. MMAL_GRAPH_PRIVATE_T *graph = (MMAL_GRAPH_PRIVATE_T *)connection->user_data;
  83. MMAL_BUFFER_HEADER_T *buffer;
  84. if (connection->flags == MMAL_CONNECTION_FLAG_DIRECT &&
  85. (buffer = mmal_queue_get(connection->queue)) != NULL)
  86. {
  87. graph_process_buffer(graph, connection, buffer);
  88. return;
  89. }
  90. vcos_semaphore_post(&graph->sema);
  91. }
  92. /*****************************************************************************/
  93. static void* graph_worker_thread(void* ctx)
  94. {
  95. MMAL_GRAPH_PRIVATE_T *graph = (MMAL_GRAPH_PRIVATE_T *)ctx;
  96. while (1)
  97. {
  98. vcos_semaphore_wait(&graph->sema);
  99. if (graph->stop_thread)
  100. break;
  101. while(graph_do_processing(graph));
  102. }
  103. LOG_TRACE("worker thread exit %p", graph);
  104. return 0;
  105. }
  106. /*****************************************************************************/
  107. static void graph_stop_worker_thread(MMAL_GRAPH_PRIVATE_T *graph)
  108. {
  109. graph->stop_thread = MMAL_TRUE;
  110. vcos_semaphore_post(&graph->sema);
  111. vcos_thread_join(&graph->thread, NULL);
  112. }
  113. /*****************************************************************************/
  114. MMAL_STATUS_T mmal_graph_create(MMAL_GRAPH_T **graph, unsigned int userdata_size)
  115. {
  116. MMAL_GRAPH_PRIVATE_T *private;
  117. LOG_TRACE("graph %p, userdata_size %u", graph, userdata_size);
  118. /* Sanity checking */
  119. if (!graph)
  120. return MMAL_EINVAL;
  121. private = vcos_calloc(1, sizeof(MMAL_GRAPH_PRIVATE_T) + userdata_size, "mmal connection graph");
  122. if (!private)
  123. return MMAL_ENOMEM;
  124. *graph = &private->graph;
  125. if (userdata_size)
  126. (*graph)->userdata = (struct MMAL_GRAPH_USERDATA_T *)&private[1];
  127. if (vcos_semaphore_create(&private->sema, "mmal graph sema", 0) != VCOS_SUCCESS)
  128. {
  129. LOG_ERROR("failed to create semaphore %p", graph);
  130. vcos_free(private);
  131. return MMAL_ENOSPC;
  132. }
  133. return MMAL_SUCCESS;
  134. }
  135. /*****************************************************************************/
  136. MMAL_STATUS_T mmal_graph_destroy(MMAL_GRAPH_T *graph)
  137. {
  138. unsigned i;
  139. MMAL_GRAPH_PRIVATE_T *private = (MMAL_GRAPH_PRIVATE_T *)graph;
  140. if (!graph)
  141. return MMAL_EINVAL;
  142. LOG_TRACE("%p", graph);
  143. /* Notify client of destruction */
  144. if (graph->pf_destroy)
  145. graph->pf_destroy(graph);
  146. for (i = 0; i < private->connection_num; i++)
  147. mmal_connection_release(private->connection[i]);
  148. for (i = 0; i < private->component_num; i++)
  149. mmal_component_release(private->component[i]);
  150. vcos_semaphore_delete(&private->sema);
  151. vcos_free(graph);
  152. return MMAL_SUCCESS;
  153. }
  154. /*****************************************************************************/
  155. MMAL_STATUS_T mmal_graph_add_component(MMAL_GRAPH_T *graph, MMAL_COMPONENT_T *component)
  156. {
  157. MMAL_GRAPH_PRIVATE_T *private = (MMAL_GRAPH_PRIVATE_T *)graph;
  158. LOG_TRACE("graph: %p, component: %s(%p)", graph, component ? component->name: 0, component);
  159. if (!component)
  160. return MMAL_EINVAL;
  161. if (private->component_num >= GRAPH_CONNECTIONS_MAX)
  162. {
  163. LOG_ERROR("no space for component %s", component->name);
  164. return MMAL_ENOSPC;
  165. }
  166. mmal_component_acquire(component);
  167. private->component[private->component_num++] = component;
  168. return MMAL_SUCCESS;
  169. }
  170. /*****************************************************************************/
  171. MMAL_STATUS_T mmal_graph_component_topology(MMAL_GRAPH_T *graph, MMAL_COMPONENT_T *component,
  172. MMAL_GRAPH_TOPOLOGY_T topology, int8_t *input, unsigned int input_num,
  173. int8_t *output, unsigned int output_num)
  174. {
  175. MMAL_GRAPH_PRIVATE_T *private = (MMAL_GRAPH_PRIVATE_T *)graph;
  176. MMAL_PARAM_UNUSED(input); MMAL_PARAM_UNUSED(input_num);
  177. MMAL_PARAM_UNUSED(output); MMAL_PARAM_UNUSED(output_num);
  178. unsigned int i;
  179. LOG_TRACE("graph: %p, component: %s(%p)", graph, component ? component->name: 0, component);
  180. if (!component)
  181. return MMAL_EINVAL;
  182. for (i = 0; i < private->component_num; i++)
  183. if (component == private->component[i])
  184. break;
  185. if (i == private->component_num)
  186. return MMAL_EINVAL; /* Component not found */
  187. if (topology > MMAL_GRAPH_TOPOLOGY_STRAIGHT)
  188. return MMAL_ENOSYS; /* Currently not supported */
  189. private->topology[i] = topology;
  190. return MMAL_SUCCESS;
  191. }
  192. /*****************************************************************************/
  193. MMAL_STATUS_T mmal_graph_add_connection(MMAL_GRAPH_T *graph, MMAL_CONNECTION_T *cx)
  194. {
  195. MMAL_GRAPH_PRIVATE_T *private = (MMAL_GRAPH_PRIVATE_T *)graph;
  196. LOG_TRACE("graph: %p, connection: %s(%p)", graph, cx ? cx->name: 0, cx);
  197. if (!cx)
  198. return MMAL_EINVAL;
  199. if (private->connection_num >= GRAPH_CONNECTIONS_MAX)
  200. {
  201. LOG_ERROR("no space for connection %s", cx->name);
  202. return MMAL_ENOSPC;
  203. }
  204. mmal_connection_acquire(cx);
  205. private->connection[private->connection_num++] = cx;
  206. return MMAL_SUCCESS;
  207. }
  208. /*****************************************************************************/
  209. MMAL_STATUS_T mmal_graph_add_port(MMAL_GRAPH_T *graph, MMAL_PORT_T *port)
  210. {
  211. MMAL_GRAPH_PRIVATE_T *private = (MMAL_GRAPH_PRIVATE_T *)graph;
  212. MMAL_PORT_T **list;
  213. unsigned int *list_num;
  214. LOG_TRACE("graph: %p, port: %s(%p)", graph, port ? port->name: 0, port);
  215. if (!port || (port->type != MMAL_PORT_TYPE_INPUT && port->type != MMAL_PORT_TYPE_OUTPUT))
  216. return MMAL_EINVAL;
  217. list = port->type == MMAL_PORT_TYPE_INPUT ? private->input : private->output;
  218. list_num = port->type == MMAL_PORT_TYPE_INPUT ? &private->input_num : &private->output_num;
  219. if (*list_num >= GRAPH_CONNECTIONS_MAX)
  220. {
  221. LOG_ERROR("no space for port %s", port->name);
  222. return MMAL_ENOSPC;
  223. }
  224. list[(*list_num)++] = port;
  225. return MMAL_SUCCESS;
  226. }
  227. /*****************************************************************************/
  228. MMAL_STATUS_T mmal_graph_new_component(MMAL_GRAPH_T *graph, const char *name,
  229. MMAL_COMPONENT_T **component)
  230. {
  231. MMAL_GRAPH_PRIVATE_T *private = (MMAL_GRAPH_PRIVATE_T *)graph;
  232. MMAL_COMPONENT_T *comp;
  233. MMAL_STATUS_T status;
  234. LOG_TRACE("graph: %p, name: %s, component: %p", graph, name, component);
  235. if (private->component_num >= GRAPH_CONNECTIONS_MAX)
  236. {
  237. LOG_ERROR("no space for component %s", name);
  238. return MMAL_ENOSPC;
  239. }
  240. status = mmal_component_create(name, &comp);
  241. if (status != MMAL_SUCCESS)
  242. {
  243. LOG_ERROR("could not create component %s (%i)", name, status);
  244. return status;
  245. }
  246. private->component[private->component_num++] = comp;
  247. if (component)
  248. {
  249. mmal_component_acquire(comp);
  250. *component = comp;
  251. }
  252. return MMAL_SUCCESS;
  253. }
  254. /*****************************************************************************/
  255. MMAL_STATUS_T mmal_graph_new_connection(MMAL_GRAPH_T *graph, MMAL_PORT_T *out, MMAL_PORT_T *in,
  256. uint32_t flags, MMAL_CONNECTION_T **connection)
  257. {
  258. MMAL_GRAPH_PRIVATE_T *private = (MMAL_GRAPH_PRIVATE_T *)graph;
  259. MMAL_CONNECTION_T *cx;
  260. MMAL_STATUS_T status;
  261. if (!out || !in)
  262. return MMAL_EINVAL;
  263. if (out->type == MMAL_PORT_TYPE_CLOCK && in->type != MMAL_PORT_TYPE_CLOCK)
  264. return MMAL_EINVAL;
  265. if (out->type != MMAL_PORT_TYPE_CLOCK &&
  266. (out->type != MMAL_PORT_TYPE_OUTPUT || in->type != MMAL_PORT_TYPE_INPUT))
  267. return MMAL_EINVAL;
  268. LOG_TRACE("graph: %p, out: %s(%p), in: %s(%p), flags %x, connection: %p",
  269. graph, out->name, out, in->name, in, (int)flags, connection);
  270. if (private->connection_num >= GRAPH_CONNECTIONS_MAX)
  271. {
  272. LOG_ERROR("no space for connection %s/%s", out->name, in->name);
  273. return MMAL_ENOSPC;
  274. }
  275. status = mmal_connection_create(&cx, out, in, flags);
  276. if (status != MMAL_SUCCESS)
  277. return status;
  278. private->connection[private->connection_num++] = cx;
  279. if (connection)
  280. {
  281. mmal_connection_acquire(cx);
  282. *connection = cx;
  283. }
  284. return MMAL_SUCCESS;
  285. }
  286. /*****************************************************************************/
  287. MMAL_STATUS_T mmal_graph_enable(MMAL_GRAPH_T *graph, MMAL_GRAPH_EVENT_CB cb, void *cb_data)
  288. {
  289. MMAL_GRAPH_PRIVATE_T *private = (MMAL_GRAPH_PRIVATE_T *)graph;
  290. MMAL_STATUS_T status = MMAL_SUCCESS;
  291. unsigned int i;
  292. LOG_TRACE("graph: %p", graph);
  293. if (vcos_thread_create(&private->thread, "mmal graph thread", NULL,
  294. graph_worker_thread, private) != VCOS_SUCCESS)
  295. {
  296. LOG_ERROR("failed to create worker thread %p", graph);
  297. return MMAL_ENOSPC;
  298. }
  299. private->event_cb = cb;
  300. private->event_cb_data = cb_data;
  301. /* Enable all control ports */
  302. for (i = 0; i < private->component_num; i++)
  303. {
  304. private->component[i]->control->userdata = (void *)private;
  305. status = mmal_port_enable(private->component[i]->control, graph_control_cb);
  306. if (status != MMAL_SUCCESS)
  307. LOG_ERROR("could not enable port %s", private->component[i]->control->name);
  308. }
  309. /* Enable all our connections */
  310. for (i = 0; i < private->connection_num; i++)
  311. {
  312. MMAL_CONNECTION_T *cx = private->connection[i];
  313. cx->callback = graph_connection_cb;
  314. cx->user_data = private;
  315. status = mmal_connection_enable(cx);
  316. if (status != MMAL_SUCCESS)
  317. goto error;
  318. }
  319. /* Trigger the worker thread to populate the output ports with empty buffers */
  320. vcos_semaphore_post(&private->sema);
  321. return status;
  322. error:
  323. graph_stop_worker_thread(private);
  324. return status;
  325. }
  326. /*****************************************************************************/
  327. MMAL_STATUS_T mmal_graph_disable(MMAL_GRAPH_T *graph)
  328. {
  329. MMAL_GRAPH_PRIVATE_T *private = (MMAL_GRAPH_PRIVATE_T *)graph;
  330. MMAL_STATUS_T status = MMAL_SUCCESS;
  331. unsigned int i;
  332. LOG_TRACE("graph: %p", graph);
  333. graph_stop_worker_thread(private);
  334. /* Disable all our connections */
  335. for (i = 0; i < private->connection_num; i++)
  336. {
  337. status = mmal_connection_disable(private->connection[i]);
  338. if (status != MMAL_SUCCESS)
  339. break;
  340. }
  341. return status;
  342. }
  343. /*****************************************************************************/
  344. MMAL_STATUS_T mmal_graph_build(MMAL_GRAPH_T *graph,
  345. const char *name, MMAL_COMPONENT_T **component)
  346. {
  347. LOG_TRACE("graph: %p, name: %s, component: %p", graph, name, component);
  348. return mmal_component_create_with_constructor(name, mmal_component_create_from_graph,
  349. (MMAL_GRAPH_PRIVATE_T *)graph, component);
  350. }
  351. /*****************************************************************************/
  352. MMAL_STATUS_T mmal_graph_component_constructor(const char *name,
  353. MMAL_COMPONENT_T *component)
  354. {
  355. LOG_TRACE("name: %s, component: %p", name, component);
  356. return mmal_component_create_from_graph(name, component);
  357. }
  358. /*****************************************************************************/
  359. static void graph_component_control_cb(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
  360. {
  361. MMAL_COMPONENT_T *graph_component = (MMAL_COMPONENT_T *)port->userdata;
  362. MMAL_GRAPH_PRIVATE_T *graph_private = graph_component->priv->module;
  363. MMAL_STATUS_T status;
  364. LOG_TRACE("%s(%p),%p,%4.4s", port->name, port, buffer, (char *)&buffer->cmd);
  365. /* Call user defined function first */
  366. if (graph_private->graph.pf_control_callback)
  367. {
  368. status = graph_private->graph.pf_control_callback(&graph_private->graph,
  369. port, buffer);
  370. if (status != MMAL_ENOSYS)
  371. return;
  372. }
  373. /* Forward the event on the graph control port */
  374. mmal_port_event_send(graph_component->control, buffer);
  375. }
  376. /*****************************************************************************/
  377. static void graph_component_connection_cb(MMAL_CONNECTION_T *connection)
  378. {
  379. MMAL_COMPONENT_T *component = (MMAL_COMPONENT_T *)connection->user_data;
  380. MMAL_BUFFER_HEADER_T *buffer;
  381. if (connection->flags == MMAL_CONNECTION_FLAG_DIRECT &&
  382. (buffer = mmal_queue_get(connection->queue)) != NULL)
  383. {
  384. graph_process_buffer((MMAL_GRAPH_PRIVATE_T *)component->priv->module,
  385. connection, buffer);
  386. return;
  387. }
  388. mmal_component_action_trigger(component);
  389. }
  390. /*****************************************************************************/
  391. static void graph_port_event_handler(MMAL_CONNECTION_T *connection,
  392. MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
  393. {
  394. MMAL_STATUS_T status;
  395. LOG_TRACE("port: %s(%p), buffer: %p, event: %4.4s", port->name, port,
  396. buffer, (char *)&buffer->cmd);
  397. if (buffer->cmd == MMAL_EVENT_FORMAT_CHANGED && port->type == MMAL_PORT_TYPE_OUTPUT)
  398. {
  399. MMAL_EVENT_FORMAT_CHANGED_T *event = mmal_event_format_changed_get(buffer);
  400. if (event)
  401. {
  402. LOG_DEBUG("----------Port format changed----------");
  403. mmal_log_dump_port(port);
  404. LOG_DEBUG("-----------------to---------------------");
  405. mmal_log_dump_format(event->format);
  406. LOG_DEBUG(" buffers num (opt %i, min %i), size (opt %i, min: %i)",
  407. event->buffer_num_recommended, event->buffer_num_min,
  408. event->buffer_size_recommended, event->buffer_size_min);
  409. LOG_DEBUG("----------------------------------------");
  410. }
  411. status = mmal_connection_event_format_changed(connection, buffer);
  412. }
  413. else
  414. status = MMAL_SUCCESS; /* FIXME: ignore any other event for now */
  415. mmal_buffer_header_release(buffer);
  416. if (status != MMAL_SUCCESS)
  417. mmal_event_error_send(port->component, status);
  418. }
  419. /*****************************************************************************/
  420. static void graph_process_buffer(MMAL_GRAPH_PRIVATE_T *graph_private,
  421. MMAL_CONNECTION_T *connection, MMAL_BUFFER_HEADER_T *buffer)
  422. {
  423. MMAL_STATUS_T status;
  424. /* Call user defined function first */
  425. if (graph_private->graph.pf_connection_buffer)
  426. {
  427. status = graph_private->graph.pf_connection_buffer(&graph_private->graph, connection, buffer);
  428. if (status != MMAL_ENOSYS)
  429. return;
  430. }
  431. if (buffer->cmd)
  432. {
  433. graph_port_event_handler(connection, connection->out, buffer);
  434. return;
  435. }
  436. status = mmal_port_send_buffer(connection->in, buffer);
  437. if (status != MMAL_SUCCESS)
  438. {
  439. LOG_ERROR("%s(%p) could not send buffer to %s(%p) (%s)",
  440. connection->out->name, connection->out,
  441. connection->in->name, connection->in,
  442. mmal_status_to_string(status));
  443. mmal_buffer_header_release(buffer);
  444. mmal_event_error_send(connection->out->component, status);
  445. }
  446. }
  447. /*****************************************************************************/
  448. static MMAL_BOOL_T graph_do_processing(MMAL_GRAPH_PRIVATE_T *graph_private)
  449. {
  450. MMAL_BUFFER_HEADER_T *buffer;
  451. MMAL_BOOL_T run_again = 0;
  452. MMAL_STATUS_T status;
  453. unsigned int i, j;
  454. /* Process all the empty buffers first */
  455. for (i = 0, j = graph_private->connection_current;
  456. i < graph_private->connection_num; i++, j++)
  457. {
  458. MMAL_CONNECTION_T *connection =
  459. graph_private->connection[j%graph_private->connection_num];
  460. if ((connection->flags & MMAL_CONNECTION_FLAG_TUNNELLING) ||
  461. !connection->pool)
  462. continue; /* Nothing else to do in tunnelling mode */
  463. /* Send empty buffers to the output port of the connection */
  464. while ((buffer = mmal_queue_get(connection->pool->queue)) != NULL)
  465. {
  466. run_again = 1;
  467. status = mmal_port_send_buffer(connection->out, buffer);
  468. if (status != MMAL_SUCCESS)
  469. {
  470. LOG_ERROR("mmal_port_send_buffer failed (%i)", status);
  471. mmal_queue_put_back(connection->pool->queue, buffer);
  472. run_again = 0;
  473. // FIXME: send error ?
  474. break;
  475. }
  476. }
  477. }
  478. /* Loop through all the connections */
  479. for (i = 0, j = graph_private->connection_current++;
  480. i < graph_private->connection_num; i++, j++)
  481. {
  482. MMAL_CONNECTION_T *connection =
  483. graph_private->connection[j%graph_private->connection_num];
  484. int64_t duration = vcos_getmicrosecs64();
  485. if (connection->flags & MMAL_CONNECTION_FLAG_TUNNELLING)
  486. continue; /* Nothing else to do in tunnelling mode */
  487. if (connection->flags & MMAL_CONNECTION_FLAG_DIRECT)
  488. continue; /* Nothing else to do in direct mode */
  489. /* Send any queued buffer to the next component.
  490. * We also make sure no connection can starve the others by
  491. * having a timeout. */
  492. while (vcos_getmicrosecs64() - duration < PROCESSING_TIME_MAX &&
  493. (buffer = mmal_queue_get(connection->queue)) != NULL)
  494. {
  495. run_again = 1;
  496. graph_process_buffer(graph_private, connection, buffer);
  497. }
  498. }
  499. return run_again;
  500. }
  501. /*****************************************************************************/
  502. static void graph_do_processing_loop(MMAL_COMPONENT_T *component)
  503. {
  504. while (graph_do_processing((MMAL_GRAPH_PRIVATE_T *)component->priv->module));
  505. }
  506. /*****************************************************************************/
  507. static MMAL_PORT_T *find_port_from_graph(MMAL_GRAPH_PRIVATE_T *graph, MMAL_PORT_T *port)
  508. {
  509. MMAL_PORT_T **list;
  510. unsigned int *list_num;
  511. if (port->type != MMAL_PORT_TYPE_INPUT && port->type != MMAL_PORT_TYPE_OUTPUT)
  512. return 0;
  513. list = port->type == MMAL_PORT_TYPE_INPUT ? graph->input : graph->output;
  514. list_num = port->type == MMAL_PORT_TYPE_INPUT ? &graph->input_num : &graph->output_num;
  515. if (port->index > *list_num)
  516. return 0;
  517. return list[port->index];
  518. }
  519. static MMAL_PORT_T *find_port_to_graph(MMAL_GRAPH_PRIVATE_T *graph, MMAL_PORT_T *port)
  520. {
  521. MMAL_COMPONENT_T *component = graph->graph_component;
  522. MMAL_PORT_T **list;
  523. unsigned int i, *list_num;
  524. if (port->type != MMAL_PORT_TYPE_INPUT && port->type != MMAL_PORT_TYPE_OUTPUT)
  525. return 0;
  526. list = port->type == MMAL_PORT_TYPE_INPUT ? graph->input : graph->output;
  527. list_num = port->type == MMAL_PORT_TYPE_INPUT ? &graph->input_num : &graph->output_num;
  528. for (i = 0; i < *list_num; i++)
  529. if (list[i] == port)
  530. break;
  531. if (i == *list_num)
  532. return 0;
  533. return port->type == MMAL_PORT_TYPE_INPUT ? component->input[i] : component->output[i];
  534. }
  535. static MMAL_STATUS_T graph_port_update(MMAL_GRAPH_PRIVATE_T *graph,
  536. MMAL_PORT_T *graph_port, MMAL_BOOL_T init)
  537. {
  538. MMAL_STATUS_T status;
  539. MMAL_PORT_T *port;
  540. port = find_port_from_graph(graph, graph_port);
  541. if (!port)
  542. {
  543. LOG_ERROR("could not find matching port for %p", graph_port);
  544. return MMAL_EINVAL;
  545. }
  546. status = mmal_format_full_copy(graph_port->format, port->format);
  547. if (status != MMAL_SUCCESS)
  548. {
  549. LOG_ERROR("format copy failed on port %s", port->name);
  550. return status;
  551. }
  552. graph_port->buffer_num_min = port->buffer_num_min;
  553. graph_port->buffer_num_recommended = port->buffer_num_recommended;
  554. graph_port->buffer_size_min = port->buffer_size_min;
  555. graph_port->buffer_size_recommended = port->buffer_size_recommended;
  556. graph_port->buffer_alignment_min = port->buffer_alignment_min;
  557. graph_port->capabilities = port->capabilities;
  558. if (init)
  559. {
  560. graph_port->buffer_num = port->buffer_num;
  561. graph_port->buffer_size = port->buffer_size;
  562. }
  563. return MMAL_SUCCESS;
  564. }
  565. static MMAL_STATUS_T graph_port_update_requirements(MMAL_GRAPH_PRIVATE_T *graph,
  566. MMAL_PORT_T *graph_port)
  567. {
  568. MMAL_PORT_T *port;
  569. port = find_port_from_graph(graph, graph_port);
  570. if (!port)
  571. {
  572. LOG_ERROR("could not find matching port for %p", graph_port);
  573. return MMAL_EINVAL;
  574. }
  575. graph_port->buffer_num_min = port->buffer_num_min;
  576. graph_port->buffer_num_recommended = port->buffer_num_recommended;
  577. graph_port->buffer_size_min = port->buffer_size_min;
  578. graph_port->buffer_size_recommended = port->buffer_size_recommended;
  579. graph_port->buffer_alignment_min = port->buffer_alignment_min;
  580. return MMAL_SUCCESS;
  581. }
  582. /** Destroy a previously created component */
  583. static MMAL_STATUS_T graph_component_destroy(MMAL_COMPONENT_T *component)
  584. {
  585. MMAL_COMPONENT_MODULE_T *graph = component->priv->module;
  586. /* Notify client of destruction */
  587. if (graph->graph.pf_destroy)
  588. graph->graph.pf_destroy(&graph->graph);
  589. graph->graph.pf_destroy = NULL;
  590. if (component->input_num)
  591. mmal_ports_free(component->input, component->input_num);
  592. if (component->output_num)
  593. mmal_ports_free(component->output, component->output_num);
  594. /* coverity[address_free] Freeing the first item in the structure is safe */
  595. mmal_graph_destroy(&graph->graph);
  596. return MMAL_SUCCESS;
  597. }
  598. /** Enable processing on a component */
  599. static MMAL_STATUS_T graph_component_enable(MMAL_COMPONENT_T *component)
  600. {
  601. MMAL_GRAPH_PRIVATE_T *graph_private = component->priv->module;
  602. MMAL_STATUS_T status = MMAL_ENOSYS;
  603. /* Call user defined function first */
  604. if (graph_private->graph.pf_graph_enable)
  605. status = graph_private->graph.pf_graph_enable(&graph_private->graph, MMAL_TRUE);
  606. return status;
  607. }
  608. /** Disable processing on a component */
  609. static MMAL_STATUS_T graph_component_disable(MMAL_COMPONENT_T *component)
  610. {
  611. MMAL_GRAPH_PRIVATE_T *graph_private = component->priv->module;
  612. MMAL_STATUS_T status = MMAL_ENOSYS;
  613. /* Call user defined function first */
  614. if (graph_private->graph.pf_graph_enable)
  615. status = graph_private->graph.pf_graph_enable(&graph_private->graph, MMAL_FALSE);
  616. return status;
  617. }
  618. /** Callback given to mmal_port_enable() */
  619. static void graph_port_enable_cb(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
  620. {
  621. MMAL_GRAPH_PRIVATE_T *graph_private = (MMAL_GRAPH_PRIVATE_T *)port->userdata;
  622. MMAL_PORT_T *graph_port;
  623. MMAL_STATUS_T status;
  624. graph_port = find_port_to_graph(graph_private, port);
  625. if (!graph_port)
  626. {
  627. vcos_assert(0);
  628. mmal_buffer_header_release(buffer);
  629. return;
  630. }
  631. /* Call user defined function first */
  632. if (graph_private->graph.pf_return_buffer)
  633. {
  634. status = graph_private->graph.pf_return_buffer(&graph_private->graph, graph_port, buffer);
  635. if (status != MMAL_ENOSYS)
  636. return;
  637. }
  638. /* Forward the callback */
  639. if (buffer->cmd)
  640. mmal_port_event_send(graph_port, buffer);
  641. else
  642. mmal_port_buffer_header_callback(graph_port, buffer);
  643. }
  644. /** Check whether 2 ports of a component are linked */
  645. static MMAL_BOOL_T graph_component_topology_ports_linked(MMAL_GRAPH_PRIVATE_T *graph,
  646. MMAL_PORT_T *port1, MMAL_PORT_T *port2)
  647. {
  648. MMAL_COMPONENT_T *component = port1->component;
  649. unsigned int i;
  650. for (i = 0; i < graph->component_num; i++)
  651. if (component == graph->component[i])
  652. break;
  653. if (i == graph->component_num)
  654. return MMAL_FALSE; /* Component not found */
  655. if (graph->topology[i] == MMAL_GRAPH_TOPOLOGY_STRAIGHT)
  656. return port1->index == port2->index;
  657. return MMAL_TRUE;
  658. }
  659. /** Propagate a port enable */
  660. static MMAL_STATUS_T graph_port_state_propagate(MMAL_GRAPH_PRIVATE_T *graph,
  661. MMAL_PORT_T *port, MMAL_BOOL_T enable)
  662. {
  663. MMAL_COMPONENT_T *component = port->component;
  664. MMAL_STATUS_T status = MMAL_SUCCESS;
  665. MMAL_PORT_TYPE_T type = port->type;
  666. unsigned int i, j;
  667. LOG_TRACE("graph: %p, port %s(%p)", graph, port->name, port);
  668. if (port->type == MMAL_PORT_TYPE_OUTPUT)
  669. type = MMAL_PORT_TYPE_INPUT;
  670. if (port->type == MMAL_PORT_TYPE_INPUT)
  671. type = MMAL_PORT_TYPE_OUTPUT;
  672. /* Loop through all the output ports of the component and if they are not enabled and
  673. * match one of the connections we maintain, then we need to propagate the port enable. */
  674. for (i = 0; i < component->port_num; i++)
  675. {
  676. if (component->port[i]->type != type)
  677. continue;
  678. if ((component->port[i]->is_enabled && enable) ||
  679. (!component->port[i]->is_enabled && !enable))
  680. continue;
  681. /* Find the matching connection */
  682. for (j = 0; j < graph->connection_num; j++)
  683. if (graph->connection[j]->out == component->port[i] ||
  684. graph->connection[j]->in == component->port[i])
  685. break;
  686. if (j == graph->connection_num)
  687. continue; /* No match */
  688. if (!graph_component_topology_ports_linked(graph, port, component->port[i]))
  689. continue; /* Ports are independent */
  690. if (enable)
  691. {
  692. status = mmal_connection_enable(graph->connection[j]);
  693. if (status != MMAL_SUCCESS)
  694. break;
  695. mmal_log_dump_port(graph->connection[j]->out);
  696. mmal_log_dump_port(graph->connection[j]->in);
  697. }
  698. status = graph_port_state_propagate(graph, graph->connection[j]->in == component->port[i] ?
  699. graph->connection[j]->out : graph->connection[j]->in, enable);
  700. if (status != MMAL_SUCCESS)
  701. break;
  702. if (!enable)
  703. {
  704. status = mmal_connection_disable(graph->connection[j]);
  705. if (status != MMAL_SUCCESS)
  706. break;
  707. }
  708. }
  709. return status;
  710. }
  711. /** Enable processing on a port */
  712. static MMAL_STATUS_T graph_port_enable(MMAL_PORT_T *graph_port, MMAL_PORT_BH_CB_T cb)
  713. {
  714. MMAL_GRAPH_PRIVATE_T *graph_private = graph_port->component->priv->module;
  715. MMAL_PORT_T *port;
  716. MMAL_STATUS_T status;
  717. MMAL_PARAM_UNUSED(cb);
  718. port = find_port_from_graph(graph_private, graph_port);
  719. if (!port)
  720. return MMAL_EINVAL;
  721. /* Update the buffer requirements */
  722. port->buffer_num = graph_port->buffer_num;
  723. port->buffer_size = graph_port->buffer_size;
  724. /* Call user defined function first */
  725. if (graph_private->graph.pf_enable)
  726. {
  727. status = graph_private->graph.pf_enable(&graph_private->graph, graph_port);
  728. if (status != MMAL_ENOSYS)
  729. return status;
  730. }
  731. /* We'll intercept the callback */
  732. port->userdata = (void *)graph_private;
  733. status = mmal_port_enable(port, graph_port_enable_cb);
  734. if (status != MMAL_SUCCESS)
  735. return status;
  736. /* We need to enable all the connected connections */
  737. status = graph_port_state_propagate(graph_private, port, 1);
  738. mmal_component_action_trigger(graph_port->component);
  739. return status;
  740. }
  741. /** Disable processing on a port */
  742. static MMAL_STATUS_T graph_port_disable(MMAL_PORT_T *graph_port)
  743. {
  744. MMAL_GRAPH_PRIVATE_T *graph_private = graph_port->component->priv->module;
  745. MMAL_STATUS_T status;
  746. MMAL_PORT_T *port;
  747. port = find_port_from_graph(graph_port->component->priv->module, graph_port);
  748. if (!port)
  749. return MMAL_EINVAL;
  750. /* Call user defined function first */
  751. if (graph_private->graph.pf_disable)
  752. {
  753. status = graph_private->graph.pf_disable(&graph_private->graph, graph_port);
  754. if (status != MMAL_ENOSYS)
  755. return status;
  756. }
  757. /* We need to disable all the connected connections.
  758. * Since disable does an implicit flush, we only want to do that if
  759. * we're acting on an input port or we risk discarding buffers along
  760. * the way. */
  761. if (!graph_private->input_num || port->type == MMAL_PORT_TYPE_INPUT)
  762. {
  763. MMAL_STATUS_T status = graph_port_state_propagate(graph_private, port, 0);
  764. if (status != MMAL_SUCCESS)
  765. return status;
  766. }
  767. /* Forward the call */
  768. return mmal_port_disable(port);
  769. }
  770. /** Propagate a port flush */
  771. static MMAL_STATUS_T graph_port_flush_propagate(MMAL_GRAPH_PRIVATE_T *graph,
  772. MMAL_PORT_T *port)
  773. {
  774. MMAL_COMPONENT_T *component = port->component;
  775. MMAL_STATUS_T status;
  776. unsigned int i, j;
  777. LOG_TRACE("graph: %p, port %s(%p)", graph, port->name, port);
  778. status = mmal_port_flush(port);
  779. if (status != MMAL_SUCCESS)
  780. return status;
  781. if (port->type == MMAL_PORT_TYPE_OUTPUT)
  782. return MMAL_SUCCESS;
  783. /* Loop through all the output ports of the component and if they match one
  784. * of the connections we maintain, then we need to propagate the flush. */
  785. for (i = 0; i < component->port_num; i++)
  786. {
  787. if (component->port[i]->type != MMAL_PORT_TYPE_OUTPUT)
  788. continue;
  789. if (!component->port[i]->is_enabled)
  790. continue;
  791. /* Find the matching connection */
  792. for (j = 0; j < graph->connection_num; j++)
  793. if (graph->connection[j]->out == component->port[i])
  794. break;
  795. if (j == graph->connection_num)
  796. continue; /* No match */
  797. if (!graph_component_topology_ports_linked(graph, port, component->port[i]))
  798. continue; /* Ports are independent */
  799. /* Flush any buffer waiting in the connection queue */
  800. if (graph->connection[j]->queue)
  801. {
  802. MMAL_BUFFER_HEADER_T *buffer = mmal_queue_get(graph->connection[j]->queue);
  803. while(buffer)
  804. {
  805. mmal_buffer_header_release(buffer);
  806. buffer = mmal_queue_get(graph->connection[j]->queue);
  807. }
  808. }
  809. status = graph_port_flush_propagate(graph, graph->connection[j]->in);
  810. if (status != MMAL_SUCCESS)
  811. break;
  812. }
  813. return status;
  814. }
  815. /** Flush a port */
  816. static MMAL_STATUS_T graph_port_flush(MMAL_PORT_T *graph_port)
  817. {
  818. MMAL_GRAPH_PRIVATE_T *graph_private = graph_port->component->priv->module;
  819. MMAL_STATUS_T status;
  820. MMAL_PORT_T *port;
  821. port = find_port_from_graph(graph_private, graph_port);
  822. if (!port)
  823. return MMAL_EINVAL;
  824. /* Call user defined function first */
  825. if (graph_private->graph.pf_flush)
  826. {
  827. status = graph_private->graph.pf_flush(&graph_private->graph, graph_port);
  828. if (status != MMAL_ENOSYS)
  829. return status;
  830. }
  831. /* Forward the call */
  832. return graph_port_flush_propagate(graph_private, port);
  833. }
  834. /** Send a buffer header to a port */
  835. static MMAL_STATUS_T graph_port_send(MMAL_PORT_T *graph_port, MMAL_BUFFER_HEADER_T *buffer)
  836. {
  837. MMAL_GRAPH_PRIVATE_T *graph_private = graph_port->component->priv->module;
  838. MMAL_STATUS_T status;
  839. MMAL_PORT_T *port;
  840. port = find_port_from_graph(graph_port->component->priv->module, graph_port);
  841. if (!port)
  842. return MMAL_EINVAL;
  843. /* Call user defined function first */
  844. if (graph_private->graph.pf_send_buffer)
  845. {
  846. status = graph_private->graph.pf_send_buffer(&graph_private->graph, graph_port, buffer);
  847. if (status != MMAL_ENOSYS)
  848. return status;
  849. }
  850. /* Forward the call */
  851. return mmal_port_send_buffer(port, buffer);
  852. }
  853. /** Propagate a format change */
  854. static MMAL_STATUS_T graph_port_format_commit_propagate(MMAL_GRAPH_PRIVATE_T *graph,
  855. MMAL_PORT_T *port)
  856. {
  857. MMAL_COMPONENT_T *component = port->component;
  858. MMAL_STATUS_T status = MMAL_SUCCESS;
  859. unsigned int i, j;
  860. LOG_TRACE("graph: %p, port %s(%p)", graph, port->name, port);
  861. if (port->type == MMAL_PORT_TYPE_OUTPUT)
  862. return MMAL_SUCCESS; /* Nothing to do */
  863. /* Loop through all the output ports of the component and if they are not enabled and
  864. * match one of the connections we maintain, then we need to propagate the format change. */
  865. for (i = 0; i < component->output_num; i++)
  866. {
  867. MMAL_PORT_T *in, *out;
  868. if (component->output[i]->is_enabled)
  869. continue;
  870. /* Find the matching connection */
  871. for (j = 0; j < graph->connection_num; j++)
  872. if (graph->connection[j]->out == component->output[i])
  873. break;
  874. if (j == graph->connection_num)
  875. continue; /* No match */
  876. if (!graph_component_topology_ports_linked(graph, port, component->output[i]))
  877. continue; /* Ports are independent */
  878. in = graph->connection[j]->in;
  879. out = graph->connection[j]->out;
  880. /* Apply the format to the input port */
  881. status = mmal_format_full_copy(in->format, out->format);
  882. if (status != MMAL_SUCCESS)
  883. break;
  884. status = mmal_port_format_commit(in);
  885. if (status != MMAL_SUCCESS)
  886. break;
  887. mmal_log_dump_port(out);
  888. mmal_log_dump_port(in);
  889. status = graph_port_format_commit_propagate(graph, in);
  890. if (status != MMAL_SUCCESS)
  891. break;
  892. }
  893. return status;
  894. }
  895. /** Set format on a port */
  896. static MMAL_STATUS_T graph_port_format_commit(MMAL_PORT_T *graph_port)
  897. {
  898. MMAL_GRAPH_PRIVATE_T *graph_private = graph_port->component->priv->module;
  899. MMAL_STATUS_T status;
  900. MMAL_PORT_T *port;
  901. unsigned int i;
  902. /* Call user defined function first */
  903. if (graph_private->graph.pf_format_commit)
  904. {
  905. status = graph_private->graph.pf_format_commit(&graph_private->graph, graph_port);
  906. if (status == MMAL_SUCCESS)
  907. goto end;
  908. if (status != MMAL_ENOSYS)
  909. return status;
  910. }
  911. port = find_port_from_graph(graph_private, graph_port);
  912. if (!port)
  913. return MMAL_EINVAL;
  914. /* Update actual port */
  915. status = mmal_format_full_copy(port->format, graph_port->format);
  916. if (status != MMAL_SUCCESS)
  917. return status;
  918. port->buffer_num = graph_port->buffer_num;
  919. port->buffer_size = graph_port->buffer_size;
  920. /* Forward the call */
  921. status = mmal_port_format_commit(port);
  922. if (status != MMAL_SUCCESS)
  923. return status;
  924. /* Propagate format changes to the connections */
  925. status = graph_port_format_commit_propagate(graph_private, port);
  926. if (status != MMAL_SUCCESS)
  927. {
  928. LOG_ERROR("couldn't propagate format commit of port %s(%p)", port->name, port);
  929. return status;
  930. }
  931. end:
  932. /* Read the values back */
  933. status = graph_port_update(graph_private, graph_port, MMAL_FALSE);
  934. if (status != MMAL_SUCCESS)
  935. return status;
  936. /* Get the settings for the output ports in case they have changed */
  937. if (graph_port->type == MMAL_PORT_TYPE_INPUT)
  938. {
  939. for (i = 0; i < graph_private->output_num; i++)
  940. {
  941. status = graph_port_update(graph_private, graph_port->component->output[i], MMAL_FALSE);
  942. if (status != MMAL_SUCCESS)
  943. return status;
  944. }
  945. }
  946. return MMAL_SUCCESS;
  947. }
  948. static MMAL_STATUS_T graph_port_control_parameter_get(MMAL_PORT_T *graph_port,
  949. MMAL_PARAMETER_HEADER_T *param)
  950. {
  951. MMAL_GRAPH_PRIVATE_T *graph_private = graph_port->component->priv->module;
  952. MMAL_STATUS_T status = MMAL_ENOSYS;
  953. unsigned int i;
  954. /* Call user defined function first */
  955. if (graph_private->graph.pf_parameter_get)
  956. {
  957. status = graph_private->graph.pf_parameter_get(&graph_private->graph, graph_port, param);
  958. if (status != MMAL_ENOSYS)
  959. return status;
  960. }
  961. /* By default we do a get parameter on each component until one succeeds */
  962. for (i = 0; i < graph_private->component_num && status != MMAL_SUCCESS; i++)
  963. status = mmal_port_parameter_get(graph_private->component[i]->control, param);
  964. return status;
  965. }
  966. static MMAL_STATUS_T graph_port_parameter_get(MMAL_PORT_T *graph_port,
  967. MMAL_PARAMETER_HEADER_T *param)
  968. {
  969. MMAL_GRAPH_PRIVATE_T *graph_private = graph_port->component->priv->module;
  970. MMAL_STATUS_T status;
  971. MMAL_PORT_T *port;
  972. /* Call user defined function first */
  973. if (graph_private->graph.pf_parameter_get)
  974. {
  975. status = graph_private->graph.pf_parameter_get(&graph_private->graph, graph_port, param);
  976. if (status != MMAL_ENOSYS)
  977. return status;
  978. }
  979. port = find_port_from_graph(graph_private, graph_port);
  980. if (!port)
  981. return MMAL_EINVAL;
  982. /* Forward the call */
  983. return mmal_port_parameter_get(port, param);
  984. }
  985. static MMAL_STATUS_T graph_port_control_parameter_set(MMAL_PORT_T *graph_port,
  986. const MMAL_PARAMETER_HEADER_T *param)
  987. {
  988. MMAL_GRAPH_PRIVATE_T *graph_private = graph_port->component->priv->module;
  989. MMAL_STATUS_T status = MMAL_ENOSYS;
  990. unsigned int i;
  991. /* Call user defined function first */
  992. if (graph_private->graph.pf_parameter_set)
  993. {
  994. status = graph_private->graph.pf_parameter_set(&graph_private->graph, graph_port, param);
  995. if (status != MMAL_ENOSYS)
  996. return status;
  997. }
  998. /* By default we do a set parameter on each component until one succeeds */
  999. for (i = 0; i < graph_private->component_num && status != MMAL_SUCCESS; i++)
  1000. status = mmal_port_parameter_set(graph_private->component[i]->control, param);
  1001. return status;
  1002. }
  1003. static MMAL_STATUS_T graph_port_parameter_set(MMAL_PORT_T *graph_port,
  1004. const MMAL_PARAMETER_HEADER_T *param)
  1005. {
  1006. MMAL_GRAPH_PRIVATE_T *graph_private = graph_port->component->priv->module;
  1007. MMAL_STATUS_T status;
  1008. MMAL_PORT_T *port;
  1009. /* Call user defined function first */
  1010. if (graph_private->graph.pf_parameter_set)
  1011. {
  1012. status = graph_private->graph.pf_parameter_set(&graph_private->graph, graph_port, param);
  1013. if (status != MMAL_ENOSYS)
  1014. return status;
  1015. }
  1016. port = find_port_from_graph(graph_private, graph_port);
  1017. if (!port)
  1018. return MMAL_EINVAL;
  1019. /* Forward the call */
  1020. status = mmal_port_parameter_set(port, param);
  1021. if (status != MMAL_SUCCESS)
  1022. goto end;
  1023. if (param->id == MMAL_PARAMETER_BUFFER_REQUIREMENTS)
  1024. {
  1025. /* This might have changed the buffer requirements of other ports so fetch them all */
  1026. MMAL_COMPONENT_T *component = graph_port->component;
  1027. unsigned int i;
  1028. for (i = 0; status == MMAL_SUCCESS && i < component->input_num; i++)
  1029. status = graph_port_update_requirements(graph_private, component->input[i]);
  1030. for (i = 0; status == MMAL_SUCCESS && i < component->output_num; i++)
  1031. status = graph_port_update_requirements(graph_private, component->output[i]);
  1032. }
  1033. end:
  1034. return status;
  1035. }
  1036. static MMAL_STATUS_T graph_port_connect(MMAL_PORT_T *graph_port, MMAL_PORT_T *other_port)
  1037. {
  1038. MMAL_PORT_T *port;
  1039. LOG_TRACE("%s(%p) %s(%p)", graph_port->name, graph_port, other_port->name, other_port);
  1040. port = find_port_from_graph(graph_port->component->priv->module, graph_port);
  1041. if (!port)
  1042. return MMAL_EINVAL;
  1043. /* Forward the call */
  1044. return other_port ? mmal_port_connect(port, other_port) : mmal_port_disconnect(port);
  1045. }
  1046. static uint8_t *graph_port_payload_alloc(MMAL_PORT_T *graph_port, uint32_t payload_size)
  1047. {
  1048. MMAL_GRAPH_PRIVATE_T *graph_private = graph_port->component->priv->module;
  1049. MMAL_STATUS_T status;
  1050. MMAL_PORT_T *port;
  1051. uint8_t *payload;
  1052. port = find_port_from_graph(graph_port->component->priv->module, graph_port);
  1053. if (!port)
  1054. return 0;
  1055. /* Call user defined function first */
  1056. if (graph_private->graph.pf_payload_alloc)
  1057. {
  1058. status = graph_private->graph.pf_payload_alloc(&graph_private->graph, graph_port,
  1059. payload_size, &payload);
  1060. if (status != MMAL_ENOSYS)
  1061. return status == MMAL_SUCCESS ? payload : NULL;
  1062. }
  1063. /* Forward the call */
  1064. return mmal_port_payload_alloc(port, payload_size);
  1065. }
  1066. static void graph_port_payload_free(MMAL_PORT_T *graph_port, uint8_t *payload)
  1067. {
  1068. MMAL_GRAPH_PRIVATE_T *graph_private = graph_port->component->priv->module;
  1069. MMAL_STATUS_T status;
  1070. MMAL_PORT_T *port;
  1071. port = find_port_from_graph(graph_port->component->priv->module, graph_port);
  1072. if (!port)
  1073. return;
  1074. /* Call user defined function first */
  1075. if (graph_private->graph.pf_payload_free)
  1076. {
  1077. status = graph_private->graph.pf_payload_free(&graph_private->graph, graph_port, payload);
  1078. if (status == MMAL_SUCCESS)
  1079. return;
  1080. }
  1081. /* Forward the call */
  1082. mmal_port_payload_free(port, payload);
  1083. }
  1084. /** Create an instance of a component */
  1085. static MMAL_STATUS_T mmal_component_create_from_graph(const char *name, MMAL_COMPONENT_T *component)
  1086. {
  1087. MMAL_STATUS_T status = MMAL_ENOMEM;
  1088. /* Our context is already allocated and available */
  1089. MMAL_GRAPH_PRIVATE_T *graph = component->priv->module;
  1090. unsigned int i;
  1091. MMAL_PARAM_UNUSED(name);
  1092. component->control->priv->pf_parameter_get = graph_port_control_parameter_get;
  1093. component->control->priv->pf_parameter_set = graph_port_control_parameter_set;
  1094. /* Allocate the ports for this component */
  1095. if(graph->input_num)
  1096. {
  1097. component->input = mmal_ports_alloc(component, graph->input_num, MMAL_PORT_TYPE_INPUT, 0);
  1098. if(!component->input)
  1099. goto error;
  1100. }
  1101. component->input_num = graph->input_num;
  1102. for(i = 0; i < component->input_num; i++)
  1103. {
  1104. component->input[i]->priv->pf_enable = graph_port_enable;
  1105. component->input[i]->priv->pf_disable = graph_port_disable;
  1106. component->input[i]->priv->pf_flush = graph_port_flush;
  1107. component->input[i]->priv->pf_send = graph_port_send;
  1108. component->input[i]->priv->pf_set_format = graph_port_format_commit;
  1109. component->input[i]->priv->pf_parameter_get = graph_port_parameter_get;
  1110. component->input[i]->priv->pf_parameter_set = graph_port_parameter_set;
  1111. if (graph->input[i]->priv->pf_connect && 0 /* FIXME: disabled for now */)
  1112. component->input[i]->priv->pf_connect = graph_port_connect;
  1113. component->input[i]->priv->pf_payload_alloc = graph_port_payload_alloc;
  1114. component->input[i]->priv->pf_payload_free = graph_port_payload_free;
  1115. /* Mirror the port values */
  1116. status = graph_port_update(graph, component->input[i], MMAL_TRUE);
  1117. if (status != MMAL_SUCCESS)
  1118. goto error;
  1119. }
  1120. if(graph->output_num)
  1121. {
  1122. component->output = mmal_ports_alloc(component, graph->output_num, MMAL_PORT_TYPE_OUTPUT, 0);
  1123. if(!component->output)
  1124. goto error;
  1125. }
  1126. component->output_num = graph->output_num;
  1127. for(i = 0; i < component->output_num; i++)
  1128. {
  1129. component->output[i]->priv->pf_enable = graph_port_enable;
  1130. component->output[i]->priv->pf_disable = graph_port_disable;
  1131. component->output[i]->priv->pf_flush = graph_port_flush;
  1132. component->output[i]->priv->pf_send = graph_port_send;
  1133. component->output[i]->priv->pf_set_format = graph_port_format_commit;
  1134. component->output[i]->priv->pf_parameter_get = graph_port_parameter_get;
  1135. component->output[i]->priv->pf_parameter_set = graph_port_parameter_set;
  1136. if (graph->output[i]->priv->pf_connect && 0 /* FIXME: disabled for now */)
  1137. component->output[i]->priv->pf_connect = graph_port_connect;
  1138. component->output[i]->priv->pf_payload_alloc = graph_port_payload_alloc;
  1139. component->output[i]->priv->pf_payload_free = graph_port_payload_free;
  1140. /* Mirror the port values */
  1141. status = graph_port_update(graph, component->output[i], MMAL_TRUE);
  1142. if (status != MMAL_SUCCESS)
  1143. goto error;
  1144. }
  1145. status = mmal_component_action_register(component, graph_do_processing_loop);
  1146. if (status != MMAL_SUCCESS)
  1147. goto error;
  1148. #if 1 // FIXME
  1149. /* Set our connection callback */
  1150. for (i = 0; i < graph->connection_num; i++)
  1151. {
  1152. graph->connection[i]->callback = graph_component_connection_cb;
  1153. graph->connection[i]->user_data = (void *)component;
  1154. }
  1155. #endif
  1156. component->priv->pf_destroy = graph_component_destroy;
  1157. component->priv->pf_enable = graph_component_enable;
  1158. component->priv->pf_disable = graph_component_disable;
  1159. graph->graph_component = component;
  1160. /* Enable all the control ports */
  1161. for (i = 0; i < graph->component_num; i++)
  1162. {
  1163. graph->component[i]->control->userdata = (void *)component;
  1164. status = mmal_port_enable(graph->component[i]->control, graph_component_control_cb);
  1165. if (status != MMAL_SUCCESS)
  1166. LOG_ERROR("could not enable port %s", component->control->name);
  1167. }
  1168. return MMAL_SUCCESS;
  1169. error:
  1170. graph_component_destroy(component);
  1171. return status;
  1172. }
  1173. MMAL_PORT_T *mmal_graph_find_port(MMAL_GRAPH_T *graph,
  1174. const char *name,
  1175. MMAL_PORT_TYPE_T type,
  1176. unsigned index)
  1177. {
  1178. unsigned i;
  1179. MMAL_GRAPH_PRIVATE_T *private = (MMAL_GRAPH_PRIVATE_T *)graph;
  1180. for (i=0; i<private->component_num; i++)
  1181. {
  1182. MMAL_COMPONENT_T *comp = private->component[i];
  1183. if (vcos_strcasecmp(name, comp->name) == 0)
  1184. {
  1185. unsigned num;
  1186. MMAL_PORT_T **ports;
  1187. if (type == MMAL_PORT_TYPE_INPUT) {
  1188. num = comp->input_num;
  1189. ports = comp->input;
  1190. }
  1191. else if (type == MMAL_PORT_TYPE_OUTPUT) {
  1192. num = comp->output_num;
  1193. ports = comp->output;
  1194. }
  1195. else if (type == MMAL_PORT_TYPE_CONTROL) {
  1196. num = 1;
  1197. ports = &comp->control;
  1198. }
  1199. else {
  1200. vcos_assert(0);
  1201. return NULL;
  1202. }
  1203. if (index < num)
  1204. {
  1205. /* coverity[ptr_arith] num is 1 at this point */
  1206. return ports[index];
  1207. }
  1208. }
  1209. }
  1210. LOG_INFO("port %s:%d not found", name, index);
  1211. return NULL;
  1212. }