mirror of
https://github.com/pion/mediadevices.git
synced 2025-11-01 12:22:36 +08:00
Add more granular error message
This commit is contained in:
@@ -4,6 +4,12 @@
|
||||
#include <stdlib.h>
|
||||
#include <x264.h>
|
||||
|
||||
#define ERR_DEFAULT_PRESET -1
|
||||
#define ERR_APPLY_PROFILE -2
|
||||
#define ERR_ALLOC_PICTURE -3
|
||||
#define ERR_OPEN_ENGINE -4
|
||||
#define ERR_ENCODE -5
|
||||
|
||||
typedef struct Slice {
|
||||
unsigned char *data;
|
||||
int data_len;
|
||||
@@ -18,8 +24,10 @@ typedef struct Encoder {
|
||||
Encoder *enc_new(x264_param_t param) {
|
||||
Encoder *e = (Encoder *)malloc(sizeof(Encoder));
|
||||
|
||||
if (x264_param_default_preset(&e->param, "veryfast", "zerolatency") < 0)
|
||||
if (x264_param_default_preset(&e->param, "veryfast", "zerolatency") < 0) {
|
||||
errno = ERR_DEFAULT_PRESET;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
/* Configure non-default params */
|
||||
e->param.i_csp = param.i_csp;
|
||||
@@ -37,15 +45,21 @@ Encoder *enc_new(x264_param_t param) {
|
||||
e->param.b_repeat_headers = 1;
|
||||
e->param.b_annexb = 1;
|
||||
|
||||
if (x264_param_apply_profile(&e->param, "baseline") < 0)
|
||||
if (x264_param_apply_profile(&e->param, "baseline") < 0) {
|
||||
errno = ERR_APPLY_PROFILE;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (x264_picture_alloc(&e->pic_in, param.i_csp, param.i_width, param.i_height) < 0)
|
||||
if (x264_picture_alloc(&e->pic_in, param.i_csp, param.i_width, param.i_height) < 0) {
|
||||
errno = ERR_ALLOC_PICTURE;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
e->h = x264_encoder_open(&e->param);
|
||||
if (!e->h)
|
||||
if (!e->h) {
|
||||
errno = ERR_OPEN_ENGINE;
|
||||
goto fail2;
|
||||
}
|
||||
|
||||
return e;
|
||||
|
||||
@@ -54,8 +68,6 @@ fail2:
|
||||
|
||||
fail:
|
||||
free(e);
|
||||
// TODO: set appropriate errno
|
||||
errno = -1;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -70,8 +82,7 @@ Slice enc_encode(Encoder *e, uint8_t *y, uint8_t *cb, uint8_t *cr) {
|
||||
int frame_size = x264_encoder_encode(e->h, &nal, &i_nal, &e->pic_in, &e->pic_out);
|
||||
Slice s = {.data_len = frame_size};
|
||||
if (frame_size <= 0) {
|
||||
// TODO: set appropriate errno
|
||||
errno = -1;
|
||||
errno = ERR_ENCODE;
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,8 @@ import (
|
||||
"fmt"
|
||||
"image"
|
||||
"io"
|
||||
"sync"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
|
||||
"github.com/pion/mediadevices/pkg/codec"
|
||||
@@ -27,12 +29,32 @@ type encoder struct {
|
||||
closed bool
|
||||
}
|
||||
|
||||
type cerror int
|
||||
|
||||
func (e cerror) Error() string {
|
||||
switch e {
|
||||
case C.ERR_DEFAULT_PRESET:
|
||||
return errDefaultPreset.Error()
|
||||
case C.ERR_APPLY_PROFILE:
|
||||
return errApplyProfile.Error()
|
||||
case C.ERR_ALLOC_PICTURE:
|
||||
return errAllocPicture.Error()
|
||||
case C.ERR_OPEN_ENGINE:
|
||||
return errOpenEngine.Error()
|
||||
case C.ERR_ENCODE:
|
||||
return errEncode.Error()
|
||||
default:
|
||||
return "unknown error"
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
errInitEngine = fmt.Errorf("failed to initialize x264")
|
||||
errApplyProfile = fmt.Errorf("failed to apply profile")
|
||||
errAllocPicture = fmt.Errorf("failed to alloc picture")
|
||||
errOpenEngine = fmt.Errorf("failed to open x264")
|
||||
errEncode = fmt.Errorf("failed to encode")
|
||||
errInitEngine = fmt.Errorf("failed to initialize x264")
|
||||
errDefaultPreset = fmt.Errorf("failed to set default preset")
|
||||
errApplyProfile = fmt.Errorf("failed to apply profile")
|
||||
errAllocPicture = fmt.Errorf("failed to alloc picture")
|
||||
errOpenEngine = fmt.Errorf("failed to open x264")
|
||||
errEncode = fmt.Errorf("failed to encode")
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -51,7 +73,7 @@ func newEncoder(r video.Reader, p prop.Media) (io.ReadCloser, error) {
|
||||
i_keyint_max: C.int(p.KeyFrameInterval),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errInitEngine
|
||||
return nil, cerror(err.(syscall.Errno))
|
||||
}
|
||||
|
||||
e := encoder{
|
||||
|
||||
Reference in New Issue
Block a user