-
-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathsession_video.c
More file actions
268 lines (244 loc) · 10.3 KB
/
Copy pathsession_video.c
File metadata and controls
268 lines (244 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include "session_video.h"
#include <stddef.h>
#include "stream/session.h"
#include "sps_parser.h"
#include "ui/streaming/streaming.controller.h"
#include "util/bus.h"
#include "logging.h"
#include "ss4s.h"
#include "stream/connection/session_connection.h"
#include "stream/session_priv.h"
#include "app.h"
#include <SDL.h>
#include <assert.h>
// Starting capacity for the decode-unit reassembly buffer. Grows on
// demand to accommodate larger frames (e.g. 4K IDR frames at high
// bitrate routinely exceed 2 MB), capped to keep a malformed stream
// from exhausting memory.
#define DECODER_BUFFER_INITIAL_SIZE (2 * 1024 * 1024)
#define DECODER_BUFFER_MAX_SIZE (32 * 1024 * 1024)
static session_t *session = NULL;
static SS4S_Player *player = NULL;
static unsigned char *buffer = NULL;
static size_t buffer_size = 0;
static int lastFrameNumber;
// Set when SS4S_PlayerVideoFeed returns NOT_READY (decoder is in an
// exclusive op like HDR toggle or resolution change). Consumed on the
// next successful Feed: we ask Limelight for one IDR so the decoder
// can resync from a known-good keyframe instead of decoding the next
// P-frame against a discontinuity.
static bool need_idr_on_resume = false;
static struct VIDEO_STATS vdec_temp_stats;
static int vdec_stream_format = 0;
VIDEO_STATS vdec_summary_stats;
VIDEO_INFO vdec_stream_info;
static int vdec_delegate_setup(int videoFormat, int width, int height, int redrawRate, void *context, int drFlags);
static void vdec_delegate_cleanup();
static int vdec_delegate_submit(PDECODE_UNIT decodeUnit);
static void vdec_stat_submit(const struct VIDEO_STATS *src, unsigned long now);
static void stream_info_parse_size(PDECODE_UNIT decodeUnit, struct VIDEO_INFO *info);
DECODER_RENDERER_CALLBACKS ss4s_dec_callbacks = {
.setup = vdec_delegate_setup,
.cleanup = vdec_delegate_cleanup,
.submitDecodeUnit = vdec_delegate_submit,
.capabilities = CAPABILITY_DIRECT_SUBMIT,
};
static const char *video_format_name(int videoFormat) {
switch (videoFormat) {
case VIDEO_FORMAT_H264:
return "H264";
case VIDEO_FORMAT_H265:
return "H265";
case VIDEO_FORMAT_H265_MAIN10:
return "H265 10bit";
case VIDEO_FORMAT_AV1_MAIN8:
return "AV1 8bit";
case VIDEO_FORMAT_AV1_MAIN10:
return "AV1 10bit";
default:
return "Unknown";
}
}
int vdec_delegate_setup(int videoFormat, int width, int height, int redrawRate, void *context, int drFlags) {
(void) redrawRate;
(void) drFlags;
session = context;
player = session->player;
buffer_size = DECODER_BUFFER_INITIAL_SIZE;
buffer = malloc(buffer_size);
memset(&vdec_temp_stats, 0, sizeof(vdec_temp_stats));
memset(&vdec_stream_info, 0, sizeof(vdec_stream_info));
vdec_stream_format = videoFormat;
vdec_stream_info.format = video_format_name(videoFormat);
lastFrameNumber = 0;
need_idr_on_resume = false;
SS4S_VideoInfo info = {
.width = width,
.height = height,
.frameRateNumerator = redrawRate,
.frameRateDenominator = 1,
};
switch (videoFormat) {
case VIDEO_FORMAT_H264:
info.codec = SS4S_VIDEO_H264;
break;
case VIDEO_FORMAT_H265:
case VIDEO_FORMAT_H265_MAIN10:
info.codec = SS4S_VIDEO_H265;
break;
case VIDEO_FORMAT_AV1_MAIN8:
case VIDEO_FORMAT_AV1_MAIN10:
info.codec = SS4S_VIDEO_AV1;
break;
default: {
commons_log_error("Session", "Unsupported codec %s", vdec_stream_info.format);
return CALLBACKS_SESSION_ERROR_VDEC_UNSUPPORTED;
}
}
app_t *app = session->app;
if (app->ss4s.video_cap.transform & SS4S_VIDEO_CAP_TRANSFORM_UI_EXCLUSIVE) {
app_bus_post_sync(app, (bus_actionfunc) app_ui_close, &app->ui);
}
switch (SS4S_PlayerVideoOpen(player, &info)) {
case SS4S_VIDEO_OPEN_OK: {
return 0;
}
case SS4S_VIDEO_OPEN_UNSUPPORTED_CODEC:
return CALLBACKS_SESSION_ERROR_VDEC_UNSUPPORTED;
default:
return CALLBACKS_SESSION_ERROR_VDEC_ERROR;
}
}
void vdec_delegate_cleanup() {
assert(player != NULL);
free(buffer);
buffer = NULL;
buffer_size = 0;
SS4S_PlayerVideoClose(player);
session = NULL;
}
int vdec_delegate_submit(PDECODE_UNIT decodeUnit) {
if ((size_t) decodeUnit->fullLength > buffer_size) {
if ((size_t) decodeUnit->fullLength > DECODER_BUFFER_MAX_SIZE) {
commons_log_error("Session", "Decode unit %d bytes exceeds %zu byte cap, dropping",
decodeUnit->fullLength, (size_t) DECODER_BUFFER_MAX_SIZE);
return DR_NEED_IDR;
}
size_t new_size = buffer_size > 0 ? buffer_size : DECODER_BUFFER_INITIAL_SIZE;
while (new_size < (size_t) decodeUnit->fullLength) {
new_size *= 2;
}
unsigned char *new_buffer = realloc(buffer, new_size);
if (new_buffer == NULL) {
commons_log_error("Session", "Failed to grow decode buffer to %zu bytes", new_size);
return DR_NEED_IDR;
}
commons_log_info("Session", "Grew decode buffer %zu -> %zu bytes (frame needed %d)",
buffer_size, new_size, decodeUnit->fullLength);
buffer = new_buffer;
buffer_size = new_size;
}
unsigned long ticksms = SDL_GetTicks();
if (lastFrameNumber <= 0) {
vdec_temp_stats.measurementStartTimestamp = ticksms;
lastFrameNumber = decodeUnit->frameNumber;
} else {
// Any frame number greater than m_LastFrameNumber + 1 represents a dropped frame
vdec_temp_stats.networkDroppedFrames += decodeUnit->frameNumber - (lastFrameNumber + 1);
vdec_temp_stats.totalFrames += decodeUnit->frameNumber - (lastFrameNumber + 1);
lastFrameNumber = decodeUnit->frameNumber;
}
// Flip stats windows roughly every second
if (ticksms - vdec_temp_stats.measurementStartTimestamp > 1000) {
vdec_stat_submit(&vdec_temp_stats, ticksms);
// Move this window into the last window slot and clear it for next window
memset(&vdec_temp_stats, 0, sizeof(vdec_temp_stats));
vdec_temp_stats.measurementStartTimestamp = ticksms;
}
vdec_temp_stats.receivedFrames++;
vdec_temp_stats.totalFrames++;
vdec_temp_stats.totalCaptureLatency += decodeUnit->frameHostProcessingLatency;
vdec_temp_stats.totalReassemblyTime += decodeUnit->enqueueTimeMs - decodeUnit->receiveTimeMs;
vdec_stream_info.has_host_latency |= decodeUnit->frameHostProcessingLatency > 0;
size_t length = 0;
for (PLENTRY entry = decodeUnit->bufferList; entry != NULL; entry = entry->next) {
memcpy(buffer + length, entry->data, entry->length);
length += entry->length;
}
SS4S_VideoFeedFlags flags = SS4S_VIDEO_FEED_DATA_FRAME_START | SS4S_VIDEO_FEED_DATA_FRAME_END;
if (decodeUnit->frameType == FRAME_TYPE_IDR) {
flags |= SS4S_VIDEO_FEED_DATA_KEYFRAME;
}
SS4S_VideoFeedResult result = SS4S_PlayerVideoFeed(player, buffer, length, flags);
if (result == SS4S_VIDEO_FEED_OK) {
if (vdec_stream_info.width == 0 || vdec_stream_info.height == 0) {
stream_info_parse_size(decodeUnit, &vdec_stream_info);
}
vdec_temp_stats.totalSubmitTime += LiGetMillis() - decodeUnit->enqueueTimeMs;
vdec_temp_stats.submittedFrames++;
if (need_idr_on_resume) {
// We dropped at least one frame while the decoder was in a
// brief exclusive op (HDR toggle, SizeChanged). Ask for one
// IDR so the next frame the decoder sees is a known-good
// keyframe instead of a P-frame referencing missing data.
need_idr_on_resume = false;
return DR_NEED_IDR;
}
return DR_OK;
} else if (result == SS4S_VIDEO_FEED_REQUEST_KEYFRAME) {
return DR_NEED_IDR;
} else if (result == SS4S_VIDEO_FEED_NOT_READY) {
// Transient: the ss4s FeedGuard is in an exclusive op
// (SetHDRInfo / SizeChanged on a driver that does Unload+Load
// internally). Drop the frame silently; set the flag so the
// next successful Feed requests an IDR. This avoids tearing
// down the stream on a routine HDR toggle, and avoids spamming
// NEED_IDR on every dropped frame for the duration of the
// toggle.
need_idr_on_resume = true;
return DR_OK;
} else {
commons_log_error("Session", "Video feed error %d", result);
session_interrupt(session, false, STREAMING_INTERRUPT_DECODER);
return DR_OK;
}
}
void vdec_stat_submit(const struct VIDEO_STATS *src, unsigned long now) {
struct VIDEO_STATS *dst = &vdec_summary_stats;
memcpy(dst, src, sizeof(struct VIDEO_STATS));
unsigned long delta = now - dst->measurementStartTimestamp;
if (delta <= 0) { return; }
dst->totalFps = (float) dst->totalFrames / ((float) delta / 1000);
dst->receivedFps = (float) dst->receivedFrames / ((float) delta / 1000);
dst->decodedFps = (float) dst->submittedFrames / ((float) delta / 1000);
LiGetEstimatedRttInfo(&dst->rtt, &dst->rttVariance);
if (!streaming_stats_shown()) {
return;
}
int latencyUs = 0;
if (SS4S_PlayerGetVideoLatency(player, 0, &latencyUs)) {
dst->avgDecoderLatency = (float) latencyUs / 1000.0f;
vdec_stream_info.has_decoder_latency = true;
} else {
dst->avgDecoderLatency = 0;
}
app_bus_post(session->app, (bus_actionfunc) streaming_refresh_stats, NULL);
}
void stream_info_parse_size(PDECODE_UNIT decodeUnit, struct VIDEO_INFO *info) {
if (decodeUnit->frameType != FRAME_TYPE_IDR) { return; }
for (PLENTRY entry = decodeUnit->bufferList; entry != NULL; entry = entry->next) {
if (entry->bufferType != BUFFER_TYPE_SPS) { continue; }
sps_dimension_t dimension;
if (vdec_stream_format & VIDEO_FORMAT_MASK_H264) {
sps_parse_dimension_h264((const unsigned char *) &entry->data[4], &dimension);
} else if (vdec_stream_format & VIDEO_FORMAT_MASK_H265) {
sps_parse_dimension_hevc((const unsigned char *) &entry->data[4], &dimension);
} else {
info->width = info->height = -1;
return;
}
info->width = dimension.width;
info->height = dimension.height;
return;
}
}