#!/bin/bash
# HLS Streaming Diagnosis Script
# Run this script to diagnose the HLS streaming issue

echo "=========================================="
echo "HLS STREAMING DIAGNOSIS SCRIPT"
echo "=========================================="
echo ""

echo "1. Checking for orphaned FFmpeg processes..."
echo "-------------------------------------------"
ps aux | grep ffmpeg | grep -v grep
ORPHANED_FFMPEG=$(ps aux | grep ffmpeg | grep -v grep | wc -l)
echo ""
echo "Found $ORPHANED_FFMPEG ffmpeg processes"
echo ""

echo "2. Checking for orphaned temp files in /tmp..."
echo "-------------------------------------------"
echo "FIFO pipes:"
ls -la /tmp/mbetterc_fifo* 2>/dev/null || echo "No FIFO pipes found"
echo ""
echo "HLS directories:"
ls -la /tmp/mbetterc_hls* 2>/dev/null || echo "No HLS directories found"
echo ""
echo "Black screen files:"
ls -la /tmp/mbetterc_black* 2>/dev/null || echo "No black screen files found"
echo ""

echo "3. Analyzing INTRO.mp4 video file..."
echo "-------------------------------------------"
INTRO_PATH="/home/mbetterclient/mbetterc/assets/INTRO.mp4"
if [ -f "$INTRO_PATH" ]; then
    echo "File found: $INTRO_PATH"
    echo ""
    echo "File size:"
    ls -lh "$INTRO_PATH"
    echo ""
    echo "Video format info:"
    ffprobe -v error -show_entries format=duration,bit_rate,size -of default=noprint_wrappers=1 "$INTRO_PATH" 2>&1
    echo ""
    echo "Video stream info:"
    ffprobe -v error -select_streams v:0 -show_entries stream=codec_name,width,height,r_frame_rate,duration,nb_frames -of default=noprint_wrappers=1 "$INTRO_PATH" 2>&1
    echo ""
    echo "Audio stream info:"
    ffprobe -v error -select_streams a:0 -show_entries stream=codec_name,sample_rate,channels,duration -of default=noprint_wrappers=1 "$INTRO_PATH" 2>&1
    echo ""
    echo "Checking for keyframes around 260 seconds (4:20 - 4:40)..."
    ffprobe -v error -select_streams v:0 -show_entries frame=pkt_pts_time,key_frame -of csv=p=0 "$INTRO_PATH" 2>&1 | awk -F',' '$1 >= 250 && $1 <= 280 {print}'
else
    echo "INTRO.mp4 not found at $INTRO_PATH"
fi
echo ""

echo "4. Testing FFmpeg streaming command (5 seconds)..."
echo "-------------------------------------------"
FIFO_PATH="/tmp/test_fifo_$$"
HLS_DIR="/tmp/test_hls_$$"
mkdir -p "$HLS_DIR"
mkfifo "$FIFO_PATH"

# Start input process with stream_loop
echo "Starting input process with -stream_loop -1..."
timeout 5 ffmpeg -re -stream_loop -1 -i "$INTRO_PATH" -c copy -f mpegts -y "$FIFO_PATH" 2>&1 &
INPUT_PID=$!

# Start HLS process
echo "Starting HLS process..."
timeout 5 ffmpeg -y -f mpegts -i "$FIFO_PATH" -c:v libx264 -preset ultrafast -tune zerolatency -g 48 -c:a aac -b:a 128k -f hls -hls_time 4 -hls_list_size 15 -hls_segment_filename "${HLS_DIR}/test_%04d.ts" "${HLS_DIR}/test.m3u8" 2>&1 &
HLS_PID=$!

# Wait and check
sleep 6

# Check if processes are still running
if ps -p $INPUT_PID > /dev/null 2>&1; then
    echo "Input process still running (PID: $INPUT_PID)"
    kill $INPUT_PID 2>/dev/null
else
    echo "Input process terminated"
fi

if ps -p $HLS_PID > /dev/null 2>&1; then
    echo "HLS process still running (PID: $HLS_PID)"
    kill $HLS_PID 2>/dev/null
else
    echo "HLS process terminated"
fi

# Cleanup
rm -f "$FIFO_PATH"
rm -rf "$HLS_DIR"

echo ""
echo "5. Memory and disk space check..."
echo "-------------------------------------------"
echo "Memory:"
free -h
echo ""
echo "Disk space in /tmp:"
df -h /tmp
echo ""

echo "=========================================="
echo "DIAGNOSIS COMPLETE"
echo "=========================================="