mirror of
https://github.com/pion/mediadevices.git
synced 2025-11-02 04:43:22 +08:00
Add more granular error message
This commit is contained in:
@@ -4,6 +4,12 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <x264.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 {
|
typedef struct Slice {
|
||||||
unsigned char *data;
|
unsigned char *data;
|
||||||
int data_len;
|
int data_len;
|
||||||
@@ -18,8 +24,10 @@ typedef struct Encoder {
|
|||||||
Encoder *enc_new(x264_param_t param) {
|
Encoder *enc_new(x264_param_t param) {
|
||||||
Encoder *e = (Encoder *)malloc(sizeof(Encoder));
|
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;
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
/* Configure non-default params */
|
/* Configure non-default params */
|
||||||
e->param.i_csp = param.i_csp;
|
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_repeat_headers = 1;
|
||||||
e->param.b_annexb = 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;
|
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;
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
e->h = x264_encoder_open(&e->param);
|
e->h = x264_encoder_open(&e->param);
|
||||||
if (!e->h)
|
if (!e->h) {
|
||||||
|
errno = ERR_OPEN_ENGINE;
|
||||||
goto fail2;
|
goto fail2;
|
||||||
|
}
|
||||||
|
|
||||||
return e;
|
return e;
|
||||||
|
|
||||||
@@ -54,8 +68,6 @@ fail2:
|
|||||||
|
|
||||||
fail:
|
fail:
|
||||||
free(e);
|
free(e);
|
||||||
// TODO: set appropriate errno
|
|
||||||
errno = -1;
|
|
||||||
return NULL;
|
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);
|
int frame_size = x264_encoder_encode(e->h, &nal, &i_nal, &e->pic_in, &e->pic_out);
|
||||||
Slice s = {.data_len = frame_size};
|
Slice s = {.data_len = frame_size};
|
||||||
if (frame_size <= 0) {
|
if (frame_size <= 0) {
|
||||||
// TODO: set appropriate errno
|
errno = ERR_ENCODE;
|
||||||
errno = -1;
|
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"image"
|
"image"
|
||||||
"io"
|
"io"
|
||||||
|
"sync"
|
||||||
|
"syscall"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"github.com/pion/mediadevices/pkg/codec"
|
"github.com/pion/mediadevices/pkg/codec"
|
||||||
@@ -27,12 +29,32 @@ type encoder struct {
|
|||||||
closed bool
|
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 (
|
var (
|
||||||
errInitEngine = fmt.Errorf("failed to initialize x264")
|
errInitEngine = fmt.Errorf("failed to initialize x264")
|
||||||
errApplyProfile = fmt.Errorf("failed to apply profile")
|
errDefaultPreset = fmt.Errorf("failed to set default preset")
|
||||||
errAllocPicture = fmt.Errorf("failed to alloc picture")
|
errApplyProfile = fmt.Errorf("failed to apply profile")
|
||||||
errOpenEngine = fmt.Errorf("failed to open x264")
|
errAllocPicture = fmt.Errorf("failed to alloc picture")
|
||||||
errEncode = fmt.Errorf("failed to encode")
|
errOpenEngine = fmt.Errorf("failed to open x264")
|
||||||
|
errEncode = fmt.Errorf("failed to encode")
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -51,7 +73,7 @@ func newEncoder(r video.Reader, p prop.Media) (io.ReadCloser, error) {
|
|||||||
i_keyint_max: C.int(p.KeyFrameInterval),
|
i_keyint_max: C.int(p.KeyFrameInterval),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errInitEngine
|
return nil, cerror(err.(syscall.Errno))
|
||||||
}
|
}
|
||||||
|
|
||||||
e := encoder{
|
e := encoder{
|
||||||
|
|||||||
Reference in New Issue
Block a user