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.

scheduler.c 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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 "mmal_logging.h"
  27. #include "core/mmal_port_private.h"
  28. #include "core/mmal_component_private.h"
  29. #include "core/mmal_clock_private.h"
  30. #define SCHEDULER_CLOCK_PORTS_NUM 1
  31. #define SCHEDULER_INPUT_PORTS_NUM 1
  32. #define SCHEDULER_OUTPUT_PORTS_NUM 1
  33. #define SCHEDULER_REQUEST_SLOTS 16
  34. /*****************************************************************************/
  35. typedef struct MMAL_COMPONENT_MODULE_T
  36. {
  37. MMAL_STATUS_T status; /**< current status of the component */
  38. } MMAL_COMPONENT_MODULE_T;
  39. typedef struct MMAL_PORT_MODULE_T
  40. {
  41. MMAL_QUEUE_T *queue; /**< queue for the buffers sent to the port */
  42. int64_t last_ts; /***< Last timestamp seen on the input port */
  43. } MMAL_PORT_MODULE_T;
  44. /*****************************************************************************/
  45. /** Process an event buffer */
  46. static MMAL_STATUS_T scheduler_event_process(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
  47. {
  48. MMAL_STATUS_T status = MMAL_EINVAL;
  49. if (buffer->cmd == MMAL_EVENT_FORMAT_CHANGED)
  50. {
  51. MMAL_EVENT_FORMAT_CHANGED_T *event =
  52. mmal_event_format_changed_get(buffer);
  53. if (!event)
  54. goto end;
  55. status = mmal_format_full_copy(port->format, event->format);
  56. if (status == MMAL_SUCCESS)
  57. status = mmal_port_format_commit(port);
  58. if (status != MMAL_SUCCESS)
  59. {
  60. LOG_ERROR("format commit failed on port %s (%i)",
  61. port->name, status);
  62. goto end;
  63. }
  64. status = MMAL_SUCCESS;
  65. }
  66. /* Forward any other event as is to the next component */
  67. else
  68. {
  69. LOG_DEBUG("forwarding unknown event %4.4s", (char *)&buffer->cmd);
  70. status = mmal_event_forward(buffer, port->component->output[port->index]);
  71. if (status != MMAL_SUCCESS)
  72. {
  73. LOG_ERROR("unable to forward event %4.4s", (char *)&buffer->cmd);
  74. goto end;
  75. }
  76. }
  77. end:
  78. buffer->length = 0;
  79. mmal_port_buffer_header_callback(port, buffer);
  80. return status;
  81. }
  82. /** Invoked when a clock request has been serviced */
  83. static void scheduler_component_clock_port_request_cb(MMAL_PORT_T *port, int64_t media_time, void *cb_data)
  84. {
  85. MMAL_COMPONENT_T *component = port->component;;
  86. MMAL_PORT_T *port_in = component->input[0];
  87. MMAL_PORT_T *port_out = component->output[0];
  88. MMAL_BUFFER_HEADER_T *buffer = (MMAL_BUFFER_HEADER_T*)cb_data;
  89. LOG_TRACE("media-time %"PRIi64" pts %"PRIi64" delta %"PRIi64,
  90. media_time, buffer->pts, media_time - buffer->pts);
  91. if (buffer->cmd)
  92. scheduler_event_process(port_in, buffer);
  93. else
  94. /* Forward the buffer to the next component */
  95. mmal_port_buffer_header_callback(port_out, buffer);
  96. }
  97. /** Process buffers on the input and output ports */
  98. static MMAL_BOOL_T scheduler_component_process_buffers(MMAL_COMPONENT_T *component)
  99. {
  100. MMAL_COMPONENT_MODULE_T *module = component->priv->module;
  101. MMAL_PORT_T *port_in = component->input[0];
  102. MMAL_PORT_T *port_out = component->output[0];
  103. MMAL_QUEUE_T *queue_in = port_in->priv->module->queue;
  104. MMAL_QUEUE_T *queue_out = port_out->priv->module->queue;
  105. MMAL_BUFFER_HEADER_T *in, *out;
  106. MMAL_STATUS_T cb_status = MMAL_EINVAL;
  107. /* Don't do anything if we've already seen an error */
  108. if (module->status != MMAL_SUCCESS)
  109. {
  110. LOG_ERROR("module failure");
  111. return MMAL_FALSE;
  112. }
  113. in = mmal_queue_get(queue_in);
  114. /* Special case for dealing with event buffers */
  115. if (in && in->cmd)
  116. {
  117. /* We normally schedule cmds so they come out in the right order,
  118. * except when we don't know when to schedule them, which will only
  119. * happen at the start of the stream.
  120. * The fudge factor added to the last timestamp here is because the
  121. * cmd really applies to the next buffer so we want to make sure
  122. * we leave enough time to the next component to process the previous
  123. * buffer before forwarding the event. */
  124. in->pts = port_in->priv->module->last_ts + 1000;
  125. if (in->pts != MMAL_TIME_UNKNOWN)
  126. cb_status = mmal_port_clock_request_add(component->clock[0],
  127. in->pts, scheduler_component_clock_port_request_cb, in);
  128. if (cb_status != MMAL_SUCCESS)
  129. {
  130. if (in->pts != MMAL_TIME_UNKNOWN)
  131. LOG_ERROR("failed to add request for cmd");
  132. scheduler_event_process(port_in, in);
  133. }
  134. return MMAL_TRUE;
  135. }
  136. /* Need both an input and output buffer to be able to go any further */
  137. out = mmal_queue_get(queue_out);
  138. if (!in || !out)
  139. goto end;
  140. if (port_out->capabilities & MMAL_PORT_CAPABILITY_PASSTHROUGH)
  141. {
  142. /* Just need to keep a reference to the input buffer */
  143. module->status = mmal_buffer_header_replicate(out, in);
  144. }
  145. else
  146. {
  147. /* Make a full copy of the input payload */
  148. if (out->alloc_size < in->length)
  149. {
  150. LOG_ERROR("output buffer too small");
  151. module->status = MMAL_EINVAL;
  152. if (mmal_event_error_send(component, module->status) != MMAL_SUCCESS)
  153. LOG_ERROR("unable to send an error event buffer");
  154. goto end;
  155. }
  156. mmal_buffer_header_mem_lock(out);
  157. mmal_buffer_header_mem_lock(in);
  158. memcpy(out->data, in->data + in->offset, in->length);
  159. mmal_buffer_header_mem_unlock(in);
  160. mmal_buffer_header_mem_unlock(out);
  161. out->length = in->length;
  162. out->offset = 0;
  163. out->flags = in->flags;
  164. out->pts = in->pts;
  165. out->dts = in->dts;
  166. *out->type = *in->type;
  167. }
  168. /* Finished with the input buffer, so return it */
  169. in->length = 0;
  170. mmal_port_buffer_header_callback(port_in, in);
  171. in = 0;
  172. if (module->status != MMAL_SUCCESS)
  173. {
  174. LOG_ERROR("failed to replicate buffer");
  175. goto end;
  176. }
  177. /* Request a clock callback when media-time >= pts */
  178. LOG_TRACE("requesting callback at %"PRIi64,out->pts);
  179. port_in->priv->module->last_ts = out->pts;
  180. cb_status = mmal_port_clock_request_add(component->clock[0], out->pts,
  181. scheduler_component_clock_port_request_cb, out);
  182. if (cb_status != MMAL_SUCCESS)
  183. {
  184. LOG_ERROR("failed to add request");
  185. out->length = 0;
  186. mmal_port_buffer_header_callback(port_out, out);
  187. if (cb_status != MMAL_ECORRUPT)
  188. module->status = cb_status;
  189. }
  190. out = 0;
  191. end:
  192. if (in)
  193. mmal_queue_put_back(queue_in, in);
  194. if (out)
  195. mmal_queue_put_back(queue_out, out);
  196. return mmal_queue_length(queue_in) && mmal_queue_length(queue_out);
  197. }
  198. /** Main processing action */
  199. static void scheduler_component_action(MMAL_COMPONENT_T *component)
  200. {
  201. /* Send requests to the clock */
  202. while (scheduler_component_process_buffers(component));
  203. }
  204. /** Destroy a scheduler component */
  205. static MMAL_STATUS_T scheduler_component_destroy(MMAL_COMPONENT_T *component)
  206. {
  207. unsigned int i;
  208. for (i = 0; i < component->input_num; i++)
  209. if (component->input[i]->priv->module->queue)
  210. mmal_queue_destroy(component->input[i]->priv->module->queue);
  211. if (component->input_num)
  212. mmal_ports_free(component->input, component->input_num);
  213. for (i = 0; i < component->output_num; i++)
  214. if (component->output[i]->priv->module->queue)
  215. mmal_queue_destroy(component->output[i]->priv->module->queue);
  216. if (component->output_num)
  217. mmal_ports_free(component->output, component->output_num);
  218. if (component->clock_num)
  219. mmal_ports_clock_free(component->clock, component->clock_num);
  220. vcos_free(component->priv->module);
  221. return MMAL_SUCCESS;
  222. }
  223. /** Enable processing on a port */
  224. static MMAL_STATUS_T scheduler_port_enable(MMAL_PORT_T *port, MMAL_PORT_BH_CB_T cb)
  225. {
  226. MMAL_PARAM_UNUSED(port);
  227. MMAL_PARAM_UNUSED(cb);
  228. return MMAL_SUCCESS;
  229. }
  230. /** Flush a port */
  231. static MMAL_STATUS_T scheduler_port_flush(MMAL_PORT_T *port)
  232. {
  233. MMAL_PORT_MODULE_T *port_module = port->priv->module;
  234. MMAL_BUFFER_HEADER_T *buffer;
  235. /* Flush buffers associated with pending clock requests */
  236. mmal_port_clock_request_flush(port->component->clock[0]);
  237. /* Flush buffers that our component is holding on to */
  238. buffer = mmal_queue_get(port_module->queue);
  239. while (buffer)
  240. {
  241. mmal_port_buffer_header_callback(port, buffer);
  242. buffer = mmal_queue_get(port_module->queue);
  243. }
  244. port->priv->module->last_ts = MMAL_TIME_UNKNOWN;
  245. return MMAL_SUCCESS;
  246. }
  247. /** Disable processing on a port */
  248. static MMAL_STATUS_T scheduler_port_disable(MMAL_PORT_T *port)
  249. {
  250. /* We just need to flush our internal queue */
  251. return scheduler_port_flush(port);
  252. }
  253. /** Send a buffer header to a port */
  254. static MMAL_STATUS_T scheduler_port_send(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
  255. {
  256. MMAL_COMPONENT_T *component = port->component;
  257. /* notify the clock port */
  258. if (port->type == MMAL_PORT_TYPE_INPUT && !buffer->cmd)
  259. {
  260. MMAL_CLOCK_BUFFER_INFO_T info = { buffer->pts, vcos_getmicrosecs() };
  261. mmal_port_clock_input_buffer_info(port->component->clock[0], &info);
  262. }
  263. mmal_queue_put(port->priv->module->queue, buffer);
  264. return mmal_component_action_trigger(component);
  265. }
  266. /** Set format on an input port */
  267. static MMAL_STATUS_T scheduler_input_port_format_commit(MMAL_PORT_T *port)
  268. {
  269. MMAL_COMPONENT_T *component = port->component;
  270. MMAL_EVENT_FORMAT_CHANGED_T *event;
  271. MMAL_PORT_T *output = component->output[0];
  272. MMAL_BUFFER_HEADER_T *buffer;
  273. MMAL_STATUS_T status;
  274. /* If the output port is not enabled we just need to update its format.
  275. * Otherwise we'll have to trigger a format changed event for it. */
  276. if (!output->is_enabled)
  277. {
  278. status = mmal_format_full_copy(output->format, port->format);
  279. return status;
  280. }
  281. /* Send an event on the output port */
  282. status = mmal_port_event_get(output, &buffer, MMAL_EVENT_FORMAT_CHANGED);
  283. if (status != MMAL_SUCCESS)
  284. {
  285. LOG_ERROR("unable to get an event buffer");
  286. return status;
  287. }
  288. event = mmal_event_format_changed_get(buffer);
  289. if (!event)
  290. {
  291. mmal_buffer_header_release(buffer);
  292. LOG_ERROR("failed to set format");
  293. return MMAL_EINVAL;
  294. }
  295. mmal_format_copy(event->format, port->format);
  296. /* Pass on the buffer requirements */
  297. event->buffer_num_min = port->buffer_num_min;
  298. event->buffer_size_min = port->buffer_size_min;
  299. event->buffer_num_recommended = port->buffer_num_recommended;
  300. event->buffer_size_recommended = port->buffer_size_recommended;
  301. mmal_port_event_send(component->output[port->index], buffer);
  302. return status;
  303. }
  304. /** Set format on an output port */
  305. static MMAL_STATUS_T scheduler_output_port_format_commit(MMAL_PORT_T *port)
  306. {
  307. /* The format of the output port needs to match the input port */
  308. if (mmal_format_compare(port->format, port->component->input[port->index]->format))
  309. LOG_DEBUG("output port format different from input port");
  310. return MMAL_SUCCESS;
  311. }
  312. static MMAL_STATUS_T scheduler_port_parameter_set(MMAL_PORT_T *port, const MMAL_PARAMETER_HEADER_T *param)
  313. {
  314. MMAL_COMPONENT_T *component = port->component;
  315. MMAL_PORT_T *in = component->input[port->index], *out = component->input[port->index];
  316. switch (param->id)
  317. {
  318. case MMAL_PARAMETER_BUFFER_REQUIREMENTS:
  319. {
  320. /* Propagate the requirements to the matching input and output the ports */
  321. const MMAL_PARAMETER_BUFFER_REQUIREMENTS_T *req = (const MMAL_PARAMETER_BUFFER_REQUIREMENTS_T *)param;
  322. uint32_t buffer_num_min = MMAL_MAX(port->buffer_num_min, req->buffer_num_min);
  323. uint32_t buffer_num_recommended = MMAL_MAX(port->buffer_num_recommended, req->buffer_num_recommended);
  324. uint32_t buffer_size_min = MMAL_MAX(port->buffer_size_min, req->buffer_size_min);
  325. uint32_t buffer_size_recommended = MMAL_MAX(port->buffer_size_recommended, req->buffer_size_recommended);
  326. in->buffer_num_min = buffer_num_min;
  327. in->buffer_num_recommended = buffer_num_recommended;
  328. in->buffer_size_min = buffer_size_min;
  329. in->buffer_size_recommended = buffer_size_recommended;
  330. out->buffer_num_min = buffer_num_min;
  331. out->buffer_num_recommended = buffer_num_recommended;
  332. out->buffer_size_min = buffer_size_min;
  333. out->buffer_size_recommended = buffer_size_recommended;
  334. }
  335. return MMAL_SUCCESS;
  336. default:
  337. return MMAL_ENOSYS;
  338. }
  339. }
  340. /** Create an instance of a component */
  341. static MMAL_STATUS_T mmal_component_create_scheduler(const char *name, MMAL_COMPONENT_T *component)
  342. {
  343. MMAL_COMPONENT_MODULE_T *module;
  344. MMAL_STATUS_T status = MMAL_ENOMEM;
  345. int disable_passthrough = 0;
  346. unsigned int i;
  347. /* Allocate the context for our module */
  348. component->priv->module = module = vcos_calloc(1, sizeof(*module), "mmal module");
  349. if (!module)
  350. return MMAL_ENOMEM;
  351. component->priv->pf_destroy = scheduler_component_destroy;
  352. /* Allocate and initialise all the ports for this component */
  353. component->input = mmal_ports_alloc(component, SCHEDULER_INPUT_PORTS_NUM,
  354. MMAL_PORT_TYPE_INPUT, sizeof(MMAL_PORT_MODULE_T));
  355. if (!component->input)
  356. goto error;
  357. component->input_num = SCHEDULER_INPUT_PORTS_NUM;
  358. for (i = 0; i < component->input_num; i++)
  359. {
  360. component->input[i]->priv->pf_enable = scheduler_port_enable;
  361. component->input[i]->priv->pf_disable = scheduler_port_disable;
  362. component->input[i]->priv->pf_flush = scheduler_port_flush;
  363. component->input[i]->priv->pf_send = scheduler_port_send;
  364. component->input[i]->priv->pf_set_format = scheduler_input_port_format_commit;
  365. component->input[i]->priv->pf_parameter_set = scheduler_port_parameter_set;
  366. component->input[i]->buffer_num_min = 1;
  367. component->input[i]->buffer_num_recommended = 0;
  368. component->input[i]->capabilities = MMAL_PORT_CAPABILITY_SUPPORTS_EVENT_FORMAT_CHANGE;
  369. component->input[i]->priv->module->queue = mmal_queue_create();
  370. if (!component->input[i]->priv->module->queue)
  371. goto error;
  372. component->input[i]->priv->module->last_ts = MMAL_TIME_UNKNOWN;
  373. }
  374. /* Override passthrough behaviour */
  375. if (strstr(name, ".copy"))
  376. {
  377. LOG_TRACE("disable passthrough on output ports");
  378. disable_passthrough = 1;
  379. }
  380. component->output = mmal_ports_alloc(component, SCHEDULER_OUTPUT_PORTS_NUM,
  381. MMAL_PORT_TYPE_OUTPUT, sizeof(MMAL_PORT_MODULE_T));
  382. if (!component->output)
  383. goto error;
  384. component->output_num = SCHEDULER_OUTPUT_PORTS_NUM;
  385. for (i = 0; i < component->output_num; i++)
  386. {
  387. component->output[i]->priv->pf_enable = scheduler_port_enable;
  388. component->output[i]->priv->pf_disable = scheduler_port_disable;
  389. component->output[i]->priv->pf_flush = scheduler_port_flush;
  390. component->output[i]->priv->pf_send = scheduler_port_send;
  391. component->output[i]->priv->pf_set_format = scheduler_output_port_format_commit;
  392. component->output[i]->priv->pf_parameter_set = scheduler_port_parameter_set;
  393. component->output[i]->buffer_num_min = 1;
  394. component->output[i]->buffer_num_recommended = 0;
  395. component->output[i]->capabilities = disable_passthrough ? 0 : MMAL_PORT_CAPABILITY_PASSTHROUGH;
  396. component->output[i]->priv->module->queue = mmal_queue_create();
  397. if (!component->output[i]->priv->module->queue)
  398. goto error;
  399. }
  400. /* Create the clock port (clock ports are managed by the framework) */
  401. component->clock = mmal_ports_clock_alloc(component, SCHEDULER_CLOCK_PORTS_NUM, 0, NULL);
  402. if (!component->clock)
  403. goto error;
  404. component->clock_num = SCHEDULER_CLOCK_PORTS_NUM;
  405. status = mmal_component_action_register(component, scheduler_component_action);
  406. if (status != MMAL_SUCCESS)
  407. goto error;
  408. return MMAL_SUCCESS;
  409. error:
  410. scheduler_component_destroy(component);
  411. return status;
  412. }
  413. MMAL_CONSTRUCTOR(mmal_register_component_scheduler);
  414. void mmal_register_component_scheduler(void)
  415. {
  416. mmal_component_supplier_register("scheduler", mmal_component_create_scheduler);
  417. }