diff --git a/res/index.html b/res/index.html index dd33458b..2d17bb94 100644 --- a/res/index.html +++ b/res/index.html @@ -177,6 +177,13 @@ '=' + encodeURIComponent(elems[i].value) + '&'; } dest = dest.slice(0, -1); + // An applied setting is also written to the config file, so it is still + // there after a restart. Several applies in a row make one save. + req.onreadystatechange = function() { + if (this.readyState != 4 || this.status != 200) return; + clearTimeout(window.saveTimer); + window.saveTimer = setTimeout(saveConfig, 700); + }; req.open('GET', dest); req.send(); } @@ -295,34 +302,261 @@ apiApply('osd/' + i); } + // Each time setting is sent on its own: sending the stale clock value + // from the form along with another change would set the clock back. + function timeSend(q, save) { + var req = new XMLHttpRequest(); + req.onreadystatechange = function() { + if (this.readyState != 4 || this.status != 200) return; + apiRead('time'); + if (save) saveConfig(); + }; + req.open('GET', '/api/time?' + q); + req.send(); + } + function setFormatTime() { - var fmt = document.getElementById('time_fmt').value; - apiRead('time'); - document.getElementById('time_fmt').value = fmt; - apiApply('time'); + timeSend('fmt=' + encodeURIComponent(document.getElementById('time_fmt').value), true); + } + + function setTimezone() { + timeSend('tz=' + encodeURIComponent(document.getElementById('time_tz').value), false); } function setLocalTime() { - var ts = Math.floor(new Date().getTime() / 1000); - document.getElementById('time_ts').value = ts; - apiApply('time'); - apiRead('time'); + timeSend('ts=' + Math.floor(new Date().getTime() / 1000), false); + } + + function setManualTime() { + timeSend('ts=' + encodeURIComponent(document.getElementById('time_ts').value), false); } + // Live MP4 through Media Source Extensions: the page feeds the stream to the + // decoder itself, so it can jump to the newest frame instead of sitting + // seconds behind in the browser's progressive-download buffer. + // Talk-back: microphone -> 16 kHz mono PCM -> POST api/speak in small pieces + var talk = { on: false, ctx: null, stream: null, node: null, buf: [], timer: null }; + function talkSay(t) { document.getElementById('talkstate').textContent = t; } + function talkVolume(v) { + document.getElementById('talkvolval').textContent = v; + try { localStorage.setItem('talkvol', v); } catch (e) {} + clearTimeout(talk.volTimer); + talk.volTimer = setTimeout(function() { fetch('api/speak?volume=' + v).catch(function(){}); }, 150); + } + function talkVolumeInit() { + var v = 70; + try { v = parseInt(localStorage.getItem('talkvol')) || 70; } catch (e) {} + document.getElementById('talkvol').value = v; + talkVolume(v); + } + // Fill the microphone list (names are only available once access was granted) + function talkMics() { + if (!navigator.mediaDevices || !navigator.mediaDevices.enumerateDevices) return; + navigator.mediaDevices.enumerateDevices().then(function(devs) { + var sel = document.getElementById('talkmic'), cur = sel.value, saved = ''; + try { saved = localStorage.getItem('talkmic') || ''; } catch (e) {} + sel.innerHTML = ''; + devs.forEach(function(d) { + if (d.kind != 'audioinput' || d.deviceId == 'default' || d.deviceId == 'communications') return; + var o = document.createElement('option'); + o.value = d.deviceId; o.textContent = d.label || 'Microphone ' + sel.length; + sel.appendChild(o); + }); + sel.value = cur || saved; + if (sel.selectedIndex < 0) sel.value = ''; + }).catch(function(){}); + } + function talkSend() { + if (!talk.buf.length) return; + var n = 0, i, o = 0; + for (i = 0; i < talk.buf.length; i++) n += talk.buf[i].length; + var pcm = new Int16Array(n); + for (i = 0; i < talk.buf.length; i++) { pcm.set(talk.buf[i], o); o += talk.buf[i].length; } + talk.buf = []; + fetch('api/speak', { method: 'POST', headers: { 'Content-Type': 'application/octet-stream' }, body: pcm.buffer }) + .then(function(r) { if (!r.ok) talkSay('camera refused the audio'); }) + .catch(function() { talkSay('no answer from camera'); }); + } + function liveTalk(on) { + if (on == talk.on) return; + talk.on = on; + if (!on) { + if (talk.timer) { clearInterval(talk.timer); talk.timer = null; } + talkSend(); + if (talk.node) { talk.node.disconnect(); talk.node = null; } + if (talk.stream) { talk.stream.getTracks().forEach(function(t) { t.stop(); }); talk.stream = null; } + if (talk.ctx) { talk.ctx.close(); talk.ctx = null; } + talkSay(''); + return; + } + if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) { + talk.on = false; + talkSay('Microphone blocked: this page is not HTTPS. In Chrome open chrome://flags/#unsafely-treat-insecure-origin-as-secure, add ' + location.origin + ', enable it and relaunch.'); + return; + } + talkSay('asking for the microphone...'); + var micId = document.getElementById('talkmic').value, want = { echoCancellation: true, noiseSuppression: true }; + if (micId) want.deviceId = { exact: micId }; + navigator.mediaDevices.getUserMedia({ audio: want }).then(function(stream) { + talkMics(); + if (!talk.on) { stream.getTracks().forEach(function(t) { t.stop(); }); return; } + talk.stream = stream; + talk.ctx = new (window.AudioContext || window.webkitAudioContext)(); + var src = talk.ctx.createMediaStreamSource(stream), step = talk.ctx.sampleRate / 16000, pos = 0; + talk.node = talk.ctx.createScriptProcessor(4096, 1, 1); + talk.node.onaudioprocess = function(e) { + var inp = e.inputBuffer.getChannelData(0), out = [], acc = 0, cnt = 0; + // Average the samples that fall into each 16 kHz slot + for (var i = 0; i < inp.length; i++) { + acc += inp[i]; cnt++; pos += 1; + if (pos >= step) { pos -= step; var s = acc / cnt; acc = 0; cnt = 0; + out.push(Math.max(-32768, Math.min(32767, Math.round(s * 32767)))); } + } + talk.buf.push(Int16Array.from(out)); + var pk = 0; for (var j = 0; j < out.length; j++) pk = Math.max(pk, Math.abs(out[j])); + talkSay('talking... microphone level ' + Math.round(pk / 327.67) + '%'); + }; + src.connect(talk.node); + talk.node.connect(talk.ctx.destination); + talk.timer = setInterval(talkSend, 200); + talkSay('talking...'); + }).catch(function(e) { talk.on = false; talkSay('microphone not available: ' + e.name); }); + } + function liveLamp(q) { + fetch('api/lamp?' + q).then(function(r) { return r.json(); }).then(function(j) { + document.getElementById('lampstate').textContent = 'Lamp is ' + (j.on ? 'ON' : 'OFF'); + }).catch(function() { document.getElementById('lampstate').textContent = 'no answer from camera'; }); + } + // Add ?debug to the page address to see the player state under the picture. + var liveDebug = location.search.indexOf('debug') > -1; + function liveStop() { + if (window.liveTimer) { clearInterval(window.liveTimer); window.liveTimer = null; } + if (window.liveAbort) { try { window.liveAbort.abort(); } catch (e) {} window.liveAbort = null; } + } + function liveCodecs(u8) { + function find(tag) { + for (var i = 0; i + 4 <= u8.length; i++) + if (u8[i] == tag.charCodeAt(0) && u8[i+1] == tag.charCodeAt(1) && + u8[i+2] == tag.charCodeAt(2) && u8[i+3] == tag.charCodeAt(3)) return i; + return -1; + } + function hex(n) { return (n < 16 ? '0' : '') + n.toString(16).toUpperCase(); } + var c = [], i = find('avcC'); + if (i >= 0) c.push('avc1.' + hex(u8[i+5]) + hex(u8[i+6]) + hex(u8[i+7])); + else if (find('hvcC') >= 0) c.push('hvc1.1.6.L120.90'); + if (find('Opus') >= 0) c.push('opus'); + else if (find('mp4a') >= 0) c.push('mp4a.69'); + return 'video/mp4; codecs="' + c.join(', ') + '"'; + } + function liveMse(id, src) { + var frame = document.getElementById(id); + frame.innerHTML = '' + + '
'; + var v = document.getElementById('player'), ms = new MediaSource(), sb = null, queue = [], dead = false; + var ctl = new AbortController(), last = -1, stuck = 0, note = ''; + window.liveAbort = ctl; + v.src = URL.createObjectURL(ms); + function restart(why) { + if (dead) return; dead = true; + setTimeout(function() { if (document.getElementById('player') === v) setSource(id, src); }, 500); + } + function pump() { + if (!sb || sb.updating || !queue.length || dead) return; + try { sb.appendBuffer(queue.shift()); } catch (e) { restart('append ' + e.name); } + } + ms.addEventListener('sourceopen', function() { + fetch(src, { signal: ctl.signal }).then(function(r) { + var rd = r.body.getReader(); + (function next() { + rd.read().then(function(x) { + if (x.done) { restart('stream ended'); return; } + if (!sb) { + var type = liveCodecs(x.value); + if (!MediaSource.isTypeSupported(type)) { note = 'unsupported ' + type; dead = true; return; } + sb = ms.addSourceBuffer(type); + sb.mode = 'segments'; + sb.addEventListener('updateend', pump); + sb.addEventListener('error', function() { restart('buffer error'); }); + } + queue.push(x.value); + pump(); + next(); + }).catch(function(e) { if (e.name != 'AbortError') restart('read ' + e.name); }); + })(); + }).catch(function(e) { if (e.name != 'AbortError') restart('fetch ' + e.name); }); + }); + window.liveTimer = setInterval(function() { + if (dead || document.getElementById('player') !== v) return; + var b = v.buffered, lag = -1; + if (b.length) { + var start = b.start(b.length - 1), end = b.end(b.length - 1); + lag = end - v.currentTime; + // Jump to the newest data when behind (or before the first keyframe) + if (lag > 0.7 || v.currentTime < start) v.currentTime = Math.max(start, end - 0.25); + if (v.paused) { var p = v.play(); if (p && p.catch) p.catch(function(){}); } + // Forget what was already shown + if (sb && !sb.updating && !queue.length && v.currentTime - b.start(0) > 20) + try { sb.remove(0, v.currentTime - 5); } catch (e) {} + } + if (v.currentTime == last) stuck++; else stuck = 0; + last = v.currentTime; + var st = 'mse t=' + v.currentTime.toFixed(1) + ' lag=' + lag.toFixed(2) + ' rs=' + v.readyState + + ' q=' + queue.length + ' ' + v.videoWidth + 'x' + v.videoHeight + + ' drop=' + (v.getVideoPlaybackQuality ? v.getVideoPlaybackQuality().droppedVideoFrames : '?') + + ' err=' + (v.error ? v.error.code + ':' + v.error.message : 'none') + ' ' + note; + var d = document.getElementById('playerdbg'); if (d) d.textContent = st; + if (v.error) restart('error ' + v.error.message); + else if (stuck >= 16) restart('stuck'); + }, 250); + } function setSource(id, src) { var frame = document.getElementById(id), ext = '.mp4'; - if (frame.firstChild) frame.firstChild.src = "#"; - if (src.indexOf(ext, src.length - ext.length) > -1) - frame.innerHTML = ''; + liveStop(); + if (frame.firstChild && frame.firstChild.tagName == 'IMG') frame.firstChild.src = "#"; + if (src.indexOf(ext, src.length - ext.length) > -1) { + if (window.MediaSource && window.fetch && window.AbortController && window.ReadableStream) + liveMse(id, src); + else + frame.innerHTML = ''; + } else if (src) frame.innerHTML = ''; else while (frame.firstChild) frame.removeChild(frame.firstChild); } + function clipList() { + var box = document.getElementById('record_clips'), req = new XMLHttpRequest(); + box.textContent = 'Loading...'; + req.onreadystatechange = function() { + if (this.readyState != 4) return; + if (this.status != 200) { box.textContent = 'Could not read the clip list.'; return; } + var files = JSON.parse(req.responseText).files || []; + if (!files.length) { box.textContent = 'No recordings on the card.'; return; } + var tbl = document.createElement('table'); + tbl.className = 'w-100'; + files.forEach(function(f) { + var row = tbl.insertRow(), url = '/recordings/' + encodeURIComponent(f.name); + // recording_20261010-212517.mp4 -> 2026-10-10 21:25:17 + var m = f.name.match(/_(\d{4})(\d\d)(\d\d)-(\d\d)(\d\d)(\d\d)/); + row.insertCell().textContent = m ? m[1] + '-' + m[2] + '-' + m[3] + ' ' + m[4] + ':' + m[5] + ':' + m[6] : f.name; + row.insertCell().textContent = (f.size / 1048576).toFixed(1) + ' MB'; + var play = document.createElement('a'), save = document.createElement('a'); + play.href = url; play.target = '_blank'; play.textContent = 'Play'; + save.href = url + '?download'; save.textContent = 'Download'; + row.insertCell().appendChild(play); + row.insertCell().appendChild(save); + }); + box.textContent = ''; + box.appendChild(tbl); + }; + req.open('GET', '/api/recordings'); + req.send(); + } + function toggleRecord(btn) { if (!btn) return; @@ -332,6 +566,7 @@ var dec = JSON.parse(req.responseText); for (var key in dec) { if (!dec.hasOwnProperty(key)) continue; + if (key == 'storage') document.getElementById('record_storage').value = dec[key]; if (key != 'recording') continue; if (dec[key] == true) { btn.innerHTML = 'Stop'; @@ -374,6 +609,7 @@ apiRead('onvif'); apiRead('record'); apiRead('rtsp'); + apiRead('sub'); toggleRecord(document.getElementById('record_state')); apiRead('time'); @@ -386,7 +622,7 @@ } - +

Divinus

Live

+

+ Lamp on + Lamp off + +

+

+ Hold to talk + + + 70 + + +

@@ -471,6 +720,10 @@

Media

Recordings + + + + + + + +
+
+ Show clips Apply
@@ -660,6 +919,7 @@

Media

+ Not available while the sub-stream is on. Apply @@ -704,7 +964,12 @@

OSD

- + + @@ -738,6 +1003,46 @@

OSD

Network

+
+
+ Sub-stream +
+ + + + + + + + + + + + + + + + + + + + + + + + +
+ H.264 over RTSP only, at the main stream's frame rate. Changing a value restarts the streamer (about 20 seconds). + Apply + +
RTSP @@ -866,6 +1171,13 @@

System

+ + + + @@ -902,9 +1214,39 @@

System

