| 1 | /* |
| 2 | Copyright (c) 2012, Broadcom Europe Ltd |
| 3 | All rights reserved. |
| 4 | |
| 5 | Redistribution and use in source and binary forms, with or without |
| 6 | modification, are permitted provided that the following conditions are met: |
| 7 | * Redistributions of source code must retain the above copyright |
| 8 | notice, this list of conditions and the following disclaimer. |
| 9 | * Redistributions in binary form must reproduce the above copyright |
| 10 | notice, this list of conditions and the following disclaimer in the |
| 11 | documentation and/or other materials provided with the distribution. |
| 12 | * Neither the name of the copyright holder nor the |
| 13 | names of its contributors may be used to endorse or promote products |
| 14 | derived from this software without specific prior written permission. |
| 15 | |
| 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 17 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY |
| 20 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "mmal.h" |
| 29 | #include "mmal_port_private.h" |
| 30 | #include "mmal_buffer.h" |
| 31 | #include "mmal_logging.h" |
| 32 | |
| 33 | MMAL_EVENT_FORMAT_CHANGED_T *mmal_event_format_changed_get(MMAL_BUFFER_HEADER_T *buffer) |
| 34 | { |
| 35 | MMAL_EVENT_FORMAT_CHANGED_T *event; |
| 36 | MMAL_ES_FORMAT_T *format; |
| 37 | uint32_t size; |
| 38 | |
| 39 | size = sizeof(MMAL_EVENT_FORMAT_CHANGED_T); |
| 40 | size += sizeof(MMAL_ES_FORMAT_T) + sizeof(MMAL_ES_SPECIFIC_FORMAT_T); |
| 41 | |
| 42 | if (!buffer || buffer->cmd != MMAL_EVENT_FORMAT_CHANGED || buffer->length < size) |
| 43 | return 0; |
| 44 | |
| 45 | event = (MMAL_EVENT_FORMAT_CHANGED_T *)buffer->data; |
| 46 | format = event->format = (MMAL_ES_FORMAT_T *)&event[1]; |
| 47 | format->es = (MMAL_ES_SPECIFIC_FORMAT_T *)&format[1]; |
| 48 | format->extradata = (uint8_t *)&format->es[1]; |
| 49 | format->extradata_size = buffer->length - size; |
| 50 | return event; |
| 51 | } |
| 52 | |
| 53 | MMAL_STATUS_T mmal_event_error_send(MMAL_COMPONENT_T *component, MMAL_STATUS_T error_status) |
| 54 | { |
| 55 | MMAL_BUFFER_HEADER_T* event; |
| 56 | MMAL_STATUS_T status; |
| 57 | |
| 58 | if(!component) |
| 59 | { |
| 60 | LOG_ERROR("invalid component" ); |
| 61 | return MMAL_EINVAL; |
| 62 | } |
| 63 | |
| 64 | status = mmal_port_event_get(component->control, &event, MMAL_EVENT_ERROR); |
| 65 | if (status != MMAL_SUCCESS) |
| 66 | { |
| 67 | LOG_ERROR("event not available for component %s %p, result %d" , component->name, component, status); |
| 68 | return status; |
| 69 | } |
| 70 | |
| 71 | event->length = sizeof(MMAL_STATUS_T); |
| 72 | *(MMAL_STATUS_T *)event->data = error_status; |
| 73 | mmal_port_event_send(component->control, event); |
| 74 | |
| 75 | return MMAL_SUCCESS; |
| 76 | } |
| 77 | |
| 78 | MMAL_STATUS_T mmal_event_eos_send(MMAL_PORT_T *port) |
| 79 | { |
| 80 | MMAL_EVENT_END_OF_STREAM_T *event; |
| 81 | MMAL_BUFFER_HEADER_T *buffer; |
| 82 | MMAL_STATUS_T status; |
| 83 | |
| 84 | if(!port) |
| 85 | { |
| 86 | LOG_ERROR("invalid port" ); |
| 87 | return MMAL_EINVAL; |
| 88 | } |
| 89 | |
| 90 | status = mmal_port_event_get(port->component->control, &buffer, MMAL_EVENT_EOS); |
| 91 | if (status != MMAL_SUCCESS) |
| 92 | { |
| 93 | LOG_ERROR("event not available for port %s %p, result %d" , port->name, port, status); |
| 94 | return status; |
| 95 | } |
| 96 | |
| 97 | buffer->length = sizeof(*event); |
| 98 | event = (MMAL_EVENT_END_OF_STREAM_T *)buffer->data; |
| 99 | event->port_type = port->type; |
| 100 | event->port_index = port->index; |
| 101 | mmal_port_event_send(port->component->control, buffer); |
| 102 | |
| 103 | return MMAL_SUCCESS; |
| 104 | } |
| 105 | |
| 106 | MMAL_STATUS_T mmal_event_forward(MMAL_BUFFER_HEADER_T *event, MMAL_PORT_T *port) |
| 107 | { |
| 108 | MMAL_BUFFER_HEADER_T *buffer; |
| 109 | MMAL_STATUS_T status; |
| 110 | |
| 111 | if(!port || port->type != MMAL_PORT_TYPE_OUTPUT) |
| 112 | { |
| 113 | LOG_ERROR("invalid port" ); |
| 114 | return MMAL_EINVAL; |
| 115 | } |
| 116 | |
| 117 | status = mmal_port_event_get(port->component->control, &buffer, event->cmd); |
| 118 | if (status != MMAL_SUCCESS) |
| 119 | { |
| 120 | LOG_ERROR("event not available for port %s %p, result %d" , port->name, port, status); |
| 121 | return status; |
| 122 | } |
| 123 | |
| 124 | if (buffer->alloc_size < event->length) |
| 125 | { |
| 126 | LOG_ERROR("event buffer too small (%i/%i)" , buffer->alloc_size, event->length); |
| 127 | mmal_buffer_header_release(buffer); |
| 128 | return MMAL_ENOSPC; |
| 129 | } |
| 130 | |
| 131 | memcpy(buffer->data, event->data, event->length); |
| 132 | buffer->length = event->length; |
| 133 | buffer->offset = event->offset; |
| 134 | buffer->flags = event->flags; |
| 135 | buffer->pts = event->pts; |
| 136 | mmal_port_event_send(port->component->control, buffer); |
| 137 | return MMAL_SUCCESS; |
| 138 | } |
| 139 | |