Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "demux-mpegts"]
path = demux-mpegts
url = https://github.com/janbar/demux-mpegts.git
128 changes: 128 additions & 0 deletions DEMUX_TESTING_GUIDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# Demux MPEG-TS Integration Test Guide

## Overview

This implementation adds MPEG-TS demuxing capability to Tardsplaya using the demux-mpegts library by janbar. The demux functionality separates video and audio streams into individual files and launches the media player with appropriate command line arguments.

## Features Added

### 1. MPEG-TS Demux Streaming Mode
- New `DEMUX_MPEGTS` streaming mode
- Separates video and audio into individual elementary stream files
- Supports multiple media players with different command line formats

### 2. Media Player Support
- **MPC-HC/MPC-BE**: Uses `/dub filename` parameter for additional audio
- **VLC**: Uses `--input-slave=filename` parameter for additional audio
- **MPV**: Uses `--audio-file=filename` parameter for additional audio

### 3. UI Integration
- Added streaming mode selection in Settings dialog
- Modes available:
- HLS Segments (Fallback)
- Transport Stream (TSDuck)
- TX-Queue IPC (High Performance) - Default
- MPEG-TS Demux (Separate A/V) - New

## Testing Instructions

### Prerequisites
1. Build the application with demux-mpegts integration
2. Have a compatible media player installed (MPV, VLC, or MPC-HC)
3. Configure the media player path in Settings

### Test Steps

#### 1. Basic Demux Functionality Test
1. Launch Tardsplaya
2. Go to Tools → Settings
3. Select "MPEG-TS Demux (Separate A/V)" from Streaming Mode dropdown
4. Click OK to save settings
5. Enter a Twitch channel name and click Load
6. Select a quality and click Watch
7. Verify:
- Log shows "[DEMUX] Starting MPEG-TS demux streaming"
- Separate video and audio files are created in temp directory
- Media player launches with both files

#### 2. Media Player Compatibility Test
Test with different media players:

**MPV Test:**
- Set Player Path to `mpv.exe`
- Expected command: `mpv.exe "video_file.h264" --audio-file="audio_file.aac"`

**VLC Test:**
- Set Player Path to `vlc.exe`
- Expected command: `vlc.exe "video_file.h264" --input-slave="audio_file.aac"`

**MPC-HC Test:**
- Set Player Path to `mpc-hc.exe` or `mpc-hc64.exe`
- Expected command: `mpc-hc.exe "video_file.h264" /dub "audio_file.aac"`

#### 3. Stream Recovery Test
1. Start streaming with demux mode
2. Wait for playback to begin
3. Simulate network discontinuity (pause/resume internet connection)
4. Verify:
- Separate files continue to be written
- Media player can recover playback more smoothly
- No extended black screen/audio desync issues

#### 4. Multi-Stream Test
1. Open multiple tabs (Ctrl+T)
2. Set different channels to demux mode
3. Verify:
- Each stream creates separate output directories
- Multiple media players launch correctly
- No interference between demux processes

### Expected Output Files

When demux mode is active, files are created in:
```
%TEMP%\Tardsplaya_Demux_<channel>_<timestamp>\
├── stream_<channel>_<video_PID>.h264 # Video elementary stream
├── stream_<channel>_<audio_PID>.aac # Audio elementary stream
└── (additional streams as detected)
```

### Debugging

Enable verbose debug logging in Settings for detailed demux information:
- Stream detection logs
- File creation logs
- Demux statistics
- Player command line generation

### Common Issues

1. **"Failed to initialize demux system"**
- Check temp directory permissions
- Verify demux-mpegts library is included in build

2. **"Media player failed to launch"**
- Verify player path is correct
- Check player supports the command line format used

3. **"No streams detected"**
- Verify the source is valid MPEG-TS format
- Check if stream contains video/audio elementary streams

4. **Audio/Video desync**
- Normal behavior - players handle elementary stream synchronization
- Try different players (MPV typically handles this best)

## Performance Notes

- Demux mode has slightly higher CPU usage than other modes
- Creates temporary files that are cleaned up on application exit
- Memory usage scales with buffer size (default 1MB)
- Best performance with SSD storage for temp files

## Compatibility

