Compare commits

..

1 Commits

Author SHA1 Message Date
Romain Vimont
be6c6325d9 hw_dec 2022-03-14 21:29:07 +01:00
11 changed files with 81 additions and 67 deletions

View File

@@ -448,9 +448,6 @@ scrcpy --serial 0123456789abcdef
scrcpy -s 0123456789abcdef # short version
```
The serial may also be provided via the environment variable `ANDROID_SERIAL`
(also used by `adb`).
If the device is connected over TCP/IP:
```bash

View File

@@ -477,10 +477,6 @@ Push file to device (see \fB\-\-push\-target\fR)
.B ADB
Path to adb.
.TP
.B ANDROID_SERIAL
Device serial to use if no selector (-s, -d, -e or --tcpip=<addr>) is specified.
.TP
.B SCRCPY_ICON_PATH
Path to the program icon.

View File

@@ -473,12 +473,9 @@ sc_adb_accept_device(const struct sc_adb_device *device,
}
return !strcmp(selector->serial, device->serial);
case SC_ADB_DEVICE_SELECT_USB:
return sc_adb_device_get_type(device->serial) ==
SC_ADB_DEVICE_TYPE_USB;
return !sc_adb_is_serial_tcpip(device->serial);
case SC_ADB_DEVICE_SELECT_TCPIP:
// Both emulators and TCP/IP devices are selected via -e
return sc_adb_device_get_type(device->serial) !=
SC_ADB_DEVICE_TYPE_USB;
return sc_adb_is_serial_tcpip(device->serial);
default:
assert(!"Missing SC_ADB_DEVICE_SELECT_* handling");
break;
@@ -512,10 +509,8 @@ sc_adb_devices_log(enum sc_log_level level, struct sc_adb_device *devices,
for (size_t i = 0; i < count; ++i) {
struct sc_adb_device *d = &devices[i];
const char *selection = d->selected ? "-->" : " ";
bool is_usb =
sc_adb_device_get_type(d->serial) == SC_ADB_DEVICE_TYPE_USB;
const char *type = is_usb ? " (usb)"
: "(tcpip)";
const char *type = sc_adb_is_serial_tcpip(d->serial) ? "(tcpip)"
: " (usb)";
LOG(level, " %s %s %-20s %16s %s",
selection, type, d->serial, d->state, d->model ? d->model : "");
}
@@ -536,8 +531,6 @@ sc_adb_device_check_state(struct sc_adb_device *device,
LOGE("A popup should open on the device to request authorization.");
LOGE("Check the FAQ: "
"<https://github.com/Genymobile/scrcpy/blob/master/FAQ.md>");
} else {
LOGE("Device could not be connected (state=%s)", state);
}
return false;
@@ -712,3 +705,8 @@ sc_adb_get_device_ip(struct sc_intr *intr, const char *serial, unsigned flags) {
return sc_adb_parse_device_ip_from_output(buf);
}
bool
sc_adb_is_serial_tcpip(const char *serial) {
return strchr(serial, ':');
}

View File

@@ -114,4 +114,13 @@ sc_adb_getprop(struct sc_intr *intr, const char *serial, const char *prop,
char *
sc_adb_get_device_ip(struct sc_intr *intr, const char *serial, unsigned flags);
/**
* Indicate if the serial represents an IP address
*
* In practice, it just returns true if and only if it contains a ':', which is
* sufficient to distinguish an ip:port from a real USB serial.
*/
bool
sc_adb_is_serial_tcpip(const char *serial);
#endif

View File

@@ -25,18 +25,3 @@ sc_adb_devices_destroy(struct sc_vec_adb_devices *devices) {
sc_vector_destroy(devices);
}
enum sc_adb_device_type
sc_adb_device_get_type(const char *serial) {
// Starts with "emulator-"
if (!strncmp(serial, "emulator-", sizeof("emulator-") - 1)) {
return SC_ADB_DEVICE_TYPE_EMULATOR;
}
// If the serial contains a ':', then it is a TCP/IP device (it is
// sufficient to distinguish an ip:port from a real USB serial)
if (strchr(serial, ':')) {
return SC_ADB_DEVICE_TYPE_TCPIP;
}
return SC_ADB_DEVICE_TYPE_USB;
}

View File

@@ -15,12 +15,6 @@ struct sc_adb_device {
bool selected;
};
enum sc_adb_device_type {
SC_ADB_DEVICE_TYPE_USB,
SC_ADB_DEVICE_TYPE_TCPIP,
SC_ADB_DEVICE_TYPE_EMULATOR,
};
struct sc_vec_adb_devices SC_VECTOR(struct sc_adb_device);
void
@@ -41,10 +35,4 @@ sc_adb_device_move(struct sc_adb_device *dst, struct sc_adb_device *src);
void
sc_adb_devices_destroy(struct sc_vec_adb_devices *devices);
/**
* Deduce the device type from the serial
*/
enum sc_adb_device_type
sc_adb_device_get_type(const char *serial);
#endif

View File

@@ -660,11 +660,6 @@ static const struct sc_envvar envvars[] = {
.name = "ADB",
.text = "Path to adb executable",
},
{
.name = "ANDROID_SERIAL",
.text = "Device serial to use if no selector (-s, -d, -e or "
"--tcpip=<addr>) is specified",
},
{
.name = "SCRCPY_ICON_PATH",
.text = "Path to the program icon",

View File

@@ -2,6 +2,7 @@
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/pixdesc.h>
#include "events.h"
#include "video_buffer.h"
@@ -11,6 +12,39 @@
/** Downcast packet_sink to decoder */
#define DOWNCAST(SINK) container_of(SINK, struct sc_decoder, packet_sink)
static int hw_decoder_init(struct sc_decoder *decoder, const enum AVHWDeviceType type)
{
int err = 0;
AVBufferRef *hw_device_ctx;
if ((err = av_hwdevice_ctx_create(&hw_device_ctx, type,
NULL, NULL, 0)) < 0) {
LOGE("Failed to create specified HW device.");
return err;
}
decoder->codec_ctx->hw_device_ctx = av_buffer_ref(hw_device_ctx);
return err;
}
static enum AVPixelFormat get_hw_format(AVCodecContext *ctx,
const enum AVPixelFormat *pix_fmts)
{
(void) ctx;
const enum AVPixelFormat *p;
LOGD("== get_hw_format ==");
for (p = pix_fmts; *p != AV_PIX_FMT_NONE; p++) {
LOGD("==== %s (%d)", av_get_pix_fmt_name(*p), *p);
if (*p == AV_PIX_FMT_VAAPI)
return *p;
}
LOGE("Failed to get HW surface format.");
return AV_PIX_FMT_NONE;
}
static void
sc_decoder_close_first_sinks(struct sc_decoder *decoder, unsigned count) {
while (count) {
@@ -48,6 +82,10 @@ sc_decoder_open(struct sc_decoder *decoder, const AVCodec *codec) {
decoder->codec_ctx->flags |= AV_CODEC_FLAG_LOW_DELAY;
int r = hw_decoder_init(decoder, AV_HWDEVICE_TYPE_VAAPI);
assert(!r);
decoder->codec_ctx->get_format = get_hw_format;
if (avcodec_open2(decoder->codec_ctx, codec, NULL) < 0) {
LOGE("Could not open codec");
avcodec_free_context(&decoder->codec_ctx);
@@ -62,6 +100,9 @@ sc_decoder_open(struct sc_decoder *decoder, const AVCodec *codec) {
return false;
}
decoder->hw_frame = av_frame_alloc();
assert(decoder->hw_frame);
if (!sc_decoder_open_sinks(decoder)) {
LOGE("Could not open decoder sinks");
av_frame_free(&decoder->frame);
@@ -76,6 +117,7 @@ sc_decoder_open(struct sc_decoder *decoder, const AVCodec *codec) {
static void
sc_decoder_close(struct sc_decoder *decoder) {
sc_decoder_close_sinks(decoder);
av_frame_free(&decoder->hw_frame);
av_frame_free(&decoder->frame);
avcodec_close(decoder->codec_ctx);
avcodec_free_context(&decoder->codec_ctx);
@@ -107,15 +149,26 @@ sc_decoder_push(struct sc_decoder *decoder, const AVPacket *packet) {
LOGE("Could not send video packet: %d", ret);
return false;
}
ret = avcodec_receive_frame(decoder->codec_ctx, decoder->frame);
ret = avcodec_receive_frame(decoder->codec_ctx, decoder->hw_frame);
if (!ret) {
// a frame was received
sc_tick t = sc_tick_now();
ret = av_hwframe_transfer_data(decoder->frame, decoder->hw_frame, 0);
if (ret < 0) {
LOGE("HWFRAME transfer fail");
return false;
}
LOGD("av_hwframe_transfer_data: %ld", sc_tick_now() - t);
bool ok = push_frame_to_sinks(decoder, decoder->frame);
// A frame lost should not make the whole pipeline fail. The error, if
// any, is already logged.
(void) ok;
av_frame_unref(decoder->frame);
av_frame_unref(decoder->hw_frame);
} else if (ret != AVERROR(EAGAIN)) {
LOGE("Could not receive video frame: %d", ret);
return false;

View File

@@ -19,6 +19,7 @@ struct sc_decoder {
AVCodecContext *codec_ctx;
AVFrame *frame;
AVFrame *hw_frame;
};
void

View File

@@ -244,7 +244,7 @@ static inline SDL_Texture *
create_texture(struct sc_screen *screen) {
SDL_Renderer *renderer = screen->renderer;
struct sc_size size = screen->frame_size;
SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_YV12,
SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_NV12,
SDL_TEXTUREACCESS_STREAMING,
size.width, size.height);
if (!texture) {
@@ -711,10 +711,11 @@ prepare_for_frame(struct sc_screen *screen, struct sc_size new_frame_size) {
// write the frame into the texture
static void
update_texture(struct sc_screen *screen, const AVFrame *frame) {
SDL_UpdateYUVTexture(screen->texture, NULL,
frame->data[0], frame->linesize[0],
frame->data[1], frame->linesize[1],
frame->data[2], frame->linesize[2]);
// SDL_UpdateYUVTexture(screen->texture, NULL,
// frame->data[0], frame->linesize[0],
// frame->data[1], frame->linesize[1],
// frame->data[2], frame->linesize[2]);
SDL_UpdateTexture(screen->texture, NULL, frame->data[0], frame->linesize[0]);
if (screen->mipmaps) {
SDL_GL_BindTexture(screen->texture, NULL, NULL);

View File

@@ -649,8 +649,7 @@ sc_server_configure_tcpip_known_address(struct sc_server *server,
static bool
sc_server_configure_tcpip_unknown_address(struct sc_server *server,
const char *serial) {
bool is_already_tcpip =
sc_adb_device_get_type(serial) == SC_ADB_DEVICE_TYPE_TCPIP;
bool is_already_tcpip = sc_adb_is_serial_tcpip(serial);
if (is_already_tcpip) {
// Nothing to do
LOGI("Device already connected via TCP/IP: %s", serial);
@@ -708,15 +707,7 @@ run_server(void *data) {
} else if (params->select_tcpip) {
selector.type = SC_ADB_DEVICE_SELECT_TCPIP;
} else {
// No explicit selection, check $ANDROID_SERIAL
const char *env_serial = getenv("ANDROID_SERIAL");
if (env_serial) {
LOGI("Using ANDROID_SERIAL: %s", env_serial);
selector.type = SC_ADB_DEVICE_SELECT_SERIAL;
selector.serial = env_serial;
} else {
selector.type = SC_ADB_DEVICE_SELECT_ALL;
}
selector.type = SC_ADB_DEVICE_SELECT_ALL;
}
struct sc_adb_device device;
ok = sc_adb_select_device(&server->intr, &selector, 0, &device);