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.

copy.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 "core/mmal_component_private.h"
  27. #include "core/mmal_port_private.h"
  28. #include "mmal_logging.h"
  29. /*****************************************************************************/
  30. typedef struct MMAL_COMPONENT_MODULE_T
  31. {
  32. MMAL_STATUS_T status; /**< current status of the component */
  33. } MMAL_COMPONENT_MODULE_T;
  34. typedef struct MMAL_PORT_MODULE_T
  35. {
  36. MMAL_QUEUE_T *queue; /**< queue for the buffers sent to the ports */
  37. MMAL_BOOL_T needs_configuring; /**< port is waiting for a format commit */
  38. } MMAL_PORT_MODULE_T;
  39. /*****************************************************************************/
  40. /** Actual processing function */
  41. static MMAL_BOOL_T copy_do_processing(MMAL_COMPONENT_T *component)
  42. {
  43. MMAL_COMPONENT_MODULE_T *module = component->priv->module;
  44. MMAL_PORT_T *port_in = component->input[0];
  45. MMAL_PORT_T *port_out = component->output[0];
  46. MMAL_BUFFER_HEADER_T *in, *out;
  47. if (port_out->priv->module->needs_configuring)
  48. return 0;
  49. in = mmal_queue_get(port_in->priv->module->queue);
  50. if (!in)
  51. return 0;
  52. /* Handle event buffers */
  53. if (in->cmd)
  54. {
  55. MMAL_EVENT_FORMAT_CHANGED_T *event = mmal_event_format_changed_get(in);
  56. if (event)
  57. {
  58. module->status = mmal_format_full_copy(port_in->format, event->format);
  59. if (module->status == MMAL_SUCCESS)
  60. module->status = port_in->priv->pf_set_format(port_in);
  61. if (module->status != MMAL_SUCCESS)
  62. {
  63. LOG_ERROR("format not set on port %s %p (%i)", port_in->name, port_in, module->status);
  64. if (mmal_event_error_send(component, module->status) != MMAL_SUCCESS)
  65. LOG_ERROR("unable to send an error event buffer");
  66. }
  67. }
  68. else
  69. {
  70. LOG_ERROR("discarding event %i on port %s %p", (int)in->cmd, port_in->name, port_in);
  71. }
  72. in->length = 0;
  73. mmal_port_buffer_header_callback(port_in, in);
  74. return 1;
  75. }
  76. /* Don't do anything if we've already seen an error */
  77. if (module->status != MMAL_SUCCESS)
  78. {
  79. mmal_queue_put_back(port_in->priv->module->queue, in);
  80. return 0;
  81. }
  82. out = mmal_queue_get(port_out->priv->module->queue);
  83. if (!out)
  84. {
  85. mmal_queue_put_back(port_in->priv->module->queue, in);
  86. return 0;
  87. }
  88. /* Sanity check the output buffer is big enough */
  89. if (out->alloc_size < in->length)
  90. {
  91. module->status = MMAL_EINVAL;
  92. if (mmal_event_error_send(component, module->status) != MMAL_SUCCESS)
  93. LOG_ERROR("unable to send an error event buffer");
  94. return 0;
  95. }
  96. mmal_buffer_header_mem_lock(out);
  97. mmal_buffer_header_mem_lock(in);
  98. memcpy(out->data, in->data + in->offset, in->length);
  99. mmal_buffer_header_mem_unlock(in);
  100. mmal_buffer_header_mem_unlock(out);
  101. out->length = in->length;
  102. out->offset = 0;
  103. out->flags = in->flags;
  104. out->pts = in->pts;
  105. out->dts = in->dts;
  106. *out->type = *in->type;
  107. /* Send buffers back */
  108. in->length = 0;
  109. mmal_port_buffer_header_callback(port_in, in);
  110. mmal_port_buffer_header_callback(port_out, out);
  111. return 1;
  112. }
  113. /*****************************************************************************/
  114. static void copy_do_processing_loop(MMAL_COMPONENT_T *component)
  115. {
  116. while (copy_do_processing(component));
  117. }
  118. /** Destroy a previously created component */
  119. static MMAL_STATUS_T copy_component_destroy(MMAL_COMPONENT_T *component)
  120. {
  121. unsigned int i;
  122. for(i = 0; i < component->input_num; i++)
  123. if(component->input[i]->priv->module->queue)
  124. mmal_queue_destroy(component->input[i]->priv->module->queue);
  125. if(component->input_num)
  126. mmal_ports_free(component->input, component->input_num);
  127. for(i = 0; i < component->output_num; i++)
  128. if(component->output[i]->priv->module->queue)
  129. mmal_queue_destroy(component->output[i]->priv->module->queue);
  130. if(component->output_num)
  131. mmal_ports_free(component->output, component->output_num);
  132. vcos_free(component->priv->module);
  133. return MMAL_SUCCESS;
  134. }
  135. /** Enable processing on a port */
  136. static MMAL_STATUS_T copy_port_enable(MMAL_PORT_T *port, MMAL_PORT_BH_CB_T cb)
  137. {
  138. MMAL_PARAM_UNUSED(cb);
  139. /* We need to propagate the buffer requirements when the input port is
  140. * enabled */
  141. if (port->type == MMAL_PORT_TYPE_INPUT)
  142. return port->priv->pf_set_format(port);
  143. return MMAL_SUCCESS;
  144. }
  145. /** Flush a port */
  146. static MMAL_STATUS_T copy_port_flush(MMAL_PORT_T *port)
  147. {
  148. MMAL_PORT_MODULE_T *port_module = port->priv->module;
  149. MMAL_BUFFER_HEADER_T *buffer;
  150. /* Flush buffers that our component is holding on to */
  151. buffer = mmal_queue_get(port_module->queue);
  152. while(buffer)
  153. {
  154. mmal_port_buffer_header_callback(port, buffer);
  155. buffer = mmal_queue_get(port_module->queue);
  156. }
  157. return MMAL_SUCCESS;
  158. }
  159. /** Disable processing on a port */
  160. static MMAL_STATUS_T copy_port_disable(MMAL_PORT_T *port)
  161. {
  162. /* We just need to flush our internal queue */
  163. return copy_port_flush(port);
  164. }
  165. /** Send a buffer header to a port */
  166. static MMAL_STATUS_T copy_port_send(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
  167. {
  168. mmal_queue_put(port->priv->module->queue, buffer);
  169. mmal_component_action_trigger(port->component);
  170. return MMAL_SUCCESS;
  171. }
  172. /** Set format on input port */
  173. static MMAL_STATUS_T copy_input_port_format_commit(MMAL_PORT_T *in)
  174. {
  175. MMAL_COMPONENT_T *component = in->component;
  176. MMAL_PORT_T *out = component->output[0];
  177. MMAL_EVENT_FORMAT_CHANGED_T *event;
  178. MMAL_BUFFER_HEADER_T *buffer;
  179. MMAL_STATUS_T status;
  180. /* Check if there's anything to propagate to the output port */
  181. /* The format of the output port needs to match the input port */
  182. if (!mmal_format_compare(in->format, out->format) &&
  183. out->buffer_size_min == out->buffer_size_recommended &&
  184. out->buffer_size_min == MMAL_MAX(in->buffer_size_min, in->buffer_size))
  185. return MMAL_SUCCESS;
  186. /* If the output port is not enabled we just need to update its format.
  187. * Otherwise we'll have to trigger a format changed event for it. */
  188. if (!out->is_enabled)
  189. {
  190. out->buffer_size_min = out->buffer_size_recommended =
  191. MMAL_MAX(in->buffer_size, in->buffer_size_min);
  192. return mmal_format_full_copy(out->format, in->format);
  193. }
  194. /* Send an event on the output port */
  195. status = mmal_port_event_get(out, &buffer, MMAL_EVENT_FORMAT_CHANGED);
  196. if (status != MMAL_SUCCESS)
  197. {
  198. LOG_ERROR("unable to get an event buffer");
  199. return status;
  200. }
  201. event = mmal_event_format_changed_get(buffer);
  202. mmal_format_copy(event->format, in->format); /* FIXME: can full copy be done ? */
  203. /* Pass on the buffer requirements */
  204. event->buffer_num_min = out->buffer_num_min;
  205. event->buffer_num_recommended = out->buffer_num_recommended;
  206. event->buffer_size_min = event->buffer_size_recommended =
  207. MMAL_MAX(in->buffer_size_min, in->buffer_size);
  208. out->priv->module->needs_configuring = 1;
  209. mmal_port_event_send(out, buffer);
  210. return status;
  211. }
  212. /** Set format on output port */
  213. static MMAL_STATUS_T copy_output_port_format_commit(MMAL_PORT_T *out)
  214. {
  215. MMAL_COMPONENT_T *component = out->component;
  216. MMAL_PORT_T *in = component->input[0];
  217. /* The format of the output port needs to match the input port */
  218. if (mmal_format_compare(out->format, in->format))
  219. return MMAL_EINVAL;
  220. out->priv->module->needs_configuring = 0;
  221. mmal_component_action_trigger(out->component);
  222. return MMAL_SUCCESS;
  223. }
  224. /** Create an instance of a component */
  225. static MMAL_STATUS_T mmal_component_create_copy(const char *name, MMAL_COMPONENT_T *component)
  226. {
  227. MMAL_COMPONENT_MODULE_T *module;
  228. MMAL_STATUS_T status = MMAL_ENOMEM;
  229. MMAL_PARAM_UNUSED(name);
  230. /* Allocate the context for our module */
  231. component->priv->module = module = vcos_malloc(sizeof(*module), "mmal module");
  232. if (!module)
  233. return MMAL_ENOMEM;
  234. memset(module, 0, sizeof(*module));
  235. component->priv->pf_destroy = copy_component_destroy;
  236. /* Allocate and initialise all the ports for this component */
  237. component->input = mmal_ports_alloc(component, 1, MMAL_PORT_TYPE_INPUT, sizeof(MMAL_PORT_MODULE_T));
  238. if(!component->input)
  239. goto error;
  240. component->input_num = 1;
  241. component->input[0]->priv->pf_enable = copy_port_enable;
  242. component->input[0]->priv->pf_disable = copy_port_disable;
  243. component->input[0]->priv->pf_flush = copy_port_flush;
  244. component->input[0]->priv->pf_send = copy_port_send;
  245. component->input[0]->priv->pf_set_format = copy_input_port_format_commit;
  246. component->input[0]->buffer_num_min = 1;
  247. component->input[0]->buffer_num_recommended = 0;
  248. component->input[0]->priv->module->queue = mmal_queue_create();
  249. if(!component->input[0]->priv->module->queue)
  250. goto error;
  251. component->output = mmal_ports_alloc(component, 1, MMAL_PORT_TYPE_OUTPUT, sizeof(MMAL_PORT_MODULE_T));
  252. if(!component->output)
  253. goto error;
  254. component->output_num = 1;
  255. component->output[0]->priv->pf_enable = copy_port_enable;
  256. component->output[0]->priv->pf_disable = copy_port_disable;
  257. component->output[0]->priv->pf_flush = copy_port_flush;
  258. component->output[0]->priv->pf_send = copy_port_send;
  259. component->output[0]->priv->pf_set_format = copy_output_port_format_commit;
  260. component->output[0]->buffer_num_min = 1;
  261. component->output[0]->buffer_num_recommended = 0;
  262. component->output[0]->priv->module->queue = mmal_queue_create();
  263. if(!component->output[0]->priv->module->queue)
  264. goto error;
  265. status = mmal_component_action_register(component, copy_do_processing_loop);
  266. if (status != MMAL_SUCCESS)
  267. goto error;
  268. return MMAL_SUCCESS;
  269. error:
  270. copy_component_destroy(component);
  271. return status;
  272. }
  273. MMAL_CONSTRUCTOR(mmal_register_component_copy);
  274. void mmal_register_component_copy(void)
  275. {
  276. mmal_component_supplier_register("copy", mmal_component_create_copy);
  277. }