Skip to content

Commit 7243d0a

Browse files
author
bijunda
committed
sim: adapt macos avfoundation backend for camera indexing and capture
Adapt the macOS AVFoundation backend for multi-device discovery, camera index mapping and capture startup compatibility. This enables reliable use of multiple cameras through the SIM camera framework. Signed-off-by: Peter Bee <bijunda@bytedance.com>
1 parent c652436 commit 7243d0a

8 files changed

Lines changed: 1188 additions & 6 deletions

File tree

arch/sim/src/Makefile

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,17 @@ HOSTCFLAGS += ${INCDIR_PREFIX}$(ARCH_SRCDIR)
115115
HOSTCFLAGS += ${INCDIR_PREFIX}$(ARCH_SRCDIR)$(DELIM)chip
116116
ifeq ($(CONFIG_HOST_MACOS),y)
117117
HOSTCFLAGS += -Wno-deprecated-declarations
118+
ifeq ($(CONFIG_SIM_X11FB),y)
119+
HOSTCFLAGS += -I/opt/X11/include
120+
endif
118121
endif
119122

120123
ifeq ($(CONFIG_FS_LARGEFILE),y)
121124
HOSTCFLAGS += -D_FILE_OFFSET_BITS=64
122125
endif
123126

127+
HOST_OBJCFLAGS := $(filter-out -I$(TOPDIR)/include,$(HOSTCFLAGS))
128+
124129
HOSTSRCS = sim_hostirq.c sim_hostmemory.c sim_hostmisc.c sim_hosttime.c sim_hostuart.c
125130
HOSTSRCS += sim_hostfs.c sim_errno.c
126131

@@ -145,6 +150,11 @@ ifeq ($(CONFIG_HAVE_CXXINITIALIZE),y)
145150
LDFLAGS += -Wl,-ld_classic,-no_fixup_chains
146151
endif
147152
endif
153+
154+
# Keep the simulator executable from exporting NuttX symbols. Otherwise dyld
155+
# may resolve host libc references against NuttX's internal libc implementation
156+
# during process initialization, which can crash before main() runs.
157+
LDFLAGS += -Wl,-exported_symbol,__mh_execute_header
148158
else
149159
STDLIBS += -lrt
150160
endif
@@ -313,6 +323,14 @@ ifeq ($(CONFIG_SIM_CAMERA_V4L2),y)
313323
STDLIBS += -lv4l2
314324
endif
315325

326+
ifeq ($(CONFIG_SIM_CAMERA_AVFOUNDATION),y)
327+
HOSTSRCS += sim_host_avfoundation.c
328+
HOSTMSRCS += sim/macos/sim_host_avfoundation_backend.m
329+
CSRCS += sim_camera.c
330+
STDLIBS += -framework AVFoundation -framework CoreMedia \
331+
-framework CoreVideo -framework Foundation
332+
endif
333+
316334
ifeq ($(CONFIG_SIM_VIDEO_DECODER),y)
317335
CSRCS += sim_decoder.c
318336
CSRCS += sim_openh264dec.c
@@ -327,10 +345,10 @@ endif
327345
COBJS = $(CSRCS:.c=$(OBJEXT))
328346

329347
NUTTXOBJS = $(AOBJS) $(COBJS)
330-
HOSTOBJS = $(HOSTSRCS:.c=$(OBJEXT))
348+
HOSTOBJS = $(HOSTSRCS:.c=$(OBJEXT)) $(HOSTMSRCS:.m=$(OBJEXT))
331349
HEADOBJ = $(HEADSRC:.c=$(OBJEXT))
332350

333-
SRCS = $(ASRCS) $(CSRCS) $(HOSTSRCS)
351+
SRCS = $(ASRCS) $(CSRCS) $(HOSTSRCS) $(HOSTMSRCS)
334352
OBJS = $(AOBJS) $(COBJS) $(HOSTOBJS)
335353

