Skip to content

Commit

Permalink
show fps on incoming and outgoing video. tweak layout a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
zoff99 committed Nov 7, 2023
1 parent 0cf47a9 commit 269b3f4
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 67 deletions.
18 changes: 9 additions & 9 deletions src/main/java/com/zoffcc/applications/ffmpegav/AVActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
public class AVActivity {

private static final String TAG = "ffmpegav.AVActivity";
static final String Version = "0.99.4";
static final String Version = "0.99.5";

public static native String ffmpegav_version();
public static native String ffmpegav_libavutil_version();
Expand All @@ -27,7 +27,7 @@ public class AVActivity {
final static java.nio.ByteBuffer audio_buffer_2 = java.nio.ByteBuffer.allocateDirect(audio_buffer_size_in_bytes2);

public static interface video_capture_callback {
void onSuccess(long width, long height, long pts);
void onSuccess(long width, long height, long pts, int fps);
void onError();
}
static video_capture_callback video_capture_callback_function = null;
Expand All @@ -52,11 +52,11 @@ public static void ffmpegav_set_video_capture_callback(video_capture_callback ca
video_capture_callback_function = callback;
}

public static void ffmpegav_callback_video_capture_frame_pts_cb_method(long width, long height, long pts)
public static void ffmpegav_callback_video_capture_frame_pts_cb_method(long width, long height, long pts, int fps)
{
// Log.i(TAG, "capture video frame w: " + width + " h: " + height + " pts: " + pts);
if (video_capture_callback_function != null) {
video_capture_callback_function.onSuccess(width, height, pts);
video_capture_callback_function.onSuccess(width, height, pts, fps);
}
}

Expand Down Expand Up @@ -235,7 +235,7 @@ public static void main(String[] args) {
vdevice = video_in_devices[i];
vsource = ":0";
final int res_vd = ffmpegav_open_video_in_device(vdevice,
vsource, 640, 480, 15);
vsource, 640, 480, 30);
Log.i(TAG, "ffmpeg open video capture device: " + res_vd);
}
}
Expand Down Expand Up @@ -296,8 +296,8 @@ public static void main(String[] args) {

ffmpegav_set_video_capture_callback(new video_capture_callback() {
@Override
public void onSuccess(long width, long height, long pts) {
Log.i(TAG, "ffmpeg open video capture onSuccess:" + width + " " + height + " " + pts);
public void onSuccess(long width, long height, long pts, int fps) {
Log.i(TAG, "ffmpeg open video capture onSuccess:" + width + " " + height + " " + pts + " fps:" + fps);
}
@Override
public void onError() {
Expand Down Expand Up @@ -329,7 +329,7 @@ public void onError() {
ffmpegav_start_audio_in_capture();
try
{
Thread.sleep(1000);
Thread.sleep(10000);
}
catch(Exception e)
{
Expand Down Expand Up @@ -383,7 +383,7 @@ public void onError() {
// -----------------------
// -----------------------
final int res_vd3 = ffmpegav_open_video_in_device("",
"", 640, 480, 15);
"", 640, 480, 30);
Log.i(TAG, "ffmpeg open video capture device: " + res_vd3);

final int res_ad3 = ffmpegav_open_audio_in_device("",
Expand Down
24 changes: 23 additions & 1 deletion src/main/java/com/zoffcc/applications/trifa/VideoInFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import java.nio.ByteBuffer;
import java.util.concurrent.Semaphore;

import static com.zoffcc.applications.trifa.MainActivity.*;
import static com.zoffcc.applications.trifa.MainActivity.setVideo_play_count_frames;

public class VideoInFrame {
private static final String TAG = "trifa.VideoInFrame";

Expand Down Expand Up @@ -216,7 +219,26 @@ public void run()
{
if (i != null)
{
final long tt3 = System.currentTimeMillis();
setVideo_play_count_frames(getVideo_play_count_frames() + 1);
final int play_measure_after_frame = getVideo_play_measure_after_frame();
if (getVideo_play_count_frames() >= play_measure_after_frame) {
setVideo_play_count_frames(0);
int fps = (int)((System.currentTimeMillis() - getVideo_play_last_timestamp()) / (float)play_measure_after_frame);
if (fps > 0) {
fps = 1000 / fps;
} else {
fps = 0;
}
if ((fps < 1) || (fps > 120))
{
setVideo_play_fps(0);
}
else
{
setVideo_play_fps(fps);
}
MainActivity.setVideo_play_last_timestamp(System.currentTimeMillis());
}
JPictureBox.videoinbox.setIcon(i);
//Log.i(TAG, "new_video_in_frame:008:" + (System.currentTimeMillis() - tt3) + " ms");
EventQueue.invokeLater(() -> {
Expand Down
120 changes: 68 additions & 52 deletions src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,12 @@ val SPACE_BEFORE_FIRST_MESSAGE = 10.dp
val CAPTURE_VIDEO_FPS = 20
val VIDEO_IN_BOX_WIDTH_SMALL = 80.dp
val VIDEO_IN_BOX_HEIGHT_SMALL = 80.dp
val VIDEO_IN_BOX_WIDTH_FRACTION_SMALL = 0.5f
val VIDEO_IN_BOX_WIDTH_FRACTION_SMALL = 0.3f
val VIDEO_IN_BOX_WIDTH_FRACTION_BIG = 0.9f
val VIDEO_IN_BOX_WIDTH_BIG = 800.dp
val VIDEO_IN_BOX_HEIGHT_BIG = 800.dp
val VIDEO_OUT_BOX_WIDTH_SMALL = 50.dp
val VIDEO_OUT_BOX_HEIGHT_SMALL = 50.dp
val VIDEO_OUT_BOX_WIDTH_SMALL = 130.dp
val VIDEO_OUT_BOX_HEIGHT_SMALL = 100.dp
val VIDEO_OUT_BOX_WIDTH_BIG = 500.dp
val VIDEO_OUT_BOX_HEIGHT_BIG = 500.dp
val SAVEDATA_PATH_WIDTH = 200.dp
Expand Down Expand Up @@ -265,7 +265,7 @@ fun App()
}
var online_button_text by remember { mutableStateOf("offline") }
Button( // self connection state button
onClick = {}, colors = ButtonDefaults.buttonColors(), enabled = false) {
modifier = Modifier.width(120.dp), onClick = {}, colors = ButtonDefaults.buttonColors(), enabled = false) {
Box(modifier = Modifier.size(16.dp).border(1.dp, Color.Black, CircleShape).background(Color(online_button_color_wrapper), CircleShape))
Spacer(Modifier.size(ButtonDefaults.IconSpacing))
Text(getOnlineButtonText(online_button_text))
Expand Down Expand Up @@ -294,30 +294,37 @@ fun App()
var video_in_box_height by remember { mutableStateOf(VIDEO_IN_BOX_HEIGHT_SMALL) }
var video_in_box_small by remember { mutableStateOf(true)}
var video_in_box_width_fraction by remember { mutableStateOf(VIDEO_IN_BOX_WIDTH_FRACTION_SMALL)}
SwingPanel(
background = Color.Green,
modifier = Modifier.fillMaxWidth(video_in_box_width_fraction)
.padding(5.dp)
.fillMaxHeight(1.0f)
.combinedClickable(onClick = {
if (video_in_box_small)
{
video_in_box_width = VIDEO_IN_BOX_WIDTH_BIG
video_in_box_height = VIDEO_IN_BOX_HEIGHT_BIG
}
else
{
video_in_box_width = VIDEO_IN_BOX_WIDTH_SMALL
video_in_box_height = VIDEO_IN_BOX_HEIGHT_SMALL
Column(modifier = Modifier.fillMaxHeight(1.0f)) {
SwingPanel(
background = Color.Green,
modifier = Modifier.fillMaxWidth(video_in_box_width_fraction)
.padding(5.dp)
.weight(80.0f)
.combinedClickable(onClick = {
if (video_in_box_small)
{
video_in_box_width = VIDEO_IN_BOX_WIDTH_BIG
video_in_box_height = VIDEO_IN_BOX_HEIGHT_BIG
}
else
{
video_in_box_width = VIDEO_IN_BOX_WIDTH_SMALL
video_in_box_height = VIDEO_IN_BOX_HEIGHT_SMALL
}
video_in_box_small != video_in_box_small
}),
factory = {
JPanel(SingleComponentAspectRatioKeeperLayout(),true).apply {
add(JPictureBox.videoinbox)
}
video_in_box_small != video_in_box_small
}),
factory = {
JPanel(SingleComponentAspectRatioKeeperLayout(),true).apply {
add(JPictureBox.videoinbox)
}
}
)
)
val current_vplayfps_state by avstatestorevplayfpsstate.stateFlow.collectAsState()
Text(" fps: " + current_vplayfps_state.videoplayfps_state,
fontSize = 13.sp,
modifier = Modifier.height(20.dp),
maxLines = 1)
}
Column {
Icon(modifier = Modifier.padding(5.dp)
.combinedClickable(onClick = {
Expand Down Expand Up @@ -353,33 +360,42 @@ fun App()
imageVector = Icons.Default.Audiotrack, contentDescription = "",
tint = if (audio_filter_current_value == 1) Color.Red else Color.DarkGray)
}
var video_out_box_width by remember { mutableStateOf(VIDEO_OUT_BOX_WIDTH_SMALL) }
var video_out_box_height by remember { mutableStateOf(VIDEO_OUT_BOX_HEIGHT_SMALL) }
var video_out_box_small by remember { mutableStateOf(true)}
SwingPanel(
background = Color.Green,
modifier = Modifier.size(video_out_box_width, video_out_box_height)
.combinedClickable(onClick = {
if (video_out_box_small)
{
video_out_box_width = VIDEO_OUT_BOX_WIDTH_BIG
video_out_box_height = VIDEO_OUT_BOX_HEIGHT_BIG
}
else
{
video_out_box_width = VIDEO_OUT_BOX_WIDTH_SMALL
video_out_box_height = VIDEO_OUT_BOX_HEIGHT_SMALL

Column {
Spacer(modifier = Modifier.height(5.dp))
var video_out_box_width by remember { mutableStateOf(VIDEO_OUT_BOX_WIDTH_SMALL) }
var video_out_box_height by remember { mutableStateOf(VIDEO_OUT_BOX_HEIGHT_SMALL) }
var video_out_box_small by remember { mutableStateOf(true)}
SwingPanel(
background = Color.Green,
modifier = Modifier.size(video_out_box_width, video_out_box_height)
.combinedClickable(onClick = {
if (video_out_box_small)
{
video_out_box_width = VIDEO_OUT_BOX_WIDTH_BIG
video_out_box_height = VIDEO_OUT_BOX_HEIGHT_BIG
}
else
{
video_out_box_width = VIDEO_OUT_BOX_WIDTH_SMALL
video_out_box_height = VIDEO_OUT_BOX_HEIGHT_SMALL
}
video_out_box_small = !video_out_box_small
Log.i(TAG, "update1: " + video_out_box_small)
}),
factory = {
JPanel(SingleComponentAspectRatioKeeperLayout(),true).apply {
add(JPictureBoxOut.videooutbox)
}
video_out_box_small = !video_out_box_small
Log.i(TAG, "update1: " + video_out_box_small)
}),
factory = {
JPanel(SingleComponentAspectRatioKeeperLayout(),true).apply {
add(JPictureBoxOut.videooutbox)
}
},
update = {Log.i(TAG, "update2: " + video_out_box_small) }
)
},
update = {Log.i(TAG, "update2: " + video_out_box_small) }
)
val current_vicfps_state by avstatestorevcapfpsstate.stateFlow.collectAsState()
Text("fps: " + current_vicfps_state.videocapfps_state,
fontSize = 13.sp,
maxLines = 1)
}

var expanded_a by remember { mutableStateOf(false) }
var expanded_v by remember { mutableStateOf(false) }
var expanded_as by remember { mutableStateOf(false) }
Expand Down
69 changes: 66 additions & 3 deletions src/main/kotlin/com/zoffcc/applications/trifa/AVState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.zoffcc.applications.trifa
import CAPTURE_VIDEO_FPS
import RESOURCESDIR
import avstatestorecallstate
import avstatestorevcapfpsstate
import com.zoffcc.applications.ffmpegav.AVActivity
import com.zoffcc.applications.ffmpegav.AVActivity.ffmpegav_apply_audio_filter
import com.zoffcc.applications.ffmpegav.AVActivity.ffmpegav_init
Expand All @@ -16,9 +17,10 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import java.io.File
import java.nio.ByteBuffer
import java.util.concurrent.Semaphore

data class AVStateCallState(val call_state: AVState.CALL_STATUS = AVState.CALL_STATUS.CALL_STATUS_NONE)
data class AVStateVideoCaptureFpsState(val videocapfps_state: Int = 0)
data class AVStateVideoPlayFpsState(val videoplayfps_state: Int = 0)

data class AVState(val a: Int)
{
Expand Down Expand Up @@ -53,6 +55,7 @@ data class AVState(val a: Int)
private var video_in_source = ""
private var video_in_resolution_width = 640
private var video_in_resolution_height = 480
private var current_video_in_fps = 0
var calling_state = CALL_STATUS.CALL_STATUS_NONE
private var devices_state = CALL_DEVICES_STATE.CALL_DEVICES_STATE_CLOSED
private var call_with_friend_pubkey: String? = null
Expand Down Expand Up @@ -159,6 +162,16 @@ data class AVState(val a: Int)
}
}

fun current_video_in_fps_get(): Int
{
return current_video_in_fps
}

fun current_video_in_fps_set(value: Int)
{
current_video_in_fps = value
}

fun call_with_friend_pubkey_get(): String?
{
return call_with_friend_pubkey
Expand Down Expand Up @@ -492,9 +505,13 @@ data class AVState(val a: Int)

AVActivity.ffmpegav_set_video_capture_callback(object : AVActivity.video_capture_callback
{
override fun onSuccess(width: Long, height: Long, pts: Long)
override fun onSuccess(width: Long, height: Long, pts: Long, fps: Int)
{
// Log.i(TAG, "ffmpeg open video capture onSuccess: $width $height $pts")
// Log.i(TAG, "ffmpeg open video capture onSuccess: $width $height $pts FPS: $fps")
if (current_video_in_fps_get() != fps) {
current_video_in_fps_set(fps)
avstatestorevcapfpsstate.update(fps)
}
val frame_width_px: Int = width.toInt()
val frame_height_px: Int = height.toInt()
val buffer_size_in_bytes3 = (frame_width_px * frame_height_px * 1.5f).toInt()
Expand Down Expand Up @@ -549,6 +566,52 @@ fun CoroutineScope.createAVStateStore(): AVStateStore
}
}

interface AVStateStoreVideoCaptureFpsState
{
val stateFlow: StateFlow<AVStateVideoCaptureFpsState>
val state get() = stateFlow.value
fun update(fps: Int)
}

fun CoroutineScope.createAVStateStoreVideoCaptureFpsState(): AVStateStoreVideoCaptureFpsState
{
val mutableStateFlow = MutableStateFlow(AVStateVideoCaptureFpsState())

return object : AVStateStoreVideoCaptureFpsState
{
override val stateFlow: StateFlow<AVStateVideoCaptureFpsState> = mutableStateFlow
override fun update(fps: Int)
{
launch {
mutableStateFlow.value = state.copy(videocapfps_state = fps)
}
}
}
}

interface AVStateStoreVideoPlayFpsState
{
val stateFlow: StateFlow<AVStateVideoPlayFpsState>
val state get() = stateFlow.value
fun update(fps: Int)
}

fun CoroutineScope.createAVStateStoreVideoPlayFpsState(): AVStateStoreVideoPlayFpsState
{
val mutableStateFlow = MutableStateFlow(AVStateVideoPlayFpsState())

return object : AVStateStoreVideoPlayFpsState
{
override val stateFlow: StateFlow<AVStateVideoPlayFpsState> = mutableStateFlow
override fun update(fps: Int)
{
launch {
mutableStateFlow.value = state.copy(videoplayfps_state = fps)
}
}
}
}

interface AVStateStoreCallState
{
val stateFlow: StateFlow<AVStateCallState>
Expand Down
Loading

0 comments on commit 269b3f4

Please sign in to comment.