2017-12-12 22:12:07 +08:00
|
|
|
#include "decoder.h"
|
|
|
|
|
|
|
|
#include <libavformat/avformat.h>
|
2018-10-11 13:12:36 +08:00
|
|
|
#include <libavutil/time.h>
|
Replace SDL_net by custom implementation
SDL_net is not very suitable for scrcpy.
For example, SDLNet_TCP_Accept() is non-blocking, so we have to wrap it
by calling many SDL_Net-specific functions to make it blocking.
But above all, SDLNet_TCP_Open() is a server socket only when no IP is
provided; otherwise, it's a client socket. Therefore, it is not possible
to create a server socket bound to localhost, so it accepts connections
from anywhere.
This is a problem for scrcpy, because on start, the application listens
for nearly 1 second until it accepts the first connection, supposedly
from the device. If someone on the local network manages to connect to
the server socket first, then they can stream arbitrary H.264 video.
This may be troublesome, for example during a public presentation ;-)
Provide our own simplified API (net.h) instead, implemented for the
different platforms.
2018-02-16 05:59:21 +08:00
|
|
|
#include <SDL2/SDL_events.h>
|
2017-12-12 22:12:07 +08:00
|
|
|
#include <SDL2/SDL_mutex.h>
|
|
|
|
#include <SDL2/SDL_thread.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2018-02-07 19:25:52 +08:00
|
|
|
#include "config.h"
|
2019-09-30 04:36:56 +08:00
|
|
|
#include "compat.h"
|
2017-12-12 22:12:07 +08:00
|
|
|
#include "events.h"
|
2018-11-09 19:21:17 +08:00
|
|
|
#include "recorder.h"
|
2019-03-02 22:16:55 +08:00
|
|
|
#include "video_buffer.h"
|
2019-11-24 18:53:00 +08:00
|
|
|
#include "util/buffer_util.h"
|
|
|
|
#include "util/log.h"
|
2017-12-12 22:12:07 +08:00
|
|
|
|
|
|
|
// set the decoded frame as ready for rendering, and notify
|
2019-03-03 03:09:56 +08:00
|
|
|
static void
|
|
|
|
push_frame(struct decoder *decoder) {
|
2019-03-03 07:26:48 +08:00
|
|
|
bool previous_frame_skipped;
|
|
|
|
video_buffer_offer_decoded_frame(decoder->video_buffer,
|
|
|
|
&previous_frame_skipped);
|
|
|
|
if (previous_frame_skipped) {
|
2018-02-09 02:23:24 +08:00
|
|
|
// the previous EVENT_NEW_FRAME will consume this frame
|
|
|
|
return;
|
2018-02-07 19:25:52 +08:00
|
|
|
}
|
2017-12-12 22:12:07 +08:00
|
|
|
static SDL_Event new_frame_event = {
|
|
|
|
.type = EVENT_NEW_FRAME,
|
|
|
|
};
|
|
|
|
SDL_PushEvent(&new_frame_event);
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
|
|
|
decoder_init(struct decoder *decoder, struct video_buffer *vb) {
|
2019-03-02 23:43:43 +08:00
|
|
|
decoder->video_buffer = vb;
|
2018-02-16 05:56:32 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
bool
|
2019-03-03 18:59:31 +08:00
|
|
|
decoder_open(struct decoder *decoder, const AVCodec *codec) {
|
2019-03-02 23:43:43 +08:00
|
|
|
decoder->codec_ctx = avcodec_alloc_context3(codec);
|
|
|
|
if (!decoder->codec_ctx) {
|
2018-02-13 17:10:18 +08:00
|
|
|
LOGC("Could not allocate decoder context");
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2017-12-12 22:12:07 +08:00
|
|
|
}
|
|
|
|
|
2019-03-02 23:43:43 +08:00
|
|
|
if (avcodec_open2(decoder->codec_ctx, codec, NULL) < 0) {
|
|
|
|
LOGE("Could not open codec");
|
|
|
|
avcodec_free_context(&decoder->codec_ctx);
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2017-12-12 22:12:07 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
return true;
|
2019-03-02 23:43:43 +08:00
|
|
|
}
|
2018-10-11 13:12:36 +08:00
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
|
|
|
decoder_close(struct decoder *decoder) {
|
2019-03-02 23:43:43 +08:00
|
|
|
avcodec_close(decoder->codec_ctx);
|
|
|
|
avcodec_free_context(&decoder->codec_ctx);
|
|
|
|
}
|
2017-12-12 22:12:07 +08:00
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
bool
|
2019-03-03 18:59:31 +08:00
|
|
|
decoder_push(struct decoder *decoder, const AVPacket *packet) {
|
2017-12-12 22:12:07 +08:00
|
|
|
// the new decoding/encoding API has been introduced by:
|
|
|
|
// <http://git.videolan.org/?p=ffmpeg.git;a=commitdiff;h=7fc329e2dd6226dfecaa4a1d7adf353bf2773726>
|
2019-02-16 22:04:32 +08:00
|
|
|
#ifdef SCRCPY_LAVF_HAS_NEW_ENCODING_DECODING_API
|
2019-03-02 23:43:43 +08:00
|
|
|
int ret;
|
|
|
|
if ((ret = avcodec_send_packet(decoder->codec_ctx, packet)) < 0) {
|
|
|
|
LOGE("Could not send video packet: %d", ret);
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2017-12-12 22:12:07 +08:00
|
|
|
}
|
2019-03-02 23:43:43 +08:00
|
|
|
ret = avcodec_receive_frame(decoder->codec_ctx,
|
|
|
|
decoder->video_buffer->decoding_frame);
|
|
|
|
if (!ret) {
|
|
|
|
// a frame was received
|
|
|
|
push_frame(decoder);
|
|
|
|
} else if (ret != AVERROR(EAGAIN)) {
|
|
|
|
LOGE("Could not receive video frame: %d", ret);
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2018-10-11 13:12:36 +08:00
|
|
|
}
|
2019-03-02 23:43:43 +08:00
|
|
|
#else
|
|
|
|
int got_picture;
|
|
|
|
int len = avcodec_decode_video2(decoder->codec_ctx,
|
|
|
|
decoder->video_buffer->decoding_frame,
|
|
|
|
&got_picture,
|
|
|
|
packet);
|
|
|
|
if (len < 0) {
|
|
|
|
LOGE("Could not decode video packet: %d", len);
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2017-12-12 22:12:07 +08:00
|
|
|
}
|
2019-03-02 23:43:43 +08:00
|
|
|
if (got_picture) {
|
|
|
|
push_frame(decoder);
|
|
|
|
}
|
|
|
|
#endif
|
2019-03-03 06:52:22 +08:00
|
|
|
return true;
|
2017-12-12 22:12:07 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
|
|
|
decoder_interrupt(struct decoder *decoder) {
|
2019-03-03 00:01:52 +08:00
|
|
|
video_buffer_interrupt(decoder->video_buffer);
|
2018-02-09 15:42:39 +08:00
|
|
|
}
|