pion / mediadevices

Go implementation of the MediaDevices API.
https://pion.ly/
MIT License
542 stars 124 forks source link

Fixes avfoundation panic when using NV12 frame #302

Closed zyxar closed 3 years ago

zyxar commented 3 years ago

Description

This fixes panic when using NV12 frame on macos platform.

Reference issue

Fixes #292

zyxar commented 3 years ago

on my macbook i420 seems not supported:

ffmpeg -f avfoundation -framerate 30 -i default
ffmpeg version 4.3.1 Copyright (c) 2000-2020 the FFmpeg developers
  built with Apple clang version 12.0.0 (clang-1200.0.32.28)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.3.1_9 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-libsoxr --enable-videotoolbox --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack
  libavutil      56. 51.100 / 56. 51.100
  libavcodec     58. 91.100 / 58. 91.100
  libavformat    58. 45.100 / 58. 45.100
  libavdevice    58. 10.100 / 58. 10.100
  libavfilter     7. 85.100 /  7. 85.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  7.100 /  5.  7.100
  libswresample   3.  7.100 /  3.  7.100
  libpostproc    55.  7.100 / 55.  7.100
[avfoundation @ 0x7fbf0e008200] Selected pixel format (yuv420p) is not supported by the input device.
[avfoundation @ 0x7fbf0e008200] Supported pixel formats:
[avfoundation @ 0x7fbf0e008200]   uyvy422
[avfoundation @ 0x7fbf0e008200]   yuyv422
[avfoundation @ 0x7fbf0e008200]   nv12
[avfoundation @ 0x7fbf0e008200]   0rgb
[avfoundation @ 0x7fbf0e008200]   bgr0
[avfoundation @ 0x7fbf0e008200] Overriding selected pixel format to use uyvy422 instead.
Input #0, avfoundation, from 'default':
  Duration: N/A, start: 379349.975433, bitrate: N/A
    Stream #0:0: Video: rawvideo (UYVY / 0x59565955), uyvy422, 1280x720, 30 tbr, 1000k tbn, 1000k tbc
codecov[bot] commented 3 years ago

Codecov Report

Merging #302 (fb8dcb2) into master (a1087f7) will not change coverage. The diff coverage is 0.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #302   +/-   ##
=======================================
  Coverage   56.84%   56.84%           
=======================================
  Files          53       53           
  Lines        2229     2229           
=======================================
  Hits         1267     1267           
  Misses        863      863           
  Partials       99       99           
Impacted Files Coverage Δ
pkg/frame/yuv_cgo.go 87.50% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update a1087f7...fb8dcb2. Read the comment docs.

zyxar commented 3 years ago

if only I420 is specified in the constraints, it would fallback to NV12:

mediadevices DEBUG: 13:45:32.794042 mediadevices.go:156:
============ Found Properties ============

DeviceID: 85331f6b-d4e1-4063-b287-94f4262100cf
Video:
  Width: 640
  Height: 480
  FrameRate: 0
  FrameFormat: NV12

DeviceID: 85331f6b-d4e1-4063-b287-94f4262100cf
Video:
  Width: 1280
  Height: 720
  FrameRate: 0
  FrameFormat: NV12

DeviceID: 85331f6b-d4e1-4063-b287-94f4262100cf
Video:
  Width: 640
  Height: 480
  FrameRate: 0
  FrameFormat: YUY2

DeviceID: 85331f6b-d4e1-4063-b287-94f4262100cf
Video:
  Width: 1280
  Height: 720
  FrameRate: 0
  FrameFormat: YUY2

=============== Constraints ==============

DeviceID: any
VideoConstraints:
  Width: 600 (ideal)
  Height: 400 (ideal)
  FrameRate: any
  FrameFormat: I420 (ideal)

================ Best Fit ================

DeviceID: 85331f6b-d4e1-4063-b287-94f4262100cf
Video:
  Width: 640
  Height: 480
  FrameRate: 0
  FrameFormat: NV12

However, resulted in panic:

unexpected fault address 0x2f7bdfa0
fatal error: fault
[signal SIGSEGV: segmentation violation code=0x1 addr=0x2f7bdfa0 pc=0x406f3e5]

goroutine 17 [running, locked to thread]:
runtime.throw(0x434eb33, 0x5)
    /usr/local/go/src/runtime/panic.go:1117 +0x72 fp=0xc00005ad90 sp=0xc00005ad60 pc=0x4038ef2
runtime.sigpanic()
    /usr/local/go/src/runtime/signal_unix.go:741 +0x276 fp=0xc00005adc8 sp=0xc00005ad90 pc=0x404f756
runtime.memmove(0xc000180000, 0x2f728020, 0x96000)
    /usr/local/go/src/runtime/memmove_amd64.s:332 +0x3c5 fp=0xc00005add0 sp=0xc00005adc8 pc=0x406f3e5
runtime.gobytes(0x2f728020, 0x96000, 0x0, 0x0, 0x0)
    /usr/local/go/src/runtime/string.go:311 +0x97 fp=0xc00005ae08 sp=0xc00005add0 pc=0x40554f7
github.com/pion/mediadevices/pkg/avfoundation._Cfunc_GoBytes(...)
    _cgo_gotypes.go:197
github.com/pion/mediadevices/pkg/avfoundation.onData.func1(0x2f728020, 0xc000096000, 0x1, 0x0, 0x0)
    /opt/local/src/github.com/pion/mediadevices/pkg/avfoundation/avfoundation_callback_darwin.go:23 +0x59 fp=0xc00005ae40 sp=0xc00005ae08 pc=0x42b3c59
github.com/pion/mediadevices/pkg/avfoundation.onData(0xc0000a8d88, 0x2f728020, 0x96000)
    /opt/local/src/github.com/pion/mediadevices/pkg/avfoundation/avfoundation_callback_darwin.go:23 +0x5f fp=0xc00005ae80 sp=0xc00005ae40 pc=0x42b2abf
_cgoexp_3af1ed88a222_onData(0x70000ec6b848)