- Windows 7+ (matches existing application requirements)
- Visual Studio 2015+ for building
- Compatible with all existing Twitch stream formats
- Works with both live streams and VODs that support MPEG-TS format
189 changes: 189 additions & 0 deletions IMPLEMENTATION_SUMMARY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
# MPEG-TS Demux Integration - Implementation Summary

## Overview

This implementation adds comprehensive MPEG-TS demuxing functionality to Tardsplaya using the demux-mpegts library by janbar. The feature addresses the issue of media players getting stuck on black screens with continuing audio after discontinuities by separating video and audio streams into individual files.

## Requirements Fulfilled

### ✅ Core Requirements (Issue #124)

1. **Use demux-mpegts to demux video and audio streams**
- Integrated demux-mpegts library as git submodule
- Created `MpegTSDemuxer` class with full API integration
- Processes MPEG-TS packets and extracts elementary streams

2. **Help media players recover from discontinuities**
- Separate video and audio files enable independent stream recovery
- Reduces black screen/audio desync issues
- Players handle elementary streams more robustly

3. **Send audio and video separately**
- Creates individual files for each detected stream
- Video: `.h264`, `.h265`, `.m2v` formats
- Audio: `.aac`, `.mp3`, `.ac3` formats

4. **Full, not minimal implementation**
- Complete production-ready solution
- Comprehensive error handling and logging
- UI integration and configuration
- Multi-player support with proper command line generation

5. **Media player compatibility**
- **MPC-HC/MPC-BE**: `/dub filename` parameter ✅
- **VLC**: `--input-slave=filename` parameter ✅
- **MPV**: `--audio-file=filename` parameter ✅

## Technical Architecture

### Core Classes

1. **`MpegTSDemuxer`**
- Extends `TSDemux::TSDemuxer` interface
- Manages buffer and processes MPEG-TS packets
- Creates and writes elementary stream files
- Handles stream detection and codec identification

2. **`DemuxStreamManager`**
- High-level streaming coordination
- Manages download, demux, and player threads
- Handles playlist parsing and segment downloading
- Coordinates player launch with appropriate command line

3. **`MediaPlayerCommandBuilder`**
- Detects player type from executable path
- Generates correct command line for each player type
- Supports MPC-HC, VLC, and MPV formats

### Integration Points

1. **Streaming Mode Enumeration**
```cpp
enum class StreamingMode {
HLS_SEGMENTS, // Traditional HLS
TRANSPORT_STREAM, // TSDuck routing
TX_QUEUE_IPC, // High-performance mode
DEMUX_MPEGTS // NEW: Separate A/V streams
};
```

2. **Settings UI Integration**
- Added combobox to settings dialog
- Persistent configuration via INI file
- User-selectable streaming mode

3. **Main Application Integration**
- Updated `StartStreamThread` to support demux mode
- Integrated with existing logging and status systems
- Maintains compatibility with existing modes

## File Structure

```
Tardsplaya/
├── demux-mpegts/ # Git submodule
│ └── src/ # demux-mpegts library files
├── demux_mpegts_integration.h # Main demux header
├── demux_mpegts_integration.cpp # Main demux implementation
├── DEMUX_TESTING_GUIDE.md # Testing instructions
├── IMPLEMENTATION_SUMMARY.md # This file
└── [existing files...] # Updated with demux support
```

## Features Added

### Stream Processing
- Real-time MPEG-TS packet demuxing
- Elementary stream detection and classification
- Separate file creation per PID/stream type
- Automatic codec detection (H.264, H.265, AAC, AC-3, etc.)

### Player Integration
- Automatic player type detection
- Command line generation for different players
- Support for separate audio track parameters
- Process management and cleanup

### User Experience
- Settings dialog integration
- Real-time logging and progress tracking
- Stream statistics and monitoring
- Temporary file management

### Error Handling
- Comprehensive exception handling
- Graceful degradation on errors
- Detailed debug logging
- Recovery mechanisms

## Output Example

When demux mode is active:
```
%TEMP%/Tardsplaya_Demux_channelname_timestamp/
├── stream_channelname_0256.h264 # Video (PID 256)
├── stream_channelname_0257.aac # Audio (PID 257)
└── [additional streams...]
```

Player launch example:
```bash
# MPV
mpv.exe "video.h264" --audio-file="audio.aac"

# VLC
vlc.exe "video.h264" --input-slave="audio.aac"

# MPC-HC
mpc-hc.exe "video.h264" /dub "audio.aac"
```