Sync - Apply + Apply + + + + Set + Refresh
diff --git a/src/app_config.c b/src/app_config.c index 9d163672..8e20bb58 100644 --- a/src/app_config.c +++ b/src/app_config.c @@ -101,6 +101,8 @@ int app_config_save(void) { fprintf(file, " ir_cut_pin1: %d\n", app_config.ir_cut_pin1); fprintf(file, " ir_cut_pin2: %d\n", app_config.ir_cut_pin2); fprintf(file, " ir_led_pin: %d\n", app_config.ir_led_pin); + if (app_config.white_led_pin != 999) + fprintf(file, " white_led_pin: %d\n", app_config.white_led_pin); fprintf(file, " pin_switch_delay_us: %d\n", app_config.pin_switch_delay_us); fprintf(file, " adc_device: %s\n", app_config.adc_device); { @@ -114,6 +116,8 @@ int app_config_save(void) { fprintf(file, " lamp: %s\n", lamp); } fprintf(file, " adc_threshold: %d\n", app_config.adc_threshold); + fprintf(file, " exposure_night: %u\n", app_config.exposure_night); + fprintf(file, " exposure_day: %u\n", app_config.exposure_day); fprintf(file, "isp:\n"); fprintf(file, " mirror: %s\n", app_config.mirror ? "true" : "false"); @@ -144,6 +148,7 @@ int app_config_save(void) { fprintf(file, " filename: %s\n", app_config.record_filename); fprintf(file, " segment_duration: %d\n", app_config.record_segment_duration); fprintf(file, " segment_size: %d\n", app_config.record_segment_size); + fprintf(file, " reserve: %d\n", app_config.record_reserve); fprintf(file, "stream:\n"); fprintf(file, " enable: %s\n", app_config.stream_enable ? "true" : "false"); @@ -164,6 +169,7 @@ int app_config_save(void) { fprintf(file, "mp4:\n"); fprintf(file, " enable: %s\n", app_config.mp4_enable ? "true" : "false"); fprintf(file, " codec: %s\n", app_config.mp4_codecH265 ? "H.265" : "H.264"); + fprintf(file, " audio: %s\n", app_config.mp4_audio ? "true" : "false"); fprintf(file, " mode: %d\n", app_config.mp4_mode); fprintf(file, " width: %d\n", app_config.mp4_width); fprintf(file, " height: %d\n", app_config.mp4_height); @@ -172,6 +178,14 @@ int app_config_save(void) { fprintf(file, " profile: %d\n", app_config.mp4_profile); fprintf(file, " bitrate: %d\n", app_config.mp4_bitrate); + fprintf(file, "sub:\n"); + fprintf(file, " enable: %s\n", app_config.sub_enable ? "true" : "false"); + fprintf(file, " width: %d\n", app_config.sub_width); + fprintf(file, " height: %d\n", app_config.sub_height); + fprintf(file, " gop: %d\n", app_config.sub_gop); + fprintf(file, " bitrate: %d\n", app_config.sub_bitrate); + fprintf(file, " rtsp_port: %d\n", app_config.sub_rtsp_port); + fprintf(file, "osd:\n"); fprintf(file, " enable: %s\n", app_config.osd_enable ? "true" : "false"); for (char i = 0; i < MAX_OSD; i++) { @@ -185,6 +199,7 @@ int app_config_save(void) { fprintf(file, " reg%d_text: %s\n", i, osds[i].text); fprintf(file, " reg%d_font: %s\n", i, osds[i].font); fprintf(file, " reg%d_opal: %d\n", i, osds[i].opal); + fprintf(file, " reg%d_anchor: %s\n", i, osd_anchors[(unsigned char)osds[i].anchor % 9]); fprintf(file, " reg%d_posx: %d\n", i, osds[i].posx); fprintf(file, " reg%d_posy: %d\n", i, osds[i].posy); fprintf(file, " reg%d_size: %.1f\n", i, osds[i].size); @@ -256,8 +271,9 @@ enum ConfigError app_config_parse(void) { app_config.record_continuous = false; app_config.record_filename[0] = '\0'; strcpy(app_config.record_path, "/mnt/sdcard/recordings"); - app_config.record_segment_duration = 0; + app_config.record_segment_duration = 300; app_config.record_segment_size = 0; + app_config.record_reserve = 512; app_config.stream_enable = false; app_config.stream_udp_srcport = 0; @@ -269,6 +285,13 @@ enum ConfigError app_config_parse(void) { app_config.audio_gain = 0; app_config.jpeg_enable = false; app_config.mp4_enable = false; + app_config.mp4_audio = true; + app_config.sub_enable = false; + app_config.sub_width = 640; + app_config.sub_height = 360; + app_config.sub_gop = 30; + app_config.sub_bitrate = 512; + app_config.sub_rtsp_port = 555; app_config.mjpeg_enable = false; app_config.mjpeg_fps = 15; @@ -286,11 +309,14 @@ enum ConfigError app_config_parse(void) { app_config.ir_cut_pin1 = 999; app_config.ir_cut_pin2 = 999; app_config.ir_led_pin = 999; + app_config.white_led_pin = 999; app_config.pin_switch_delay_us = 250; app_config.check_interval_s = 10; app_config.adc_device[0] = 0; app_config.night_lamp = HAL_LIGHT_IR; app_config.adc_threshold = 128; + app_config.exposure_night = 0; + app_config.exposure_day = 0; struct IniConfig ini; memset(&ini, 0, sizeof(struct IniConfig)); @@ -354,7 +380,7 @@ enum ConfigError app_config_parse(void) { err = parse_bool(&ini, "night_mode", "enable", &app_config.night_mode_enable); - #define PIN_MAX 95 + #define PIN_MAX 127 /* Infinity3 has GPIOs up to 109 (IR-cut on 96/97) */ if (app_config.night_mode_enable) { { int pin; @@ -374,6 +400,9 @@ enum ConfigError app_config_parse(void) { parse_int( &ini, "night_mode", "ir_led_pin", 0, PIN_MAX, &app_config.ir_led_pin); + parse_int( + &ini, "night_mode", "white_led_pin", 0, PIN_MAX, + &app_config.white_led_pin); parse_int( &ini, "night_mode", "pin_switch_delay_us", 0, 1000, &app_config.pin_switch_delay_us); @@ -403,6 +432,12 @@ enum ConfigError app_config_parse(void) { parse_int( &ini, "night_mode", "adc_threshold", INT_MIN, INT_MAX, &app_config.adc_threshold); + parse_int( + &ini, "night_mode", "exposure_night", 0, 100000, + &app_config.exposure_night); + parse_int( + &ini, "night_mode", "exposure_day", 0, 100000, + &app_config.exposure_day); } err = parse_bool(&ini, "isp", "mirror", &app_config.mirror); @@ -415,6 +450,13 @@ enum ConfigError app_config_parse(void) { parse_bool(&ini, "mdns", "enable", &app_config.mdns_enable); + parse_bool(&ini, "sub", "enable", &app_config.sub_enable); + parse_int(&ini, "sub", "width", 160, INT_MAX, &app_config.sub_width); + parse_int(&ini, "sub", "height", 120, INT_MAX, &app_config.sub_height); + parse_int(&ini, "sub", "gop", 1, INT_MAX, &app_config.sub_gop); + parse_int(&ini, "sub", "bitrate", 32, INT_MAX, &app_config.sub_bitrate); + parse_int(&ini, "sub", "rtsp_port", 1, USHRT_MAX, &app_config.sub_rtsp_port); + parse_bool(&ini, "osd", "enable", &app_config.osd_enable); if (app_config.osd_enable) { for (char i = 0; i < MAX_OSD; i++) { @@ -430,6 +472,12 @@ enum ConfigError app_config_parse(void) { sprintf(param, "reg%d_opal", i); err = parse_int(&ini, "osd", param, 0, UCHAR_MAX, &val); if (err == CONFIG_OK) osds[i].opal = (unsigned char)val; + { + char anchor[16] = {0}; + sprintf(param, "reg%d_anchor", i); + if (parse_param_value(&ini, "osd", param, anchor) == CONFIG_OK) + osds[i].anchor = region_anchor_parse(anchor); + } sprintf(param, "reg%d_posx", i); err = parse_int(&ini, "osd", param, 0, SHRT_MAX, &val); if (err == CONFIG_OK) osds[i].posx = (short)val; @@ -467,6 +515,8 @@ enum ConfigError app_config_parse(void) { &app_config.record_segment_duration); parse_int(&ini, "record", "segment_size", 0, INT_MAX, &app_config.record_segment_size); + parse_int(&ini, "record", "reserve", 0, INT_MAX, + &app_config.record_reserve); parse_bool(&ini, "rtsp", "enable", &app_config.rtsp_enable); parse_int(&ini, "rtsp", "port", 0, USHRT_MAX, &app_config.rtsp_port); @@ -511,6 +561,8 @@ enum ConfigError app_config_parse(void) { parse_bool(&ini, "mp4", "enable", &app_config.mp4_enable); if (app_config.mp4_enable) { + /* MP3 from the built-in encoder is rejected by strict browser decoders */ + parse_bool(&ini, "mp4", "audio", &app_config.mp4_audio); { const char *possible_values[] = {"H.264", "H.265", "H264", "H265", "AVC", "HEVC"}; const int count = sizeof(possible_values) / sizeof(const char *); diff --git a/src/app_config.h b/src/app_config.h index 65056455..b85287bd 100644 --- a/src/app_config.h +++ b/src/app_config.h @@ -31,12 +31,15 @@ struct AppConfig { unsigned int ir_cut_pin1; unsigned int ir_cut_pin2; unsigned int ir_led_pin; + unsigned int white_led_pin; unsigned int ir_sensor_pin; unsigned int check_interval_s; unsigned int pin_switch_delay_us; char adc_device[128]; hal_light night_lamp; int adc_threshold; + unsigned int exposure_night; + unsigned int exposure_day; // [isp] bool mirror; @@ -70,6 +73,7 @@ struct AppConfig { char record_path[128]; int record_segment_duration; int record_segment_size; + int record_reserve; // [stream] bool stream_enable; @@ -85,6 +89,11 @@ struct AppConfig { // [mp4] bool mp4_enable; bool mp4_codecH265; + bool mp4_audio; + + // [sub] second, smaller H.264 stream on its own RTSP port + bool sub_enable; + unsigned int sub_width, sub_height, sub_gop, sub_bitrate, sub_rtsp_port; unsigned int mp4_mode; unsigned int mp4_fps; unsigned int mp4_gop; diff --git a/src/compat.c b/src/compat.c index ab2ebf3b..ee5bc434 100644 --- a/src/compat.c +++ b/src/compat.c @@ -35,18 +35,23 @@ int stat(const char *restrict path, struct stat *restrict buf) void __assert(void) {} void akuio_clean_invalidate_dcache(void) {} -#ifndef __GLIBC__ +// GLIBC_COMPAT: linked against OpenIPC's glibc-compat shim (infinity3 on +// musl), which, with musl, has working versions of these; an empty one here +// would be found first and break the vendor libraries that call them. +#if !defined(__GLIBC__) && !defined(GLIBC_COMPAT) void backtrace(void) {} void backtrace_symbols(void) {} #endif void __ctype_b(void) {} -#ifndef __GLIBC__ +#if !defined(__GLIBC__) && !defined(GLIBC_COMPAT) void __ctype_b_loc(void) {} void __ctype_tolower(void) {} #endif void _MI_PRINT_GetDebugLevel(void) {} +#ifndef GLIBC_COMPAT void __pthread_register_cancel(void) {} void __pthread_unregister_cancel(void) {} +#endif #ifndef __UCLIBC__ void _stdlib_mb_cur_max(void) {} #endif @@ -56,9 +61,14 @@ int __fgetc_unlocked(FILE *stream) { return fgetc(stream); } double __log_finite(double x) { return log(x); } #if defined(__arm__) || defined(__aarch64__) || defined(__mips__) +// With GLIBC_COMPAT (infinity3 on musl) the vendor libraries' 32-bit mmap +// calls go through the shim's mm32, which calls mmap with musl's 64-bit off_t, +// so musl's own mmap has to stay in place there. +#ifndef GLIBC_COMPAT void *mmap(void *start, size_t len, int prot, int flags, int fd, uint32_t off) { return (void*)syscall(SYS_mmap2, start, len, prot, flags, fd, off >> 12); } +#endif void *__mmap64(void *start, size_t len, int prot, int flags, int fd, off_t off) { return (void*)syscall(SYS_mmap2, start, len, prot, flags, fd, (long)(off >> 12)); diff --git a/src/fmt/moov.c b/src/fmt/moov.c index 350b38cc..ce3956fd 100644 --- a/src/fmt/moov.c +++ b/src/fmt/moov.c @@ -757,7 +757,8 @@ enum BufError write_mp4a(struct BitBuf *ptr, const struct MoovInfo *moov_info) { err = put_u32_be(ptr, 0); chk_err; - err = put_str4(ptr, "mp4a"); + char is_opus = (unsigned char)moov_info->audio_codec == ACODEC_ID_OPUS; + err = put_str4(ptr, is_opus ? "Opus" : "mp4a"); chk_err; err = put_skip(ptr, 6); chk_err; // 6 reserved @@ -771,11 +772,31 @@ enum BufError write_mp4a(struct BitBuf *ptr, const struct MoovInfo *moov_info) { chk_err; // 2 sample size err = put_skip(ptr, 4); chk_err; // 4 reserved - err = put_u16_be(ptr, moov_info->audio_samplerate); - chk_err; // 2 samplerate + err = put_u16_be(ptr, is_opus ? 48000 : moov_info->audio_samplerate); + chk_err; // 2 samplerate (Opus in ISOBMFF is always signalled as 48 kHz) err = put_skip(ptr, 2); chk_err; // 2 reserved - err = write_esds(ptr, moov_info); + if (is_opus) { + uint32_t start_dops = ptr->offset; + err = put_u32_be(ptr, 0); + chk_err; + err = put_str4(ptr, "dOps"); + chk_err; + err = put_u8(ptr, 0); + chk_err; // 1 version + err = put_u8(ptr, moov_info->audio_channels); + chk_err; // 1 output channel count + err = put_u16_be(ptr, 312); + chk_err; // 2 pre-skip (48 kHz samples) + err = put_u32_be(ptr, 48000); + chk_err; // 4 input sample rate (informational; Chrome wants it equal to the entry's rate) + err = put_u16_be(ptr, 0); + chk_err; // 2 output gain + err = put_u8(ptr, 0); + chk_err; // 1 channel mapping family + err = put_u32_be_to_offset(ptr, start_dops, ptr->offset - start_dops); + } else + err = write_esds(ptr, moov_info); chk_err; err = write_btrt(ptr, moov_info); chk_err; diff --git a/src/fmt/mp4.c b/src/fmt/mp4.c index 86cd6320..b71fc3fd 100644 --- a/src/fmt/mp4.c +++ b/src/fmt/mp4.c @@ -3,7 +3,7 @@ pthread_mutex_t mp4_mtx = PTHREAD_MUTEX_INITIALIZER; uint32_t default_sample_size = 40000; -uint32_t last_fragment_duration, timescale; +uint32_t last_video_duration, last_audio_duration, timescale; uint64_t last_vid_tstamp; uint32_t vid_duration; @@ -19,6 +19,8 @@ uint16_t buf_sps_len = 0; char buf_vps[128]; uint16_t buf_vps_len = 0; struct BitBuf buf_aud; +#define MP4_AUD_MAX 16 +static uint32_t aud_frm_size[MP4_AUD_MAX], aud_frm_samp[MP4_AUD_MAX], aud_frm_cnt; struct BitBuf buf_header; struct BitBuf buf_mdat; struct BitBuf buf_moof; @@ -57,6 +59,7 @@ enum BufError create_header(char is_h265) { moov_info.vps_length = buf_vps_len; buf_aud.offset = 0; + aud_frm_cnt = 0; buf_header.offset = 0; enum BufError err = write_header(&buf_header, &moov_info); chk_err return BUF_OK; @@ -109,43 +112,94 @@ void mp4_set_timestamp(uint64_t tstamp) { enum BufError mp4_set_slice(const char *nal_data, const uint32_t nal_len, char is_iframe) { - uint64_t aud_ticks = 0; enum BufError err; - struct SampleInfo samples_info[2]; + /* One MP4 sample per MPEG audio frame: decoders that validate packet + * lengths reject several frames glued into a single sample. */ + struct SampleInfo samples_info[1 + MP4_AUD_MAX]; + uint32_t aud_cnt = 0, aud_len = 0, aud_dur = 0; memset(samples_info, 0, sizeof(samples_info)); samples_info[0].size = nal_len + 4; // add size of sample samples_info[0].duration = vid_duration; samples_info[0].flags = is_iframe ? 0 : 65536; - samples_info[1].size = buf_aud.offset; - if (aud_bitrate > 0 && buf_aud.offset > 0) { - aud_ticks = ((uint64_t)(buf_aud.offset << 3) * timescale) / - (aud_bitrate * 1000); - samples_info[1].duration = (uint32_t)aud_ticks; - last_fragment_duration = MAX(samples_info[1].duration, samples_info[0].duration); - } else { - samples_info[1].duration = 0; - last_fragment_duration = samples_info[0].duration; + if (aud_frm_cnt > 0 && aud_samplerate > 0) { + /* Frames handed over one by one with their length (Opus) */ + for (; aud_cnt < aud_frm_cnt; aud_cnt++) { + samples_info[1 + aud_cnt].size = aud_frm_size[aud_cnt]; + samples_info[1 + aud_cnt].duration = + (uint32_t)((uint64_t)aud_frm_samp[aud_cnt] * timescale / aud_samplerate); + aud_dur += samples_info[1 + aud_cnt].duration; + aud_len += aud_frm_size[aud_cnt]; + } + } else if (aud_codec != 0x69 /* MP3 */ && aud_bitrate > 0 && buf_aud.offset > 0) { + samples_info[1].size = aud_len = buf_aud.offset; + samples_info[1].duration = aud_dur = (uint32_t)(((uint64_t)(buf_aud.offset << 3) * + timescale) / (aud_bitrate * 1000)); + aud_cnt = 1; + } else if (aud_bitrate > 0 && aud_samplerate > 0 && buf_aud.offset > 0) { + static const uint16_t kbps[2][15] = { + {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320}, + {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}}; + static const uint16_t rates[3] = {44100, 48000, 32000}; + const unsigned char *a = (const unsigned char*)buf_aud.buf; + while (aud_cnt < MP4_AUD_MAX && aud_len + 4 <= buf_aud.offset) { + const unsigned char *h = a + aud_len; + if (h[0] != 0xFF || (h[1] & 0xE6) != 0xE2) break; // Layer III sync + unsigned ver = (h[1] >> 3) & 3, bi = h[2] >> 4, ri = (h[2] >> 2) & 3; + if (ver == 1 || bi == 0 || bi == 15 || ri == 3) break; + unsigned lsf = ver != 3, rate = rates[ri] >> (ver == 3 ? 0 : ver == 2 ? 1 : 2); + unsigned spf = lsf ? 576 : 1152; + unsigned len = spf / 8 * kbps[lsf][bi] * 1000 / rate + ((h[2] >> 1) & 1); + if (aud_len + len > buf_aud.offset) break; + samples_info[1 + aud_cnt].size = len; + samples_info[1 + aud_cnt].duration = (uint32_t)((uint64_t)spf * timescale / rate); + aud_dur += samples_info[1 + aud_cnt].duration; + aud_len += len; + aud_cnt++; + } } + /* Each track keeps its own clock; sharing one made every video frame + * inherit the length of the audio chunk muxed with it. */ + last_video_duration = samples_info[0].duration; + last_audio_duration = aud_dur; buf_moof.offset = 0; err = write_moof( &buf_moof, 0, 0, 0, default_sample_size, samples_info, - 1, samples_info + 1, 1); + 1, samples_info + 1, aud_cnt ? aud_cnt : 1); chk_err; buf_mdat.offset = 0; err = write_mdat(&buf_mdat, nal_data, nal_len, - buf_aud.buf, buf_aud.offset); + buf_aud.buf, aud_len); chk_err; + /* Anything that is not a whole frame is unusable: drop it and resync. */ buf_aud.offset = 0; + aud_frm_cnt = 0; return BUF_OK; } +enum BufError mp4_ingest_audio_frame(const char *data, const uint32_t len, + const uint32_t samples) { + if (aud_bitrate == 0) return BUF_OK; + /* Nobody draining (no client): keep only the newest frames */ + if (aud_frm_cnt >= MP4_AUD_MAX) { aud_frm_cnt = 0; buf_aud.offset = 0; } + enum BufError err = put(&buf_aud, data, len); + chk_err; + aud_frm_size[aud_frm_cnt] = len; + aud_frm_samp[aud_frm_cnt++] = samples; + return BUF_OK; +} + enum BufError mp4_ingest_audio(const char *data, const uint32_t len) { enum BufError err; + /* Audio is only drained when a slice is muxed; with no client attached it + * would pile up and the next fragment would jump the timeline by minutes. */ + if (aud_bitrate == 0) return BUF_OK; + if (buf_aud.offset + len > (uint32_t)aud_bitrate * 1000 / 8 / 4) + buf_aud.offset = 0; err = put(&buf_aud, data, len); chk_err; @@ -161,16 +215,14 @@ enum BufError mp4_set_state(struct Mp4State *state) { &buf_moof, pos_base_data_offset, state->base_data_offset); chk_err if (pos_audio_media_decode_time > 0) err = put_u64_be_to_offset( &buf_moof, pos_audio_media_decode_time, - state->base_media_decode_time); + state->audio_media_decode_time); chk_err if (pos_video_media_decode_time > 0) err = put_u64_be_to_offset( &buf_moof, pos_video_media_decode_time, state->base_media_decode_time); chk_err state->sequence_number++; state->base_data_offset += buf_moof.offset + buf_mdat.offset; - if (last_vid_tstamp) - state->base_media_decode_time += vid_duration; - else - state->base_media_decode_time += last_fragment_duration; + state->base_media_decode_time += last_video_duration; + state->audio_media_decode_time += last_audio_duration; return BUF_OK; } diff --git a/src/fmt/mp4.h b/src/fmt/mp4.h index e91373c8..3918896e 100644 --- a/src/fmt/mp4.h +++ b/src/fmt/mp4.h @@ -22,6 +22,7 @@ struct Mp4State { uint32_t sequence_number; uint64_t base_data_offset; uint64_t base_media_decode_time; + uint64_t audio_media_decode_time; uint32_t default_sample_duration; uint32_t nals_count; @@ -37,6 +38,8 @@ void mp4_set_timestamp(uint64_t tstamp); enum BufError mp4_set_slice(const char *nal_data, const uint32_t nal_len, char is_iframe); enum BufError mp4_ingest_audio(const char *data, const uint32_t len); +enum BufError mp4_ingest_audio_frame(const char *data, const uint32_t len, + const uint32_t samples); enum BufError mp4_set_state(struct Mp4State *state); diff --git a/src/hal/star/i3_aud.h b/src/hal/star/i3_aud.h deleted file mode 100644 index 892b685f..00000000 --- a/src/hal/star/i3_aud.h +++ /dev/null @@ -1,95 +0,0 @@ -#pragma once - -#include "i3_common.h" - -typedef struct { - // Accept industry standards from 8000 to 48000Hz - int rate; - // Accepts from 4 to 24 bits - int bit; - int stereoOn; - unsigned int length; - int leftOrRight; - int absTimeOn; -} i3_aud_cnf; - -typedef struct { - unsigned char *addr; - int bit; - int stereoOn; - unsigned long long timestamp; - unsigned int sequence; - unsigned int length; - void *reserved; -} i3_aud_frm; - -typedef struct { - void *handle; - - int (*fnDisableDevice)(int device); - int (*fnEnableDevice)(int device); - int (*fnSetDeviceConfig)(int device, i3_aud_cnf *config); - - int (*fnDisableChannel)(int device, int channel); - int (*fnEnableChannel)(int device, int channel); - - int (*fnSetMute)(int device, int *muteOn); - int (*fnSetVolume)(int device, int dbLevel); - - int (*fnFreeFrame)(int device, int channel, i3_aud_frm *frame); - int (*fnGetFrame)(int device, int channel, i3_aud_frm *frame, int millis); - int (*fnStartFrame)(int device, int channel); -} i3_aud_impl; - -static int i3_aud_load(i3_aud_impl *aud_lib) { - if (!(aud_lib->handle = dlopen("libmi.so", RTLD_LAZY | RTLD_GLOBAL))) - HAL_ERROR("i3_aud", "Failed to load library!\nError: %s\n", dlerror()); - - if (!(aud_lib->fnDisableDevice = (int(*)(int device)) - hal_symbol_load("i3_aud", aud_lib->handle, "MI_AI_Disable"))) - return EXIT_FAILURE; - - if (!(aud_lib->fnEnableDevice = (int(*)(int device)) - hal_symbol_load("i3_aud", aud_lib->handle, "MI_AI_Enable"))) - return EXIT_FAILURE; - - if (!(aud_lib->fnSetDeviceConfig = (int(*)(int device, i3_aud_cnf *config)) - hal_symbol_load("i3_aud", aud_lib->handle, "MI_AI_SetDevAttr"))) - return EXIT_FAILURE; - - if (!(aud_lib->fnDisableChannel = (int(*)(int device, int channel)) - hal_symbol_load("i3_aud", aud_lib->handle, "MI_AI_DisableChn"))) - return EXIT_FAILURE; - - if (!(aud_lib->fnEnableChannel = (int(*)(int device, int channel)) - hal_symbol_load("i3_aud", aud_lib->handle, "MI_AI_EnableChn"))) - return EXIT_FAILURE; - - if (!(aud_lib->fnSetMute = (int(*)(int device,int *muteOn)) - hal_symbol_load("i3_aud", aud_lib->handle, "MI_AI_SetMute"))) - return EXIT_FAILURE; - - if (!(aud_lib->fnSetVolume = (int(*)(int device, int dbLevel)) - hal_symbol_load("i3_aud", aud_lib->handle, "MI_AI_SetVolume"))) - return EXIT_FAILURE; - - if (!(aud_lib->fnFreeFrame = (int(*)(int device, int channel, i3_aud_frm *frame)) - hal_symbol_load("i3_aud", aud_lib->handle, "MI_AI_ReleaseFrame"))) - return EXIT_FAILURE; - - if (!(aud_lib->fnGetFrame = (int(*)(int device, int channel, i3_aud_frm *frame, int millis)) - hal_symbol_load("i3_aud", aud_lib->handle, "MI_AI_GetFrame"))) - return EXIT_FAILURE; - - if (!(aud_lib->fnStartFrame = (int(*)(int device, int channel)) - hal_symbol_load("i3_aud", aud_lib->handle, "MI_AI_StartFrame"))) - return EXIT_FAILURE; - - return EXIT_SUCCESS; -} - -static void i3_aud_unload(i3_aud_impl *aud_lib) { - if (aud_lib->handle) dlclose(aud_lib->handle); - aud_lib->handle = NULL; - memset(aud_lib, 0, sizeof(*aud_lib)); -} \ No newline at end of file diff --git a/src/hal/star/i3_common.h b/src/hal/star/i3_common.h deleted file mode 100644 index d8e9af7e..00000000 --- a/src/hal/star/i3_common.h +++ /dev/null @@ -1,50 +0,0 @@ -#pragma once - -#include -#include -#include - -#include "../symbols.h" -#include "../types.h" - -typedef enum { - I3_MODE_TUNNEL, - I3_MODE_USER, - I3_MODE_SHARE, - I3_MODE_NOT_TUNNEL, - I3_MODE_INVALID -} i3_common_mode; - -typedef enum { - I3_PIXFMT_UNUSED, - I3_PIXFMT_YUV422_YUYV, - I3_PIXFMT_MSTAR_RAW16, - I3_PIXFMT_MSTAR_YC16, - I3_PIXFMT_MSTAR_STS16, - I3_PIXFMT_YUV420SP, - I3_PIXFMT_YUV420P, - I3_PIXFMT_BGR565, - I3_PIXFMT_ARGB4444, - I3_PIXFMT_ARGB1555, - I3_PIXFMT_RGB888, - I3_PIXFMT_ABGR8888, - I3_PIXFMT_YUV400, - I3_PIXFMT_END -} i3_common_pixfmt; - -typedef struct { - unsigned int width; - unsigned int height; -} i3_common_dim; - -typedef struct { - int x; - int y; -} i3_common_pnt; - -typedef struct { - int x; - int y; - unsigned int width; - unsigned int height; -} i3_common_rect; \ No newline at end of file diff --git a/src/hal/star/i3_hal.c b/src/hal/star/i3_hal.c index 3f3953ee..9dc8b79f 100644 --- a/src/hal/star/i3_hal.c +++ b/src/hal/star/i3_hal.c @@ -2,525 +2,756 @@ #include "i3_hal.h" -i3_aud_impl i3_aud; -i3_isp_impl i3_isp; -i3_osd_impl i3_osd; -i3_sys_impl i3_sys; -i3_venc_impl i3_venc; -i3_vi_impl i3_vi; +/* + * SigmaStar Infinity3 (MSC313E / MSC316DC / MSC316DM) HAL. + * + * The call sequence and structure layouts below were verified on hardware + * (Imou LOOC 2, OV2735) against the vendor libmi.so, not taken from headers: + * - libmi.so and libmi_isp.so reference each other's symbols, so they must be + * in the process before us (LD_PRELOAD both); dlopen() of one alone fails. + * - MI_VENC_GetStreamTimeOut() hands back one Annex-B buffer per frame: + * word 0 = data pointer, word 1 = length, words 2-3 = timestamp, word 4 = type. + * - Rate control modes are 0 none, 1 VBR, 2 CBR, 3 fixed QP: VBR and CBR + * are the other way round from later chips (_MI_VENC_SetRcParamEx, called + * from MI_VENC_CreateChn, hands 1 to _MI_VENC_SetVbr and 2 to + * _MI_VENC_SetCbr). With 0 the H.264 encoder ignores the bitrate and sits + * at QP 48; the JPEG encoder is left at 0, its quality sets the size. + */ + +typedef struct { unsigned int width, height; } i3_dim; +typedef struct { int enable; unsigned int x, y, width, height; } i3_crop; +typedef struct { int source; float srcFps; i3_crop crop; int reserved[1]; } i3_vi_dev; +typedef struct { i3_dim dest; int pixFmt; int mode; void *reserved1; int reserved2[1]; } i3_vi_chn; + +typedef struct { + int codec; + union { struct { unsigned int profile; int reserved[1]; } h26x; unsigned int pad[2]; }; + i3_dim capt; int pixFmt; unsigned int dstFps; int mode; int reserved[1]; +} i3_venc_attr; +typedef struct { unsigned int gop, statTime, bitrate, avgLvl, maxQual, minQual; int ipQpDelta; int reserved[4]; } i3_rate_cbr; +typedef struct { unsigned int gop, statTime, maxBitrate, maxIQual, minIQual, maxPQual, minPQual; int ipQpDelta; int chgPos; } i3_rate_vbr; +typedef struct { + int mode; + union { i3_rate_cbr cbr; i3_rate_vbr vbr; unsigned int pad[12]; }; + unsigned int rowQpDelta; void *extend; unsigned int rcVersion; +} i3_venc_rate; +typedef struct { i3_venc_attr attrib; i3_venc_rate rate; } i3_venc_chn; +typedef struct { int module; unsigned int device, channel; } i3_sys_bind; + +#define I3_VI_SRC_CAMERA 12 +#define I3_PIXFMT_YUV420SP 5 +#define I3_MODE_TUNNEL 0 +#define I3_SYS_MOD_VI 4 +#define I3_SYS_MOD_VENC 5 +#define I3_STRM_WORDS 32 + +static struct { + void *handle; + int (*sysInit)(void); + int (*sysExit)(void); + int (*sysBind)(i3_sys_bind*, i3_sys_bind*); + int (*sysUnbind)(i3_sys_bind*, i3_sys_bind*); + int (*viSetDev)(i3_vi_dev*); + int (*viSetChn)(int, i3_vi_chn*); + int (*viEnable)(int); + int (*viDisable)(int); + int (*vencCreate)(int, i3_venc_chn*); + int (*vencDestroy)(int); + int (*vencStart)(int); + int (*vencStop)(int); + int (*vencGet)(int, void*, unsigned int); + int (*vencRelease)(int, void*); + int (*vencIdr)(int, char); +} i3; hal_chnstate i3_state[I3_VENC_CHN_NUM] = {0}; int (*i3_aud_cb)(hal_audframe*); int (*i3_vid_cb)(char, hal_vidstream*); -char _i3_aud_chn = 0; -char _i3_aud_dev = 0; -char _i3_isp_chn = 0; -char _i3_venc_port = 0; -char _i3_vi_chn = 0; -char _i3_vi_dev = 0; -char _i3_vi_port = 0; +static char i3_vi_on[I3_VENC_CHN_NUM], i3_venc_on[I3_VENC_CHN_NUM], i3_bound[I3_VENC_CHN_NUM]; +static i3_dim i3_chn_dim[I3_VENC_CHN_NUM]; +// VI channel feeding each encoder. Normally the same index; snapshots use the +// camera component's dedicated still-image port (VI channel 3), the only one +// besides the two video ports that an encoder will bind to. +static char i3_vi_of[I3_VENC_CHN_NUM] = { 0, 1, 2 }; +#define I3_VI_CHN_IMAGE 3 + +#define I3_LOAD(field, name) \ + if (!(i3.field = hal_symbol_load("i3_hal", i3.handle, name))) return EXIT_FAILURE + +/* + * The vendor libraries read tuning values ("mi.vi.sub.width" and so on) through + * an Android-style property store that the stock firmware fills with a + * property service. There is no such service here, so the store is written + * directly: a 32 KiB area in /dev/__properties__ in the classic bionic layout + * (count, serial, magic, version, table of contents at 32, 128-byte records + * from 1024: name[32], serial = value length << 24, value[92]). + * Call i3_property_set() before i3_hal_init(). + */ +#define I3_PROP_SIZE 32768 +#define I3_PROP_MAX 64 +static struct { char name[32]; char value[92]; } i3_props[I3_PROP_MAX]; +static int i3_prop_num; + +void i3_property_set(const char *name, const char *value) +{ + int i; + for (i = 0; i < i3_prop_num; i++) + if (!strcmp(i3_props[i].name, name)) break; + if (i == I3_PROP_MAX || strlen(name) >= sizeof(i3_props[0].name) || + strlen(value) >= sizeof(i3_props[0].value)) return; + strcpy(i3_props[i].name, name); + strcpy(i3_props[i].value, value); + if (i == i3_prop_num) i3_prop_num++; +} + +static void i3_property_commit(void) +{ + if (!i3_prop_num) return; + unsigned char *area = calloc(1, I3_PROP_SIZE); + if (!area) return; + unsigned int *head = (unsigned int*)area; + head[0] = i3_prop_num; + head[2] = 0x504f5250; + head[3] = 0x45434f76; + for (int i = 0; i < i3_prop_num; i++) { + unsigned int off = 1024 + i * 128; + head[8 + i] = (unsigned int)strlen(i3_props[i].name) << 24 | off; + strcpy((char*)area + off, i3_props[i].name); + *(unsigned int*)(area + off + 32) = (unsigned int)strlen(i3_props[i].value) << 24; + strcpy((char*)area + off + 36, i3_props[i].value); + } + // The reader insists on a root-owned file that others cannot write + FILE *file = fopen("/dev/__properties__", "wb"); + if (file) { + fwrite(area, 1, I3_PROP_SIZE, file); + fclose(file); + chmod("/dev/__properties__", 0644); + } + free(area); + + // OpenIPC's own libat.so keeps the settings in the process instead (read + // from /etc/sigmastar.conf), so hand them to it as well where it is used. + void *at = dlopen("libat.so", RTLD_LAZY | RTLD_GLOBAL); + if (at) { + int (*set)(const char*, const char*) = dlsym(at, "property_set"); + if (set && !dlsym(at, "__system_property_area__")) + for (int i = 0; i < i3_prop_num; i++) + set(i3_props[i].name, i3_props[i].value); + } +} void i3_hal_deinit(void) { - i3_vi_unload(&i3_vi); - i3_venc_unload(&i3_venc); - i3_osd_unload(&i3_osd); - i3_isp_unload(&i3_isp); - i3_aud_unload(&i3_aud); - i3_sys_unload(&i3_sys); + if (i3.handle) dlclose(i3.handle); + memset(&i3, 0, sizeof(i3)); } int i3_hal_init(void) { - int ret; - - if (ret = i3_sys_load(&i3_sys)) - return ret; - if (ret = i3_aud_load(&i3_aud)) - return ret; - if (ret = i3_isp_load(&i3_isp)) - return ret; - if (ret = i3_osd_load(&i3_osd)) - return ret; - if (ret = i3_venc_load(&i3_venc)) - return ret; - if (ret = i3_vi_load(&i3_vi)) - return ret; + i3_property_commit(); + + // libmi.so and libmi_isp.so use each other's symbols. glibc resolves that + // lazily when both are preloaded; musl needs libmi_isp.so opened first with + // RTLD_GLOBAL, so its pending symbols are bound when libmi.so arrives. + if (!dlopen("libmi_isp.so", RTLD_LAZY | RTLD_GLOBAL)) + HAL_WARNING("i3_hal", "libmi_isp.so: %s\n", dlerror()); + if (!(i3.handle = dlopen("libmi.so", RTLD_LAZY | RTLD_GLOBAL))) + HAL_ERROR("i3_hal", "Failed to load libmi.so (it needs libmi_isp.so " + "preloaded alongside it)!\nError: %s\n", dlerror()); + + I3_LOAD(sysInit, "MI_SYS_Init"); + I3_LOAD(sysExit, "MI_SYS_Exit"); + I3_LOAD(sysBind, "MI_SYS_Bind"); + I3_LOAD(sysUnbind, "MI_SYS_UnBind"); + I3_LOAD(viSetDev, "MI_VI_SetDevAttr"); + I3_LOAD(viSetChn, "MI_VI_SetChnAttr"); + I3_LOAD(viEnable, "MI_VI_EnableChn"); + I3_LOAD(viDisable, "MI_VI_DisableChn"); + I3_LOAD(vencCreate, "MI_VENC_CreateChn"); + I3_LOAD(vencDestroy, "MI_VENC_DestroyChn"); + I3_LOAD(vencStart, "MI_VENC_StartRecvPic"); + I3_LOAD(vencStop, "MI_VENC_StopRecvPic"); + I3_LOAD(vencGet, "MI_VENC_GetStreamTimeOut"); + I3_LOAD(vencRelease, "MI_VENC_ReleaseStream"); + I3_LOAD(vencIdr, "MI_VENC_RequestIDR"); return EXIT_SUCCESS; } -/*void i3_audio_deinit(void) +/* + * Audio input goes through the kernel's ALSA device (card 0, "infinity_snd_machine") + * using the tinyalsa library the vendor package ships. MI_AI exists in libmi.so but + * its channel start fails without vendor-side setup we have no description of, + * while ALSA capture works out of the box. + */ +typedef struct { + unsigned int channels, rate, period_size, period_count; + int format; + unsigned int start_threshold, stop_threshold, silence_threshold; + int avail_min; + unsigned int pad[8]; +} i3_pcm_config; + +#define I3_PCM_IN 0x10000000 +#define I3_AUD_SAMPLES 640 + +static struct { + void *handle, *pcm; + unsigned int rate; + void *(*pcmOpen)(unsigned int, unsigned int, unsigned int, i3_pcm_config*); + int (*pcmReady)(void*); + int (*pcmRead)(void*, void*, unsigned int); + int (*pcmClose)(void*); + void *(*mixerOpen)(unsigned int); + void (*mixerClose)(void*); + void *(*mixerByName)(void*, const char*); + int (*mixerSet)(void*, unsigned int, int); + int (*mixerSetEnum)(void*, const char*); +} i3a; + +static pthread_t i3_spk_pid; +static char i3_spk_run; + +void i3_audio_deinit(void) { - i3_aud.fnDisableChannel(_i3_aud_dev, _i3_aud_chn); - - i3_aud.fnDisableDevice(_i3_aud_dev); + if (i3_spk_run) { i3_spk_run = 0; pthread_join(i3_spk_pid, NULL); } + if (i3a.pcm) { i3a.pcmClose(i3a.pcm); i3a.pcm = NULL; } + if (i3a.handle) { dlclose(i3a.handle); i3a.handle = NULL; } } -int i3_audio_init(int samplerate) +int i3_audio_init(int samplerate, int gain) { - int ret; - - { - i3_aud_cnf config; - config.rate = samplerate; - config.bit24On = 0; - config.intf = I3_AUD_INTF_I2S_SLAVE; - config.sound = I3_AUD_SND_MONO; - config.frmNum = 4; - config.packNumPerFrm = 640; - config.chnNum = 1; - if (ret = i3_aud.fnSetDeviceConfig(_i3_aud_dev, &config)) - return ret; + if (!(i3a.handle = dlopen("libtinyalsa.so", RTLD_LAZY))) + HAL_ERROR("i3_aud", "Failed to load libtinyalsa.so!\nError: %s\n", dlerror()); + + if (!(i3a.pcmOpen = hal_symbol_load("i3_aud", i3a.handle, "pcm_open")) || + !(i3a.pcmReady = hal_symbol_load("i3_aud", i3a.handle, "pcm_is_ready")) || + !(i3a.pcmRead = hal_symbol_load("i3_aud", i3a.handle, "pcm_read")) || + !(i3a.pcmClose = hal_symbol_load("i3_aud", i3a.handle, "pcm_close"))) + return EXIT_FAILURE; + i3a.mixerOpen = dlsym(i3a.handle, "mixer_open"); + i3a.mixerClose = dlsym(i3a.handle, "mixer_close"); + i3a.mixerByName = dlsym(i3a.handle, "mixer_get_ctl_by_name"); + i3a.mixerSet = dlsym(i3a.handle, "mixer_ctl_set_value"); + i3a.mixerSetEnum = dlsym(i3a.handle, "mixer_ctl_set_enum_by_string"); + + // Select the microphone and map the generic gain (dB-ish, 0..) onto the + // codec's capture volume range (0..94, 64 is the driver default). + void *mixer; + if (i3a.mixerOpen && i3a.mixerByName && (mixer = i3a.mixerOpen(0))) { + void *ctl; + if (i3a.mixerSetEnum && (ctl = i3a.mixerByName(mixer, "ADC Mux"))) + i3a.mixerSetEnum(ctl, "Mic-in"); + if (i3a.mixerSet && gain > 0 && (ctl = i3a.mixerByName(mixer, "Main Capture Volume"))) + i3a.mixerSet(ctl, 0, MIN(MAX(gain, 0), 94)); + if (i3a.mixerClose) i3a.mixerClose(mixer); } - if (ret = i3_aud.fnEnableDevice(_i3_aud_dev)) - return ret; - if (ret = i3_aud.fnEnableChannel(_i3_aud_dev, _i3_aud_chn)) - return ret; - if (ret = i3_aud.fnSetVolume(_i3_aud_dev, _i3_aud_chn, 13)) - return ret; - - { - i3_sys_bind bind = { .module = I3_SYS_MOD_AI, - .device = _i3_aud_dev, .channel = _i3_aud_chn }; - if (ret = i3_sys.fnSetOutputDepth(&bind, 2, 4)) - return ret; + i3_pcm_config config; + memset(&config, 0, sizeof(config)); + config.channels = 1; + config.rate = samplerate; + i3a.rate = samplerate; + config.period_size = I3_AUD_SAMPLES; + config.period_count = 4; + config.format = 0; // S16_LE + i3a.pcm = i3a.pcmOpen(0, 0, I3_PCM_IN, &config); + if (!i3a.pcm || !i3a.pcmReady(i3a.pcm)) { + if (i3a.pcm) { i3a.pcmClose(i3a.pcm); i3a.pcm = NULL; } + HAL_ERROR("i3_aud", "Opening the capture device at %d Hz failed!\n", samplerate); } return EXIT_SUCCESS; } -void *i3_audio_thread(void) +/* + * Speaker (talk-back). PCM handed to i3_speaker_write() is queued and played by + * a thread through the same ALSA card. The amplifier on this board (Imou LOOC 2) + * is not the kernel's amp-gpio: it sits on GPIO 18 (PAD_FUART_CTS, RIU 0x103c2c, + * bit 4 out / bit 5 oen) and wants the vendor's enable sequence - idle low, + * seven fast low/high pulses, then high (pdc.ko gPaCfg). Sysfs GPIO is far too + * slow for that, so the pad register is driven directly. + */ +#include +#include +#include + +#define I3_SPK_RING (16000 * 2) +static short i3_spk_ring[I3_SPK_RING]; +static unsigned int i3_spk_head, i3_spk_fill; +static pthread_mutex_t i3_spk_mtx = PTHREAD_MUTEX_INITIALIZER; +static void i3_spk_spin(int n) { volatile int i; for (i = 0; i < n; i++); } + +static void i3_speaker_amp(char on) { - int ret; + int fd = open("/dev/mem", O_RDWR | O_SYNC); + if (fd < 0) return; + void *map = mmap(NULL, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0x1F207000); + close(fd); + if (map == MAP_FAILED) return; + volatile unsigned short *reg = (volatile unsigned short*)((char*)map + 0x858); + unsigned short lo = *reg & ~0x30, hi = lo | 0x10; + *reg = lo; + if (on) { + usleep(2000); + for (int i = 0; i < 7; i++) { *reg = lo; i3_spk_spin(75); *reg = hi; i3_spk_spin(75); } + *reg = hi; + } + munmap(map, 0x1000); +} - i3_aud_frm frame; - memset(&frame, 0, sizeof(frame)); - - while (keepRunning && audioOn) { - while ((ret = i3_aud.fnGetFrame(_i3_aud_dev, _i3_aud_chn, - &frame, NULL, 0)) == 0) { - if (i3_aud_cb) { - hal_audframe outFrame; - outFrame.channelCnt = 1; - outFrame.data[0] = frame.addr[0]; - outFrame.length[0] = frame.length; - outFrame.seq = frame.sequence; - outFrame.timestamp = frame.timestamp; - (i3_aud_cb)(&outFrame); +static void *i3_speaker_thread(void *arg) +{ + static short period[I3_AUD_SAMPLES]; + void *pcm = NULL; + int (*pcmWrite)(void*, const void*, unsigned int) = dlsym(i3a.handle, "pcm_write"); + unsigned int idle = 0; + + while (keepRunning && i3_spk_run && pcmWrite) { + pthread_mutex_lock(&i3_spk_mtx); + unsigned int n = MIN(i3_spk_fill, I3_AUD_SAMPLES); + for (unsigned int i = 0; i < n; i++) + period[i] = i3_spk_ring[(i3_spk_head + i) % I3_SPK_RING]; + i3_spk_head = (i3_spk_head + n) % I3_SPK_RING; + i3_spk_fill -= n; + pthread_mutex_unlock(&i3_spk_mtx); + + if (!n) { + // Nothing to say: play silence for a moment (network gaps), then + // switch the amplifier off and release the device. + if (pcm && ++idle > 20) { + i3_speaker_amp(0); + i3a.pcmClose(pcm); + pcm = NULL; } - - if (ret = i3_aud.fnFreeFrame(_i3_aud_dev, _i3_aud_chn, - &frame, NULL)) { - HAL_WARNING("i3_aud", "Releasing the frame failed" - " with %#x!\n", ret); + if (!pcm) { usleep(20000); continue; } + } else idle = 0; + memset(period + n, 0, (I3_AUD_SAMPLES - n) * sizeof(short)); + + if (!pcm) { + i3_pcm_config config; + memset(&config, 0, sizeof(config)); + config.channels = 1; + config.rate = 16000; + config.period_size = I3_AUD_SAMPLES; + config.period_count = 4; + pcm = i3a.pcmOpen(0, 0, 0, &config); + if (!pcm || !i3a.pcmReady(pcm)) { + if (pcm) i3a.pcmClose(pcm); + pcm = NULL; + usleep(200000); + continue; } + // DAC first, then the amplifier, so it does not pop + static short silence[I3_AUD_SAMPLES]; + pcmWrite(pcm, silence, sizeof(silence)); + i3_speaker_amp(1); } - - usleep(5000); + if (pcmWrite(pcm, period, sizeof(period))) usleep(10000); } - HAL_INFO("i3_aud", "Shutting down capture thread...\n"); + + i3_speaker_amp(0); + if (pcm) i3a.pcmClose(pcm); + return NULL; } -int i3_channel_bind(char index, char framerate) +// Codec playback volume, 0..94 (the driver default is 64); -1 only reads it +int i3_speaker_volume(int volume) { - int ret; - - if (ret = i3_vpe.fnEnablePort(_i3_vpe_chn, index)) - return ret; - - { - unsigned int device; - if (ret = i3_venc.fnGetChannelDeviceId(index, &device)) - return ret; - i3_sys_bind source = { .module = I3_SYS_MOD_VPE, - .device = _i3_vpe_dev, .channel = _i3_vpe_chn, .port = index }; - i3_sys_bind dest = { .module = I3_SYS_MOD_VENC, - .device = device, .channel = index, .port = _i3_venc_port }; - if (ret = i3_sys.fnBindExt(&source, &dest, framerate, framerate, - I3_SYS_LINK_FRAMEBASE, 0)) - return ret; + static int current = -1; + void *mixer, *ctl; + if (volume >= 0 && i3a.mixerOpen && i3a.mixerByName && i3a.mixerSet && (mixer = i3a.mixerOpen(0))) { + if ((ctl = i3a.mixerByName(mixer, "Main Playback Volume")) && + !i3a.mixerSet(ctl, 0, MIN(volume, 94))) + current = MIN(volume, 94); + if (i3a.mixerClose) i3a.mixerClose(mixer); } + return current; +} +// 16 kHz mono signed 16-bit samples for the speaker +int i3_speaker_write(const short *pcm, unsigned int samples) +{ + if (!i3a.handle || !i3a.pcmOpen) return EXIT_FAILURE; + if (!i3_spk_run) { + i3_spk_run = 1; + if (pthread_create(&i3_spk_pid, NULL, i3_speaker_thread, NULL)) { + i3_spk_run = 0; + return EXIT_FAILURE; + } + } + pthread_mutex_lock(&i3_spk_mtx); + for (unsigned int i = 0; i < samples; i++) { + if (i3_spk_fill == I3_SPK_RING) { // full: drop the oldest + i3_spk_head = (i3_spk_head + 1) % I3_SPK_RING; + i3_spk_fill--; + } + i3_spk_ring[(i3_spk_head + i3_spk_fill++) % I3_SPK_RING] = pcm[i]; + } + pthread_mutex_unlock(&i3_spk_mtx); return EXIT_SUCCESS; } -int i3_channel_create(char index, short width, short height, char mirror, char flip, char jpeg) +void *i3_audio_thread(void) { - i3_vpe_port port; - port.output.width = width; - port.output.height = height; - port.mirror = mirror; - port.flip = flip; - port.compress = I3_COMPR_NONE; - port.pixFmt = jpeg ? I3_PIXFMT_YUV422_YUYV : I3_PIXFMT_YUV420SP; + static short buffer[I3_AUD_SAMPLES]; + unsigned int seq = 0, rate = i3a.rate ? i3a.rate : 16000; + unsigned long long samples = 0; - return i3_vpe.fnSetPortConfig(_i3_vpe_chn, index, &port); -} + while (keepRunning && audioOn && i3a.pcm) { + if (i3a.pcmRead(i3a.pcm, buffer, sizeof(buffer))) { + usleep(10000); + continue; + } -int i3_channel_grayscale(char enable) -{ - return i3_isp.fnSetColorToGray(0, &enable); + if (i3_aud_cb) { + hal_audframe outFrame; + memset(&outFrame, 0, sizeof(outFrame)); + outFrame.channelCnt = 1; + outFrame.data[0] = (unsigned char*)buffer; + outFrame.length[0] = sizeof(buffer); + outFrame.seq = seq++; + // ALSA gives no capture timestamps; derive them from the sample count. + outFrame.timestamp = (unsigned int)(samples * 1000ULL / rate); + samples += I3_AUD_SAMPLES; + (i3_aud_cb)(&outFrame); + } + } + HAL_INFO("i3_aud", "Shutting down capture thread...\n"); + return NULL; } -int i3_channel_unbind(char index) +int i3_channel_create(char index, short width, short height, char jpeg) { int ret; + if (index < 0 || index >= I3_VENC_CHN_NUM) return EXIT_FAILURE; - if (ret = i3_vpe.fnDisablePort(_i3_vpe_chn, index)) + i3_vi_chn channel; + memset(&channel, 0, sizeof(channel)); + channel.dest.width = width; + channel.dest.height = height; + channel.pixFmt = I3_PIXFMT_YUV420SP; + channel.mode = I3_MODE_TUNNEL; + if (ret = i3.viSetChn(index, &channel)) return ret; - { - unsigned int device; - if (ret = i3_venc.fnGetChannelDeviceId(index, &device)) - return ret; - i3_sys_bind source = { .module = I3_SYS_MOD_VPE, - .device = _i3_vpe_dev, .channel = _i3_vpe_chn, .port = index }; - i3_sys_bind dest = { .module = I3_SYS_MOD_VENC, - .device = device, .channel = index, .port = _i3_venc_port }; - if (ret = i3_sys.fnUnbind(&source, &dest)) - return ret; - } - + i3_chn_dim[index].width = width; + i3_chn_dim[index].height = height; return EXIT_SUCCESS; -}*/ - -int i3_config_load(char *path) -{ - return i3_isp.fnLoadChannelConfig(_i3_isp_chn, path, 1234); } -/*int i3_pipeline_create(char sensor, short width, short height, char framerate) +int i3_channel_bind(char index, char framerate) { int ret; + i3_sys_bind source = { I3_SYS_MOD_VI, 0, i3_vi_of[index] }; + i3_sys_bind dest = { I3_SYS_MOD_VENC, 0, index }; - { - i3_vif_dev device; - memset(&device, 0, sizeof(device)); - device.intf = _i3_snr_pad.intf; - device.work = device.intf == I3_INTF_BT656 ? - I3_VIF_WORK_1MULTIPLEX : I3_VIF_WORK_RGB_REALTIME; - device.hdr = I3_HDR_OFF; - if (device.intf == I3_INTF_MIPI) { - device.edge = I3_EDGE_DOUBLE; - device.input = _i3_snr_pad.intfAttr.mipi.input; - } else if (device.intf == I3_INTF_BT656) { - device.edge = _i3_snr_pad.intfAttr.bt656.edge; - device.sync = _i3_snr_pad.intfAttr.bt656.sync; - } - if (ret = i3_vif.fnSetDeviceConfig(_i3_vif_dev, &device)) + if (!i3_vi_on[index]) { + if (ret = i3.viEnable(i3_vi_of[index])) return ret; + i3_vi_on[index] = 1; } - if (ret = i3_vif.fnEnableDevice(_i3_vif_dev)) - return ret; - { - i3_vif_port port; - port.capt = _i3_snr_plane.capt; - port.dest.height = _i3_snr_plane.capt.height; - port.dest.width = _i3_snr_plane.capt.width; - port.field = 0; - port.interlaceOn = 0; - port.pixFmt = (i3_common_pixfmt)(_i3_snr_plane.bayer > I3_BAYER_END ? - _i3_snr_plane.pixFmt : (I3_PIXFMT_RGB_BAYER + _i3_snr_plane.precision * I3_BAYER_END + _i3_snr_plane.bayer)); - port.frate = I3_VIF_FRATE_FULL; - port.frameLineCnt = 0; - if (ret = i3_vif.fnSetPortConfig(_i3_vif_chn, _i3_vif_port, &port)) + if (!i3_bound[index]) { + if (ret = i3.sysBind(&source, &dest)) return ret; + i3_bound[index] = 1; } - if (ret = i3_vif.fnEnablePort(_i3_vif_chn, _i3_vif_port)) - return ret; - if (series == 0xF1) { - i3e_vpe_chn channel; - memset(&channel, 0, sizeof(channel)); - channel.capt.height = _i3_snr_plane.capt.height; - channel.capt.width = _i3_snr_plane.capt.width; - channel.pixFmt = (i3_common_pixfmt)(_i3_snr_plane.bayer > I3_BAYER_END ? - _i3_snr_plane.pixFmt : (I3_PIXFMT_RGB_BAYER + _i3_snr_plane.precision * I3_BAYER_END + _i3_snr_plane.bayer)); - channel.hdr = I3_HDR_OFF; - channel.sensor = (i3_vpe_sens)(_i3_snr_index + 1); - channel.mode = I3_VPE_MODE_REALTIME; - if (ret = i3_vpe.fnCreateChannel(_i3_vpe_chn, (i3_vpe_chn*)&channel)) - return ret; - } else { - i3_vpe_chn channel; - memset(&channel, 0, sizeof(channel)); - channel.capt.height = _i3_snr_plane.capt.height; - channel.capt.width = _i3_snr_plane.capt.width; - channel.pixFmt = (i3_common_pixfmt)(_i3_snr_plane.bayer > I3_BAYER_END ? - _i3_snr_plane.pixFmt : (I3_PIXFMT_RGB_BAYER + _i3_snr_plane.precision * I3_BAYER_END + _i3_snr_plane.bayer)); - channel.hdr = I3_HDR_OFF; - channel.sensor = (i3_vpe_sens)(_i3_snr_index + 1); - channel.mode = I3_VPE_MODE_REALTIME; - if (ret = i3_vpe.fnCreateChannel(_i3_vpe_chn, &channel)) - return ret; - } + return i3.vencStart(index); +} - { - i3_vpe_para param; - param.hdr = I3_HDR_OFF; - param.level3DNR = 1; - param.mirror = 0; - param.flip = 0; - param.lensAdjOn = 0; - if (ret = i3_vpe.fnSetChannelParam(_i3_vpe_chn, ¶m)) - return ret; - } - if (ret = i3_vpe.fnStartChannel(_i3_vpe_chn)) - return ret; +// Grayscale for night mode is done by the ISP, so every channel (streams and +// snapshots) turns gray. MI_ISP_SetColorToGray(chn, attr) takes the attribute +// by value; its layout is the ColorToGray page of I3_isp_api.xml: Enable, +// WBCtrlEnable, OpType (0 auto, 1 manual), Auto.Enable[16], Manual.Enable. +// MI_VENC_SetColor2Grey() is only the fallback: it leaves the bottom of a +// 1080p frame in colour and the MJPEG encoder does not support it at all. +typedef struct { + unsigned int enable, wbCtrlEnable, opType; + unsigned int autoEnable[16]; + unsigned int manualEnable; +} i3_isp_colortogray; - { - i3_sys_bind source = { .module = I3_SYS_MOD_VIF, - .device = _i3_vif_dev, .channel = _i3_vif_chn, .port = _i3_vif_port }; - i3_sys_bind dest = { .module = I3_SYS_MOD_VPE, - .device = _i3_vpe_dev, .channel = _i3_vpe_chn, .port = _i3_vpe_port }; - return i3_sys.fnBindExt(&source, &dest, _i3_snr_framerate, _i3_snr_framerate, - I3_SYS_LINK_REALTIME, 0); +int i3_channel_grayscale(char enable) +{ + int (*getGray)(int, i3_isp_colortogray*) = + dlsym(RTLD_DEFAULT, "MI_ISP_GetColorToGray"); + int (*setGray)(int, i3_isp_colortogray) = + dlsym(RTLD_DEFAULT, "MI_ISP_SetColorToGray"); + + if (getGray && setGray) { + i3_isp_colortogray attr; + memset(&attr, 0, sizeof(attr)); + getGray(0, &attr); + attr.enable = 1; + attr.opType = 1; + attr.manualEnable = enable ? 1 : 0; + if (!setGray(0, attr)) + return EXIT_SUCCESS; } - return EXIT_SUCCESS; + int (*setGrey)(int, int*) = dlsym(i3.handle, "MI_VENC_SetColor2Grey"); + if (!setGrey) return EXIT_FAILURE; + + int ret = EXIT_SUCCESS, grey = enable ? 1 : 0; + for (char i = 0; i < I3_VENC_CHN_NUM; i++) + if (i3_venc_on[i] && (i3_state[i].payload == HAL_VIDCODEC_H264 || + i3_state[i].payload == HAL_VIDCODEC_H265) && setGrey(i, &grey)) + ret = EXIT_FAILURE; + return ret; } -void i3_pipeline_destroy(void) -{ - for (char i = 0; i < 4; i++) - i3_vpe.fnDisablePort(_i3_vpe_chn, i); +/* + * On-screen display through the vendor MI_OSD "widget" API (signatures recovered + * from libmi.so). Bitmap widgets need the hardware graphics plane, which the + * library keeps off (mi.osd.gop.use, also 0 in the stock firmware; forcing it + * to 1 or 3 through the property store kills the pipeline at start), so this + * uses the software-blended YUV widgets the stock firmware uses: + * MI_OSD_CreateYuvWidget(&handle, viChn, data, size, &position, width, height) + * MI_OSD_UpdateYuvWidget(handle, data, size, &position, width, height) + * MI_OSD_UpdateCanvas(viChn), MI_OSD_DestroyWidget(handle) + * The picture is NV12 (Y plane, then interleaved UV at half height) and a Y of + * zero marks a transparent pixel. Widgets are attached to a VI channel, so + * they show up on the stream encoded from it. + */ +#define I3_OSD_NUM 10 +typedef struct { unsigned int x, y; } i3_osd_pos; +// One widget per region and encoder channel: the main stream, a sub-stream and +// the snapshot channel come from different VI channels at different sizes. +static void *i3_osd_widget[I3_OSD_NUM][I3_VENC_CHN_NUM]; +static hal_rect i3_osd_rect[I3_OSD_NUM]; +static char i3_osd_plate[I3_OSD_NUM]; + +void i3_region_init(void) { memset(i3_osd_widget, 0, sizeof(i3_osd_widget)); } - { - i3_sys_bind source = { .module = I3_SYS_MOD_VIF, - .device = _i3_vif_dev, .channel = _i3_vif_chn, .port = _i3_vif_port }; - i3_sys_bind dest = { .module = I3_SYS_MOD_VPE, - .device = _i3_vif_dev, .channel = _i3_vpe_chn, .port = _i3_vpe_port }; - i3_sys.fnUnbind(&source, &dest); +void i3_region_destroy(char handle) +{ + int (*destroy)(void*) = dlsym(i3.handle, "MI_OSD_DestroyWidget"); + int (*canvas)(int) = dlsym(i3.handle, "MI_OSD_UpdateCanvas"); + if (handle < 0 || handle >= I3_OSD_NUM || !destroy) return; + for (char c = 0; c < I3_VENC_CHN_NUM; c++) { + if (!i3_osd_widget[handle][c]) continue; + destroy(i3_osd_widget[handle][c]); + i3_osd_widget[handle][c] = NULL; + if (canvas) canvas(i3_vi_of[c]); } +} - i3_vpe.fnStopChannel(_i3_vpe_chn); - i3_vpe.fnDestroyChannel(_i3_vpe_chn); - - i3_vif.fnDisablePort(_i3_vif_chn, 0); - i3_vif.fnDisableDevice(_i3_vif_dev); +void i3_region_deinit(void) +{ + for (char i = 0; i < I3_OSD_NUM; i++) i3_region_destroy(i); } int i3_region_create(char handle, hal_rect rect, short opacity) { - int ret; - - i3_sys_bind dest = { .module = 0, - .device = _i3_vpe_dev, .channel = _i3_vpe_chn }; - i3_rgn_cnf region, regionCurr; - i3_rgn_chn attrib, attribCurr; - - region.type = I3_RGN_TYPE_OSD; - region.pixFmt = I3_RGN_PIXFMT_ARGB1555; - region.size.width = rect.width; - region.size.height = rect.height; - - if (i3_rgn.fnGetRegionConfig(handle, ®ionCurr)) { - HAL_INFO("i3_rgn", "Creating region %d...\n", handle); - if (ret = i3_rgn.fnCreateRegion(handle, ®ion)) - return ret; - } else if (regionCurr.type != region.type || - regionCurr.size.height != region.size.height || - regionCurr.size.width != region.size.width) { - HAL_INFO("i3_rgn", "Parameters are different, recreating " - "region %d...\n", handle); - for (char i = 0; i < I3_VENC_CHN_NUM; i++) { - if (!i3_state[i].enable) continue; - dest.port = i; - i3_rgn.fnDetachChannel(handle, &dest); - } - i3_rgn.fnDestroyRegion(handle); - if (ret = i3_rgn.fnCreateRegion(handle, ®ion)) - return ret; - } + if (handle < 0 || handle >= I3_OSD_NUM) return EXIT_FAILURE; + i3_osd_rect[handle] = rect; + i3_osd_plate[handle] = opacity < 255; // translucent: text on a plate + return EXIT_SUCCESS; +} - if (i3_rgn.fnGetChannelConfig(handle, &dest, &attribCurr)) - HAL_INFO("i3_rgn", "Attaching region %d...\n", handle); - else if (attribCurr.point.x != rect.x || attribCurr.point.y != rect.y || - attribCurr.osd.bgFgAlpha[1] != opacity) { - HAL_INFO("i3_rgn", "Parameters are different, reattaching " - "region %d...\n", handle); - for (char i = 0; i < I3_VENC_CHN_NUM; i++) { - if (!i3_state[i].enable) continue; - dest.port = i; - i3_rgn.fnDetachChannel(handle, &dest); +int i3_region_setbitmap(int handle, hal_bitmap *bitmap) +{ + int (*create)(void**, int, void*, unsigned int, i3_osd_pos*, unsigned int, unsigned int) = + dlsym(i3.handle, "MI_OSD_CreateYuvWidget"); + int (*update)(void*, void*, unsigned int, i3_osd_pos*, unsigned int, unsigned int) = + dlsym(i3.handle, "MI_OSD_UpdateYuvWidget"); + int (*canvas)(int) = dlsym(i3.handle, "MI_OSD_UpdateCanvas"); + if (handle < 0 || handle >= I3_OSD_NUM || !create || !update || !bitmap->data) + return EXIT_FAILURE; + if (!i3_chn_dim[0].width || !i3_chn_dim[0].height) return EXIT_FAILURE; + + const unsigned short *src = bitmap->data; + unsigned int sw = bitmap->dim.width, sh = bitmap->dim.height; + int ret = EXIT_SUCCESS; + + for (char c = 0; c < I3_VENC_CHN_NUM; c++) { + if (!i3_venc_on[c] || !i3_chn_dim[c].width) continue; + // Positions and sizes are given for the main stream; other channels + // get the same picture scaled to their frame (16.16 fixed point). + unsigned int scale = ((unsigned int)i3_chn_dim[c].width << 16) / i3_chn_dim[0].width; + unsigned int tw = MAX(2, (sw * scale) >> 16), th = MAX(2, (sh * scale) >> 16); + unsigned int pad = i3_osd_plate[handle] ? MAX(2, (8 * scale) >> 16) : 0; + + // ARGB1555 -> NV12 with Y = 0 as the transparent key; even dimensions. + // The widgets have no alpha, so the plate behind the text (opacity + // below 255) is drawn as a sparse grid of black pixels: one in four + // reads as roughly a quarter opacity once scaled and encoded. + unsigned int w = (tw + 2 * pad + 1) & ~1, h = (th + 2 * pad + 1) & ~1, size = w * h * 3 / 2; + unsigned char *yuv = calloc(1, size); + if (!yuv) return EXIT_FAILURE; + memset(yuv + w * h, 128, w * h / 2); + if (pad) + for (unsigned int y = 0; y < h; y += 2) + for (unsigned int x = 0; x < w; x += 2) + yuv[y * w + x] = 16; + for (unsigned int y = 0; y < th; y++) { + unsigned int y0 = ((unsigned long long)y << 16) / scale, y1 = ((unsigned long long)(y + 1) << 16) / scale; + if (y1 > sh) y1 = sh; + if (y1 <= y0) y1 = y0 + 1; + for (unsigned int x = 0; x < tw; x++) { + unsigned int x0 = ((unsigned long long)x << 16) / scale, x1 = ((unsigned long long)(x + 1) << 16) / scale; + if (x1 > sw) x1 = sw; + if (x1 <= x0) x1 = x0 + 1; + // Of the source pixels under this one keep the brightest opaque + // one, so thin strokes survive scaling down + int best = -1, br = 0, bg = 0, bb = 0; + for (unsigned int yy = y0; yy < y1 && yy < sh; yy++) + for (unsigned int xx = x0; xx < x1 && xx < sw; xx++) { + unsigned short p = src[yy * sw + xx]; + if (!(p & 0x8000)) continue; + int r = (p >> 10 & 31) * 255 / 31, g = (p >> 5 & 31) * 255 / 31, b = (p & 31) * 255 / 31; + int luma = (77 * r + 150 * g + 29 * b) >> 8; + if (luma > best) { best = luma; br = r; bg = g; bb = b; } + } + if (best < 0) continue; + unsigned int ox = x + pad, oy = y + pad; + yuv[oy * w + ox] = best < 16 ? 16 : best; + unsigned char *uv = yuv + w * h + (oy / 2) * w + (ox & ~1); + uv[0] = ((-43 * br - 85 * bg + 128 * bb) >> 8) + 128; + uv[1] = ((128 * br - 107 * bg - 21 * bb) >> 8) + 128; + } } - } - memset(&attrib, 0, sizeof(attrib)); - attrib.show = 1; - attrib.point.x = rect.x; - attrib.point.y = rect.y; - attrib.osd.layer = 0; - attrib.osd.constAlphaOn = 0; - attrib.osd.bgFgAlpha[0] = 0; - attrib.osd.bgFgAlpha[1] = opacity; - - for (char i = 0; i < I3_VENC_CHN_NUM; i++) { - if (!i3_state[i].enable) continue; - dest.port = i; - i3_rgn.fnAttachChannel(handle, &dest, &attrib); + // The plate grows around the text instead of pushing it away from its place + int px0 = (int)((i3_osd_rect[handle].x * scale) >> 16) - (int)pad; + int py0 = (int)((i3_osd_rect[handle].y * scale) >> 16) - (int)pad; + int maxx = (int)i3_chn_dim[c].width - (int)w, maxy = (int)i3_chn_dim[c].height - (int)h; + if (px0 > maxx) px0 = maxx; + if (py0 > maxy) py0 = maxy; + i3_osd_pos pos = { (px0 < 0 ? 0 : px0) & ~1, (py0 < 0 ? 0 : py0) & ~1 }; + int cret; + if (i3_osd_widget[handle][c]) + cret = update(i3_osd_widget[handle][c], yuv, size, &pos, w, h); + else if ((cret = create(&i3_osd_widget[handle][c], i3_vi_of[c], yuv, size, &pos, w, h))) + i3_osd_widget[handle][c] = NULL; + if (!cret && canvas) canvas(i3_vi_of[c]); + if (cret) ret = cret; + free(yuv); } + return ret; +} +// Auto-exposure state from the vendor ISP library. MI_ISP_QueryExposureInfo(chn, out) +// fills a 556-byte record: stable, reach-boundary, FNx10, sensor gain, ISP gain, +// shutter (us), LumY, AvgY, 128-bin histogram, LVx10, BV, scene target. +int i3_exposure_info(unsigned int *sensorGain, unsigned int *ispGain, + unsigned int *shutterUs, unsigned int *avgY, int *bv) +{ + static int (*query)(int, void*); + static unsigned int info[160]; + if (!query && !(query = dlsym(RTLD_DEFAULT, "MI_ISP_QueryExposureInfo"))) + return EXIT_FAILURE; + memset(info, 0, sizeof(info)); + if (query(0, info)) return EXIT_FAILURE; + if (sensorGain) *sensorGain = info[3]; + if (ispGain) *ispGain = info[4]; + if (shutterUs) *shutterUs = info[5]; + if (avgY) *avgY = info[7]; + if (bv) *bv = (int)info[137]; return EXIT_SUCCESS; } -void i3_region_destroy(char handle) +int i3_channel_unbind(char index) { - i3_sys_bind dest = { .module = 0, - .device = _i3_vpe_dev, .channel = _i3_vpe_chn }; + i3_sys_bind source = { I3_SYS_MOD_VI, 0, i3_vi_of[index] }; + i3_sys_bind dest = { I3_SYS_MOD_VENC, 0, index }; - dest.port = 1; - i3_rgn.fnDetachChannel(handle, &dest); - dest.port = 0; - i3_rgn.fnDetachChannel(handle, &dest); - i3_rgn.fnDestroyRegion(handle); + if (i3_venc_on[index]) i3.vencStop(index); + if (i3_bound[index]) { i3.sysUnbind(&source, &dest); i3_bound[index] = 0; } + if (i3_vi_on[index]) { i3.viDisable(i3_vi_of[index]); i3_vi_on[index] = 0; } + + return EXIT_SUCCESS; } -int i3_region_setbitmap(int handle, hal_bitmap *bitmap) +int i3_config_load(char *path) { return EXIT_SUCCESS; } + +int i3_pipeline_create(char sensor, short width, short height, char mirror, char flip, char framerate) { - i3_rgn_bmp nativeBmp = { .data = bitmap->data, .pixFmt = I3_RGN_PIXFMT_ARGB1555, - .size.height = bitmap->dim.height, .size.width = bitmap->dim.width }; + i3_vi_dev device; + memset(&device, 0, sizeof(device)); + device.source = I3_VI_SRC_CAMERA; + device.srcFps = (float)framerate; - return i3_rgn.fnSetBitmap(handle, &nativeBmp); + return i3.viSetDev(&device); +} + +void i3_pipeline_destroy(void) +{ + for (char i = 0; i < I3_VENC_CHN_NUM; i++) + if (i3_vi_on[i]) { i3.viDisable(i3_vi_of[i]); i3_vi_on[i] = 0; } } int i3_video_create(char index, hal_vidconfig *config) { int ret; i3_venc_chn channel; - i3_venc_attr_h26x *attrib; memset(&channel, 0, sizeof(channel)); - if (config->codec == HAL_VIDCODEC_JPG || config->codec == HAL_VIDCODEC_MJPG) { - channel.attrib.codec = I3_VENC_CODEC_MJPG; - switch (config->mode) { - case HAL_VIDMODE_CBR: - channel.rate.mode = series == 0xEF ? I3OG_VENC_RATEMODE_MJPGCBR : I3_VENC_RATEMODE_MJPGCBR; - channel.rate.mjpgCbr.bitrate = config->bitrate << 10; - channel.rate.mjpgCbr.fpsNum = - config->codec == HAL_VIDCODEC_JPG ? 1 : config->framerate; - channel.rate.mjpgCbr.fpsDen = 1; - break; - case HAL_VIDMODE_QP: - channel.rate.mode = series == 0xEF ? I3OG_VENC_RATEMODE_MJPGQP : I3_VENC_RATEMODE_MJPGQP; - channel.rate.mjpgQp.fpsNum = - config->codec == HAL_VIDCODEC_JPG ? 1 : config->framerate; - channel.rate.mjpgQp.fpsDen = 1; - channel.rate.mjpgQp.quality = MAX(config->minQual, config->maxQual); - break; - default: - HAL_ERROR("i3_venc", "MJPEG encoder can only support CBR or fixed QP modes!"); - } - - channel.attrib.mjpg.maxHeight = config->height; - channel.attrib.mjpg.maxWidth = config->width; - channel.attrib.mjpg.bufSize = config->width * config->height; - channel.attrib.mjpg.byFrame = 1; - channel.attrib.mjpg.height = config->height; - channel.attrib.mjpg.width = config->width; - channel.attrib.mjpg.dcfThumbs = 0; - channel.attrib.mjpg.markPerRow = 0; - - goto attach; - } else if (config->codec == HAL_VIDCODEC_H265) { - channel.attrib.codec = I3_VENC_CODEC_H265; - attrib = &channel.attrib.h265; - switch (config->mode) { - case HAL_VIDMODE_CBR: - channel.rate.mode = series == 0xEF ? I3OG_VENC_RATEMODE_H265CBR : - I3_VENC_RATEMODE_H265CBR; - channel.rate.h265Cbr = (i3_venc_rate_h26xcbr){ .gop = config->gop, - .statTime = 1, .fpsNum = config->framerate, .fpsDen = 1, .bitrate = - (unsigned int)(config->bitrate) << 10, .avgLvl = 1 }; break; - case HAL_VIDMODE_VBR: - channel.rate.mode = series == 0xEF ? I3OG_VENC_RATEMODE_H265VBR : - I3_VENC_RATEMODE_H265VBR; - channel.rate.h265Vbr = (i3_venc_rate_h26xvbr){ .gop = config->gop, - .statTime = 1, .fpsNum = config->framerate, .fpsDen = 1, .maxBitrate = - (unsigned int)(MAX(config->bitrate, config->maxBitrate)) << 10, - .maxQual = config->maxQual, .minQual = config->minQual }; break; - case HAL_VIDMODE_QP: - channel.rate.mode = series == 0xEF ? I3OG_VENC_RATEMODE_H265QP : - I3_VENC_RATEMODE_H265QP; - channel.rate.h265Qp = (i3_venc_rate_h26xqp){ .gop = config->gop, - .fpsNum = config->framerate, .fpsDen = 1, .interQual = config->maxQual, - .predQual = config->minQual }; break; - case HAL_VIDMODE_ABR: - HAL_ERROR("i3_venc", "H.265 encoder does not support ABR mode!"); - case HAL_VIDMODE_AVBR: - channel.rate.mode = series == 0xEF ? I3OG_VENC_RATEMODE_H265AVBR : - I3_VENC_RATEMODE_H265AVBR; - channel.rate.h265Avbr = (i3_venc_rate_h26xvbr){ .gop = config->gop, - .statTime = 1, .fpsNum = config->framerate, .fpsDen = 1, .maxBitrate = - (unsigned int)(MAX(config->bitrate, config->maxBitrate)) << 10, - .maxQual = config->maxQual, .minQual = config->minQual }; break; - default: - HAL_ERROR("i3_venc", "H.265 encoder does not support this mode!"); - } - } else if (config->codec == HAL_VIDCODEC_H264) { - channel.attrib.codec = I3_VENC_CODEC_H264; - attrib = &channel.attrib.h264; - if (series == 0xEF && config->mode == HAL_VIDMODE_ABR) - config->mode = -1; - switch (config->mode) { - case HAL_VIDMODE_CBR: - channel.rate.mode = series == 0xEF ? I3OG_VENC_RATEMODE_H264CBR : - I3_VENC_RATEMODE_H264CBR; - channel.rate.h264Cbr = (i3_venc_rate_h26xcbr){ .gop = config->gop, - .statTime = 1, .fpsNum = config->framerate, .fpsDen = 1, .bitrate = - (unsigned int)(config->bitrate) << 10, .avgLvl = 1 }; break; - case HAL_VIDMODE_VBR: - channel.rate.mode = series == 0xEF ? I3OG_VENC_RATEMODE_H264VBR : - I3_VENC_RATEMODE_H264VBR; - channel.rate.h264Vbr = (i3_venc_rate_h26xvbr){ .gop = config->gop, - .statTime = 1, .fpsNum = config->framerate, .fpsDen = 1, .maxBitrate = - (unsigned int)(MAX(config->bitrate, config->maxBitrate)) << 10, - .maxQual = config->maxQual, .minQual = config->minQual }; break; - case HAL_VIDMODE_QP: - channel.rate.mode = series == 0xEF ? I3OG_VENC_RATEMODE_H264QP : - I3_VENC_RATEMODE_H264QP; - channel.rate.h264Qp = (i3_venc_rate_h26xqp){ .gop = config->gop, - .fpsNum = config->framerate, .fpsDen = 1, .interQual = config->maxQual, - .predQual = config->minQual }; break; - case HAL_VIDMODE_ABR: - channel.rate.mode = I3_VENC_RATEMODE_H264ABR; - channel.rate.h264Abr = (i3_venc_rate_h26xabr){ .gop = config->gop, - .statTime = 1, .fpsNum = config->framerate, .fpsDen = 1, - .avgBitrate = (unsigned int)(config->bitrate) << 10, - .maxBitrate = (unsigned int)(config->maxBitrate) << 10 }; break; - case HAL_VIDMODE_AVBR: - channel.rate.mode = series == 0xEF ? I3OG_VENC_RATEMODE_H264AVBR : - I3_VENC_RATEMODE_H264AVBR; - channel.rate.h264Avbr = (i3_venc_rate_h26xvbr){ .gop = config->gop, .statTime = 1, - .fpsNum = config->framerate, .fpsDen = 1, .maxBitrate = - (unsigned int)(MAX(config->bitrate, config->maxBitrate)) << 10, - .maxQual = config->maxQual, .minQual = config->minQual }; break; - default: - HAL_ERROR("i3_venc", "H.264 encoder does not support this mode!"); - } - } else HAL_ERROR("i3_venc", "This codec is not supported by the hardware!"); - attrib->maxHeight = config->height; - attrib->maxWidth = config->width; - attrib->bufSize = config->height * config->width; - attrib->profile = MIN((series == 0xEF || config->codec == HAL_VIDCODEC_H265) ? 1 : 2, - config->profile); - attrib->byFrame = 1; - attrib->height = config->height; - attrib->width = config->width; - attrib->bFrameNum = 0; - attrib->refNum = 1; -attach: - if (ret = i3_venc.fnCreateChannel(index, &channel)) - return ret; + switch (config->codec) { + case HAL_VIDCODEC_H264: channel.attrib.codec = 0; break; + case HAL_VIDCODEC_H265: channel.attrib.codec = 1; break; + // The single-shot JPEG encoder refuses to bind to a video port, so + // snapshots are taken from an MJPEG channel as well. + case HAL_VIDCODEC_MJPG: + case HAL_VIDCODEC_JPG: channel.attrib.codec = 2; break; + default: HAL_ERROR("i3_venc", "This codec is not supported by the hardware!\n"); + } + if (config->codec == HAL_VIDCODEC_JPG && i3_vi_of[index] != I3_VI_CHN_IMAGE) { + i3_vi_chn still; + memset(&still, 0, sizeof(still)); + still.dest.width = config->width; + still.dest.height = config->height; + still.pixFmt = I3_PIXFMT_YUV420SP; + still.mode = I3_MODE_TUNNEL; + if (ret = i3.viSetChn(I3_VI_CHN_IMAGE, &still)) + return ret; + i3_vi_of[index] = I3_VI_CHN_IMAGE; + } + if (channel.attrib.codec == 2) { + // JPEG quality (rate control is not applied to this encoder, quality + // sets the size): the configured qfactor, else generous for stills + // and moderate for the MJPEG stream. Fixed for the channel's life. + channel.attrib.pad[0] = config->maxQual ? MIN(config->maxQual, 100) : + config->codec == HAL_VIDCODEC_JPG ? 85 : 50; + config->mode = HAL_VIDMODE_CBR; + if (!config->bitrate) config->bitrate = 4096; + if (!config->framerate) config->framerate = 5; + if (!config->gop) config->gop = config->framerate; + } else + channel.attrib.h26x.profile = MIN(config->profile, 2); + channel.attrib.capt.width = config->width; + channel.attrib.capt.height = config->height; + channel.attrib.pixFmt = I3_PIXFMT_YUV420SP; + channel.attrib.dstFps = config->framerate; + channel.attrib.mode = I3_MODE_TUNNEL; + + if (config->mode == HAL_VIDMODE_CBR) { + channel.rate.mode = channel.attrib.codec == 2 ? 0 : 2; + channel.rate.cbr.gop = config->gop; + channel.rate.cbr.statTime = 1; + channel.rate.cbr.bitrate = (unsigned int)config->bitrate << 10; + channel.rate.cbr.avgLvl = 1; + channel.rate.cbr.maxQual = config->maxQual ? config->maxQual : 45; + channel.rate.cbr.minQual = config->minQual ? config->minQual : 20; + } else { + channel.rate.mode = 1; + channel.rate.vbr.gop = config->gop; + channel.rate.vbr.statTime = 1; + channel.rate.vbr.maxBitrate = (unsigned int)MAX(config->bitrate, config->maxBitrate) << 10; + channel.rate.vbr.maxIQual = config->maxQual ? config->maxQual : 45; + channel.rate.vbr.minIQual = config->minQual ? config->minQual : 20; + channel.rate.vbr.maxPQual = config->maxQual ? config->maxQual : 45; + channel.rate.vbr.minPQual = config->minQual ? config->minQual : 20; + channel.rate.vbr.chgPos = 80; + } - if (config->codec != HAL_VIDCODEC_JPG && - (ret = i3_venc.fnStartReceiving(index))) + if (ret = i3.vencCreate(index, &channel)) return ret; + i3_venc_on[index] = 1; i3_state[index].payload = config->codec; return EXIT_SUCCESS; @@ -528,332 +759,185 @@ int i3_video_create(char index, hal_vidconfig *config) int i3_video_destroy(char index) { - int ret; - i3_state[index].enable = 0; i3_state[index].payload = HAL_VIDCODEC_UNSPEC; - i3_venc.fnStopReceiving(index); - - { - unsigned int device; - if (ret = i3_venc.fnGetChannelDeviceId(index, &device)) - return ret; - i3_sys_bind source = { .module = I3_SYS_MOD_VPE, - .device = _i3_vpe_dev, .channel = _i3_vpe_chn, .port = index }; - i3_sys_bind dest = { .module = I3_SYS_MOD_VENC, - .device = device, .channel = index, .port = _i3_venc_port }; - if (ret = i3_sys.fnUnbind(&source, &dest)) - return ret; - } - - if (ret = i3_venc.fnDestroyChannel(index)) - return ret; - - if (ret = i3_vpe.fnDisablePort(_i3_vpe_chn, index)) - return ret; + i3_channel_unbind(index); + if (i3_venc_on[index]) { i3.vencDestroy(index); i3_venc_on[index] = 0; } + i3_vi_of[index] = index; return EXIT_SUCCESS; } int i3_video_destroy_all(void) { - int ret; - for (char i = 0; i < I3_VENC_CHN_NUM; i++) - if (i3_state[i].enable) - if (ret = i3_video_destroy(i)) - return ret; + if (i3_state[i].enable || i3_venc_on[i]) + i3_video_destroy(i); return EXIT_SUCCESS; } void i3_video_request_idr(char index) { - i3_venc.fnRequestIdr(index, 1); + if (i3_venc_on[index]) i3.vencIdr(index, 1); } int i3_video_snapshot_grab(char index, char quality, hal_jpegdata *jpeg) { int ret; + unsigned int stream[I3_STRM_WORDS]; - if (ret = i3_channel_bind(index, 1)) { - HAL_DANGER("i3_venc", "Binding the encoder channel " - "%d failed with %#x!\n", index, ret); - goto abort; - } - - i3_venc_jpg param; - memset(¶m, 0, sizeof(param)); - if (ret = i3_venc.fnGetJpegParam(index, ¶m)) { - HAL_DANGER("i3_venc", "Reading the JPEG settings " - "%d failed with %#x!\n", index, ret); - goto abort; - } - - param.quality = quality; - if (ret = i3_venc.fnSetJpegParam(index, ¶m)) { - HAL_DANGER("i3_venc", "Writing the JPEG settings " - "%d failed with %#x!\n", index, ret); - goto abort; + // Per-request quality. The snapshot channel is an MJPEG encoder, whose + // parameters start with the quality it was created with (i3_video_create); + // only that word is changed, and only while the channel is stopped + // between snapshots, the one state in which the vendor accepts it. + { + static int (*getJpeg)(int, void*), (*setJpeg)(int, void*); + unsigned int param[64]; + if (!getJpeg) getJpeg = dlsym(RTLD_DEFAULT, "MI_VENC_GetMjpegParam"); + if (!setJpeg) setJpeg = dlsym(RTLD_DEFAULT, "MI_VENC_SetMjpegParam"); + memset(param, 0, sizeof(param)); + if (getJpeg && setJpeg && quality > 0 && quality <= 100 && !getJpeg(index, param) && + param[0] >= 1 && param[0] <= 100 && param[0] != (unsigned char)quality) { + param[0] = (unsigned char)quality; + if (setJpeg(index, param)) + HAL_WARNING("i3_venc", "Snapshot quality %d was not applied\n", quality); + } } - unsigned int count = 1; - if (ret = i3_venc.fnStartReceivingEx(index, &count)) { - HAL_DANGER("i3_venc", "Requesting one frame " + if (ret = i3_channel_bind(index, 1)) { + HAL_DANGER("i3_venc", "Binding the encoder channel " "%d failed with %#x!\n", index, ret); goto abort; } - int fd = i3_venc.fnGetDescriptor(index); - - struct timeval timeout = { .tv_sec = 2, .tv_usec = 0 }; - fd_set readFds; - FD_ZERO(&readFds); - FD_SET(fd, &readFds); - ret = select(fd + 1, &readFds, NULL, NULL, &timeout); - if (ret < 0) { - HAL_DANGER("i3_venc", "Select operation failed!\n"); - goto abort; - } else if (ret == 0) { - HAL_DANGER("i3_venc", "Capture stream timed out!\n"); - goto abort; - } - - if (FD_ISSET(fd, &readFds)) { - i3_venc_stat stat; - if (ret = i3_venc.fnQuery(index, &stat)) { - HAL_DANGER("i3_venc", "Querying the encoder channel " - "%d failed with %#x!\n", index, ret); - goto abort; - } - - if (!stat.curPacks) { - HAL_DANGER("i3_venc", "Current frame is empty, skipping it!\n"); - goto abort; - } - - i3_venc_strm strm; - memset(&strm, 0, sizeof(strm)); - i3_venc_pack packs[8]; - if (stat.curPacks > 8) - strm.packet = (i3_venc_pack*)malloc(sizeof(i3_venc_pack) * stat.curPacks); - else - strm.packet = packs; - - if (!strm.packet) { - HAL_DANGER("i3_venc", "Memory allocation on channel %d failed!\n", index); - goto abort; - } - strm.count = stat.curPacks; - - if (ret = i3_venc.fnGetStream(index, &strm, stat.curPacks)) { - HAL_DANGER("i3_venc", "Getting the stream on " - "channel %d failed with %#x!\n", index, ret); - if (stat.curPacks > 8) free(strm.packet); - strm.packet = NULL; - goto abort; - } - - { - jpeg->jpegSize = 0; - for (unsigned int i = 0; i < strm.count; i++) { - i3_venc_pack *pack = &strm.packet[i]; - unsigned int packLen = pack->length - pack->offset; - unsigned char *packData = pack->data + pack->offset; - - unsigned int newLen = jpeg->jpegSize + packLen; - if (newLen > jpeg->length) { - jpeg->data = realloc(jpeg->data, newLen); - jpeg->length = newLen; - } - memcpy(jpeg->data + jpeg->jpegSize, packData, packLen); - jpeg->jpegSize += packLen; + ret = EXIT_FAILURE; + for (int attempt = 0; attempt < 15; attempt++) { + memset(stream, 0, sizeof(stream)); + if (i3.vencGet(index, stream, 200)) continue; + + unsigned char *data = (unsigned char*)(unsigned long)stream[0]; + unsigned int length = stream[1]; + // Skip anything that is not a complete JPEG (starts FF D8). + if (data && length > 4 && data[0] == 0xFF && data[1] == 0xD8) { + if (length > jpeg->length) { + jpeg->data = realloc(jpeg->data, length); + jpeg->length = length; } + memcpy(jpeg->data, data, length); + jpeg->jpegSize = length; + ret = EXIT_SUCCESS; } - -abort: - i3_venc.fnFreeStream(index, &strm); - if (stat.curPacks > 8) free(strm.packet); + i3.vencRelease(index, stream); + if (!ret) break; } + if (ret) HAL_DANGER("i3_venc", "Capture stream timed out!\n"); - i3_venc.fnStopReceiving(index); - +abort: i3_channel_unbind(index); - return ret; } void *i3_video_thread(void) { - int ret, maxFd = 0; - - for (int i = 0; i < I3_VENC_CHN_NUM; i++) { - if (!i3_state[i].enable) continue; - if (!i3_state[i].mainLoop) continue; - - ret = i3_venc.fnGetDescriptor(i); - if (ret < 0) { - HAL_DANGER("i3_venc", "Getting the encoder descriptor failed with %#x!\n", ret); - return NULL; - } - i3_state[i].fileDesc = ret; - - if (maxFd <= i3_state[i].fileDesc) - maxFd = i3_state[i].fileDesc; - } - - i3_venc_stat stat; - i3_venc_strm stream; - struct timeval timeout; - fd_set readFds; + int ret; + unsigned int stream[I3_STRM_WORDS]; while (keepRunning) { - FD_ZERO(&readFds); - for(int i = 0; i < I3_VENC_CHN_NUM; i++) { - if (!i3_state[i].enable) continue; - if (!i3_state[i].mainLoop) continue; - FD_SET(i3_state[i].fileDesc, &readFds); - } - - timeout.tv_sec = 2; - timeout.tv_usec = 0; - ret = select(maxFd + 1, &readFds, NULL, NULL, &timeout); - if (ret < 0) { - HAL_DANGER("i3_venc", "Select operation failed!\n"); - break; - } else if (ret == 0) { - HAL_WARNING("i3_venc", "Main stream loop timed out!\n"); - continue; - } else { - for (int i = 0; i < I3_VENC_CHN_NUM; i++) { - if (!i3_state[i].enable) continue; - if (!i3_state[i].mainLoop) continue; - if (FD_ISSET(i3_state[i].fileDesc, &readFds)) { - memset(&stream, 0, sizeof(stream)); - - if (ret = i3_venc.fnQuery(i, &stat)) { - HAL_DANGER("i3_venc", "Querying the encoder channel " - "%d failed with %#x!\n", i, ret); - break; - } - - if (!stat.curPacks) { - HAL_WARNING("i3_venc", "Current frame is empty, skipping it!\n"); - continue; - } - - stream.packet = (i3_venc_pack*)malloc( - sizeof(i3_venc_pack) * stat.curPacks); - if (!stream.packet) { - HAL_DANGER("i3_venc", "Memory allocation on channel %d failed!\n", i); - break; - } - stream.count = stat.curPacks; - - if (ret = i3_venc.fnGetStream(i, &stream, 40)) { - HAL_DANGER("i3_venc", "Getting the stream on " - "channel %d failed with %#x!\n", i, ret); - break; - } - - if (i3_vid_cb) { - hal_vidstream outStrm; - hal_vidpack outPack[stream.count]; - outStrm.count = stream.count; - outStrm.seq = stream.sequence; - for (int j = 0; j < stream.count; j++) { - i3_venc_pack *pack = &stream.packet[j]; - outPack[j].data = pack->data; - outPack[j].length = pack->length; - outPack[j].naluCnt = pack->packNum; - if (series == 0xEF) { - unsigned int n = 0; - unsigned int sc, scanOff = 0; - unsigned int pktLen = pack->length; - while (n < 8 && - (sc = nal_find_startcode(outPack[j].data, - scanOff, pktLen)) < pktLen) { - unsigned int scLen = (outPack[j].data[sc + 2] == 1) ? 3 : 4; - outPack[0].nalu[n].type = - i3_state[i].payload == HAL_VIDCODEC_H264 - ? (outPack[j].data[sc + scLen] & 0x1F) - : ((outPack[j].data[sc + scLen] & 0x7E) >> 1); - outPack[0].nalu[n].offset = sc; - n++; - scanOff = sc + scLen; - } - outPack[0].naluCnt = n; - outPack[0].nalu[n].offset = pktLen; - for (n = 0; n < outPack[0].naluCnt; n++) - outPack[0].nalu[n].length = - outPack[0].nalu[n + 1].offset - - outPack[0].nalu[n].offset; - } else switch (i3_state[i].payload) { - case HAL_VIDCODEC_H264: - for (char k = 0; k < outPack[j].naluCnt; k++) { - outPack[j].nalu[k].length = - pack->packetInfo[k].length; - outPack[j].nalu[k].offset = - pack->packetInfo[k].offset; - outPack[j].nalu[k].type = - pack->packetInfo[k].packType.h264Nalu; - } - break; - case HAL_VIDCODEC_H265: - for (char k = 0; k < outPack[j].naluCnt; k++) { - outPack[j].nalu[k].length = - pack->packetInfo[k].length; - outPack[j].nalu[k].offset = - pack->packetInfo[k].offset; - outPack[j].nalu[k].type = - pack->packetInfo[k].packType.h265Nalu; - } - break; - } - outPack[j].offset = pack->offset; - outPack[j].timestamp = pack->timestamp; - } - outStrm.pack = outPack; - (*i3_vid_cb)(i, &outStrm); - } - - if (ret = i3_venc.fnFreeStream(i, &stream)) { - HAL_DANGER("i3_venc", "Releasing the stream on " - "channel %d failed with %#x!\n", i, ret); - } - if (stat.curPacks > 8) free(stream.packet); + char active = 0; + for (char i = 0; i < I3_VENC_CHN_NUM; i++) { + if (!i3_state[i].enable || !i3_state[i].mainLoop) continue; + if (!i3_venc_on[i] || !i3_bound[i]) continue; + active = 1; + + memset(stream, 0, sizeof(stream)); + // A channel that is being restarted fails at once instead of + // waiting out the timeout; don't spin on it. + if (i3.vencGet(i, stream, 200)) { usleep(10000); continue; } + + unsigned char *data = (unsigned char*)(unsigned long)stream[0]; + unsigned int length = stream[1]; + if (data && length && i3_vid_cb) { + hal_vidstream outStrm; + hal_vidpack outPack[1]; + memset(outPack, 0, sizeof(outPack)); + outPack[0].data = data; + outPack[0].length = length; + outPack[0].offset = 0; + outPack[0].timestamp = + ((unsigned long long)stream[3] << 32) | stream[2]; + + unsigned int n = 0, sc, scanOff = 0; + char h26x = i3_state[i].payload == HAL_VIDCODEC_H264 || + i3_state[i].payload == HAL_VIDCODEC_H265; + while (h26x && n < 7 && + (sc = nal_find_startcode(data, scanOff, length)) < length) { + unsigned int scLen = (data[sc + 2] == 1) ? 3 : 4; + outPack[0].nalu[n].type = i3_state[i].payload == HAL_VIDCODEC_H264 + ? (data[sc + scLen] & 0x1F) + : ((data[sc + scLen] & 0x7E) >> 1); + outPack[0].nalu[n].offset = sc; + n++; + scanOff = sc + scLen; } + outPack[0].naluCnt = n; + outPack[0].nalu[n].offset = length; + for (n = 0; n < outPack[0].naluCnt; n++) + outPack[0].nalu[n].length = + outPack[0].nalu[n + 1].offset - outPack[0].nalu[n].offset; + + outStrm.count = 1; + outStrm.seq = 0; + outStrm.pack = outPack; + (*i3_vid_cb)(i, &outStrm); } + + if (ret = i3.vencRelease(i, stream)) + HAL_WARNING("i3_venc", "Releasing the stream on " + "channel %d failed with %#x!\n", i, ret); } + if (!active) usleep(20000); } - HAL_INFO("i3_venc", "Shutting down encoding thread...\n"); -}*/ + return NULL; +} void i3_system_deinit(void) { - i3_sys.fnExit(); + i3.sysExit(); } -int i3_system_init(void) +// The vendor libraries take their log level from an Android-style property +// service that OpenIPC does not run, and default to tracing every buffer. +// Both export the level as plain variables, so quieten them directly. +static void i3_quiet_logs(void) { - int ret; - - { - i3_sys_ver version; - if (ret = i3_sys.fnGetVersion(&version)) - return ret; - printf("App built with headers v%s\n", I3_SYS_API); - puts(version.version); + unsigned int *level; + if ((level = dlsym(i3.handle, "g_uDbgLogLevel"))) *level = 0; + + void *omx = dlopen("/lib/libOMX_BELA.so", RTLD_LAZY | RTLD_NOLOAD); + if (!omx) omx = dlopen("libOMX_BELA.so", RTLD_LAZY | RTLD_NOLOAD); + if (omx) { + if ((level = dlsym(omx, "OmxDbgLogLevel"))) *level = 0; + if ((level = dlsym(omx, "ModuleDbgLogLevel"))) *level = 0; + dlclose(omx); } +} - if (ret = i3_sys.fnInit()) - return ret; - - return EXIT_SUCCESS; +int i3_system_init(void) +{ + int ret = i3.sysInit(); + i3_quiet_logs(); + return ret; } +#define I3_SYS_REG_ID 0x1F2025A4 +#define I3_SYS_CHIP_MASK 0xF000 +#define I3_SYS_CHIP_MSC313E 0x6000 +#define I3_SYS_CHIP_MSC316DC 0x7000 +#define I3_SYS_CHIP_MSC318 0x8000 + int i3_system_identify(void) { unsigned int val = 0; @@ -871,6 +955,8 @@ int i3_system_identify(void) strcpy(family, "infinity3"); chnCount = I3_VENC_CHN_NUM; chnState = (hal_chnstate*)i3_state; + aud_thread = i3_audio_thread; + vid_thread = i3_video_thread; return 1; } diff --git a/src/hal/star/i3_hal.h b/src/hal/star/i3_hal.h index 127b8577..47482a4d 100644 --- a/src/hal/star/i3_hal.h +++ b/src/hal/star/i3_hal.h @@ -1,51 +1,58 @@ #pragma once -#include "i3_common.h" -#include "i3_aud.h" -#include "i3_isp.h" -#include "i3_osd.h" -#include "i3_sys.h" -#include "i3_venc.h" -#include "i3_vi.h" +#include +#include +#include +#include +#include +#include +#include +#include "../symbols.h" +#include "../types.h" #include "../globals.h" #include "../tools.h" #include "../../fmt/nal.h" -#include -#include +#define I3_VENC_CHN_NUM 3 extern hal_chnstate i3_state[I3_VENC_CHN_NUM]; extern int (*i3_aud_cb)(hal_audframe*); extern int (*i3_vid_cb)(char, hal_vidstream*); void i3_hal_deinit(void); +void i3_property_set(const char *name, const char *value); int i3_hal_init(void); -/*void i3_audio_deinit(void); -int i3_audio_init(int samplerate); +void i3_audio_deinit(void); +int i3_audio_init(int samplerate, int gain); void *i3_audio_thread(void); +int i3_region_create(char handle, hal_rect rect, short opacity); +void i3_region_deinit(void); +void i3_region_destroy(char handle); +void i3_region_init(void); +int i3_region_setbitmap(int handle, hal_bitmap *bitmap); +int i3_speaker_write(const short *pcm, unsigned int samples); +int i3_speaker_volume(int volume); int i3_channel_bind(char index, char framerate); -int i3_channel_create(char index, short width, short height, char mirror, char flip, char jpeg); +int i3_channel_create(char index, short width, short height, char jpeg); int i3_channel_grayscale(char enable); -int i3_channel_unbind(char index);*/ +int i3_channel_unbind(char index); +int i3_exposure_info(unsigned int *sensorGain, unsigned int *ispGain, + unsigned int *shutterUs, unsigned int *avgY, int *bv); int i3_config_load(char *path); -/*int i3_pipeline_create(char sensor, short width, short height, char framerate); +int i3_pipeline_create(char sensor, short width, short height, char mirror, char flip, char framerate); void i3_pipeline_destroy(void); -int i3_region_create(char handle, hal_rect rect, short opacity); -void i3_region_destroy(char handle); -int i3_region_setbitmap(int handle, hal_bitmap *bitmap); - int i3_video_create(char index, hal_vidconfig *config); int i3_video_destroy(char index); int i3_video_destroy_all(void); void i3_video_request_idr(char index); int i3_video_snapshot_grab(char index, char quality, hal_jpegdata *jpeg); -void *i3_video_thread(void);*/ +void *i3_video_thread(void); void i3_system_deinit(void); int i3_system_init(void); diff --git a/src/hal/star/i3_isp.h b/src/hal/star/i3_isp.h deleted file mode 100644 index b7edf2dc..00000000 --- a/src/hal/star/i3_isp.h +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once - -#include "i3_common.h" - -typedef struct { - void *handle; - - int (*fnLoadChannelConfig)(int channel, char *path, unsigned int key); - int (*fnSetColorToGray)(int channel, char *enable); -} i3_isp_impl; - -static int i3_isp_load(i3_isp_impl *isp_lib) { - if (!(isp_lib->handle = dlopen("libmi_isp.so", RTLD_LAZY | RTLD_GLOBAL))) - HAL_ERROR("i3_isp", "Failed to load library!\nError: %s\n", dlerror()); - - if (!(isp_lib->fnLoadChannelConfig = (int(*)(int channel, char *path, unsigned int key)) - hal_symbol_load("i3_isp", isp_lib->handle, "MI_ISP_Load_ISPCmdBinFile"))) - return EXIT_FAILURE; - - if (!(isp_lib->fnSetColorToGray = (int(*)(int channel, char *enable)) - hal_symbol_load("i3_isp", isp_lib->handle, "MI_ISP_SetColorToGray"))) - return EXIT_FAILURE; - - return EXIT_SUCCESS; -} - -static void i3_isp_unload(i3_isp_impl *isp_lib) { - if (isp_lib->handle) dlclose(isp_lib->handle); - isp_lib->handle = NULL; - memset(isp_lib, 0, sizeof(*isp_lib)); -} \ No newline at end of file diff --git a/src/hal/star/i3_osd.h b/src/hal/star/i3_osd.h deleted file mode 100644 index d11c6253..00000000 --- a/src/hal/star/i3_osd.h +++ /dev/null @@ -1,44 +0,0 @@ -#pragma once - -#include "i3_common.h" -#include "i3_sys.h" - -typedef struct { - i3_common_pixfmt pixFmt; - unsigned short width; - unsigned short height; - unsigned char *data; -} i3_osd_bmp; - -typedef struct { - void *handle; - - int (*fnCreateRegion)(void *handle, int channel, i3_osd_bmp *bitmap, i3_common_pnt *point, i3_common_rect *rect); - int (*fnDestroyRegion)(void *handle); - int (*fnUpdateRegion)(void *handle, i3_osd_bmp *bitmap, i3_common_pnt *point, i3_common_rect *rect); -} i3_osd_impl; - -static int i3_osd_load(i3_osd_impl *osd_lib) { - if (!(osd_lib->handle = dlopen("libmi.so", RTLD_LAZY | RTLD_GLOBAL))) - HAL_ERROR("i3_osd", "Failed to load library!\nError: %s\n", dlerror()); - - if (!(osd_lib->fnCreateRegion = (int(*)(void *handle, int channel, i3_osd_bmp *bitmap, i3_common_pnt *point, i3_common_rect *rect)) - hal_symbol_load("i3_osd", osd_lib->handle, "MI_OSD_CreateBitmapWidget"))) - return EXIT_FAILURE; - - if (!(osd_lib->fnDestroyRegion = (int(*)(void *handle)) - hal_symbol_load("i3_osd", osd_lib->handle, "MI_OSD_DestroyWidget"))) - return EXIT_FAILURE; - - if (!(osd_lib->fnUpdateRegion = (int(*)(void *handle, i3_osd_bmp *bitmap, i3_common_pnt *point, i3_common_rect *rect)) - hal_symbol_load("i3_osd", osd_lib->handle, "MI_OSD_UpdateBitmapWidget"))) - return EXIT_FAILURE; - - return EXIT_SUCCESS; -} - -static void i3_osd_unload(i3_osd_impl *osd_lib) { - if (osd_lib->handle) dlclose(osd_lib->handle); - osd_lib->handle = NULL; - memset(osd_lib, 0, sizeof(*osd_lib)); -} \ No newline at end of file diff --git a/src/hal/star/i3_sys.h b/src/hal/star/i3_sys.h deleted file mode 100644 index 39b2cf8a..00000000 --- a/src/hal/star/i3_sys.h +++ /dev/null @@ -1,80 +0,0 @@ -#pragma once - -#include "i3_common.h" - -#define I3_SYS_API "1.0" - -#define I3_SYS_REG_ID 0x1F2025A4 -#define I3_SYS_CHIP_MASK 0xF000 -#define I3_SYS_CHIP_MSC313E 0x6000 -#define I3_SYS_CHIP_MSC316DC 0x7000 -#define I3_SYS_CHIP_MSC318 0x8000 - -typedef enum { - I3_SYS_MOD_AI, - I3_SYS_MOD_AO, - I3_SYS_MOD_AENC, - I3_SYS_MOD_ADEC, - I3_SYS_MOD_VI, - I3_SYS_MOD_VENC, - I3_SYS_MOD_END, -} i3_sys_mod; - -typedef struct { - i3_sys_mod module; - unsigned int device; - unsigned int channel; -} i3_sys_bind; - -typedef struct { - char version[64]; -} i3_sys_ver; - -typedef struct { - void *handle; - - int (*fnExit)(void); - int (*fnGetVersion)(i3_sys_ver *version); - int (*fnInit)(void); - int (*fnSetAlignment)(unsigned int *width); - - int (*fnBind)(i3_sys_bind *source, i3_sys_bind *dest); - int (*fnUnbind)(i3_sys_bind *source, i3_sys_bind *dest); -} i3_sys_impl; - -static int i3_sys_load(i3_sys_impl *sys_lib) { - if (!(sys_lib->handle = dlopen("libmi.so", RTLD_LAZY | RTLD_GLOBAL))) - HAL_ERROR("i3_sys", "Failed to load library!\nError: %s\n", dlerror()); - - if (!(sys_lib->fnExit = (int(*)(void)) - hal_symbol_load("i3_sys", sys_lib->handle, "MI_SYS_Exit"))) - return EXIT_FAILURE; - - if (!(sys_lib->fnGetVersion = (int(*)(i3_sys_ver *version)) - hal_symbol_load("i3_sys", sys_lib->handle, "MI_SYS_GetVersion"))) - return EXIT_FAILURE; - - if (!(sys_lib->fnInit = (int(*)(void)) - hal_symbol_load("i3_sys", sys_lib->handle, "MI_SYS_Init"))) - return EXIT_FAILURE; - - if (!(sys_lib->fnSetAlignment = (int(*)(unsigned int *width)) - hal_symbol_load("i3_sys", sys_lib->handle, "MI_SYS_SetConf"))) - return EXIT_FAILURE; - - if (!(sys_lib->fnBind = (int(*)(i3_sys_bind *source, i3_sys_bind *dest)) - hal_symbol_load("i3_sys", sys_lib->handle, "MI_SYS_Bind"))) - return EXIT_FAILURE; - - if (!(sys_lib->fnUnbind = (int(*)(i3_sys_bind *source, i3_sys_bind *dest)) - hal_symbol_load("i3_sys", sys_lib->handle, "MI_SYS_UnBind"))) - return EXIT_FAILURE; - - return EXIT_SUCCESS; -} - -static void i3_sys_unload(i3_sys_impl *sys_lib) { - if (sys_lib->handle) dlclose(sys_lib->handle); - sys_lib->handle = NULL; - memset(sys_lib, 0, sizeof(*sys_lib)); -} \ No newline at end of file diff --git a/src/hal/star/i3_venc.h b/src/hal/star/i3_venc.h deleted file mode 100644 index 7ee36d51..00000000 --- a/src/hal/star/i3_venc.h +++ /dev/null @@ -1,213 +0,0 @@ -#pragma once - -#include "i3_common.h" - -#define I3_VENC_CHN_NUM 3 - -typedef enum { - I3_VENC_CODEC_H264, - I3_VENC_CODEC_H265, - I3_VENC_CODEC_MJPEG, - I3_VENC_CODEC_JPEG, - I3_VENC_CODEC_END -} i3_venc_codec; - -typedef enum { - I3_VENC_NALU_H26X_PSLICE, - I3_VENC_NALU_H26X_BSLICE, - I3_VENC_NALU_H26X_ISLICE, - I3_VENC_NALU_H26X_IPSLICE, - I3_VENC_NALU_H26X_END -} i3_venc_nalu_h26x; - -typedef enum { - I3_VENC_RATE_H26X_OFF, - I3_VENC_RATE_H26X_CBR, - I3_VENC_RATE_H26X_VBR, - I3_VENC_RATE_H26X_QP, - I3_VENC_RATE_H26X_END -} i3_venc_rate_h26x; - -typedef struct { - // Accepts values from 0 to 2 (BP, MP, HP) - // N.B. H.265 only supports 0 (BP) - unsigned int profile; - int reserved[1]; -} i3_venc_attr_h26x; - -typedef struct { - unsigned int quality; - int snapOn; -} i3_venc_attr_mjpg; - -typedef struct { - unsigned int quality; - int reserved[1]; -} i3_venc_attr_jpg; - -typedef struct { - i3_venc_codec codec; - union { - i3_venc_attr_h26x h264; - i3_venc_attr_h26x h265; - i3_venc_attr_mjpg mjpg; - i3_venc_attr_jpg jpg; - }; - i3_common_dim capt; - i3_common_pixfmt pixFmt; - unsigned int dstFps; - i3_common_mode mode; - int reserved[1]; -} i3_venc_attrib; - -typedef struct { - unsigned int gop; - unsigned int statTime; - unsigned int bitrate; - unsigned int avgLvl; - unsigned int maxQual; - unsigned int minQual; - int ipQpDelta; - int reserved[4]; -} i3_venc_rate_h26xcbr; - -typedef struct { - unsigned int gop; - unsigned int statTime; - unsigned int maxBitrate; - unsigned int maxIQual; - unsigned int minIQual; - unsigned int maxPQual; - unsigned int minPQual; - int ipQpDelta; - int chgPos; -} i3_venc_rate_h26xvbr; - -typedef struct { - unsigned int gop; - unsigned int interQual; - unsigned int predQual; -} i3_venc_rate_h26xqp; - -typedef struct { - i3_venc_rate_h26x mode; - union { - i3_venc_rate_h26xcbr h26xCbr; - i3_venc_rate_h26xvbr h26xVbr; - i3_venc_rate_h26xqp h26xQp; - }; - unsigned int rowQpDelta; - void *extend; - unsigned int rcVersion; -} i3_venc_rate; - -typedef struct { - i3_venc_attrib attrib; - i3_venc_rate rate; -} i3_venc_chn; - -typedef struct { - unsigned int quality; - unsigned char qtLuma[64]; - unsigned char qtChroma[64]; - int reserved[1]; -} i3_venc_jpg; - -typedef struct { - unsigned char *data; - unsigned int length; - unsigned long long timestamp; - i3_venc_nalu_h26x naluType; - unsigned int offset; -} i3_venc_pack; - -typedef struct { - i3_venc_pack *packet; - unsigned int data1; - unsigned int data2; - unsigned int count; - int toDispose; - i3_venc_codec codec; -} i3_venc_strm; - -typedef struct { - void *handle; - - int (*fnCreateChannel)(int channel, i3_venc_chn *config); - int (*fnDestroyChannel)(int channel); - int (*fnGetChannelConfig)(int channel, i3_venc_chn *config); - int (*fnSetChannelConfig)(int channel, i3_venc_chn *config); - - int (*fnGetDescriptor)(int channel); - - int (*fnGetJpegParam)(int channel, i3_venc_jpg *param); - int (*fnSetJpegParam)(int channel, i3_venc_jpg *param); - - int (*fnFreeStream)(int channel, i3_venc_strm *stream); - int (*fnGetStream)(int channel, i3_venc_strm *stream, unsigned int timeout); - - int (*fnRequestIdr)(int channel, char instant); - int (*fnStartReceiving)(int channel); - int (*fnStopReceiving)(int channel); -} i3_venc_impl; - -static int i3_venc_load(i3_venc_impl *venc_lib) { - if (!(venc_lib->handle = dlopen("libmi.so", RTLD_LAZY | RTLD_GLOBAL))) - HAL_ERROR("i3_venc", "Failed to load library!\nError: %s\n", dlerror()); - - if (!(venc_lib->fnCreateChannel = (int(*)(int channel, i3_venc_chn *config)) - hal_symbol_load("i3_venc", venc_lib->handle, "MI_VENC_CreateChn"))) - return EXIT_FAILURE; - - if (!(venc_lib->fnDestroyChannel = (int(*)(int channel)) - hal_symbol_load("i3_venc", venc_lib->handle, "MI_VENC_DestroyChn"))) - return EXIT_FAILURE; - - if (!(venc_lib->fnGetChannelConfig = (int(*)(int channel, i3_venc_chn *config)) - hal_symbol_load("i3_venc", venc_lib->handle, "MI_VENC_GetChnAttr"))) - return EXIT_FAILURE; - - if (!(venc_lib->fnSetChannelConfig = (int(*)(int channel, i3_venc_chn *config)) - hal_symbol_load("i3_venc", venc_lib->handle, "MI_VENC_SetChnAttr"))) - return EXIT_FAILURE; - - if (!(venc_lib->fnGetDescriptor = (int(*)(int channel)) - hal_symbol_load("i3_venc", venc_lib->handle, "MI_VENC_GetFd"))) - return EXIT_FAILURE; - - if (!(venc_lib->fnGetJpegParam = (int(*)(int channel, i3_venc_jpg *param)) - hal_symbol_load("i3_venc", venc_lib->handle, "MI_VENC_GetJpegParam"))) - return EXIT_FAILURE; - - if (!(venc_lib->fnSetJpegParam = (int(*)(int channel, i3_venc_jpg *param)) - hal_symbol_load("i3_venc", venc_lib->handle, "MI_VENC_SetJpegParam"))) - return EXIT_FAILURE; - - if (!(venc_lib->fnFreeStream = (int(*)(int channel, i3_venc_strm *stream)) - hal_symbol_load("i3_venc", venc_lib->handle, "MI_VENC_ReleaseStream"))) - return EXIT_FAILURE; - - if (!(venc_lib->fnGetStream = (int(*)(int channel, i3_venc_strm *stream, unsigned int timeout)) - hal_symbol_load("i3_venc", venc_lib->handle, "MI_VENC_GetStreamTimeout"))) - return EXIT_FAILURE; - - if (!(venc_lib->fnRequestIdr = (int(*)(int channel, char instant)) - hal_symbol_load("i3_venc", venc_lib->handle, "MI_VENC_RequestIDR"))) - return EXIT_FAILURE; - - if (!(venc_lib->fnStartReceiving = (int(*)(int channel)) - hal_symbol_load("i3_venc", venc_lib->handle, "MI_VENC_StartRecvPic"))) - return EXIT_FAILURE; - - if (!(venc_lib->fnStopReceiving = (int(*)(int channel)) - hal_symbol_load("i3_venc", venc_lib->handle, "MI_VENC_StopRecvPic"))) - return EXIT_FAILURE; - - return EXIT_SUCCESS; -} - -static void i3_venc_unload(i3_venc_impl *venc_lib) { - if (venc_lib->handle) dlclose(venc_lib->handle); - venc_lib->handle = NULL; - memset(venc_lib, 0, sizeof(*venc_lib)); -} \ No newline at end of file diff --git a/src/hal/star/i3_vi.h b/src/hal/star/i3_vi.h deleted file mode 100644 index 13ac849d..00000000 --- a/src/hal/star/i3_vi.h +++ /dev/null @@ -1,83 +0,0 @@ -#pragma once - -#include "i3_common.h" - -typedef enum { - I3_VI_SRC_VGA, - I3_VI_SRC_YPBPR, - I3_VI_SRC_CVBS, - I3_VI_SRC_CVBS2, - I3_VI_SRC_CVBS3, - I3_VI_SRC_SVIDEO, - I3_VI_SRC_DTV, - I3_VI_SRC_SC0_VOP, - I3_VI_SRC_SC1_VOP, - I3_VI_SRC_SC2_VOP, - I3_VI_SRC_BT656, - I3_VI_SRC_BT656_1, - I3_VI_SRC_CAMERA, - I3_VI_SRC_HVSP, - I3_VI_SRC_END, -} i3_vi_src; - -typedef struct { - i3_common_dim dest; - i3_common_pixfmt pixFmt; - i3_common_mode mode; - void *reserved1; - int reserved2[1]; -} i3_vi_chn; - -typedef struct { - int enable; - unsigned int x; - unsigned int y; - unsigned int width; - unsigned int height; -} i3_vi_crop; - -typedef struct { - i3_vi_src source; - float srcFps; - i3_vi_crop crop; - int reserved[1]; -} i3_vi_dev; - -typedef struct { - void *handle; - - int (*fnSetDeviceConfig)(i3_vi_dev *config); - - int (*fnDisableChannel)(int channel); - int (*fnEnableChannel)(int channel); - int (*fnSetChannelConfig)(int channel, i3_vi_chn *config); -} i3_vi_impl; - -static int i3_vi_load(i3_vi_impl *vi_lib) { - if (!(vi_lib->handle = dlopen("libmi.so", RTLD_LAZY | RTLD_GLOBAL))) - HAL_ERROR("i3_vi", "Failed to load library!\nError: %s\n", dlerror()); - - if (!(vi_lib->fnSetDeviceConfig = (int(*)(i3_vi_dev *config)) - hal_symbol_load("i3_vi", vi_lib->handle, "MI_VI_SetDevAttr"))) - return EXIT_FAILURE; - - if (!(vi_lib->fnDisableChannel = (int(*)(int channel)) - hal_symbol_load("i3_vi", vi_lib->handle, "MI_VI_DisableChn"))) - return EXIT_FAILURE; - - if (!(vi_lib->fnEnableChannel = (int(*)(int channel)) - hal_symbol_load("i3_vi", vi_lib->handle, "MI_VI_EnableChn"))) - return EXIT_FAILURE; - - if (!(vi_lib->fnSetChannelConfig = (int(*)(int channel, i3_vi_chn *config)) - hal_symbol_load("i3_vi", vi_lib->handle, "MI_VI_SetChnAttr"))) - return EXIT_FAILURE; - - return EXIT_SUCCESS; -} - -static void i3_vi_unload(i3_vi_impl *vi_lib) { - if (vi_lib->handle) dlclose(vi_lib->handle); - vi_lib->handle = NULL; - memset(vi_lib, 0, sizeof(*vi_lib)); -} \ No newline at end of file diff --git a/src/jpeg.c b/src/jpeg.c index b848fea8..834843e6 100644 --- a/src/jpeg.c +++ b/src/jpeg.c @@ -31,6 +31,7 @@ int jpeg_init() { { hal_vidconfig config; + memset(&config, 0, sizeof(config)); config.width = app_config.jpeg_width; config.height = app_config.jpeg_height; config.codec = HAL_VIDCODEC_JPG; @@ -39,6 +40,7 @@ int jpeg_init() { switch (plat) { #if defined(__ARM_PCS_VFP) + case HAL_PLATFORM_I3: ret = i3_video_create(jpeg_index, &config); break; case HAL_PLATFORM_I6: ret = i6_video_create(jpeg_index, &config); break; case HAL_PLATFORM_I6C: ret = i6c_video_create(jpeg_index, &config); break; case HAL_PLATFORM_M6: ret = m6_video_create(jpeg_index, &config); break; @@ -82,6 +84,7 @@ void jpeg_deinit() { switch (plat) { #if defined(__ARM_PCS_VFP) + case HAL_PLATFORM_I3: i3_video_destroy(jpeg_index); break; case HAL_PLATFORM_I6: i6_video_destroy(jpeg_index); break; case HAL_PLATFORM_I6C: i6c_video_destroy(jpeg_index); break; case HAL_PLATFORM_M6: m6_video_destroy(jpeg_index); break; @@ -135,6 +138,7 @@ int jpeg_get(short width, short height, char quality, char grayscale, switch (plat) { #if defined(__ARM_PCS_VFP) + case HAL_PLATFORM_I3: ret = i3_video_snapshot_grab(jpeg_index, quality, jpeg); break; case HAL_PLATFORM_I6: ret = i6_video_snapshot_grab(jpeg_index, quality, jpeg); break; case HAL_PLATFORM_I6C: ret = i6c_video_snapshot_grab(jpeg_index, quality, jpeg); break; case HAL_PLATFORM_M6: ret = m6_video_snapshot_grab(jpeg_index, quality, jpeg); break; diff --git a/src/main.c b/src/main.c index 3332630a..ecc84526 100644 --- a/src/main.c +++ b/src/main.c @@ -15,7 +15,7 @@ #include #include -rtsp_handle rtspHandle; +rtsp_handle rtspHandle, rtspSubHandle; volatile char graceful = 0, keepRunning = 1; void handle_error(int signo) { @@ -36,6 +36,20 @@ void handle_exit(int signo) { } int main(int argc, char *argv[]) { + /* The system keeps its time zone rule in /etc/TZ; pick it up when the + * environment did not hand one over (timestamps in the OSD and file names). */ + if (!getenv("TZ")) { + char tz[64] = {0}; + FILE *tzf = fopen("/etc/TZ", "r"); + if (tzf) { + if (fgets(tz, sizeof(tz), tzf)) { + tz[strcspn(tz, "\r\n")] = 0; + if (*tz) setenv("TZ", tz, 1); + } + fclose(tzf); + } + } + tzset(); { struct sigaction sa; memset(&sa, 0, sizeof(sa)); @@ -87,6 +101,14 @@ int main(int argc, char *argv[]) { HAL_INFO("rtsp", "Authentication enabled!\n"); } } + if (app_config.sub_enable) { + rtspSubHandle = rtsp_create(RTSP_MAXIMUM_CONNECTIONS, app_config.sub_rtsp_port, 1); + if (rtspSubHandle && app_config.rtsp_enable_auth && + !EMPTY(app_config.rtsp_auth_user) && !EMPTY(app_config.rtsp_auth_pass)) + rtsp_configure_auth(rtspSubHandle, app_config.rtsp_auth_user, app_config.rtsp_auth_pass); + if (rtspSubHandle) + HAL_INFO("rtsp", "Sub-stream server started on port %d\n", app_config.sub_rtsp_port); + } } if (app_config.stream_enable) @@ -109,11 +131,13 @@ int main(int argc, char *argv[]) { while (keepRunning) { if (app_config.rtsp_enable) rtsp_tick(rtspHandle); + if (rtspSubHandle) rtsp_tick(rtspSubHandle); + record_tick(); watchdog_reset(); sleep(1); } - if (app_config.record_enable && app_config.record_continuous) + if (record_active()) record_stop(); if (app_config.http_post_enable) @@ -132,6 +156,7 @@ int main(int argc, char *argv[]) { if (app_config.rtsp_enable) { rtsp_finish(rtspHandle); + if (rtspSubHandle) rtsp_finish(rtspSubHandle); HAL_INFO("rtsp", "Server has closed!\n"); } diff --git a/src/media.c b/src/media.c index 87b1715e..28df14db 100644 --- a/src/media.c +++ b/src/media.c @@ -13,9 +13,69 @@ short pcmSrc[SHINE_MAX_SAMPLES]; /* Raw PCM queue between the capture callback and the MP3 encoder, so the * capture thread only ever copies a frame and the SDK never drops buffers * waiting on the encode. Newest frames are dropped when it is full. */ +static char subIndex = -1; +static time_t subSecond; +static unsigned int subCount, subRate; + +/* Frames the sub-stream encoder delivered in the last full second */ +int media_sub_rate(void) { + return (subIndex >= 0 && time(NULL) - subSecond <= 1) ? subRate : 0; +} static struct ringbuf audRing; static unsigned int audDrops; +/* Opus for the MP4 muxer: browsers decode it reliably, unlike the MP3 from the + * built-in encoder. libopus is optional and loaded at run time. */ +#include +#define MP4_ACODEC_OPUS 0xAD +#define OPUS_KBPS 32 +static void *opusEnc; +static int (*opus_encode_fn)(void*, const short*, int, unsigned char*, int); +static short opusPcm[960]; +static unsigned int opusPos, opusFrame; + +static char mp4_opus_ready(void) { + static char tried; + if (tried) return opusEnc != NULL; + tried = 1; + unsigned int srate = app_config.audio_srate; + if (srate != 8000 && srate != 12000 && srate != 16000 && srate != 24000 && srate != 48000) + return 0; + void *lib = dlopen("libopus.so.0", RTLD_NOW); + if (!lib) return 0; + void *(*create)(int, int, int, int*) = dlsym(lib, "opus_encoder_create"); + int (*ctl)(void*, int, ...) = dlsym(lib, "opus_encoder_ctl"); + opus_encode_fn = dlsym(lib, "opus_encode"); + int err = 0; + if (!create || !ctl || !opus_encode_fn || + !(opusEnc = create(srate, 1, 2048 /* OPUS_APPLICATION_VOIP */, &err)) || err) { + opusEnc = NULL; + return 0; + } + ctl(opusEnc, 4002 /* OPUS_SET_BITRATE */, OPUS_KBPS * 1000); + ctl(opusEnc, 4010 /* OPUS_SET_COMPLEXITY */, 3); + opusFrame = srate / 50; /* 20 ms */ + HAL_INFO("media", "MP4 audio: Opus %u Hz, %d kbps\n", srate, OPUS_KBPS); + return 1; +} + +static void mp4_opus_feed(const short *pcm, unsigned int samples) { + static unsigned char pkt[1500]; + while (samples > 0) { + unsigned int n = opusFrame - opusPos; + if (n > samples) n = samples; + memcpy(opusPcm + opusPos, pcm, n * 2); + opusPos += n; pcm += n; samples -= n; + if (opusPos < opusFrame) break; + opusPos = 0; + int len = opus_encode_fn(opusEnc, opusPcm, opusFrame, pkt, sizeof(pkt)); + if (len <= 2) continue; /* error or DTX */ + pthread_mutex_lock(&mp4Mtx); + mp4_ingest_audio_frame((char*)pkt, len, opusFrame); + pthread_mutex_unlock(&mp4Mtx); + } +} + int save_audio_stream(hal_audframe *frame) { #ifdef DEBUG_AUDIO printf("[audio] data:%p - %02x %02x %02x %02x %02x %02x %02x %02x\n", @@ -116,11 +176,16 @@ void *aenc_thread(void) { pthread_mutex_lock(&aencMtx); if (mp3Buf.offset >= mp3FrmSize) { send_mp3_to_client(mp3Buf.buf, mp3FrmSize); - pthread_mutex_lock(&mp4Mtx); - mp4_ingest_audio(mp3Buf.buf, mp3FrmSize); - pthread_mutex_unlock(&mp4Mtx); + if (!opusEnc) { + pthread_mutex_lock(&mp4Mtx); + mp4_ingest_audio(mp3Buf.buf, mp3FrmSize); + pthread_mutex_unlock(&mp4Mtx); + } if (app_config.rtsp_enable && !rtsp_g711_active()) + { rtp_send_mp3(rtspHandle, mp3Buf.buf, mp3FrmSize); + if (rtspSubHandle) rtp_send_mp3(rtspSubHandle, mp3Buf.buf, mp3FrmSize); + } rtmp_ingest_audio(mp3Buf.buf, mp3FrmSize); mp3Buf.offset -= mp3FrmSize; if (mp3Buf.offset > 0) @@ -148,6 +213,8 @@ void *aenc_thread(void) { send_pcm_to_client(&outFrame); if (app_config.rtsp_enable && rtsp_g711_active()) rtsp_pcma_feed((short *)(frame_buf + 2), flen / 2); + if (opusEnc && app_config.mp4_audio && (record_active() || any_http_audio())) + mp4_opus_feed((short *)(frame_buf + 2), flen / 2); if (!record_active() && !app_config.stream_enable && !any_http_audio() && !(app_config.rtsp_enable && !rtsp_g711_active())) { @@ -187,10 +254,22 @@ int save_video_stream(char index, hal_vidstream *stream) { { char isH265 = chnState[index].payload == HAL_VIDCODEC_H265 ? 1 : 0; + /* The sub-stream only goes to its own RTSP server */ + if (index == subIndex) { + time_t now = time(NULL); + if (now != subSecond) { + subRate = now - subSecond == 1 ? subCount : 0; + subCount = 0; + subSecond = now; + } + subCount++; + if (rtspSubHandle) rtp_send_h26x(rtspSubHandle, stream, isH265); + break; + } + if (app_config.mp4_enable) { pthread_mutex_lock(&mp4Mtx); send_mp4_to_client(index, stream, isH265); - if (record_active()) record_ingest_stream(stream, isH265); pthread_mutex_unlock(&mp4Mtx); send_h26x_to_client(index, stream); @@ -328,6 +407,7 @@ void request_idr(void) { } if (index != -1) switch (plat) { #if defined(__ARM_PCS_VFP) + case HAL_PLATFORM_I3: i3_video_request_idr(index); break; case HAL_PLATFORM_I6: i6_video_request_idr(index); break; case HAL_PLATFORM_I6C: i6c_video_request_idr(index); break; case HAL_PLATFORM_M6: m6_video_request_idr(index); break; @@ -356,6 +436,7 @@ void set_grayscale(bool active) { pthread_mutex_lock(&chnMtx); switch (plat) { #if defined(__ARM_PCS_VFP) + case HAL_PLATFORM_I3: i3_channel_grayscale(active); break; case HAL_PLATFORM_I6: i6_channel_grayscale(active); break; case HAL_PLATFORM_I6C: i6c_channel_grayscale(active); break; case HAL_PLATFORM_M6: m6_channel_grayscale(active); break; @@ -395,6 +476,7 @@ int take_next_free_channel(bool mainLoop) { int create_channel(char index, short width, short height, char framerate, char jpeg) { switch (plat) { #if defined(__ARM_PCS_VFP) + case HAL_PLATFORM_I3: return i3_channel_create(index, width, height, jpeg); case HAL_PLATFORM_I6: return i6_channel_create(index, width, height, jpeg); case HAL_PLATFORM_I6C: return i6c_channel_create(index, width, height, jpeg); case HAL_PLATFORM_M6: return m6_channel_create(index, width, height, jpeg); @@ -428,6 +510,7 @@ int create_channel(char index, short width, short height, char framerate, char j int bind_channel(char index, char framerate, char jpeg) { switch (plat) { #if defined(__ARM_PCS_VFP) + case HAL_PLATFORM_I3: return i3_channel_bind(index, framerate); case HAL_PLATFORM_I6: return i6_channel_bind(index, framerate); case HAL_PLATFORM_I6C: return i6c_channel_bind(index, framerate); case HAL_PLATFORM_M6: return m6_channel_bind(index, framerate); @@ -454,6 +537,7 @@ int bind_channel(char index, char framerate, char jpeg) { int unbind_channel(char index, char jpeg) { switch (plat) { #if defined(__ARM_PCS_VFP) + case HAL_PLATFORM_I3: return i3_channel_unbind(index); case HAL_PLATFORM_I6: return i6_channel_unbind(index); case HAL_PLATFORM_I6C: return i6c_channel_unbind(index); case HAL_PLATFORM_M6: return m6_channel_unbind(index); @@ -480,6 +564,7 @@ int unbind_channel(char index, char jpeg) { int media_video_disable(char index, char jpeg) { switch (plat) { #if defined(__ARM_PCS_VFP) + case HAL_PLATFORM_I3: return i3_video_destroy(index); case HAL_PLATFORM_I6: return i6_video_destroy(index); case HAL_PLATFORM_I6C: return i6c_video_destroy(index); case HAL_PLATFORM_M6: return m6_video_destroy(index); @@ -516,6 +601,7 @@ void media_audio_disable(void) { switch (plat) { #if defined(__ARM_PCS_VFP) + case HAL_PLATFORM_I3: i3_audio_deinit(); break; case HAL_PLATFORM_I6: i6_audio_deinit(); break; case HAL_PLATFORM_I6C: i6c_audio_deinit(); break; case HAL_PLATFORM_M6: m6_audio_deinit(); break; @@ -549,6 +635,7 @@ int media_audio_enable(void) { switch (plat) { #if defined(__ARM_PCS_VFP) + case HAL_PLATFORM_I3: ret = i3_audio_init(app_config.audio_srate, app_config.audio_gain); break; case HAL_PLATFORM_I6: ret = i6_audio_init(app_config.audio_srate, app_config.audio_gain); break; case HAL_PLATFORM_I6C: ret = i6c_audio_init(app_config.audio_srate, app_config.audio_gain); break; case HAL_PLATFORM_M6: ret = m6_audio_init(app_config.audio_srate, app_config.audio_gain); break; @@ -611,7 +698,7 @@ int media_audio_enable(void) { pthread_attr_init(&thread_attr); size_t stacksize; pthread_attr_getstacksize(&thread_attr, &stacksize); - size_t new_stacksize = 16384; + size_t new_stacksize = 256 * 1024; /* the Opus encoder needs far more than 16K */ if (pthread_attr_setstacksize(&thread_attr, new_stacksize)) HAL_DANGER("media", "Can't set stack size %zu\n", new_stacksize); if (pthread_create( @@ -649,6 +736,14 @@ int media_mjpeg_disable(void) { int media_mjpeg_enable(void) { int ret; + /* Infinity3 has one scaled output besides the main picture; the + * sub-stream and MJPEG cannot both have it. */ + if (plat == HAL_PLATFORM_I3 && subIndex >= 0) { + HAL_WARNING("media", "MJPEG is not available while the sub-stream is on\n"); + app_config.mjpeg_enable = false; + return EXIT_FAILURE; + } + int index = take_next_free_channel(true); if (ret = create_channel(index, app_config.mjpeg_width, @@ -662,6 +757,7 @@ int media_mjpeg_enable(void) { { hal_vidconfig config; + memset(&config, 0, sizeof(config)); config.width = app_config.mjpeg_width; config.height = app_config.mjpeg_height; config.codec = HAL_VIDCODEC_MJPG; @@ -673,6 +769,7 @@ int media_mjpeg_enable(void) { switch (plat) { #if defined(__ARM_PCS_VFP) + case HAL_PLATFORM_I3: ret = i3_video_create(index, &config); break; case HAL_PLATFORM_I6: ret = i6_video_create(index, &config); break; case HAL_PLATFORM_I6C: ret = i6c_video_create(index, &config); break; case HAL_PLATFORM_M6: ret = m6_video_create(index, &config); break; @@ -753,6 +850,7 @@ int media_mp4_enable(void) { { hal_vidconfig config; + memset(&config, 0, sizeof(config)); config.width = app_config.mp4_width; config.height = app_config.mp4_height; config.codec = app_config.mp4_codecH265 ? @@ -766,6 +864,7 @@ int media_mp4_enable(void) { switch (plat) { #if defined(__ARM_PCS_VFP) + case HAL_PLATFORM_I3: ret = i3_video_create(index, &config); break; case HAL_PLATFORM_I6: ret = i6_video_create(index, &config); break; case HAL_PLATFORM_I6C: ret = i6c_video_create(index, &config); break; case HAL_PLATFORM_M6: ret = m6_video_create(index, &config); break; @@ -798,8 +897,11 @@ int media_mp4_enable(void) { } mp4_set_config(app_config.mp4_width, app_config.mp4_height, app_config.mp4_fps, - app_config.audio_enable ? HAL_AUDCODEC_MP3 : HAL_AUDCODEC_UNSPEC, - app_config.audio_bitrate, 1, app_config.audio_srate); + !(app_config.audio_enable && app_config.mp4_audio) ? HAL_AUDCODEC_UNSPEC : + mp4_opus_ready() ? MP4_ACODEC_OPUS : HAL_AUDCODEC_MP3, + !(app_config.audio_enable && app_config.mp4_audio) ? 0 : + mp4_opus_ready() ? OPUS_KBPS : app_config.audio_bitrate, + 1, app_config.audio_srate); } if (ret = bind_channel(index, app_config.mp4_fps, 0)) @@ -814,12 +916,74 @@ int media_mp4_enable(void) { return EXIT_SUCCESS; } +/* Second, smaller H.264 stream (for grids, phones, motion analysis) served by + * its own RTSP instance. Only wired up for platforms that were tested. + * It runs at the main stream's frame rate: on Infinity3 the encoder's rate + * only steers the bitrate, no frames are dropped. */ +int media_sub_enable(void) { + int ret = EXIT_FAILURE; + if (plat != HAL_PLATFORM_I3) return EXIT_SUCCESS; + + int index = take_next_free_channel(true); + if (index < 0) return EXIT_FAILURE; + if (ret = create_channel(index, app_config.sub_width, app_config.sub_height, + app_config.mp4_fps, 0)) { + chnState[index].enable = false; + HAL_ERROR("media", "Creating sub channel %d failed with %#x!\n%s\n", + index, ret, errstr(ret)); + return ret; + } + + hal_vidconfig config; + memset(&config, 0, sizeof(config)); + config.width = app_config.sub_width; + config.height = app_config.sub_height; + config.codec = HAL_VIDCODEC_H264; + config.mode = HAL_VIDMODE_CBR; + config.profile = app_config.mp4_profile; + config.gop = app_config.sub_gop; + config.framerate = app_config.mp4_fps; + config.bitrate = app_config.sub_bitrate; + config.maxBitrate = app_config.sub_bitrate * 5 / 4; +#if defined(__ARM_PCS_VFP) + ret = i3_video_create(index, &config); +#endif + if (ret) { + media_video_disable(index, 0); + chnState[index].enable = false; + HAL_ERROR("media", "Creating sub encoder %d failed with %#x!\n%s\n", + index, ret, errstr(ret)); + return ret; + } + if (ret = bind_channel(index, app_config.mp4_fps, 0)) { + media_video_disable(index, 0); + chnState[index].enable = false; + HAL_ERROR("media", "Binding sub channel %d failed with %#x!\n%s\n", + index, ret, errstr(ret)); + return ret; + } + subIndex = index; + return EXIT_SUCCESS; +} + int sdk_start(void) { int ret; switch (plat) { #if defined(__ARM_PCS_VFP) - case HAL_PLATFORM_I3: ret = i3_hal_init(); break; + case HAL_PLATFORM_I3: { + /* The scaler output for the sub-stream takes its size from the + * vendor property store, not from the channel attributes */ + char num[12]; + if (app_config.sub_enable) { + sprintf(num, "%u", app_config.sub_width); + i3_property_set("mi.vi.sub.width", num); + sprintf(num, "%u", app_config.sub_height); + i3_property_set("mi.vi.sub.height", num); + } + ret = i3_hal_init(); + break; + } case HAL_PLATFORM_I6: ret = i6_hal_init(); break; case HAL_PLATFORM_I6C: ret = i6c_hal_init(); break; case HAL_PLATFORM_M6: ret = m6_hal_init(); break; @@ -847,6 +1011,10 @@ int sdk_start(void) { switch (plat) { #if defined(__ARM_PCS_VFP) + case HAL_PLATFORM_I3: + i3_aud_cb = save_audio_stream; + i3_vid_cb = save_video_stream; + break; case HAL_PLATFORM_I6: i6_aud_cb = save_audio_stream; i6_vid_cb = save_video_stream; @@ -952,6 +1120,8 @@ int sdk_start(void) { switch (plat) { #if defined(__ARM_PCS_VFP) + case HAL_PLATFORM_I3: ret = i3_pipeline_create(0, width, + height, app_config.mirror, app_config.flip, framerate); break; case HAL_PLATFORM_I6: ret = i6_pipeline_create(0, width, height, app_config.mirror, app_config.flip, framerate); break; case HAL_PLATFORM_I6C: ret = i6c_pipeline_create(0, width, @@ -1010,6 +1180,10 @@ int sdk_start(void) { if (app_config.mp4_enable && (ret = media_mp4_enable())) HAL_ERROR("media", "MP4 initialization failed with %#x!\n", ret); + if (app_config.sub_enable && app_config.mp4_enable && app_config.rtsp_enable && + (ret = media_sub_enable())) + HAL_WARNING("media", "Sub-stream is not available\n"); + if (app_config.mjpeg_enable && (ret = media_mjpeg_enable())) HAL_ERROR("media", "MJPEG initialization failed with %#x!\n", ret); @@ -1065,6 +1239,7 @@ int sdk_stop(void) { switch (plat) { #if defined(__ARM_PCS_VFP) + case HAL_PLATFORM_I3: i3_video_destroy_all(); break; case HAL_PLATFORM_I6: i6_video_destroy_all(); break; case HAL_PLATFORM_I6C: i6c_video_destroy_all(); break; case HAL_PLATFORM_M6: m6_video_destroy_all(); break; @@ -1089,6 +1264,7 @@ int sdk_stop(void) { switch (plat) { #if defined(__ARM_PCS_VFP) + case HAL_PLATFORM_I3: i3_pipeline_destroy(); break; case HAL_PLATFORM_I6: i6_pipeline_destroy(); break; case HAL_PLATFORM_I6C: i6c_pipeline_destroy(); break; case HAL_PLATFORM_M6: m6_pipeline_destroy(); break; diff --git a/src/media.h b/src/media.h index f7172e4b..2994e52f 100644 --- a/src/media.h +++ b/src/media.h @@ -24,7 +24,7 @@ #include "server.h" #include "stream.h" -extern rtsp_handle rtspHandle; +extern rtsp_handle rtspHandle, rtspSubHandle; int sdk_start(void); int sdk_stop(void); @@ -49,3 +49,4 @@ int media_mp4_disable(void); int media_mp4_enable(void); void rtsp_latch_audio_codec(void); +int media_sub_rate(void); diff --git a/src/night.c b/src/night.c index 86e59626..bafdea89 100644 --- a/src/night.c +++ b/src/night.c @@ -1,7 +1,7 @@ #include "night.h" char nightOn = 0; -static bool grayscale = false, ircut = true, irled = false, manual = false; +static bool grayscale = false, ircut = true, irled = false, manual = false, whiteled = false; pthread_t nightPid = 0; bool night_grayscale_on(void) { return grayscale; } @@ -19,9 +19,13 @@ void night_grayscale(bool enable) { grayscale = enable; } +// 999 is the "not configured" default; it must never reach gpio_write(), whose +// pin argument is a char. +#define PIN_SET(pin) ((pin) >= 0 && (pin) <= 127) + void night_ircut(bool enable) { - bool has1 = app_config.ir_cut_pin1 != 999; - bool has2 = app_config.ir_cut_pin2 != 999; + bool has1 = PIN_SET(app_config.ir_cut_pin1); + bool has2 = PIN_SET(app_config.ir_cut_pin2); if (has1) gpio_write(app_config.ir_cut_pin1, !enable); if (has2) gpio_write(app_config.ir_cut_pin2, enable); @@ -34,11 +38,20 @@ void night_ircut(bool enable) { } void night_irled(bool enable) { - if (app_config.ir_led_pin != 999) + if (PIN_SET(app_config.ir_led_pin)) gpio_write(app_config.ir_led_pin, enable); irled = enable; } +/* Visible-light lamp (spotlight); independent of the day/night state */ +bool night_whiteled_on(void) { return whiteled; } + +void night_whiteled(bool enable) { + if (PIN_SET(app_config.white_led_pin)) + gpio_write(app_config.white_led_pin, enable); + whiteled = enable; +} + static void night_lamp(bool enable) { #if defined(__arm__) && !defined(__ARM_PCS_VFP) && __ARM_ARCH == 6 if (plat == HAL_PLATFORM_F1) @@ -48,6 +61,18 @@ static void night_lamp(bool enable) { void night_manual(bool enable) { manual = enable; } +/* Auto-exposure state of the image pipeline, where the platform exposes it */ +int night_exposure(unsigned int *sensorGain, unsigned int *ispGain, + unsigned int *shutterUs, unsigned int *avgY, int *bv) { + switch (plat) { +#if defined(__ARM_PCS_VFP) + case HAL_PLATFORM_I3: + return i3_exposure_info(sensorGain, ispGain, shutterUs, avgY, bv); +#endif + default: return EXIT_FAILURE; + } +} + void night_mode(bool enable) { HAL_INFO("night", "Changing mode to %s\n", enable ? "NIGHT" : "DAY"); #if defined(__arm__) && !defined(__ARM_PCS_VFP) && __ARM_ARCH == 6 @@ -120,15 +145,71 @@ void *night_thread(void) { usleep(app_config.check_interval_s * 1000000 / 12); } if (adc_fd) close(adc_fd); + } else if (app_config.ir_sensor_pin == 999 && app_config.exposure_night && + !night_exposure(NULL, NULL, NULL, NULL, NULL)) { + // No light sensor: decide from how hard the auto-exposure has to work. + // Exposure index = sensor gain x ISP gain x shutter (relative to 40 ms). + // raw = the same in 1/1024 steps, for comparing readings in night mode. + unsigned int sg, ig, us, luma, over = 0, drop = 0, dwell = 0, wait = 120, base = 0; + while (keepRunning && nightOn) { + for (int i = 0; i < app_config.check_interval_s * 5 && keepRunning && nightOn; i++) + usleep(200000); + dwell++; + if (manual || night_exposure(&sg, &ig, &us, &luma, NULL)) continue; + unsigned int raw = (unsigned int)((unsigned long long)sg * ig / 1024 * us / 40000); + unsigned int idx = raw / 1024; + if (!night_mode_on()) { + // Night: the exposure is (nearly) maxed out and the picture is + // still well under the auto-exposure target (about 40). + over = (idx >= app_config.exposure_night && luma < 20) ? over + 1 : 0; + // Two readings in a row and at least six intervals in day mode + if (over >= 2 && dwell >= 6) { + night_mode(true); + over = drop = dwell = base = 0; wait = 120; + } + continue; + } + // Night, IR lamps on: they light the scene, so the exposure says + // nothing about daylight by itself. Remember the darkest it settles + // at and look for it dropping (more light than the lamps give), or + // the picture getting brighter than the auto-exposure aims for. + // Each probe blanks the picture for a moment, so the timed ones + // start after ten minutes and back off to about once an hour. + if (app_config.exposure_day && idx > app_config.exposure_day) { + drop = 0; base = 0; continue; + } + if (dwell >= 2 && raw > base) base = raw; + drop = (raw * 10 < base * 8 || luma >= 80) ? drop + 1 : 0; + if (dwell < 3 || (drop < 2 && dwell < wait)) continue; + // Switch the lamps off for a moment and see whether there is light + // without them. + night_irled(false); + for (int i = 0; i < 12 && keepRunning && nightOn; i++) usleep(200000); + bool lit = !night_exposure(&sg, &ig, &us, &luma, NULL) && (luma >= 30 || + (unsigned int)((unsigned long long)sg * ig / 1024 * us / 40000 / 1024) + < app_config.exposure_night * 2 / 3); + // Interrupted (manual mode, settings change): the lamps go back on + // as they were before the probe. + if (manual || !nightOn || !keepRunning) { night_irled(true); continue; } + if (lit) night_mode(false); + else { + // Still dark: lamps back on, wait longer before the next timed probe + night_irled(true); + if (drop < 2 && wait < 720) wait = wait * 2 > 720 ? 720 : wait * 2; + } + over = drop = dwell = base = 0; + } } else if (app_config.ir_sensor_pin == 999) { - while (keepRunning && nightOn) sleep(1); + // night_disable() joins this thread, so it has to notice nightOn clearing. + while (keepRunning && nightOn) usleep(200000); } else { - while (keepRunning) { + while (keepRunning && nightOn) { bool state = false; if (!gpio_read(app_config.ir_sensor_pin, &state)) if (!manual) night_mode(state); - sleep(app_config.check_interval_s); + for (int i = 0; i < app_config.check_interval_s * 5 && keepRunning && nightOn; i++) + usleep(200000); } } @@ -150,6 +231,7 @@ int night_enable(void) { size_t new_stacksize = 16 * 1024; if (pthread_attr_setstacksize(&thread_attr, new_stacksize)) HAL_DANGER("night", "Error: Can't set stack size %zu\n", new_stacksize); + nightOn = 1; pthread_create(&nightPid, &thread_attr, (void *(*)(void *))night_thread, NULL); if (pthread_attr_setstacksize(&thread_attr, stacksize)) HAL_DANGER("night", "Error: Can't set stack size %zu\n", stacksize); diff --git a/src/night.h b/src/night.h index ace2fd6d..b4b2c63e 100644 --- a/src/night.h +++ b/src/night.h @@ -22,7 +22,11 @@ bool night_mode_on(void); void night_grayscale(bool enable); void night_ircut(bool enable); void night_irled(bool enable); +bool night_whiteled_on(void); +void night_whiteled(bool enable); void night_manual(bool enable); +int night_exposure(unsigned int *sensorGain, unsigned int *ispGain, + unsigned int *shutterUs, unsigned int *avgY, int *bv); void night_mode(bool enable); void night_disable(void); diff --git a/src/record.c b/src/record.c index 4d6add68..ecefe785 100644 --- a/src/record.c +++ b/src/record.c @@ -3,6 +3,10 @@ /* Guards the recording state: the encoder thread feeds fragments while the * server thread starts and stops sessions. */ static pthread_mutex_t recordMtx = PTHREAD_MUTEX_INITIALIZER; +/* Serializes starting and stopping with their storage remounts, which run + * outside recordMtx */ +static pthread_mutex_t recordModeMtx = PTHREAD_MUTEX_INITIALIZER; +static bool recordStorageRw; static FILE *recordFile; static struct Mp4State recordState; @@ -10,7 +14,137 @@ static unsigned long long recordSize; static time_t recordStartTime; static time_t recordLastFlush; static bool recordActive; +static bool recordWaitKey; +static time_t recordLastSync, recordLastRoom, recordLastTry; static char recordPath[RECORD_PATH_MAX]; +/* Monotonic second at which a timed recording ends, 0 for none */ +static time_t recordStopAt; + +#define RECORD_CARD_ROOT "/mnt/sdcard" +#define RECORD_CARD_TOOL "/usr/sbin/sdcard" + +static time_t record_now(void) { + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return ts.tv_sec; +} + +/* The card is kept read-only unless a recording is running; the helper + * (re)mounts it in the requested mode. Must not be called with recordMtx held. */ +static void record_storage_mode(const char *mode) { + char cmd[96]; + + if (strncmp(app_config.record_path, RECORD_CARD_ROOT, strlen(RECORD_CARD_ROOT))) return; + if (access(RECORD_CARD_TOOL, X_OK)) return; + snprintf(cmd, sizeof(cmd), RECORD_CARD_TOOL " mount %s >/dev/null 2>&1", mode); + if (system(cmd)) HAL_WARNING("record", "Storage could not be switched to '%s'\n", mode); + recordStorageRw = !strcmp(mode, "rw"); +} + +/* Recordings must land on mounted storage, never on the root filesystem: + * with no card in the slot the path would be a plain directory in flash. */ +static bool record_on_storage(struct statvfs *vfs) { + char tmp[RECORD_PATH_MAX]; + struct stat st, root; + size_t len = strlen(app_config.record_path); + + if (len == 0 || len >= sizeof(tmp)) return false; + memcpy(tmp, app_config.record_path, len + 1); + while (stat(tmp, &st)) { + char *sep = strrchr(tmp, '/'); + if (!sep || sep == tmp) return false; + *sep = '\0'; + } + if (stat("/", &root) || st.st_dev == root.st_dev) return false; + if (vfs && statvfs(tmp, vfs)) return false; + return true; +} + +/* Splits the configured file name into "" and ".". */ +static void record_name_parts(char *stem, size_t stemSize, char *ext, size_t extSize) { + strncpy(ext, ".mp4", extSize - 1); + ext[extSize - 1] = '\0'; + stem[0] = '\0'; + + if (!EMPTY(app_config.record_filename)) { + char *dot; + const char *name = strrchr(app_config.record_filename, '/'); + name = name ? name + 1 : app_config.record_filename; + + strncpy(stem, name, stemSize - 1); + stem[stemSize - 1] = '\0'; + + dot = strrchr(stem, '.'); + if (dot && dot != stem) { + strncpy(ext, dot, extSize - 1); + ext[extSize - 1] = '\0'; + *dot = '\0'; + } + } + if (EMPTY(stem)) strncpy(stem, "recording", stemSize - 1); +} + +/* Whether a name is "_YYYYMMDD-HHMMSS[-N]", as record_build_path + * makes them. */ +static bool record_own_name(const char *name, const char *stem, size_t stemLen, + const char *ext, size_t extLen) { + size_t len = strlen(name); + const char *p = name + stemLen + 1, *end = name + len - extLen; + + if (len < stemLen + 1 + 15 + extLen || strncmp(name, stem, stemLen) || + name[stemLen] != '_' || strcmp(end, ext)) return false; + for (int i = 0; i < 15; i++, p++) + if (i == 8 ? *p != '-' : (*p < '0' || *p > '9')) return false; + if (p == end) return true; + if (*p++ != '-' || p == end) return false; + for (; p < end; p++) + if (*p < '0' || *p > '9') return false; + return true; +} + +/* Loop recording: while free space is below the reserve, removes the oldest + * segment. Only files this recorder named itself are ever touched. Runs + * without recordMtx, so the encoder thread never waits for the card. */ +static void record_make_room(const char *current) { + char stem[sizeof(app_config.record_filename)], ext[16]; + size_t stemLen, extLen; + + record_name_parts(stem, sizeof(stem), ext, sizeof(ext)); + stemLen = strlen(stem); + extLen = strlen(ext); + + for (int pass = 0; pass < 256; pass++) { + struct statvfs vfs; + struct dirent *ent; + char oldest[256] = "", path[RECORD_PATH_MAX]; + DIR *dir; + + if (statvfs(app_config.record_path, &vfs)) return; + if ((unsigned long long)vfs.f_bavail * vfs.f_frsize >= + (unsigned long long)app_config.record_reserve << 20) return; + + if (!(dir = opendir(app_config.record_path))) return; + while ((ent = readdir(dir))) { + if (strlen(ent->d_name) >= sizeof(oldest)) continue; + if (!record_own_name(ent->d_name, stem, stemLen, ext, extLen)) continue; + if (!strcmp(ent->d_name, current)) continue; + /* Names carry the start time, so the smallest one is the oldest */ + if (EMPTY(oldest) || strcmp(ent->d_name, oldest) < 0) strcpy(oldest, ent->d_name); + } + closedir(dir); + + if (EMPTY(oldest)) { + HAL_WARNING("record", "Storage is low and there is no old segment left to remove\n"); + return; + } + snprintf(path, sizeof(path), "%s/%s", app_config.record_path, oldest); + if (unlink(path)) { + HAL_DANGER("record", "Cannot remove '%s': %s\n", path, strerror(errno)); + return; + } + HAL_INFO("record", "Removed oldest segment '%s' to free space\n", path); + } +} static bool record_ensure_dir(const char *path) { char tmp[RECORD_PATH_MAX]; @@ -41,7 +175,7 @@ static bool record_ensure_dir(const char *path) { static bool record_build_path(char *out, size_t outSize) { char dir[sizeof(app_config.record_path)]; char stem[sizeof(app_config.record_filename)]; - char ext[16] = ".mp4"; + char ext[16]; char stamp[32]; struct tm tm_buf, *tm_info; time_t now = recordStartTime ? recordStartTime : time(NULL); @@ -53,24 +187,7 @@ static bool record_build_path(char *out, size_t outSize) { return false; } - if (!EMPTY(app_config.record_filename)) { - char *dot; - const char *name = strrchr(app_config.record_filename, '/'); - name = name ? name + 1 : app_config.record_filename; - - strncpy(stem, name, sizeof(stem) - 1); - stem[sizeof(stem) - 1] = '\0'; - - dot = strrchr(stem, '.'); - if (dot && dot != stem) { - strncpy(ext, dot, sizeof(ext) - 1); - ext[sizeof(ext) - 1] = '\0'; - *dot = '\0'; - } - } else - strncpy(stem, "recording", sizeof(stem) - 1); - - if (EMPTY(stem)) strncpy(stem, "recording", sizeof(stem) - 1); + record_name_parts(stem, sizeof(stem), ext, sizeof(ext)); tm_info = localtime_r(&now, &tm_buf); if (!tm_info || !strftime(stamp, sizeof(stamp), "%Y%m%d-%H%M%S", tm_info)) @@ -110,6 +227,10 @@ static bool record_open_locked(void) { HAL_DANGER("record", "Destination path is not set!\n"); return false; } + if (!record_on_storage(NULL)) { + HAL_DANGER("record", "No storage mounted for '%s'\n", app_config.record_path); + return false; + } if (!record_ensure_dir(app_config.record_path)) return false; if (!record_build_path(recordPath, sizeof(recordPath))) return false; @@ -124,9 +245,11 @@ static bool record_open_locked(void) { recordState.sequence_number = 0; recordState.base_data_offset = 0; recordState.base_media_decode_time = 0; + recordState.audio_media_decode_time = 0; recordState.nals_count = 0; recordState.default_sample_duration = default_sample_size; - recordLastFlush = recordStartTime; + recordLastFlush = recordLastSync = recordLastRoom = recordStartTime; + recordWaitKey = true; HAL_INFO("record", "Recording to '%s'\n", recordPath); return true; @@ -146,49 +269,6 @@ static bool record_write_locked(const void *data, size_t len) { return true; } -/* Guards against NALU offsets and lengths that would read past the pack or - * underflow "length - scLen". */ -static void record_feed_nalu(unsigned char *data, unsigned int len, - const hal_vidnalu *nalu, char isH265) { - unsigned int offset = nalu->offset; - unsigned int nalu_len = nalu->length; - unsigned int scLen; - const char *payload; - unsigned int payload_len; - - if (offset > len || nalu_len > len - offset) return; - if (nalu_len <= 3 || offset + 3 > len) return; - - scLen = (data[offset + 2] == 1) ? 3 : 4; - if (nalu_len <= scLen) return; - - payload = (const char *)data + offset + scLen; - payload_len = nalu_len - scLen; - - switch (nalu->type) { - case NalUnitType_SPS: - case NalUnitType_SPS_HEVC: - if (payload_len <= UINT16_MAX) mp4_set_sps(payload, payload_len, isH265); - break; - case NalUnitType_PPS: - case NalUnitType_PPS_HEVC: - if (payload_len <= UINT16_MAX) mp4_set_pps(payload, payload_len, isH265); - break; - case NalUnitType_VPS_HEVC: - if (payload_len <= UINT16_MAX) mp4_set_vps(payload, payload_len); - break; - case NalUnitType_CodedSliceIdr: - case NalUnitType_CodedSliceAux: - mp4_set_slice(payload, payload_len, 1); - break; - case NalUnitType_CodedSliceNonIdr: - mp4_set_slice(payload, payload_len, 0); - break; - default: - break; - } -} - static bool record_emit_fragment_locked(void) { struct BitBuf buf; @@ -211,6 +291,7 @@ static bool record_emit_fragment_locked(void) { recordState.sequence_number = 0; recordState.base_data_offset = buf.offset; recordState.base_media_decode_time = 0; + recordState.audio_media_decode_time = 0; recordState.nals_count = 0; recordState.default_sample_duration = default_sample_size; recordState.header_sent = true; @@ -270,6 +351,12 @@ static void record_flush_locked(void) { if (fflush(recordFile)) HAL_DANGER("record", "Flushing '%s' failed: %s\n", recordPath, strerror(errno)); + + /* Keep what a power cut can lose down to a few seconds */ + if (now - recordLastSync >= 10) { + recordLastSync = now; + fdatasync(fileno(recordFile)); + } } /** @@ -277,37 +364,131 @@ static void record_flush_locked(void) { * @return true once recording, false if the output could not be opened */ bool record_start(void) { - pthread_mutex_lock(&recordMtx); + bool ok = true; - if (recordActive) { - pthread_mutex_unlock(&recordMtx); - return true; - } + if (record_active()) return true; - recordStartTime = time(NULL); - if (recordStartTime == (time_t)-1) recordStartTime = 0; + pthread_mutex_lock(&recordModeMtx); + record_storage_mode("rw"); + if (record_on_storage(NULL)) record_make_room(""); - if (!record_open_locked()) { - pthread_mutex_unlock(&recordMtx); - return false; - } + pthread_mutex_lock(&recordMtx); + recordLastTry = time(NULL); - recordActive = true; + if (!recordActive) { + recordStartTime = time(NULL); + if (recordStartTime == (time_t)-1) recordStartTime = 0; + if (record_open_locked()) recordActive = true; + else ok = false; + } pthread_mutex_unlock(&recordMtx); - return true; + + /* Nothing is being written: the card goes back to read-only */ + if (!ok) record_storage_mode("ro"); + pthread_mutex_unlock(&recordModeMtx); + return ok; } /** * Stops the current recording session and finalizes its file */ void record_stop(void) { + pthread_mutex_lock(&recordModeMtx); pthread_mutex_lock(&recordMtx); record_close_locked(); recordActive = false; recordStartTime = 0; + recordStopAt = 0; + + pthread_mutex_unlock(&recordMtx); + + record_storage_mode("ro"); + pthread_mutex_unlock(&recordModeMtx); +} + +/** + * Ends the current recording after the given number of seconds; calling it + * again moves the end, 0 cancels it. Continuous recording ignores it. + */ +void record_stop_after(unsigned int seconds) { + pthread_mutex_lock(&recordMtx); + recordStopAt = seconds ? record_now() + seconds : 0; + pthread_mutex_unlock(&recordMtx); +} + +/** + * Seconds left of a timed recording, 0 when none is set + */ +unsigned int record_stop_in(void) { + time_t left; + + pthread_mutex_lock(&recordMtx); + left = recordStopAt ? recordStopAt - record_now() : 0; + pthread_mutex_unlock(&recordMtx); + return left > 0 ? left : 0; +} + +/** + * Keeps continuous recording alive: retries when the card was missing at + * start or a write failed (card pulled). Called once a second. + */ +void record_tick(void) { + time_t now = time(NULL), stopAt; + bool active = record_active(); + + /* A write or rotation failure on the encoder thread ends the recording + * there; the card goes back to read-only from here. */ + if (!active && recordStorageRw) { + pthread_mutex_lock(&recordModeMtx); + if (!record_active() && recordStorageRw) record_storage_mode("ro"); + pthread_mutex_unlock(&recordModeMtx); + } + + /* Free space is checked here rather than on the encoder thread */ + if (active && now - recordLastRoom >= 30) { + char current[RECORD_PATH_MAX] = ""; + const char *name; + recordLastRoom = now; + pthread_mutex_lock(&recordMtx); + name = strrchr(recordPath, '/'); + if (recordFile && name) strncpy(current, name + 1, sizeof(current) - 1); + pthread_mutex_unlock(&recordMtx); + record_make_room(current); + } + pthread_mutex_lock(&recordMtx); + stopAt = recordStopAt; pthread_mutex_unlock(&recordMtx); + if (stopAt && record_now() >= stopAt) { + if (app_config.record_continuous) { + pthread_mutex_lock(&recordMtx); + recordStopAt = 0; + pthread_mutex_unlock(&recordMtx); + } else record_stop(); + } + + if (!app_config.record_enable || !app_config.record_continuous) return; + if (record_active()) return; + if (now - recordLastTry < 15) return; + record_start(); +} + +/** + * Describes the storage behind the recording path + * @param out Receives a short human readable summary + */ +void record_storage(char *out, size_t outSize) { + struct statvfs vfs; + + if (!record_on_storage(&vfs)) { + snprintf(out, outSize, "no card mounted"); + return; + } + snprintf(out, outSize, "%.1f GB, %.1f GB free, %s", + (double)vfs.f_blocks * vfs.f_frsize / 1e9, + (double)vfs.f_bavail * vfs.f_frsize / 1e9, + (vfs.f_flag & ST_RDONLY) ? "read-only" : "writable"); } /** @@ -339,48 +520,40 @@ time_t record_start_time(void) { } /** - * Ingests a video stream into the current recording - * @param stream Pointer to the hal_vidstream carrying the NALU packs - * @param isH265 1 if H.265 (HEVC), 0 for H.264 (AVC) + * Appends the fragment the muxer has just produced to the recording. + * Called with the muxer lock held, right after mp4_set_slice(). + * @param isKey Whether the fragment starts with a keyframe */ -void record_ingest_stream(hal_vidstream *stream, char isH265) { - if (!stream) return; - +void record_fragment(char isKey) { pthread_mutex_lock(&recordMtx); if (!recordActive || !recordFile) { pthread_mutex_unlock(&recordMtx); return; } - for (unsigned int i = 0; i < stream->count; ++i) { - hal_vidpack *pack = &stream->pack[i]; - unsigned char *pack_data; - unsigned int pack_len; - - if (pack->length <= pack->offset) continue; - pack_len = pack->length - pack->offset; - pack_data = pack->data + pack->offset; - - for (int j = 0; j < pack->naluCnt; j++) - record_feed_nalu(pack_data, pack_len, &pack->nalu[j], isH265); - - if (!record_emit_fragment_locked()) { - HAL_DANGER("record", "Stopping recording after a write failure!\n"); - record_close_locked(); - recordActive = false; - recordStartTime = 0; + /* Segments are cut on keyframes so each file plays from its first frame. */ + if (isKey && record_should_rotate_locked() && !record_rotate_locked()) { + recordActive = false; + recordStartTime = 0; + pthread_mutex_unlock(&recordMtx); + return; + } + if (recordWaitKey) { + if (!isKey) { pthread_mutex_unlock(&recordMtx); return; } + recordWaitKey = false; + } - /* Rotate between fragments so a segment never splits a moof from its - * mdat, and the next one starts with a fresh header. */ - if (record_should_rotate_locked() && !record_rotate_locked()) { - recordActive = false; - recordStartTime = 0; - pthread_mutex_unlock(&recordMtx); - return; - } + if (!record_emit_fragment_locked()) { + HAL_DANGER("record", "Stopping recording after a write failure!\n"); + record_close_locked(); + recordActive = false; + recordStartTime = 0; + recordLastTry = time(NULL); + pthread_mutex_unlock(&recordMtx); + return; } record_flush_locked(); diff --git a/src/record.h b/src/record.h index 16f198cb..99a05ff2 100644 --- a/src/record.h +++ b/src/record.h @@ -2,13 +2,16 @@ #define RECORD_PATH_MAX 512 +#include #include #include #include #include #include +#include #include #include +#include #include #include @@ -19,6 +22,10 @@ bool record_start(void); void record_stop(void); +void record_stop_after(unsigned int seconds); +unsigned int record_stop_in(void); bool record_active(void); time_t record_start_time(void); -void record_ingest_stream(hal_vidstream *stream, char isH265); +void record_tick(void); +void record_storage(char *out, size_t outSize); +void record_fragment(char isKey); diff --git a/src/region.c b/src/region.c index b6e7ca17..6f5960eb 100644 --- a/src/region.c +++ b/src/region.c @@ -17,6 +17,32 @@ void region_defaults(osd *o, int id) { } pthread_t regionPid = 0; char timefmt[64]; + +/* Where a region sits in the frame, row by row; posx/posy then count inward + * from that corner or edge (and shift a centred region). */ +const char *osd_anchors[9] = { "top-left", "top", "top-right", "left", "center", "right", + "bottom-left", "bottom", "bottom-right" }; + +char region_anchor_parse(const char *name) { + for (char i = 0; i < 9; i++) + if (!strcasecmp(name, osd_anchors[i])) return i; + return 0; +} + +void region_anchor_place(char id, unsigned short width, unsigned short height, hal_rect *rect) { + int fw = app_config.mp4_width, fh = app_config.mp4_height; + int col = osds[id].anchor % 3, row = osds[id].anchor / 3; + int x = col == 0 ? osds[id].posx : col == 1 ? (fw - width) / 2 + osds[id].posx : + fw - width - osds[id].posx; + int y = row == 0 ? osds[id].posy : row == 1 ? (fh - height) / 2 + osds[id].posy : + fh - height - osds[id].posy; + if (x > fw - width) x = fw - width; + if (y > fh - height) y = fh - height; + rect->x = x < 0 ? 0 : x; + rect->y = y < 0 ? 0 : y; + rect->width = width; + rect->height = height; +} unsigned int rxb_l, txb_l, cpu_l[6]; void region_fill_formatted(char* str) { @@ -343,6 +369,7 @@ int region_prepare_bitmap(char *path, hal_bitmap *bitmap) { void *region_thread(void) { switch (plat) { #if defined(__ARM_PCS_VFP) + case HAL_PLATFORM_I3: i3_region_init(); break; case HAL_PLATFORM_I6: i6_region_init(); break; case HAL_PLATFORM_I6C: i6c_region_init(); break; case HAL_PLATFORM_M6: m6_region_init(); break; @@ -390,10 +417,14 @@ void *region_thread(void) { found_font:; hal_bitmap bitmap = text_create_rendered(font, osds[id].size, out, osds[id].color, osds[id].outl, osds[id].thick); - hal_rect rect = { .height = bitmap.dim.height, .width = bitmap.dim.width, - .x = osds[id].posx, .y = osds[id].posy }; + hal_rect rect; + region_anchor_place(id, bitmap.dim.width, bitmap.dim.height, &rect); switch (plat) { #if defined(__ARM_PCS_VFP) + case HAL_PLATFORM_I3: + i3_region_create(id, rect, osds[id].opal); + i3_region_setbitmap(id, &bitmap); + break; case HAL_PLATFORM_I6: i6_region_create(id, rect, osds[id].opal); i6_region_setbitmap(id, &bitmap); @@ -476,10 +507,14 @@ found_font:; ret = region_prepare_bitmap(img, &bitmap); if (!ret) { - hal_rect rect = { .height = bitmap.dim.height, .width = bitmap.dim.width, - .x = osds[id].posx, .y = osds[id].posy }; + hal_rect rect; + region_anchor_place(id, bitmap.dim.width, bitmap.dim.height, &rect); switch (plat) { #if defined(__ARM_PCS_VFP) + case HAL_PLATFORM_I3: + i3_region_create(id, rect, osds[id].opal); + i3_region_setbitmap(id, &bitmap); + break; case HAL_PLATFORM_I6: i6_region_create(id, rect, osds[id].opal); i6_region_setbitmap(id, &bitmap); @@ -549,6 +584,7 @@ found_font:; else switch (plat) { #if defined(__ARM_PCS_VFP) + case HAL_PLATFORM_I3: i3_region_destroy(id); break; case HAL_PLATFORM_I6: i6_region_destroy(id); break; case HAL_PLATFORM_I6C: i6c_region_destroy(id); break; case HAL_PLATFORM_M6: m6_region_destroy(id); break; @@ -578,6 +614,7 @@ found_font:; switch (plat) { #if defined(__ARM_PCS_VFP) + case HAL_PLATFORM_I3: i3_region_deinit(); break; case HAL_PLATFORM_I6: i6_region_deinit(); break; case HAL_PLATFORM_I6C: i6c_region_deinit(); break; case HAL_PLATFORM_M6: m6_region_deinit(); break; diff --git a/src/region.h b/src/region.h index 9e57df9f..49bb3de2 100644 --- a/src/region.h +++ b/src/region.h @@ -63,6 +63,7 @@ typedef struct { int hand, color; short opal, posx, posy; char updt; + char anchor; /* index into osd_anchors[]; posx/posy are the offsets from it */ char font[32]; char text[80]; char img[64]; @@ -72,6 +73,9 @@ typedef struct { extern osd osds[MAX_OSD]; extern char timefmt[64]; +extern const char *osd_anchors[9]; +char region_anchor_parse(const char *name); +void region_anchor_place(char id, unsigned short width, unsigned short height, hal_rect *rect); void region_defaults(osd *o, int id); int region_start(); diff --git a/src/rtsp/rtsp.c b/src/rtsp/rtsp.c index e26a3b8d..e6af631c 100644 --- a/src/rtsp/rtsp.c +++ b/src/rtsp/rtsp.c @@ -11,6 +11,7 @@ #include "rtsp.h" #include "list.h" #include "hash.h" +#include "../sysauth.h" #include "mime.h" #include "thread.h" #include "rfc.h" @@ -76,54 +77,56 @@ static int __message_proc_sock(struct list_t *e, void *p); static inline int __set_select_sock(struct list_t *p, void *param); static inline int __find_fd_max(struct list_head_t *head); -static inline bufpool_handle __connectionpool_create(int num); +static inline bufpool_handle __connectionpool_create(int num, int inst); static int __connection_is_dead(struct list_t *l); /****************************************************************************** * PRIVATE DATA ******************************************************************************/ -static struct connection_item_t __connection_pool[RTSP_MAXIMUM_CONNECTIONS] = {}; +/* One set of pools per server instance (main stream and sub-stream) */ +#define RTSP_INSTANCES 2 +static struct connection_item_t __connection_pool[RTSP_INSTANCES][RTSP_MAXIMUM_CONNECTIONS] = {}; -static struct transfer_item_t __transfer_pool[RTSP_MAXIMUM_CONNECTIONS] = {}; +static struct transfer_item_t __transfer_pool[RTSP_INSTANCES][RTSP_MAXIMUM_CONNECTIONS] = {}; + +static int __rtsp_instances; /****************************************************************************** * PRIVATE FUNCTIONS ******************************************************************************/ -static void *__bufgetter_connection(int i) -{ - return (void *)&(__connection_pool[i]); -} +static void *__bufgetter_connection0(int i) { return (void *)&(__connection_pool[0][i]); } +static void *__bufgetter_connection1(int i) { return (void *)&(__connection_pool[1][i]); } -static inline bufpool_handle __connectionpool_create(int num) +static inline bufpool_handle __connectionpool_create(int num, int inst) { - bufpool_handle h = bufpool_create(num, (__bufgetter_connection), (__connection_reset), sizeof(struct connection_item_t)); + bufpool_handle h = bufpool_create(num, inst ? (__bufgetter_connection1) : (__bufgetter_connection0), + (__connection_reset), sizeof(struct connection_item_t)); if (h) { int i; for(i = 0; i < num; i++) { - __connection_pool[i].pool = h; - __connection_pool[i].con_state = __CON_S_DISCONNECTED; - pthread_mutex_init(&__connection_pool[i].write_mutex, NULL); + __connection_pool[inst][i].pool = h; + __connection_pool[inst][i].con_state = __CON_S_DISCONNECTED; + pthread_mutex_init(&__connection_pool[inst][i].write_mutex, NULL); } } return h; } -static void *__bufgetter_trans(int i) -{ - return (void *)&(__transfer_pool[i]); -} +static void *__bufgetter_trans0(int i) { return (void *)&(__transfer_pool[0][i]); } +static void *__bufgetter_trans1(int i) { return (void *)&(__transfer_pool[1][i]); } -static inline bufpool_handle __transpool_create(int num) +static inline bufpool_handle __transpool_create(int num, int inst) { - bufpool_handle h = bufpool_create(num, (__bufgetter_trans), NULL, sizeof(struct transfer_item_t)); + bufpool_handle h = bufpool_create(num, inst ? (__bufgetter_trans1) : (__bufgetter_trans0), + NULL, sizeof(struct transfer_item_t)); if (h) { int i; for(i = 0; i < num; i++) { - __transfer_pool[i].pool = h; - __transfer_pool[i].list_entry.cleaner = (__transfer_item_cleaner); + __transfer_pool[inst][i].pool = h; + __transfer_pool[inst][i].list_entry.cleaner = (__transfer_item_cleaner); } } @@ -424,6 +427,10 @@ static int __message_proc_sock(struct list_t *e, void *p) sprintf(cred, "%s:%s", h->user, h->pass); strcpy(valid, "Basic "); base64_encode(valid + 6, cred, strlen(cred)); + if (!strcmp(h->pass, "@system")) { + char *given = buf + strlen(__STR_AUTH) + 2; + isAuthValid = !strncmp(given, "Basic ", 6) && sysauth_check_basic(given + 6); + } else isAuthValid = !strncmp(buf + strlen(__STR_AUTH) + 2, valid, strlen(valid)); } else if (SCMP(__STR_CSEQ, buf)) { ASSERT(tok = strtok_r(buf, ": ", &last), goto error); @@ -612,13 +619,20 @@ static inline int __bind_tcp(unsigned short port) setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof(tmp)); + memset(&addr, 0, sizeof(addr)); addr.sin_port = htons(port); addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_family = AF_INET; - ASSERT(bind(server_fd, (struct sockaddr *)&addr, sizeof(addr)) == 0, ({ - ERR("bind:%s\n", strerror(errno)); - goto error;})); + /* A restart can race the previous instance releasing the port; a single + * failed bind used to leave the server silently dead, so retry briefly. */ + for (int attempt = 0; bind(server_fd, (struct sockaddr *)&addr, sizeof(addr)) != 0; attempt++) { + if (errno != EADDRINUSE || attempt >= 40) { + ERR("bind:%s\n", strerror(errno)); + goto error; + } + usleep(250000); + } ASSERT(listen(server_fd, 5) >= 0, ({ ERR("listen:%s\n", strerror(errno)); @@ -898,8 +912,10 @@ rtsp_handle rtsp_create(unsigned char max_con, unsigned int port, int priority) pthread_mutex_init(&nh->mutex,NULL); ASSERT(nh->pool = threadpool_create(nh), goto error); - ASSERT(nh->con_pool = __connectionpool_create(max_con), goto error); - ASSERT(nh->transfer_pool = __transpool_create(max_con), goto error); + ASSERT(__rtsp_instances < RTSP_INSTANCES, goto error); + int inst = __rtsp_instances++; + ASSERT(nh->con_pool = __connectionpool_create(max_con, inst), goto error); + ASSERT(nh->transfer_pool = __transpool_create(max_con, inst), goto error); /* create tcp thread */ ASSERT(CREATE_THREAD(nh->pool, rtspThrFxn, priority--, NULL), diff --git a/src/server.c b/src/server.c index c50af083..2a0f217b 100644 --- a/src/server.c +++ b/src/server.c @@ -57,6 +57,7 @@ int server_fd = -1; pthread_t server_thread_id; pthread_mutex_t client_fds_mutex; +#include "sysauth.h" static bool is_local_address(const char *client_ip) { if (!client_ip) return false; @@ -235,10 +236,12 @@ void send_mp4_to_client(char index, hal_vidstream *stream, char isH265) { for (unsigned int i = 0; i < HTTP_MAX_CLIENTS; ++i) if (client_fds[i].sockFd >= 0 && client_fds[i].type == STREAM_MP4) { anyClient = 1; break; } pthread_mutex_unlock(&client_fds_mutex); + char recording = record_active(); for (unsigned int i = 0; i < stream->count; ++i) { hal_vidpack *pack = &stream->pack[i]; unsigned char *pack_data = pack->data + pack->offset; + char gotSlice = 0, isKey = 0; if (plat == HAL_PLATFORM_AK) mp4_set_timestamp(pack->timestamp); @@ -255,13 +258,19 @@ void send_mp4_to_client(char index, hal_vidstream *stream, char isH265) { mp4_set_pps(pack_data + pack->nalu[j].offset + scLen, pack->nalu[j].length - scLen, isH265); else if (pack->nalu[j].type == NalUnitType_VPS_HEVC && pack->nalu[j].length <= UINT16_MAX) mp4_set_vps(pack_data + pack->nalu[j].offset + scLen, pack->nalu[j].length - scLen); - else if (!anyClient) + else if (!anyClient && !recording) continue; - else if (pack->nalu[j].type == NalUnitType_CodedSliceIdr || pack->nalu[j].type == NalUnitType_CodedSliceAux) + else if (pack->nalu[j].type == NalUnitType_CodedSliceIdr || pack->nalu[j].type == NalUnitType_CodedSliceAux) { mp4_set_slice(pack_data + pack->nalu[j].offset + scLen, pack->nalu[j].length - scLen, 1); - else if (pack->nalu[j].type == NalUnitType_CodedSliceNonIdr) + gotSlice = isKey = 1; + } else if (pack->nalu[j].type == NalUnitType_CodedSliceNonIdr) { mp4_set_slice(pack_data + pack->nalu[j].offset + scLen, pack->nalu[j].length - scLen, 0); + gotSlice = 1; + } } + /* A pack without a slice leaves no new fragment to hand out */ + if (!gotSlice) continue; + if (recording) record_fragment(isKey); if (!anyClient) continue; static enum BufError err; @@ -286,6 +295,7 @@ void send_mp4_to_client(char index, hal_vidstream *stream, char isH265) { client_fds[i].mp4.sequence_number = 0; client_fds[i].mp4.base_data_offset = header_buf.offset; client_fds[i].mp4.base_media_decode_time = 0; + client_fds[i].mp4.audio_media_decode_time = 0; client_fds[i].mp4.header_sent = true; client_fds[i].mp4.nals_count = 0; client_fds[i].mp4.default_sample_duration = @@ -517,6 +527,206 @@ char *request_header(const char *name) { http_header_t *request_headers(void) { return http_headers; } +/* Recorded clips are sent from their own thread: the server loop handles one + * request at a time and a download would freeze everything else. */ +#define CLIP_MAX_SENDERS 3 +typedef struct { + int fd; + char path[RECORD_PATH_MAX]; + char name[128]; + long long first, last; + bool hasRange, download; +} clip_job_t; + +static int clip_senders; +static pthread_mutex_t clip_mutex = PTHREAD_MUTEX_INITIALIZER; + +static void *send_clip_thread(void *arg) { + clip_job_t *job = arg; + char *buf = malloc(32768), header[512]; + /* Only regular files in the recording directory, not what a link points at */ + int clipFd = open(job->path, O_RDONLY | O_NOFOLLOW); + FILE *file = clipFd >= 0 ? fdopen(clipFd, "rb") : NULL; + struct stat st; + + if (clipFd >= 0 && !file) close(clipFd); + + if (!buf || !file || fstat(fileno(file), &st) || !S_ISREG(st.st_mode)) { + send_http_error(job->fd, 404); + goto done; + } + + long long size = st.st_size, first = 0, last = size - 1; + if (job->hasRange) { + if (job->first < 0) { + /* "bytes=-N": the last N bytes */ + first = size + job->first < 0 ? 0 : size + job->first; + } else { + first = job->first; + if (job->last >= 0 && job->last < last) last = job->last; + } + if (first > last || first >= size) { + int len = sprintf(header, "HTTP/1.1 416 Range Not Satisfiable\r\n" + "Content-Range: bytes */%lld\r\nContent-Length: 0\r\nConnection: close\r\n\r\n", size); + send_to_fd(job->fd, header, len); + close_socket_fd(job->fd); + goto done; + } + } + + int len = sprintf(header, "HTTP/1.1 %s\r\n" + "Content-Type: video/mp4\r\n" + "Accept-Ranges: bytes\r\n" + "Content-Length: %lld\r\n", job->hasRange ? "206 Partial Content" : "200 OK", last - first + 1); + if (job->hasRange) + len += sprintf(header + len, "Content-Range: bytes %lld-%lld/%lld\r\n", first, last, size); + if (job->download) + len += sprintf(header + len, "Content-Disposition: attachment; filename=\"%s\"\r\n", job->name); + len += sprintf(header + len, "Connection: close\r\n\r\n"); + + if (send(job->fd, header, len, MSG_NOSIGNAL) == len && !fseeko(file, first, SEEK_SET)) { + long long left = last - first + 1; + while (left > 0 && keepRunning) { + size_t got = fread(buf, 1, left > 32768 ? 32768 : (size_t)left, file); + if (!got) break; + size_t sent = 0; + while (sent < got) { + ssize_t n = send(job->fd, buf + sent, got - sent, MSG_NOSIGNAL); + if (n <= 0) goto closing; + sent += n; + } + left -= got; + } + } +closing: + close_socket_fd(job->fd); +done: + if (file) fclose(file); + free(buf); + free(job); + pthread_mutex_lock(&clip_mutex); + clip_senders--; + pthread_mutex_unlock(&clip_mutex); + return NULL; +} + +static int clip_name_cmp(const void *a, const void *b) { + /* Names carry the start time: descending order puts the newest first */ + return strcmp(*(char *const *)b, *(char *const *)a); +} + +/* Lists the recorded clips as JSON, newest first. */ +static void send_clip_list(int fd) { + char **names = NULL, *out; + size_t count = 0, cap = 0, len; + struct dirent *ent; + DIR *dir = opendir(app_config.record_path); + + while (dir && (ent = readdir(dir))) { + size_t n = strlen(ent->d_name); + if (n < 5 || n > 120 || strcmp(ent->d_name + n - 4, ".mp4")) continue; + /* Names go into JSON as they are: skip any that would need escaping */ + bool plain = true; + for (const char *c = ent->d_name; *c && plain; c++) + plain = (unsigned char)*c >= 0x20 && *c != '"' && *c != '\\'; + if (!plain) continue; + if (count == cap) { + char **grown = realloc(names, (cap = cap ? cap * 2 : 64) * sizeof(*names)); + if (!grown) break; + names = grown; + } + if (!(names[count] = strdup(ent->d_name))) break; + count++; + } + if (dir) closedir(dir); + if (count) qsort(names, count, sizeof(*names), clip_name_cmp); + + if (!(out = malloc(256 + count * 176))) { + send_http_error(fd, 500); + goto cleanup; + } + len = sprintf(out, "HTTP/1.1 200 OK\r\n" + "Content-Type: application/json;charset=UTF-8\r\n" + "Connection: close\r\n\r\n{\"recording\":%s,\"files\":[", record_active() ? "true" : "false"); + for (size_t i = 0; i < count; i++) { + char path[RECORD_PATH_MAX]; + struct stat st; + snprintf(path, sizeof(path), "%s/%s", app_config.record_path, names[i]); + if (lstat(path, &st) || !S_ISREG(st.st_mode)) continue; + len += sprintf(out + len, "%s{\"name\":\"%s\",\"size\":%lld}", + out[len - 1] == '[' ? "" : ",", names[i], (long long)st.st_size); + } + len += sprintf(out + len, "]}"); + send_and_close(fd, out, len); + free(out); +cleanup: + for (size_t i = 0; i < count; i++) free(names[i]); + free(names); +} + +/* Hands a clip over to a sender thread; the socket then belongs to it. */ +static void send_clip(http_request_t *req, const char *name) { + clip_job_t *job; + pthread_attr_t attr; + pthread_t tid; + char *range = request_header("Range"); + size_t n = strlen(name); + + if (n < 5 || n >= sizeof(job->name) || strchr(name, '/') || strcmp(name + n - 4, ".mp4")) { + send_http_error(req->clntFd, 404); + return; + } + + pthread_mutex_lock(&clip_mutex); + bool busy = clip_senders >= CLIP_MAX_SENDERS; + if (!busy) clip_senders++; + pthread_mutex_unlock(&clip_mutex); + if (busy || !(job = calloc(1, sizeof(*job)))) { + if (!busy) { + pthread_mutex_lock(&clip_mutex); + clip_senders--; + pthread_mutex_unlock(&clip_mutex); + } + send_http_error(req->clntFd, 500); + return; + } + + job->fd = req->clntFd; + strcpy(job->name, name); + snprintf(job->path, sizeof(job->path), "%s/%s", app_config.record_path, name); + job->download = req->query && strstr(req->query, "download"); + job->last = -1; + if (range && !strncmp(range, "bytes=", 6)) { + char *end; + range += 6; + if (*range == '-') { + job->first = -strtoll(range + 1, &end, 10); + job->hasRange = end != range + 1 && job->first < 0; + } else { + job->first = strtoll(range, &end, 10); + job->hasRange = end != range && *end == '-'; + if (job->hasRange && end[1] >= '0' && end[1] <= '9') job->last = strtoll(end + 1, NULL, 10); + } + } + + /* Long transfers over WiFi: a stalled client is dropped after 20 s */ + struct timeval tv = { .tv_sec = 20, .tv_usec = 0 }; + setsockopt(job->fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); + + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + pthread_attr_setstacksize(&attr, 64 * 1024); + if (pthread_create(&tid, &attr, send_clip_thread, job)) { + pthread_mutex_lock(&clip_mutex); + clip_senders--; + pthread_mutex_unlock(&clip_mutex); + free(job); + send_http_error(req->clntFd, 500); + } + pthread_attr_destroy(&attr); +} + + void parse_request(http_request_t *req) { struct sockaddr_in client_sock; socklen_t client_sock_len = sizeof(client_sock); @@ -687,7 +897,10 @@ void respond_request(http_request_t *req) { strcpy(valid, "Basic "); base64_encode(valid + 6, cred, strlen(cred)); - if (!auth || !EQUALS(auth, valid)) { + bool granted = EQUALS(app_config.web_auth_pass, "@system") ? + (auth && STARTS_WITH(auth, "Basic ") && sysauth_check_basic(auth + 6)) : + (auth && EQUALS(auth, valid)); + if (!granted) { respLen = sprintf(response, "HTTP/1.1 401 Unauthorized\r\n" "Content-Type: text/plain\r\n" @@ -1267,6 +1480,45 @@ void respond_request(http_request_t *req) { send_and_close(req->clntFd, response, respLen); return; } + if (EQUALS(req->uri, "/api/speak")) { + /* Talk-back: the body is raw 16 kHz mono signed 16-bit PCM */ + int ok = EXIT_FAILURE, volume = -1; +#if defined(__ARM_PCS_VFP) + if (plat == HAL_PLATFORM_I3 && app_config.audio_enable) { + char *vol = req->query ? strstr(req->query, "volume=") : NULL; + if (vol) ok = (volume = i3_speaker_volume(atoi(vol + 7))) < 0; + if (EQUALS(req->method, "POST") && + req->paysize >= 2 && req->total >= req->header_len + req->paysize) + ok = i3_speaker_write((short*)req->payload, req->paysize / 2); + volume = i3_speaker_volume(-1); + } +#endif + respLen = sprintf(response, + "HTTP/1.1 %s\r\n" + "Content-Type: application/json;charset=UTF-8\r\n" + "Connection: close\r\n" + "\r\n" + "{\"ok\":%s,\"volume\":%d}", ok ? "503 Service Unavailable" : "200 OK", + ok ? "false" : "true", volume); + send_and_close(req->clntFd, response, respLen); + return; + } + if (EQUALS(req->uri, "/api/lamp")) { + /* White lamp only; unlike /api/night this leaves the day/night logic alone */ + if (req->query) { + if (strstr(req->query, "on=1") || strstr(req->query, "on=true")) night_whiteled(1); + else if (strstr(req->query, "on=0") || strstr(req->query, "on=false")) night_whiteled(0); + else if (strstr(req->query, "toggle")) night_whiteled(!night_whiteled_on()); + } + respLen = sprintf(response, + "HTTP/1.1 200 OK\r\n" + "Content-Type: application/json;charset=UTF-8\r\n" + "Connection: close\r\n" + "\r\n" + "{\"on\":%s}", night_whiteled_on() ? "true" : "false"); + send_and_close(req->clntFd, response, respLen); + return; + } if (EQUALS(req->uri, "/api/night")) { if (req->query) { char *remain; @@ -1310,6 +1562,11 @@ void respond_request(http_request_t *req) { night_irled(1); else if (EQUALS_CASE(value, "false") || EQUALS(value, "0")) night_irled(0); + } else if (EQUALS(key, "whiteled")) { + if (EQUALS_CASE(value, "true") || EQUALS(value, "1")) + night_whiteled(1); + else if (EQUALS_CASE(value, "false") || EQUALS(value, "0")) + night_whiteled(0); } else if (EQUALS(key, "irled_pin")) { short result = strtol(value, &remain, 10); if (remain != value) @@ -1329,18 +1586,27 @@ void respond_request(http_request_t *req) { night_disable(); if (app_config.night_mode_enable) night_enable(); } + unsigned int expo[4] = {0}; + int expoBv = 0; + night_exposure(&expo[0], &expo[1], &expo[2], &expo[3], &expoBv); respLen = sprintf(response, "HTTP/1.1 200 OK\r\n" "Content-Type: application/json;charset=UTF-8\r\n" "Connection: close\r\n" "\r\n" "{\"active\":%s,\"manual\":%s,\"grayscale\":%s,\"ircut\":%s,\"ircut_pin1\":%d,\"ircut_pin2\":%d," - "\"irled\":%s,\"irled_pin\":%d,\"irsense_pin\":%d,\"adc_device\":\"%s\",\"adc_threshold\":%d}", + "\"irled\":%s,\"whiteled\":%s,\"irled_pin\":%d,\"irsense_pin\":%d,\"adc_device\":\"%s\",\"adc_threshold\":%d," + "\"sensor_gain\":%u,\"isp_gain\":%u,\"shutter_us\":%u,\"avg_y\":%u,\"bv\":%d," + "\"exposure\":%u,\"exposure_night\":%u,\"exposure_day\":%u}", app_config.night_mode_enable ? "true" : "false", night_manual_on() ? "true" : "false", night_grayscale_on() ? "true" : "false", night_ircut_on() ? "true" : "false", app_config.ir_cut_pin1, app_config.ir_cut_pin2, - night_irled_on() ? "true" : "false", app_config.ir_led_pin, app_config.ir_sensor_pin, - app_config.adc_device, app_config.adc_threshold); + night_irled_on() ? "true" : "false", night_whiteled_on() ? "true" : "false", + app_config.ir_led_pin, app_config.ir_sensor_pin, + app_config.adc_device, app_config.adc_threshold, + expo[0], expo[1], expo[2], expo[3], expoBv, + (unsigned int)((unsigned long long)expo[0] * expo[1] / 1024 * expo[2] / 40000 / 1024), + app_config.exposure_night, app_config.exposure_day); send_and_close(req->clntFd, response, respLen); return; } @@ -1426,6 +1692,8 @@ void respond_request(http_request_t *req) { if (remain != value) osds[id].opal = result & 0xFF; } + else if (EQUALS(key, "anchor")) + osds[id].anchor = region_anchor_parse(value); else if (EQUALS(key, "posx")) { short result = strtol(value, &remain, 10); if (remain != value) @@ -1463,25 +1731,44 @@ void respond_request(http_request_t *req) { "Content-Type: application/json;charset=UTF-8\r\n" "Connection: close\r\n" "\r\n" - "{\"id\":%d,\"color\":\"#%x\",\"opal\":%d,\"pos\":[%d,%d]," + "{\"id\":%d,\"color\":\"#%x\",\"opal\":%d,\"anchor\":\"%s\",\"pos\":[%d,%d]," "\"font\":\"%s\",\"size\":%.1f,\"text\":\"%s\",\"img\":\"%s\"," "\"outl\":\"#%x\",\"thick\":%.1f}", - id, color, osds[id].opal, osds[id].posx, osds[id].posy, + id, color, osds[id].opal, osd_anchors[(unsigned char)osds[id].anchor % 9], + osds[id].posx, osds[id].posy, osds[id].font, osds[id].size, osds[id].text, osds[id].img, osds[id].outl, osds[id].thick); send_and_close(req->clntFd, response, respLen); return; } + if (EQUALS(req->uri, "/api/recordings")) { + send_clip_list(req->clntFd); + return; + } + + if (STARTS_WITH(req->uri, "/recordings/")) { + unescape_uri(req->uri); + if (strstr(req->uri, "..")) send_http_error(req->clntFd, 404); + else send_clip(req, req->uri + 12); + return; + } + if (EQUALS(req->uri, "/api/record")) { if (req->query) { char *remain; + char wantStart = 0, wantStop = 0; + long duration = -1; while (req->query) { char *value = split(&req->query, "&"); if (!value || !*value) continue; unescape_uri(value); char *key = split(&value, "="); - if (!key || !*key || !value || !*value) continue; + if (!key || !*key) continue; + /* "start" and "stop" come without a value */ + if (EQUALS(key, "start")) { wantStart = 1; continue; } + if (EQUALS(key, "stop")) { wantStop = 1; continue; } + if (!value || !*value) continue; if (EQUALS(key, "enable")) { if (EQUALS_CASE(value, "true") || EQUALS(value, "1")) app_config.record_enable = 1; @@ -1499,29 +1786,47 @@ void respond_request(http_request_t *req) { else if (EQUALS(key, "filename")) strncpy(app_config.record_filename, value, sizeof(app_config.record_filename) - 1); else if (EQUALS(key, "segment_duration")) { - short result = strtol(value, &remain, 10); - if (remain != value) + long result = strtol(value, &remain, 10); + if (remain != value && result >= 0) app_config.record_segment_duration = result; } else if (EQUALS(key, "segment_size")) { - short result = strtol(value, &remain, 10); - if (remain != value) + long result = strtol(value, &remain, 10); + if (remain != value && result >= 0) app_config.record_segment_size = result; } + else if (EQUALS(key, "duration")) { + long result = strtol(value, &remain, 10); + if (remain != value && result >= 0 && result <= 86400) duration = result; + } + else if (EQUALS(key, "reserve")) { + long result = strtol(value, &remain, 10); + if (remain != value && result >= 0) + app_config.record_reserve = result; + } + + } - if (!app_config.record_enable) continue; - if (app_config.record_continuous) continue; - if (EQUALS(key, "start")) - record_start(); - else if (EQUALS(key, "stop")) - record_stop(); + /* Bring the recorder in line with the settings: continuous mode + * records whenever enabled, otherwise start/stop decide. An + * explicit stop also ends continuous mode, or it would restart. */ + if (!app_config.record_enable || wantStop) { + if (wantStop) app_config.record_continuous = 0; + if (record_active()) record_stop(); + } else if (app_config.record_continuous || wantStart) { + /* "start&duration=N" records for N seconds, repeating it + * moves the end so an event source can keep it going; + * a plain "start" records until "stop" */ + if (record_start() && wantStart) + record_stop_after(duration > 0 ? duration : 0); } } - char start_time[32] = ""; + char start_time[32] = "", storage[64]; + record_storage(storage, sizeof(storage)); time_t startTime = record_start_time(); if (startTime) { struct tm tm_buf, *tm_info = localtime_r(&startTime, &tm_buf); - strftime(start_time, sizeof(start_time), "%Y-%m-%dT%H:%M:%SZ", tm_info); + strftime(start_time, sizeof(start_time), "%Y-%m-%d %H:%M:%S", tm_info); } respLen = sprintf(response, @@ -1530,14 +1835,77 @@ void respond_request(http_request_t *req) { "Connection: close\r\n" "\r\n" "{\"recording\":%s,\"start_time\":\"%s\",\"continuous\":%s,\"path\":\"%s\"," - "\"filename\":\"%s\",\"segment_duration\":%d,\"segment_size\":%d}", + "\"filename\":\"%s\",\"segment_duration\":%d,\"segment_size\":%d," + "\"enable\":%s,\"reserve\":%d,\"storage\":\"%s\",\"stop_in\":%u}", record_active() ? "true" : "false", start_time, app_config.record_continuous ? "true" : "false", app_config.record_path, app_config.record_filename, - app_config.record_segment_duration, app_config.record_segment_size); + app_config.record_segment_duration, app_config.record_segment_size, + app_config.record_enable ? "true" : "false", app_config.record_reserve, storage, + record_stop_in()); send_and_close(req->clntFd, response, respLen); return; } + if (EQUALS(req->uri, "/api/sub")) { + /* The scaler size is read once when the vendor library loads and the + * second RTSP server is set up at start, so a change is saved and the + * streamer restarts itself. */ + char changed = 0; + if (req->query) { + char *remain; + while (req->query) { + char *value = split(&req->query, "&"); + if (!value || !*value) continue; + unescape_uri(value); + char *key = split(&value, "="); + if (!key || !*key || !value || !*value) continue; + if (EQUALS(key, "enable")) { + bool on = EQUALS_CASE(value, "true") || EQUALS(value, "1"); + if (on != app_config.sub_enable) changed = 1; + app_config.sub_enable = on; + continue; + } + long result = strtol(value, &remain, 10); + if (remain == value) continue; + unsigned int *field = NULL; + long min = 1, max = INT_MAX; + if (EQUALS(key, "size")) { + /* "640x360" */ + long h = *remain == 'x' ? strtol(remain + 1, NULL, 10) : 0; + /* Sizes the Infinity3 scaler was seen to deliver; others give + * no frames or stop the pipeline. */ + if (!((result == 640 && (h == 360 || h == 480)) || (result == 704 && h == 576))) + continue; + if (result != app_config.sub_width || h != app_config.sub_height) changed = 1; + app_config.sub_width = result; + app_config.sub_height = h; + continue; + } + else if (EQUALS(key, "gop")) { field = &app_config.sub_gop; max = 300; } + else if (EQUALS(key, "bitrate")) { field = &app_config.sub_bitrate; min = 32; max = 8192; } + else if (EQUALS(key, "rtsp_port")) { field = &app_config.sub_rtsp_port; max = USHRT_MAX; } + if (!field || result < min || result > max) continue; + if (*field != result) changed = 1; + *field = result; + } + } + + respLen = sprintf(response, + "HTTP/1.1 200 OK\r\n" + "Content-Type: application/json;charset=UTF-8\r\n" + "Connection: close\r\n" + "\r\n" + "{\"enable\":%s,\"size\":\"%dx%d\",\"gop\":%d,\"bitrate\":%d," + "\"rtsp_port\":%d,\"measured\":\"%d fps\",\"restarting\":%s}", + app_config.sub_enable ? "true" : "false", app_config.sub_width, app_config.sub_height, + app_config.sub_gop, app_config.sub_bitrate, + app_config.sub_rtsp_port, media_sub_rate(), changed ? "true" : "false"); + send_and_close(req->clntFd, response, respLen); + if (changed && !app_config_save()) + kill(getpid(), SIGHUP); + return; + } + if (EQUALS(req->uri, "/api/status")) { struct sysinfo si; sysinfo(&si); @@ -1576,8 +1944,19 @@ void respond_request(http_request_t *req) { if (!key || !*key || !value || !*value) continue; if (EQUALS(key, "fmt")) { strncpy(timefmt, value, 32); + } else if (EQUALS(key, "tz")) { + /* POSIX TZ rule, e.g. CST6CDT,M3.2.0,M11.1.0. Kept where the + * rest of the system reads it, and applied right away. */ + FILE *tzf; + if (strlen(value) < 64 && !strpbrk(value, "\n\r\"'`$;|&") && + (tzf = fopen("/etc/TZ", "w"))) { + fprintf(tzf, "%s\n", value); + fclose(tzf); + setenv("TZ", value, 1); + tzset(); + } } else if (EQUALS(key, "ts")) { - short result = strtol(value, &remain, 10); + long long result = strtoll(value, &remain, 10); if (remain == value) continue; t.tv_sec = result; clock_settime(CLOCK_REALTIME, &t); @@ -1590,7 +1969,8 @@ void respond_request(http_request_t *req) { "Content-Type: application/json;charset=UTF-8\r\n" "Connection: close\r\n" "\r\n" - "{\"fmt\":\"%s\",\"ts\":%zu}", timefmt, t.tv_sec); + "{\"fmt\":\"%s\",\"ts\":%zu,\"tz\":\"%s\"}", timefmt, t.tv_sec, + getenv("TZ") ? getenv("TZ") : ""); send_and_close(req->clntFd, response, respLen); return; } @@ -1616,7 +1996,14 @@ void *server_thread(void *vargp) { .sin_port = htons(app_config.web_port), .sin_addr.s_addr = htonl(INADDR_ANY) }; - if (ret = bind(server_fd, (struct sockaddr *)&server, sizeof(server))) { + /* A previous instance that is still shutting down keeps the port for a + * few seconds; giving up at once would leave the camera without its page. */ + for (int attempt = 0; attempt < 30 && keepRunning; attempt++) { + if (!(ret = bind(server_fd, (struct sockaddr *)&server, sizeof(server)))) break; + if (errno != EADDRINUSE) break; + sleep(1); + } + if (ret) { HAL_DANGER("server", "%s (%d)\n", strerror(errno), errno); keepRunning = 0; close_socket_fd(server_fd); diff --git a/src/server.h b/src/server.h index 28f4bde7..8ccc51a5 100644 --- a/src/server.h +++ b/src/server.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -13,6 +14,7 @@ #include #include #include +#include #include #include #include diff --git a/src/sysauth.c b/src/sysauth.c new file mode 100644 index 00000000..ac4354d2 --- /dev/null +++ b/src/sysauth.c @@ -0,0 +1,100 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "sysauth.h" + +/* + * System-account login: with the configured password set to "@system", a + * "Basic" credential is checked against the device's own accounts in + * /etc/shadow (the SSH login), so no password has to be kept in the config. + * + * Called from the HTTP and RTSP threads alike, so the cache and crypt() (not + * reentrant) are kept under one lock. The last good credential is remembered + * for a few minutes and only while /etc/shadow is unchanged, so a password + * change takes effect. After a failure, new credentials are refused for a + * second without checking, instead of sleeping in the caller's server loop. + */ +#define SYSAUTH_CACHE_S 300 + +static pthread_mutex_t authMtx = PTHREAD_MUTEX_INITIALIZER; +static char okCred[160]; +static time_t okAt, failAt; +static struct stat okShadow; + +static time_t sysauth_now(void) { + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return ts.tv_sec; +} + +static int sysauth_check_locked(const char *b64) { + static char *(*cryptFn)(const char*, const char*); + char raw[128], cred[160], line[256]; + unsigned int n = 0, bits = 0, acc = 0, len = 0; + struct stat st; + time_t now = sysauth_now(); + + while (b64[len] && b64[len] != '\r' && b64[len] != '\n' && b64[len] != ' ' && len < sizeof(cred) - 1) + { cred[len] = b64[len]; len++; } + cred[len] = 0; + if (!len) return 0; + if (stat("/etc/shadow", &st)) return 0; + if (now - okAt >= SYSAUTH_CACHE_S || + st.st_ino != okShadow.st_ino || st.st_mtime != okShadow.st_mtime) *okCred = 0; + if (*okCred && !strcmp(cred, okCred)) return 1; + if (failAt && now - failAt < 1) return 0; + + for (unsigned int i = 0; i < len && cred[i] != '=' && n < sizeof(raw) - 1; i++) { + char c = cred[i]; + int v = c >= 'A' && c <= 'Z' ? c - 'A' : c >= 'a' && c <= 'z' ? c - 'a' + 26 : + c >= '0' && c <= '9' ? c - '0' + 52 : c == '+' ? 62 : c == '/' ? 63 : -1; + if (v < 0) return 0; + acc = acc << 6 | v; bits += 6; + if (bits >= 8) { bits -= 8; raw[n++] = acc >> bits & 0xFF; } + } + raw[n] = 0; + char *pass = strchr(raw, ':'); + if (!pass || pass == raw) return 0; + *pass++ = 0; + + if (!cryptFn) { + void *lib = dlopen("libcrypt.so.1", RTLD_NOW); + cryptFn = dlsym(lib ? lib : RTLD_DEFAULT, "crypt"); + if (!cryptFn) return 0; + } + int ok = 0; + FILE *sh = fopen("/etc/shadow", "r"); + if (!sh) return 0; + while (fgets(line, sizeof(line), sh)) { + char *hash = strchr(line, ':'); + if (!hash) continue; + *hash++ = 0; + if (strcmp(line, raw)) continue; + char *end = strchr(hash, ':'); + if (end) *end = 0; + if (strlen(hash) < 4 || hash[0] == '!' || hash[0] == '*') break; // no usable password + char *calc = cryptFn(pass, hash); + ok = calc && !strcmp(calc, hash); + break; + } + fclose(sh); + memset(raw, 0, sizeof(raw)); + if (ok) { + strcpy(okCred, cred); + okAt = now; + okShadow = st; + } else failAt = now; + return ok; +} + +int sysauth_check_basic(const char *b64) { + pthread_mutex_lock(&authMtx); + int ok = sysauth_check_locked(b64); + pthread_mutex_unlock(&authMtx); + return ok; +} diff --git a/src/sysauth.h b/src/sysauth.h new file mode 100644 index 00000000..6d7b6230 --- /dev/null +++ b/src/sysauth.h @@ -0,0 +1,4 @@ +#pragma once + +// 1 when a Basic credential matches an account in /etc/shadow +int sysauth_check_basic(const char *b64);