-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
mmal_vc_api.c
1520 lines (1293 loc) · 49.4 KB
/
mmal_vc_api.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2012, Broadcom Europe Ltd
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "interface/mmal/mmal_logging.h"
#include "interface/mmal/mmal.h"
#include "mmal_vc_api.h"
#include "mmal_vc_msgs.h"
#include "mmal_vc_client_priv.h"
#include "mmal_vc_opaque_alloc.h"
#include "mmal_vc_shm.h"
#include "interface/mmal/util/mmal_util.h"
#include "interface/mmal/core/mmal_component_private.h"
#include "interface/mmal/core/mmal_port_private.h"
#include "interface/mmal/core/mmal_buffer_private.h"
#include "interface/vcos/vcos.h"
/** Private information for MMAL VC components
*/
typedef enum MMAL_ZEROLEN_CHECK_T
{
ZEROLEN_NOT_INITIALIZED,
ZEROLEN_COMPATIBLE,
ZEROLEN_INCOMPATIBLE
} MMAL_ZEROLEN_CHECK_T;
typedef enum MMAL_PORT_FLUSH_CHECK_T
{
PORT_FLUSH_NOT_INITIALIZED,
PORT_FLUSH_COMPATIBLE,
PORT_FLUSH_INCOMPATIBLE
} MMAL_PORT_FLUSH_CHECK_T;
typedef struct MMAL_PORT_MODULE_T
{
uint32_t magic;
uint32_t component_handle;
MMAL_PORT_T *port;
uint32_t port_handle;
MMAL_BOOL_T has_pool;
VCOS_BLOCKPOOL_T pool;
MMAL_BOOL_T is_zero_copy;
MMAL_BOOL_T zero_copy_workaround;
uint32_t opaque_allocs;
MMAL_BOOL_T sent_data_on_port;
MMAL_PORT_T *connected; /**< Connected port if any */
} MMAL_PORT_MODULE_T;
typedef struct MMAL_COMPONENT_MODULE_T
{
uint32_t component_handle;
MMAL_PORT_MODULE_T **ports;
uint32_t ports_num;
MMAL_QUEUE_T *callback_queue; /**< Used to queue the callbacks we need to make to the client */
MMAL_BOOL_T event_ctx_initialised;
MMAL_VC_CLIENT_BUFFER_CONTEXT_T event_ctx; /**< Used as the ctx for event buffers */
} MMAL_COMPONENT_MODULE_T;
/*****************************************************************************
* Local function prototypes
*****************************************************************************/
static void mmal_vc_do_callback(MMAL_COMPONENT_T *component);
static MMAL_STATUS_T mmal_vc_port_info_get(MMAL_PORT_T *port);
/*****************************************************************************/
MMAL_STATUS_T mmal_vc_get_version(uint32_t *major, uint32_t *minor, uint32_t *minimum)
{
mmal_worker_version msg;
size_t len = sizeof(msg);
MMAL_STATUS_T status;
status = mmal_vc_sendwait_message(mmal_vc_get_client(), &msg.header, sizeof(msg),
MMAL_WORKER_GET_VERSION, &msg, &len, MMAL_FALSE);
if (status != MMAL_SUCCESS)
return status;
if (!vcos_verify(len == sizeof(msg)))
return MMAL_EINVAL;
*major = msg.major;
*minor = msg.minor;
*minimum = msg.minimum;
return MMAL_SUCCESS;
}
/*****************************************************************************/
MMAL_STATUS_T mmal_vc_get_stats(MMAL_VC_STATS_T *stats, int reset)
{
mmal_worker_stats msg;
size_t len = sizeof(msg);
msg.reset = reset;
MMAL_STATUS_T status = mmal_vc_sendwait_message(mmal_vc_get_client(),
&msg.header, sizeof(msg),
MMAL_WORKER_GET_STATS,
&msg, &len, MMAL_FALSE);
if (status == MMAL_SUCCESS)
{
vcos_assert(len == sizeof(msg));
*stats = msg.stats;
}
return status;
}
/** Set port buffer requirements. */
static MMAL_STATUS_T mmal_vc_port_requirements_set(MMAL_PORT_T *port)
{
MMAL_PORT_MODULE_T *module = port->priv->module;
MMAL_STATUS_T status;
mmal_worker_reply reply;
mmal_worker_port_action msg;
size_t replylen = sizeof(reply);
msg.component_handle = module->component_handle;
msg.action = MMAL_WORKER_PORT_ACTION_SET_REQUIREMENTS;
msg.port_handle = module->port_handle;
msg.param.enable.port = *port;
status = mmal_vc_sendwait_message(mmal_vc_get_client(), &msg.header, sizeof(msg),
MMAL_WORKER_PORT_ACTION, &reply, &replylen, MMAL_FALSE);
if (status == MMAL_SUCCESS)
{
vcos_assert(replylen == sizeof(reply));
status = reply.status;
}
if (status != MMAL_SUCCESS)
LOG_ERROR("failed to set port requirements (%i/%i,%i/%i)",
port->buffer_num, port->buffer_num_min,
port->buffer_size, port->buffer_size_min);
return status;
}
/** Get port buffer requirements. */
static MMAL_STATUS_T mmal_vc_port_requirements_get(MMAL_PORT_T *port)
{
MMAL_PORT_MODULE_T *module = port->priv->module;
mmal_worker_port_info_get msg;
mmal_worker_port_info reply;
size_t replylen = sizeof(reply);
MMAL_STATUS_T status;
msg.component_handle = module->component_handle;
msg.port_type = port->type;
msg.index = port->index;
LOG_TRACE("get port requirements (%i:%i)", port->type, port->index);
status = mmal_vc_sendwait_message(mmal_vc_get_client(), &msg.header, sizeof(msg),
MMAL_WORKER_PORT_INFO_GET, &reply, &replylen, MMAL_FALSE);
if (status == MMAL_SUCCESS)
{
vcos_assert(replylen == sizeof(reply));
status = reply.status;
}
if (status != MMAL_SUCCESS)
{
LOG_ERROR("failed to get port requirements (%i:%i)", port->type, port->index);
return status;
}
port->buffer_num_min = reply.port.buffer_num_min;
port->buffer_num_recommended = reply.port.buffer_num_recommended;
port->buffer_size_min = reply.port.buffer_size_min;
port->buffer_size_recommended = reply.port.buffer_size_recommended;
port->buffer_alignment_min = reply.port.buffer_alignment_min;
return MMAL_SUCCESS;
}
/** Enable processing on a port */
static MMAL_STATUS_T mmal_vc_port_enable(MMAL_PORT_T *port, MMAL_PORT_BH_CB_T cb)
{
MMAL_PORT_MODULE_T *module = port->priv->module;
MMAL_STATUS_T status;
mmal_worker_reply reply;
mmal_worker_port_action msg;
size_t replylen = sizeof(reply);
MMAL_PARAM_UNUSED(cb);
if (!port->component->priv->module->event_ctx_initialised)
{
MMAL_POOL_T *pool = port->component->priv->event_pool;
MMAL_DRIVER_BUFFER_T *drv;
unsigned int i;
/* We need to associate our vc client context to all our event buffers.
* This only needs to be done when the first port is enabled because no event
* can be received on disabled ports. */
for (i = 0; i < pool->headers_num; i++)
{
drv = mmal_buffer_header_driver_data(pool->header[i]);
drv->client_context = &port->component->priv->module->event_ctx;
drv->magic = MMAL_MAGIC;
}
port->component->priv->module->event_ctx_initialised = MMAL_TRUE;
}
if (!module->connected)
{
if (vcos_blockpool_create_on_heap(&module->pool, port->buffer_num,
sizeof(MMAL_VC_CLIENT_BUFFER_CONTEXT_T),
VCOS_BLOCKPOOL_ALIGN_DEFAULT, VCOS_BLOCKPOOL_FLAG_NONE, "mmal vc port pool") != VCOS_SUCCESS)
{
LOG_ERROR("failed to create port pool");
return MMAL_ENOMEM;
}
module->has_pool = 1;
}
if (module->connected)
{
/* The connected port won't be enabled explicitly so make sure we apply
* the buffer requirements now. */
status = mmal_vc_port_requirements_set(module->connected);
if (status != MMAL_SUCCESS)
goto error;
}
msg.component_handle = module->component_handle;
msg.action = MMAL_WORKER_PORT_ACTION_ENABLE;
msg.port_handle = module->port_handle;
msg.param.enable.port = *port;
status = mmal_vc_sendwait_message(mmal_vc_get_client(), &msg.header, sizeof(msg),
MMAL_WORKER_PORT_ACTION, &reply, &replylen, MMAL_FALSE);
if (status == MMAL_SUCCESS)
{
vcos_assert(replylen == sizeof(reply));
status = reply.status;
}
if (status != MMAL_SUCCESS)
{
LOG_ERROR("failed to enable port %s: %s",
port->name, mmal_status_to_string(status));
goto error;
}
if (module->connected)
mmal_vc_port_info_get(module->connected);
return MMAL_SUCCESS;
error:
if (module->has_pool)
vcos_blockpool_delete(&module->pool);
return status;
}
/** Disable processing on a port */
static MMAL_STATUS_T mmal_vc_port_disable(MMAL_PORT_T *port)
{
MMAL_PORT_MODULE_T *module = port->priv->module;
MMAL_STATUS_T status;
mmal_worker_reply reply;
mmal_worker_port_action msg;
size_t replylen = sizeof(reply);
msg.component_handle = module->component_handle;
msg.action = MMAL_WORKER_PORT_ACTION_DISABLE;
msg.port_handle = module->port_handle;
status = mmal_vc_sendwait_message(mmal_vc_get_client(), &msg.header, sizeof(msg),
MMAL_WORKER_PORT_ACTION, &reply, &replylen, MMAL_FALSE);
if (status == MMAL_SUCCESS)
{
vcos_assert(replylen == sizeof(reply));
status = reply.status;
}
if (status != MMAL_SUCCESS)
LOG_ERROR("failed to disable port - reason %d", status);
if (module->has_pool)
{
/* MMAL server should make sure that all buffers are sent back before it
* disables the port. */
vcos_assert(vcos_blockpool_available_count(&module->pool) == port->buffer_num);
vcos_blockpool_delete(&module->pool);
module->has_pool = 0;
}
/* We need to make sure all the queued callbacks have been done */
while (mmal_queue_length(port->component->priv->module->callback_queue))
mmal_vc_do_callback(port->component);
if (module->connected)
mmal_vc_port_info_get(module->connected);
return status;
}
/** Flush a port using MMAL_WORKER_PORT_ACTION - when the port is zero-copy or no data has been sent */
static MMAL_STATUS_T mmal_vc_port_flush_normal(MMAL_PORT_T *port)
{
MMAL_PORT_MODULE_T *module = port->priv->module;
MMAL_STATUS_T status;
mmal_worker_reply reply;
mmal_worker_port_action msg;
size_t replylen = sizeof(reply);
msg.component_handle = module->component_handle;
msg.action = MMAL_WORKER_PORT_ACTION_FLUSH;
msg.port_handle = module->port_handle;
status = mmal_vc_sendwait_message(mmal_vc_get_client(), &msg.header, sizeof(msg),
MMAL_WORKER_PORT_ACTION, &reply, &replylen, MMAL_FALSE);
if (status == MMAL_SUCCESS)
{
vcos_assert(replylen == sizeof(reply));
status = reply.status;
}
if (status != MMAL_SUCCESS)
LOG_ERROR("failed to disable port - reason %d", status);
return status;
}
/** Flush a port using PORT_FLUSH - generates a dummy bulk transfer to keep it in sync
* with buffers being passed using bulk transfer */
static MMAL_STATUS_T mmal_vc_port_flush_sync(MMAL_PORT_T *port)
{
MMAL_PORT_MODULE_T *module = port->priv->module;
MMAL_STATUS_T status;
mmal_worker_reply reply;
MMAL_VC_CLIENT_BUFFER_CONTEXT_T client_context;
mmal_worker_buffer_from_host *msg;
size_t replylen = sizeof(reply);
msg = &client_context.msg;
client_context.magic = MMAL_MAGIC;
client_context.port = port;
msg->drvbuf.client_context = &client_context;
msg->drvbuf.component_handle = module->component_handle;
msg->drvbuf.port_handle = module->port_handle;
msg->drvbuf.magic = MMAL_MAGIC;
status = mmal_vc_sendwait_message(mmal_vc_get_client(), &msg->header, sizeof(*msg),
MMAL_WORKER_PORT_FLUSH, &reply, &replylen, MMAL_TRUE);
if (status == MMAL_SUCCESS)
{
vcos_assert(replylen == sizeof(reply));
status = reply.status;
}
if (status != MMAL_SUCCESS)
LOG_ERROR("failed to disable port - reason %d", status);
return status;
}
/** Flush a port */
static MMAL_STATUS_T mmal_vc_port_flush(MMAL_PORT_T *port)
{
static MMAL_PORT_FLUSH_CHECK_T is_port_flush_compatible = PORT_FLUSH_NOT_INITIALIZED;
uint32_t major = 0, minor = 0, minimum = 0;
MMAL_STATUS_T status;
/* Buffers sent to videocore, if not zero-copy, use vchiq bulk transfers to copy the data.
A flush could be sent while one of these buffers is being copied. If the normal flushing method
is used, the flush can arrive before the buffer, which causes confusion when a pre-flush buffer
arrives after the flush. So use a special flush mode that uses a dummy vchiq transfer to synchronise
things.
If data has never been sent on the port, then we don't need to worry about a flush overtaking data.
In that case, the port may not actually be set up on the other end to receive bulk transfers, so use
the normal flushing mechanism in that case.
*/
if (port->priv->module->is_zero_copy || !port->priv->module->sent_data_on_port)
return mmal_vc_port_flush_normal(port);
if (is_port_flush_compatible == PORT_FLUSH_NOT_INITIALIZED)
{
status = mmal_vc_get_version(&major, &minor, &minimum);
if (major >= 15)
{
is_port_flush_compatible = PORT_FLUSH_COMPATIBLE;
}
else
{
LOG_ERROR("Version number of MMAL Server incompatible. Required Major:14 Minor: 2 \
or Greater. Current Major %d , Minor %d",major,minor);
is_port_flush_compatible = PORT_FLUSH_INCOMPATIBLE;
}
}
if (is_port_flush_compatible == PORT_FLUSH_COMPATIBLE)
return mmal_vc_port_flush_sync(port);
else
return mmal_vc_port_flush_normal(port);
}
/** Connect 2 ports together */
static MMAL_STATUS_T mmal_vc_port_connect(MMAL_PORT_T *port, MMAL_PORT_T *other_port)
{
MMAL_PORT_MODULE_T *module = port->priv->module;
MMAL_STATUS_T status;
mmal_worker_reply reply;
mmal_worker_port_action msg;
size_t replylen = sizeof(reply);
/* We only support connecting vc components together */
if (other_port && port->priv->pf_enable != other_port->priv->pf_enable)
return MMAL_ENOSYS;
/* Send the request to the video side */
msg.component_handle = module->component_handle;
msg.action = other_port ? MMAL_WORKER_PORT_ACTION_CONNECT : MMAL_WORKER_PORT_ACTION_DISCONNECT;
msg.port_handle = module->port_handle;
if (other_port)
{
msg.param.connect.component_handle = other_port->priv->module->component_handle;
msg.param.connect.port_handle = other_port->priv->module->port_handle;
}
status = mmal_vc_sendwait_message(mmal_vc_get_client(), &msg.header, sizeof(msg),
MMAL_WORKER_PORT_ACTION, &reply, &replylen, MMAL_FALSE);
if (status == MMAL_SUCCESS)
{
vcos_assert(replylen == sizeof(reply));
status = reply.status;
}
if (status != MMAL_SUCCESS)
{
LOG_ERROR("failed to connect ports: %s", mmal_status_to_string(status));
return status;
}
if (other_port)
{
/* Connection */
module->connected = other_port;
other_port->priv->module->connected = port;
}
else
{
/* Disconnection */
if (module->connected)
module->connected->priv->module->connected = NULL;
module->connected = NULL;
}
return MMAL_SUCCESS;
}
/*****************************************************************************/
static void mmal_vc_do_callback(MMAL_COMPONENT_T *component)
{
MMAL_COMPONENT_MODULE_T *module = component->priv->module;
MMAL_BUFFER_HEADER_T *buffer;
MMAL_PORT_T *port;
/* Get a buffer from this port */
buffer = mmal_queue_get(module->callback_queue);
if (!buffer)
return; /* Will happen when a port gets disabled */
port = (MMAL_PORT_T *)buffer->priv->component_data;
/* Catch and report any transmission error */
if (buffer->flags & MMAL_BUFFER_HEADER_FLAG_TRANSMISSION_FAILED)
mmal_event_error_send(port->component, MMAL_EIO);
/* Events generated by this component are handled differently */
if (mmal_buffer_header_driver_data(buffer)->client_context ==
&component->priv->module->event_ctx)
{
mmal_port_event_send(port, buffer);
return;
}
buffer->data = mmal_vc_shm_lock(buffer->data, port->priv->module->zero_copy_workaround);
mmal_port_buffer_header_callback(port, buffer);
}
static void mmal_vc_do_callback_loop(MMAL_COMPONENT_T *component)
{
while (mmal_queue_length(component->priv->module->callback_queue))
mmal_vc_do_callback(component);
}
/** Called back from VCHI(Q) event handler when buffers come back from the copro.
*
* The message points to the message sent by videocore, and which should have
* a pointer back to our original client side context.
*
*/
static void mmal_vc_port_send_callback(mmal_worker_buffer_from_host *msg)
{
MMAL_BUFFER_HEADER_T *buffer;
MMAL_PORT_T *port;
MMAL_VC_CLIENT_BUFFER_CONTEXT_T *client_context = msg->drvbuf.client_context;
vcos_assert(client_context);
vcos_assert(client_context->magic == MMAL_MAGIC);
buffer = client_context->buffer;
port = client_context->port;
vcos_blockpool_free(msg->drvbuf.client_context);
vcos_assert(port->priv->module->magic == MMAL_MAGIC);
mmal_vc_msg_to_buffer_header(buffer, msg);
/* Queue the callback so it is delivered by the action thread */
buffer->priv->component_data = (void *)port;
mmal_queue_put(port->component->priv->module->callback_queue, buffer);
mmal_component_action_trigger(port->component);
}
static void mmal_vc_port_send_event_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
{
/* Queue the event to be delivered by the action thread */
buffer->priv->component_data = (void *)port;
mmal_queue_put(port->component->priv->module->callback_queue, buffer);
mmal_component_action_trigger(port->component);
}
/** Called from the client to send a buffer (empty or full) to
* the copro.
*/
static MMAL_STATUS_T mmal_vc_port_send(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
{
MMAL_PORT_MODULE_T *module = port->priv->module;
MMAL_STATUS_T status;
MMAL_VC_CLIENT_BUFFER_CONTEXT_T *client_context;
mmal_worker_buffer_from_host *msg;
uint32_t length;
uint32_t msgid = MMAL_WORKER_BUFFER_FROM_HOST;
uint32_t major = 0, minor = 0, minimum = 0;
static MMAL_ZEROLEN_CHECK_T is_vc_zerolength_compatible = ZEROLEN_NOT_INITIALIZED;
vcos_assert(port);
vcos_assert(module);
vcos_assert(module->magic == MMAL_MAGIC);
/* Handle event buffers */
if (buffer->cmd)
{
MMAL_EVENT_FORMAT_CHANGED_T *event = mmal_event_format_changed_get(buffer);
if (event)
{
mmal_format_copy(port->format, event->format);
status = port->priv->pf_set_format(port);
if(status != MMAL_SUCCESS)
LOG_ERROR("format not set on port %p", port);
}
else
{
LOG_ERROR("discarding event %i on port %p", (int)buffer->cmd, port);
}
buffer->length = 0;
mmal_port_buffer_header_callback(port, buffer);
return MMAL_SUCCESS;
}
/* We can only send buffers if we have a pool */
if (!module->has_pool)
{
LOG_ERROR("no pool on port %p", port);
return MMAL_EINVAL;
}
client_context = vcos_blockpool_alloc(&module->pool);
if(!client_context)
{
LOG_INFO("couldn't allocate client buffer context from pool");
return MMAL_ENOMEM;
}
msg = &client_context->msg;
client_context->magic = MMAL_MAGIC;
client_context->buffer = buffer;
client_context->callback = mmal_vc_port_send_callback;
client_context->callback_event = NULL;
client_context->port = port;
msg->drvbuf.client_context = client_context;
msg->drvbuf.component_handle = module->component_handle;
msg->drvbuf.port_handle = module->port_handle;
msg->drvbuf.magic = MMAL_MAGIC;
length = buffer->length;
if (length <= MMAL_VC_SHORT_DATA && !port->priv->module->is_zero_copy &&
(port->format->encoding == MMAL_ENCODING_OPAQUE ||
port->type == MMAL_PORT_TYPE_CLOCK))
{
memcpy(msg->short_data, buffer->data + buffer->offset, buffer->length);
msg->payload_in_message = length;
length = 0;
}
else
{
msg->payload_in_message = 0;
}
buffer->data =
mmal_vc_shm_unlock(buffer->data, &length, port->priv->module->zero_copy_workaround);
mmal_vc_buffer_header_to_msg(msg, buffer);
if (!VCOS_BLOCKPOOL_IS_VALID_HANDLE_FORMAT(msg->drvbuf.component_handle, 256))
{
LOG_ERROR("bad component handle 0x%x", msg->drvbuf.component_handle);
return MMAL_EINVAL;
}
if (msg->drvbuf.port_handle > 255)
{
LOG_ERROR("bad port handle 0x%x", msg->drvbuf.port_handle);
return MMAL_EINVAL;
}
if (module->is_zero_copy)
length = 0;
if (is_vc_zerolength_compatible == ZEROLEN_NOT_INITIALIZED)
{
status = mmal_vc_get_version(&major, &minor, &minimum);
if ((major > 12 ) || ((major == 12) && (minor >= 2)))
{
is_vc_zerolength_compatible = ZEROLEN_COMPATIBLE;
}
else
{
LOG_ERROR("Version number of MMAL Server incompatible. Required Major:12 Minor: 2 \
or Greater. Current Major %d , Minor %d",major,minor);
is_vc_zerolength_compatible = ZEROLEN_INCOMPATIBLE;
}
}
if ((is_vc_zerolength_compatible == ZEROLEN_COMPATIBLE) && !(module->is_zero_copy) && !length
&& (msg->buffer_header.flags & MMAL_BUFFER_HEADER_FLAG_EOS))
{
length = 8;
msgid = MMAL_WORKER_BUFFER_FROM_HOST_ZEROLEN;
}
if (length)
{
// We're doing a bulk transfer. Note this so that flushes know
// they need to use the more cumbersome fake-bulk-transfer mechanism
// to guarantee correct ordering.
port->priv->module->sent_data_on_port = MMAL_TRUE;
// Data will be received at the start of the destination buffer, so fixup
// the offset in the destination buffer header.
msg->buffer_header.offset = 0;
}
status = mmal_vc_send_message(mmal_vc_get_client(), &msg->header, sizeof(*msg),
buffer->data + buffer->offset, length,
msgid);
if (status != MMAL_SUCCESS)
{
LOG_INFO("failed %d", status);
vcos_blockpool_free(msg->drvbuf.client_context);
buffer->data = mmal_vc_shm_lock(buffer->data, port->priv->module->zero_copy_workaround);
}
return status;
}
static MMAL_STATUS_T mmal_vc_component_disable(MMAL_COMPONENT_T *component)
{
MMAL_STATUS_T status;
mmal_worker_reply reply;
mmal_worker_component_disable msg;
size_t replylen = sizeof(reply);
vcos_assert(component && component->priv && component->priv->module);
msg.component_handle = component->priv->module->component_handle;
status = mmal_vc_sendwait_message(mmal_vc_get_client(), &msg.header, sizeof(msg),
MMAL_WORKER_COMPONENT_DISABLE,
&reply, &replylen, MMAL_FALSE);
if (status == MMAL_SUCCESS)
{
vcos_assert(replylen == sizeof(reply));
status = reply.status;
}
if (status != MMAL_SUCCESS && status != MMAL_ENOSYS)
{
LOG_ERROR("failed to disable component - reason %d", status);
goto fail;
}
return status;
fail:
return status;
}
static MMAL_STATUS_T mmal_vc_component_enable(MMAL_COMPONENT_T *component)
{
MMAL_STATUS_T status;
mmal_worker_reply reply;
mmal_worker_component_enable msg;
size_t replylen = sizeof(reply);
vcos_assert(component && component->priv && component->priv->module);
msg.component_handle = component->priv->module->component_handle;
status = mmal_vc_sendwait_message(mmal_vc_get_client(), &msg.header, sizeof(msg),
MMAL_WORKER_COMPONENT_ENABLE, &reply, &replylen, MMAL_FALSE);
if (status == MMAL_SUCCESS)
{
vcos_assert(replylen == sizeof(reply));
status = reply.status;
}
if (status != MMAL_SUCCESS && status != MMAL_ENOSYS)
{
LOG_ERROR("failed to enable component: %s", mmal_status_to_string(status));
return status;
}
return MMAL_SUCCESS;
}
static MMAL_STATUS_T mmal_vc_component_destroy(MMAL_COMPONENT_T *component)
{
MMAL_STATUS_T status;
mmal_worker_component_destroy msg;
mmal_worker_reply reply;
size_t replylen = sizeof(reply);
vcos_assert(component && component->priv && component->priv->module);
msg.component_handle = component->priv->module->component_handle;
status = mmal_vc_sendwait_message(mmal_vc_get_client(), &msg.header, sizeof(msg),
MMAL_WORKER_COMPONENT_DESTROY,
&reply, &replylen, MMAL_FALSE);
if (status == MMAL_SUCCESS)
{
vcos_assert(replylen == sizeof(reply));
status = reply.status;
}
if (status != MMAL_SUCCESS)
{
LOG_ERROR("failed to destroy component - reason %d", status );
goto fail;
}
if(component->input_num)
mmal_ports_free(component->input, component->input_num);
if(component->output_num)
mmal_ports_free(component->output, component->output_num);
if(component->clock_num)
mmal_ports_free(component->clock, component->clock_num);
mmal_queue_destroy(component->priv->module->callback_queue);
vcos_free(component->priv->module);
component->priv->module = NULL;
fail:
// no longer require videocore
mmal_vc_release();
mmal_vc_shm_exit();
mmal_vc_deinit();
return status;
}
MMAL_STATUS_T mmal_vc_consume_mem(size_t size, uint32_t *handle)
{
MMAL_STATUS_T status;
mmal_worker_consume_mem req;
mmal_worker_consume_mem reply;
size_t len = sizeof(reply);
req.size = (uint32_t) size;
status = mmal_vc_sendwait_message(mmal_vc_get_client(),
&req.header, sizeof(req),
MMAL_WORKER_CONSUME_MEM,
&reply, &len, MMAL_FALSE);
if (status == MMAL_SUCCESS)
{
vcos_assert(len == sizeof(reply));
status = reply.status;
*handle = reply.handle;
}
return status;
}
MMAL_STATUS_T mmal_vc_compact(MMAL_VC_COMPACT_MODE_T mode, uint32_t *duration)
{
MMAL_STATUS_T status;
mmal_worker_compact req;
mmal_worker_compact reply;
size_t len = sizeof(reply);
req.mode = (uint32_t)mode;
status = mmal_vc_sendwait_message(mmal_vc_get_client(),
&req.header, sizeof(req),
MMAL_WORKER_COMPACT,
&reply, &len, MMAL_FALSE);
if (status == MMAL_SUCCESS)
{
vcos_assert(len == sizeof(reply));
status = reply.status;
*duration = reply.duration;
}
return status;
}
MMAL_STATUS_T mmal_vc_lmk(uint32_t alloc_size)
{
MMAL_STATUS_T status;
mmal_worker_lmk req;
mmal_worker_lmk reply;
size_t len = sizeof(reply);
req.alloc_size = alloc_size;
status = mmal_vc_sendwait_message(mmal_vc_get_client(),
&req.header, sizeof(req),
MMAL_WORKER_LMK,
&reply, &len, MMAL_FALSE);
return status;
}
MMAL_STATUS_T mmal_vc_host_log(const char *msg)
{
MMAL_STATUS_T status = MMAL_EINVAL;
if (msg)
{
mmal_worker_host_log req;
mmal_worker_reply reply;
size_t replylen = sizeof(reply);
size_t msg_len = vcos_safe_strcpy(req.msg, msg, sizeof(req.msg), 0);
/* Reduce the length if it is shorter than the max message length */
status = mmal_vc_sendwait_message(mmal_vc_get_client(), &req.header,
sizeof(req) - sizeof(req.msg) + vcos_min(sizeof(req.msg), msg_len + 1),
MMAL_WORKER_HOST_LOG,
&reply, &replylen, MMAL_FALSE);
if (status == MMAL_SUCCESS)
{
vcos_assert(replylen == sizeof(reply));
status = reply.status;
}
}
return status;
}
MMAL_STATUS_T mmal_vc_get_core_stats(MMAL_CORE_STATISTICS_T *stats,
MMAL_STATS_RESULT_T *result,
char *name,
size_t namelen,
MMAL_PORT_TYPE_T type,
unsigned component_index,
unsigned port_index,
MMAL_CORE_STATS_DIR dir,
MMAL_BOOL_T reset)
{
mmal_worker_get_core_stats_for_port req;
mmal_worker_get_core_stats_for_port_reply reply;
MMAL_STATUS_T status;
size_t len = sizeof(reply);
req.component_index = component_index;
req.port_index = port_index;
req.type = type;
req.reset = reset;
req.dir = dir;
status = mmal_vc_sendwait_message(mmal_vc_get_client(),
&req.header, sizeof(req),
MMAL_WORKER_GET_CORE_STATS_FOR_PORT,
&reply, &len, MMAL_FALSE);
if (status == MMAL_SUCCESS)
{
vcos_assert(len == sizeof(reply));
*stats = reply.stats;
*result = reply.result;
strncpy(name, reply.component_name, namelen);
name[namelen-1] = '\0';
}
return status;
}
/** Get port context data. */
static MMAL_STATUS_T mmal_vc_port_info_get(MMAL_PORT_T *port)
{
MMAL_PORT_MODULE_T *module = port->priv->module;
mmal_worker_port_info_get msg;
mmal_worker_port_info reply;
size_t replylen = sizeof(reply);
MMAL_STATUS_T status;
msg.component_handle = module->component_handle;
msg.port_type = port->type;
msg.index = port->index;
LOG_TRACE("get port info (%i:%i)", port->type, port->index);
status = mmal_vc_sendwait_message(mmal_vc_get_client(), &msg.header, sizeof(msg),
MMAL_WORKER_PORT_INFO_GET, &reply, &replylen, MMAL_FALSE);
if (status == MMAL_SUCCESS)
{
vcos_assert(replylen == sizeof(reply));
status = reply.status;
}
if (status != MMAL_SUCCESS)
{
LOG_ERROR("failed to get port info (%i:%i): %s", port->type, port->index,
mmal_status_to_string(status));
return status;
}
module->port_handle = reply.port_handle;
port->buffer_num_min = reply.port.buffer_num_min;
port->buffer_num_recommended = reply.port.buffer_num_recommended;
port->buffer_num = reply.port.buffer_num;
port->buffer_size_min = reply.port.buffer_size_min;
port->buffer_size_recommended = reply.port.buffer_size_recommended;
port->buffer_size = reply.port.buffer_size;
port->buffer_alignment_min = reply.port.buffer_alignment_min;
port->is_enabled = reply.port.is_enabled;
port->capabilities = reply.port.capabilities;
reply.format.extradata = port->format->extradata;
reply.format.es = port->format->es;
*port->format = reply.format;
*port->format->es = reply.es;
if(port->format->extradata_size)
{
status = mmal_format_extradata_alloc(port->format, port->format->extradata_size);
if(status != MMAL_SUCCESS)
{
vcos_assert(0);
port->format->extradata_size = 0;
LOG_ERROR("couldn't allocate extradata %i", port->format->extradata_size);
return MMAL_ENOMEM;
}
memcpy(port->format->extradata, reply.extradata, port->format->extradata_size);
}
return MMAL_SUCCESS;
}
/** Set port context data. */
static MMAL_STATUS_T mmal_vc_port_info_set(MMAL_PORT_T *port)
{
MMAL_PORT_MODULE_T *module = port->priv->module;
mmal_worker_port_info_set msg;
mmal_worker_port_info reply;
size_t replylen = sizeof(reply);
MMAL_STATUS_T status;