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_connection.c 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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_connection.h"
  28. #include "mmal_logging.h"
  29. #include <stdio.h>
  30. #define CONNECTION_NAME_FORMAT "%s:%.2222s:%i/%s:%.2222s:%i"
  31. typedef struct
  32. {
  33. MMAL_CONNECTION_T connection; /**< Must be the first member! */
  34. MMAL_PORT_T *pool_port; /**< Port used to create the pool */
  35. /** Reference counting */
  36. int refcount;
  37. } MMAL_CONNECTION_PRIVATE_T;
  38. /** Callback from an input port. Buffer is released. */
  39. static void mmal_connection_bh_in_cb(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
  40. {
  41. MMAL_PARAM_UNUSED(port);
  42. LOG_TRACE("(%s)%p,%p,%p,%i", port->name, port, buffer, buffer->data, (int)buffer->length);
  43. /* FIXME: Clock ports are bi-directional and a buffer coming from an
  44. * "input" clock port can potentially have valid payload data, in
  45. * which case it should be queued and not released, however this
  46. * callback doesn't have access to the pool queue. */
  47. if (port->type == MMAL_PORT_TYPE_CLOCK && buffer->length)
  48. {
  49. LOG_ERROR("clock ports not supported");
  50. }
  51. /* We're done with the buffer, just recycle it */
  52. mmal_buffer_header_release(buffer);
  53. }
  54. /** Callback from an output port. Buffer is queued for the next component. */
  55. static void mmal_connection_bh_out_cb(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
  56. {
  57. MMAL_CONNECTION_T *connection = (MMAL_CONNECTION_T *)port->userdata;
  58. MMAL_PARAM_UNUSED(port);
  59. LOG_TRACE("(%s)%p,%p,%p,%i", port->name, port, buffer, buffer->data, (int)buffer->length);
  60. /* Queue the buffer produced by the output port */
  61. mmal_queue_put(connection->queue, buffer);
  62. if (connection->callback)
  63. connection->callback(connection);
  64. }
  65. /** Callback from the pool. Buffer is available. */
  66. static MMAL_BOOL_T mmal_connection_bh_release_cb(MMAL_POOL_T *pool, MMAL_BUFFER_HEADER_T *buffer,
  67. void *userdata)
  68. {
  69. MMAL_CONNECTION_T *connection = (MMAL_CONNECTION_T *)userdata;
  70. MMAL_PARAM_UNUSED(pool);
  71. /* Queue the buffer produced by the output port */
  72. mmal_queue_put(pool->queue, buffer);
  73. if (connection->callback)
  74. connection->callback(connection);
  75. return 0;
  76. }
  77. /*****************************************************************************/
  78. static MMAL_STATUS_T mmal_connection_destroy_internal(MMAL_CONNECTION_T *connection)
  79. {
  80. MMAL_STATUS_T status;
  81. if (connection->is_enabled)
  82. {
  83. status = mmal_connection_disable(connection);
  84. if (status != MMAL_SUCCESS)
  85. return status;
  86. }
  87. /* Special case for tunnelling */
  88. if (connection->flags & MMAL_CONNECTION_FLAG_TUNNELLING)
  89. {
  90. status = mmal_port_disconnect(connection->out);
  91. if (status != MMAL_SUCCESS)
  92. LOG_ERROR("connection %s could not be cleared", connection->name);
  93. }
  94. /* Cleanup resources */
  95. if (connection->pool)
  96. mmal_pool_destroy(connection->pool);
  97. if (connection->queue)
  98. mmal_queue_destroy(connection->queue);
  99. vcos_free(connection);
  100. return MMAL_SUCCESS;
  101. }
  102. /*****************************************************************************/
  103. MMAL_STATUS_T mmal_connection_destroy(MMAL_CONNECTION_T *connection)
  104. {
  105. MMAL_CONNECTION_PRIVATE_T *private = (MMAL_CONNECTION_PRIVATE_T *)connection;
  106. LOG_TRACE("%p, %s", connection, connection->name);
  107. if (--private->refcount)
  108. {
  109. LOG_DEBUG("delaying destruction of %s (refount %i)", connection->name,
  110. private->refcount);
  111. return MMAL_SUCCESS;
  112. }
  113. return mmal_connection_destroy_internal(connection);
  114. }
  115. /*****************************************************************************/
  116. MMAL_STATUS_T mmal_connection_create(MMAL_CONNECTION_T **cx,
  117. MMAL_PORT_T *out, MMAL_PORT_T *in, uint32_t flags)
  118. {
  119. MMAL_STATUS_T status = MMAL_ENOMEM;
  120. unsigned int name_size = strlen(out->component->name) + strlen(in->component->name) + sizeof(CONNECTION_NAME_FORMAT);
  121. unsigned int size = sizeof(MMAL_CONNECTION_PRIVATE_T) + name_size;
  122. MMAL_CONNECTION_PRIVATE_T *private;
  123. MMAL_CONNECTION_T *connection;
  124. char *name;
  125. /* Sanity checking */
  126. if (!cx)
  127. return MMAL_EINVAL;
  128. private = vcos_malloc(size, "mmal connection");
  129. if (!private)
  130. return MMAL_ENOMEM;
  131. memset(private, 0, size);
  132. connection = &private->connection;
  133. private->refcount = 1;
  134. name = (char *)&private[1];
  135. vcos_snprintf(name, name_size - 1, CONNECTION_NAME_FORMAT,
  136. out->component->name,
  137. mmal_port_type_to_string(out->type), (int)out->index,
  138. in->component->name,
  139. mmal_port_type_to_string(in->type), (int)in->index);
  140. LOG_TRACE("out %p, in %p, flags %x, %s", out, in, flags, name);
  141. connection->out = out;
  142. connection->in = in;
  143. connection->flags = flags;
  144. connection->name = name;
  145. connection->time_setup = vcos_getmicrosecs();
  146. /* Set the format of the input port to match the output one */
  147. status = mmal_format_full_copy(in->format, out->format);
  148. if (status == MMAL_SUCCESS)
  149. status = mmal_port_format_commit(in);
  150. if (status != MMAL_SUCCESS)
  151. {
  152. LOG_ERROR("format not set on input port");
  153. goto error;
  154. }
  155. /* In pass-through mode we need to propagate the buffer requirements of the
  156. * connected input port */
  157. if (out->capabilities & MMAL_PORT_CAPABILITY_PASSTHROUGH)
  158. {
  159. MMAL_PARAMETER_BUFFER_REQUIREMENTS_T param =
  160. {{MMAL_PARAMETER_BUFFER_REQUIREMENTS, sizeof(MMAL_PARAMETER_BUFFER_REQUIREMENTS_T)},
  161. in->buffer_num_min, in->buffer_size_min, in->buffer_alignment_min,
  162. in->buffer_num_recommended, in->buffer_size_recommended};
  163. status = mmal_port_parameter_set(out, &param.hdr);
  164. if (status != MMAL_SUCCESS && status != MMAL_ENOSYS)
  165. {
  166. LOG_ERROR("failed to propagate buffer requirements");
  167. goto error;
  168. }
  169. status = MMAL_SUCCESS;
  170. }
  171. /* Special case for tunnelling */
  172. if (connection->flags & MMAL_CONNECTION_FLAG_TUNNELLING)
  173. {
  174. status = mmal_port_connect(out, in);
  175. if (status != MMAL_SUCCESS)
  176. LOG_ERROR("connection could not be made");
  177. goto done;
  178. }
  179. /* Create empty pool of buffer headers for now (will be resized later on) */
  180. private->pool_port = (in->capabilities & MMAL_PORT_CAPABILITY_ALLOCATION) ? in : out;
  181. if (flags & MMAL_CONNECTION_FLAG_ALLOCATION_ON_INPUT)
  182. private->pool_port = in;
  183. if (flags & MMAL_CONNECTION_FLAG_ALLOCATION_ON_OUTPUT)
  184. private->pool_port = out;
  185. connection->pool = mmal_port_pool_create(private->pool_port, 0, 0);
  186. if (!connection->pool)
  187. goto error;
  188. mmal_pool_callback_set(connection->pool, mmal_connection_bh_release_cb, (void *)connection);
  189. /* Create a queue to store the buffers from the output port */
  190. connection->queue = mmal_queue_create();
  191. if (!connection->queue)
  192. goto error;
  193. done:
  194. out->userdata = (void *)connection;
  195. in->userdata = (void *)connection;
  196. connection->time_setup = vcos_getmicrosecs() - connection->time_setup;
  197. *cx = connection;
  198. return status;
  199. error:
  200. /* coverity[var_deref_model] mmal_connection_destroy_internal will check connection->pool correctly */
  201. mmal_connection_destroy_internal(connection);
  202. return status == MMAL_SUCCESS ? MMAL_ENOMEM : status;
  203. }
  204. /*****************************************************************************/
  205. void mmal_connection_acquire(MMAL_CONNECTION_T *connection)
  206. {
  207. MMAL_CONNECTION_PRIVATE_T *private = (MMAL_CONNECTION_PRIVATE_T *)connection;
  208. LOG_TRACE("connection %s(%p), refcount %i", connection->name, connection,
  209. private->refcount);
  210. private->refcount++;
  211. }
  212. /*****************************************************************************/
  213. MMAL_STATUS_T mmal_connection_release(MMAL_CONNECTION_T *connection)
  214. {
  215. MMAL_CONNECTION_PRIVATE_T *private = (MMAL_CONNECTION_PRIVATE_T *)connection;
  216. LOG_TRACE("connection %s(%p), refcount %i", connection->name, connection,
  217. private->refcount);
  218. if (--private->refcount)
  219. return MMAL_SUCCESS;
  220. LOG_TRACE("destroying connection %s(%p)", connection->name, connection);
  221. return mmal_connection_destroy_internal(connection);
  222. }
  223. /*****************************************************************************/
  224. MMAL_STATUS_T mmal_connection_enable(MMAL_CONNECTION_T *connection)
  225. {
  226. MMAL_PORT_T *in = connection->in, *out = connection->out;
  227. uint32_t buffer_num, buffer_size;
  228. MMAL_STATUS_T status;
  229. LOG_TRACE("%p, %s", connection, connection->name);
  230. if (connection->is_enabled)
  231. return MMAL_SUCCESS;
  232. connection->time_enable = vcos_getmicrosecs();
  233. /* Override the buffer values with the recommended ones (the port probably knows best) */
  234. if (!(connection->flags & MMAL_CONNECTION_FLAG_KEEP_BUFFER_REQUIREMENTS))
  235. {
  236. if (out->buffer_num_recommended)
  237. out->buffer_num = out->buffer_num_recommended;
  238. if (out->buffer_size_recommended)
  239. out->buffer_size = out->buffer_size_recommended;
  240. if (in->buffer_num_recommended)
  241. in->buffer_num = in->buffer_num_recommended;
  242. if (in->buffer_size_recommended)
  243. in->buffer_size = in->buffer_size_recommended;
  244. }
  245. /* Special case for tunnelling */
  246. if (connection->flags & MMAL_CONNECTION_FLAG_TUNNELLING)
  247. {
  248. /* Enable port. No callback because the port is connected. Other end of the connection
  249. * will be enabled automatically. */
  250. status = mmal_port_enable(out, NULL);
  251. if (status)
  252. LOG_ERROR("output port couldn't be enabled");
  253. goto done;
  254. }
  255. /* Set the buffering properties on both ports */
  256. buffer_num = MMAL_MAX(out->buffer_num, in->buffer_num);
  257. buffer_size = MMAL_MAX(out->buffer_size, in->buffer_size);
  258. out->buffer_num = in->buffer_num = buffer_num;
  259. out->buffer_size = in->buffer_size = buffer_size;
  260. /* In pass-through mode there isn't any need to allocate memory */
  261. if (out->capabilities & MMAL_PORT_CAPABILITY_PASSTHROUGH)
  262. buffer_size = 0;
  263. /* Resize the output pool */
  264. status = mmal_pool_resize(connection->pool, buffer_num, buffer_size);
  265. if (status != MMAL_SUCCESS)
  266. {
  267. LOG_ERROR("couldn't resize pool");
  268. goto done;
  269. }
  270. /* Enable output port. The callback specified here is the function which
  271. * will be called when an empty buffer header comes back to the port. */
  272. status = mmal_port_enable(out, mmal_connection_bh_out_cb);
  273. if(status)
  274. {
  275. LOG_ERROR("output port couldn't be enabled");
  276. goto done;
  277. }
  278. /* Enable input port. The callback specified here is the function which
  279. * will be called when an empty buffer header comes back to the port. */
  280. status = mmal_port_enable(in, mmal_connection_bh_in_cb);
  281. if(status)
  282. {
  283. LOG_ERROR("input port couldn't be enabled");
  284. mmal_port_disable(out);
  285. goto done;
  286. }
  287. /* Clock ports need buffers to send clock updates, so
  288. * populate both connected clock ports */
  289. if ((out->type == MMAL_PORT_TYPE_CLOCK) && (in->type == MMAL_PORT_TYPE_CLOCK))
  290. {
  291. MMAL_BUFFER_HEADER_T *buffer = mmal_queue_get(connection->pool->queue);
  292. while (buffer)
  293. {
  294. mmal_port_send_buffer(out, buffer);
  295. buffer = mmal_queue_get(connection->pool->queue);
  296. if (buffer)
  297. {
  298. mmal_port_send_buffer(in, buffer);
  299. buffer = mmal_queue_get(connection->pool->queue);
  300. }
  301. }
  302. }
  303. done:
  304. connection->time_enable = vcos_getmicrosecs() - connection->time_enable;
  305. connection->is_enabled = status == MMAL_SUCCESS;
  306. return status;
  307. }
  308. /*****************************************************************************/
  309. MMAL_STATUS_T mmal_connection_disable(MMAL_CONNECTION_T *connection)
  310. {
  311. MMAL_STATUS_T status;
  312. MMAL_BUFFER_HEADER_T *buffer;
  313. LOG_TRACE("%p, %s", connection, connection->name);
  314. if (!connection->is_enabled)
  315. return MMAL_SUCCESS;
  316. connection->time_disable = vcos_getmicrosecs();
  317. /* Special case for tunnelling */
  318. if (connection->flags & MMAL_CONNECTION_FLAG_TUNNELLING)
  319. {
  320. /* Disable port. Other end of the connection will be disabled automatically. */
  321. status = mmal_port_disable(connection->out);
  322. if (status)
  323. LOG_ERROR("output port couldn't be disabled");
  324. goto done;
  325. }
  326. /* Disable input port. */
  327. status = mmal_port_disable(connection->in);
  328. if(status)
  329. {
  330. LOG_ERROR("input port couldn't be disabled");
  331. goto done;
  332. }
  333. /* Disable output port */
  334. status = mmal_port_disable(connection->out);
  335. if(status)
  336. {
  337. LOG_ERROR("output port couldn't be disabled");
  338. goto done;
  339. }
  340. /* Flush the queue */
  341. buffer = mmal_queue_get(connection->queue);
  342. while (buffer)
  343. {
  344. mmal_buffer_header_release(buffer);
  345. buffer = mmal_queue_get(connection->queue);
  346. }
  347. vcos_assert(mmal_queue_length(connection->pool->queue) == connection->pool->headers_num);
  348. done:
  349. connection->time_disable = vcos_getmicrosecs() - connection->time_disable;
  350. connection->is_enabled = !(status == MMAL_SUCCESS);
  351. return status;
  352. }
  353. /*****************************************************************************/
  354. static MMAL_STATUS_T mmal_connection_reconfigure(MMAL_CONNECTION_T *connection, MMAL_ES_FORMAT_T *format)
  355. {
  356. MMAL_STATUS_T status;
  357. LOG_TRACE("%p, %s", connection, connection->name);
  358. status = mmal_connection_disable(connection);
  359. if (status != MMAL_SUCCESS)
  360. {
  361. LOG_ERROR("connection couldn't be disabled");
  362. return status;
  363. }
  364. /* Set the new format for the output port */
  365. status = mmal_format_full_copy(connection->out->format, format);
  366. if (status == MMAL_SUCCESS)
  367. status = mmal_port_format_commit(connection->out);
  368. if (status != MMAL_SUCCESS)
  369. {
  370. LOG_ERROR("commit failed on port %s(%p) (%i)",
  371. connection->out->name, connection->out, status);
  372. return status;
  373. }
  374. /* Set the new format for the input port */
  375. status = mmal_format_full_copy(connection->in->format, connection->out->format);
  376. if (status == MMAL_SUCCESS)
  377. status = mmal_port_format_commit(connection->in);
  378. if (status != MMAL_SUCCESS)
  379. {
  380. LOG_ERROR("commit failed on port %s(%p) (%i)",
  381. connection->in->name, connection->in, status);
  382. return status;
  383. }
  384. /* Enable ports */
  385. status = mmal_connection_enable(connection);
  386. if (status)
  387. {
  388. LOG_ERROR("connection couldn't be enabled");
  389. return status;
  390. }
  391. return MMAL_SUCCESS;
  392. }
  393. /*****************************************************************************/
  394. MMAL_STATUS_T mmal_connection_event_format_changed(MMAL_CONNECTION_T *connection,
  395. MMAL_BUFFER_HEADER_T *buffer)
  396. {
  397. MMAL_EVENT_FORMAT_CHANGED_T *event;
  398. MMAL_STATUS_T status;
  399. LOG_TRACE("%p, %s", connection, connection->name);
  400. if (buffer->cmd != MMAL_EVENT_FORMAT_CHANGED)
  401. return MMAL_EINVAL;
  402. event = mmal_event_format_changed_get(buffer);
  403. if (!event)
  404. return MMAL_EINVAL;
  405. /* If we don't need to recreate our buffers then we can just forward the event
  406. * to the next component (so it gets configured properly) */
  407. if ((connection->in->capabilities & MMAL_PORT_CAPABILITY_SUPPORTS_EVENT_FORMAT_CHANGE) &&
  408. event->buffer_size_min <= connection->out->buffer_size &&
  409. event->buffer_num_min <= connection->out->buffer_num)
  410. {
  411. status = mmal_format_full_copy(connection->out->format, event->format);
  412. if (status == MMAL_SUCCESS)
  413. status = mmal_port_format_commit(connection->out);
  414. if (status != MMAL_SUCCESS)
  415. {
  416. LOG_ERROR("format commit failed on port %s(%p) (%i)",
  417. connection->out->name, connection->out, status);
  418. return status;
  419. }
  420. mmal_buffer_header_acquire(buffer);
  421. status = mmal_port_send_buffer(connection->in, buffer);
  422. if (status != MMAL_SUCCESS)
  423. {
  424. LOG_ERROR("buffer send failed on port %s(%p) (%i)",
  425. connection->in->name, connection->in, status);
  426. mmal_buffer_header_release(buffer);
  427. return status;
  428. }
  429. return MMAL_SUCCESS;
  430. }
  431. /* Otherwise we have to reconfigure our pipeline */
  432. return mmal_connection_reconfigure(connection, event->format);
  433. }