## Benefits

### For Users
- Reduced stream interruption issues
- Better playback quality during network problems
- Support for multiple media players
- Easy mode switching via settings

### For Developers
- Clean separation of concerns
- Extensible architecture for additional formats
- Comprehensive logging for debugging
- Well-documented API integration

## Testing

Comprehensive testing guide provided in `DEMUX_TESTING_GUIDE.md`:
- Basic functionality testing
- Multi-player compatibility testing
- Stream recovery testing
- Performance validation

## Compatibility

- **OS**: Windows 7+ (existing requirement)
- **Build**: Visual Studio 2015+ (existing requirement)
- **Players**: MPV, VLC, MPC-HC, MPC-BE
- **Streams**: All MPEG-TS compatible Twitch streams

## Performance Impact

- **CPU**: Slight increase due to demux processing
- **Memory**: ~1MB buffer per stream (configurable)
- **Storage**: Temporary files in system temp directory
- **Network**: Same as existing modes

## Future Enhancements

Potential areas for future improvement:
- Additional codec support (AV1, VP9, etc.)
- Custom player command line templates
- Stream quality per-track selection
- Advanced synchronization options

## Conclusion

This implementation provides a complete solution for MPEG-TS demuxing in Tardsplaya, addressing all requirements from issue #124. The integration is production-ready with comprehensive error handling, UI integration, and support for multiple media players.

The separate video/audio stream approach should significantly improve playback robustness during network discontinuities, providing a better user experience for Twitch stream viewing.
34 changes: 30 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ A C++ application that buffers Twitch streams to media players like MPC-HC, MPC-
- **Stream Buffering**: Downloads and buffers stream segments before sending to media player for smooth playback
- **Multiple Quality Support**: Automatically detects and allows selection of available stream qualities
- **Multi-Stream Support**: Open multiple tabs to watch different streams simultaneously
- **MPEG-TS Demux Mode**: NEW! Separates video and audio streams for better discontinuity recovery
- **Windows 7 Compatibility**: Includes certificate validation workarounds for older Windows versions
- **Real-time Logging**: Shows detailed logs of streaming operations
- **Modern C++ Implementation**: Clean, efficient C++17 code with minimal dependencies
Expand Down Expand Up @@ -124,19 +125,44 @@ A C++ application that buffers Twitch streams to media players like MPC-HC, MPC-

## Configuration

The application defaults to using MPV as the media player. To use a different player:

1. Go to **Tools → Settings** (when implemented)
2. Set the player path and arguments
The application defaults to using TX-Queue IPC mode for high-performance streaming. To change the streaming mode or media player:

1. Go to **Tools → Settings**
2. Configure your preferred options:
- **Player Path**: Set path to your media player (mpv.exe, vlc.exe, mpc-hc.exe, etc.)
- **Player Arguments**: Additional arguments (default: `-` for stdin)
- **Streaming Mode**: Choose from available modes:
- **TX-Queue IPC (High Performance)**: Default mode with lock-free queues
- **MPEG-TS Demux (Separate A/V)**: NEW! Demuxes streams for better discontinuity recovery
- **Transport Stream (TSDuck)**: Professional transport stream routing
- **HLS Segments (Fallback)**: Traditional segment-based streaming

### MPEG-TS Demux Mode

When using MPEG-TS Demux mode, the application:
- Separates video and audio into individual elementary stream files
- Uses media player-specific command line arguments:
- **MPC-HC/MPC-BE**: `/dub filename`
- **VLC**: `--input-slave=filename`
- **MPV**: `--audio-file=filename`
- Creates temporary files in system temp directory
- Helps media players recover from stream discontinuities more effectively

Default settings:
- Player: `mpv.exe`
- Arguments: `-` (reads from stdin)
- Streaming Mode: `TX-Queue IPC (High Performance)`

## Technical Improvements

This C++ version includes several improvements over the original:

### MPEG-TS Demux Integration
- Integrates janbar/demux-mpegts library for elementary stream separation
- Creates separate video and audio files for better discontinuity recovery
- Supports multiple media player command line formats (/dub, --audio-file, --input-slave)
- Helps prevent black screen/audio desync issues during stream discontinuities

### Windows 7 Compatibility
- Certificate validation bypass for HTTPS requests
- Compatible with older root certificate stores
Expand Down
Loading