336354
$(foreach lib,$(notdir $(wildcard $(APPDIR)$(DELIM)staging$(DELIM)*$(LIBEXT))), \
@@ -386,11 +404,16 @@ $(AOBJS): %$(OBJEXT): %.S
386404
$(COBJS) $(LINKOBJS): %$(OBJEXT): %.c
387405
$(call COMPILE, $<, $@)
388406

389-
$(HOSTOBJS) $(HEADOBJ): %$(OBJEXT): %.c
407+
$(HOSTSRCS:.c=$(OBJEXT)) $(HEADOBJ): %$(OBJEXT): %.c
390408
$(Q) $(ECHO_BEGIN)"CC: $<"
391409
$(Q) "$(CC)" -c $(HOSTCFLAGS) $< -o $@
392410
$(Q) $(ECHO_END)
393411

412+
$(HOSTMSRCS:.m=$(OBJEXT)): %$(OBJEXT): %.m
413+
$(Q) $(ECHO_BEGIN)"CC: $<"
414+
$(Q) $(HOSTCC) -c $(HOST_OBJCFLAGS) -x objective-c $< -o $@
415+
$(Q) $(ECHO_END)
416+
394417
# The architecture-specific library
395418

396419
libarch$(LIBEXT): $(NUTTXOBJS)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# ##############################################################################
2+
# arch/sim/src/sim/macos/CMakeLists.txt
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Licensed to the Apache Software Foundation (ASF) under one or more contributor
7+
# license agreements. See the NOTICE file distributed with this work for
8+
# additional information regarding copyright ownership. The ASF licenses this
9+
# file to you under the Apache License, Version 2.0 (the "License"); you may not
10+
# use this file except in compliance with the License. You may obtain a copy of
11+
# the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
# License for the specific language governing permissions and limitations under
19+
# the License.
20+
#
21+
# ##############################################################################
22+
23+
add_library(sim_avf_backend OBJECT EXCLUDE_FROM_ALL
24+
sim_host_avfoundation_backend.m)
25+
26+
set_property(TARGET sim_avf_backend PROPERTY INCLUDE_DIRECTORIES "")
27+
set_property(TARGET sim_avf_backend PROPERTY COMPILE_OPTIONS "")
28+
set_property(TARGET sim_avf_backend PROPERTY COMPILE_DEFINITIONS "")
29+
set_property(TARGET sim_avf_backend PROPERTY INTERFACE_INCLUDE_DIRECTORIES "")
30+
set_property(TARGET sim_avf_backend PROPERTY INTERFACE_COMPILE_OPTIONS "")
31+
set_property(TARGET sim_avf_backend PROPERTY INTERFACE_COMPILE_DEFINITIONS "")
32+
33+
target_include_directories(sim_avf_backend PRIVATE ${CMAKE_CURRENT_LIST_DIR})
34+
target_compile_options(sim_avf_backend
35+
PRIVATE $<$<COMPILE_LANGUAGE:OBJC>:-fobjc-arc>)
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/****************************************************************************
2+
* arch/sim/src/sim/macos/sim_avfoundation_backend.h
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed to the Apache Software Foundation (ASF) under one or more
7+
* contributor license agreements. See the NOTICE file distributed with
8+
* this work for additional information regarding copyright ownership. The
9+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance with the
11+
* License. You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
* License for the specific language governing permissions and limitations
19+
* under the License.
20+
*
21+
****************************************************************************/
22+
23+
#ifndef __ARCH_SIM_SRC_SIM_POSIX_SIM_AVFOUNDATION_BACKEND_H
24+
#define __ARCH_SIM_SRC_SIM_POSIX_SIM_AVFOUNDATION_BACKEND_H
25+
26+
/****************************************************************************
27+
* Included Files
28+
****************************************************************************/
29+
30+
#include <stdbool.h>
31+
#include <stddef.h>
32+
#include <stdint.h>
33+
34+
#ifdef __cplusplus
35+
extern "C"
36+
{
37+
#endif
38+
39+
/****************************************************************************
40+
* Public Types
41+
****************************************************************************/
42+
43+
/* Opaque handle implemented by Objective-C backend. */
44+
45+
struct avf_dev_s;
46+
47+
/****************************************************************************
48+
* Public Function Prototypes
49+
****************************************************************************/
50+
51+
int avf_get_device_count(void);
52+
53+
bool avf_is_available(const char *dev_path);
54+
55+
struct avf_dev_s *avf_open(const char *dev_path);
56+
int avf_close(struct avf_dev_s *dev);
57+
58+
int avf_start(struct avf_dev_s *dev);
59+
int avf_stop(struct avf_dev_s *dev);
60+
61+
/* Read one NV12 frame into caller buffer.
62+
*
63+
* Return:
64+
* >0: bytes copied
65+
* 0: no frame available yet
66+
* <0: -errno
67+
*/
68+
69+
int avf_read_frame(struct avf_dev_s *dev, uint8_t *dst, uint32_t dst_size);
70+
71+
/* Configure capture format.
72+
*
73+
* Note: backend only supports NV12.
74+
*/
75+
76+
int avf_set_format(struct avf_dev_s *dev,
77+
uint16_t width, uint16_t height,
78+
uint32_t pixfmt,
79+
uint32_t fps_denom, uint32_t fps_numer);
80+
81+
int avf_try_format(struct avf_dev_s *dev,
82+
uint16_t width, uint16_t height,
83+
uint32_t pixfmt,
84+
uint32_t fps_denom, uint32_t fps_numer);
85+
86+
#ifdef __cplusplus
87+
}
88+
#endif
89+
90+
#endif /* __ARCH_SIM_SRC_SIM_POSIX_SIM_AVFOUNDATION_BACKEND_H */

0 commit comments

Comments
 (0)