Skip to content

feature/Add MP4 video handling and update IPCamera configuration #191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

cedricve
Copy link
Member

@cedricve cedricve commented May 22, 2025

Description

Pull Request Description: feature/Add MP4 video handling and update IPCamera configuration

Motivation

The primary motivation behind this pull request is to enhance the video handling capabilities of the project by integrating MP4 video processing and updating the IPCamera configuration. This improvement ensures more efficient, accurate, and standardized video recording and playback, which is essential for high-quality video surveillance and monitoring applications.

Changes and Improvements

  1. Integration of MP4 Video Handling:

    • Added the github.com/Eyevinn/mp4ff library to handle MP4 video files.
    • Implemented a new MP4 struct in machinery/src/video/mp4.go to manage MP4 video creation, track addition, and sample handling.
    • Updated HandleRecordStream in machinery/src/capture/main.go to utilize the new MP4 handling methods for video and audio streams.
    • Added sample conversion and track management functions to ensure proper video recording and playback.
  2. Update IPCamera Configuration:

    • Extended the IPCamera struct in machinery/src/models/Config.go to include SPS and PPS NALUs for H264 codec, which are critical for MP4 video initialization.
    • Updated Kerberos.go to set SPS and PPS values from the camera configuration dynamically.
    • Enhanced stream handling in gortsplib.go by adding stream index management for better organization and retrieval.
  3. Miscellaneous Improvements:

    • Refined the packet and stream handling logic to include stream indices, improving the clarity and management of different streams.
    • Ensured compatibility with existing video handling mechanisms while introducing new MP4 capabilities.

Benefits

  • Enhanced Video Quality: The new MP4 handling ensures that video files are created with proper codecs and configurations, improving playback quality and compatibility.
  • Improved Configuration Management: By updating the IPCamera configuration to include SPS and PPS NALUs, the project ensures that video streams are initialized correctly, reducing errors and improving reliability.
  • Streamlined Codebase: The integration of the mp4ff library and the introduction of new video handling methods make the codebase more modular and easier to maintain.

This pull request significantly improves the project's video handling capabilities, making it more robust and efficient for surveillance and monitoring applications.

- Introduced a new video package with MP4 struct for video file handling.
- Updated IPCamera struct to include SPS and PPS NALUs.
- Enhanced stream handling in the capture process to utilize the new MP4 library.
- Added stream index management for better tracking of video and audio streams.
@Copilot Copilot AI review requested due to automatic review settings May 22, 2025 06:49
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces an MP4 handling layer using the mp4ff library, extends IPCamera config to carry SPS/PPS NALUs, and updates capture flows to leverage the new MP4 package and stream indexing.

  • New video.MP4 type with init-segment, fragment and writing support
  • Added SPSNALUs/PPSNALUs to IPCamera and populated them in Kerberos.go
  • Updated capture routines to use video.MP4, manage stream indices, and write segments with both libraries

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
machinery/src/video/mp4.go New MP4 wrapper type and methods using Eyevinn/mp4ff
machinery/src/packets/stream.go Added Index field for stream identification
machinery/src/models/Config.go Extended IPCamera with SPS/PPS NALU slices
machinery/src/components/Kerberos.go Set IPCamera.SPSNALUs/PPSNALUs from discovered streams
machinery/src/capture/main.go Integrated video.MP4 into recording logic and file naming
machinery/src/capture/gortsplib.go Assigned Stream.Index when appending new streams
machinery/go.mod Added github.com/Eyevinn/mp4ff dependency
Comments suppressed due to low confidence (5)

machinery/src/video/mp4.go:8

  • Duplicate import of the same mp4ff package under two names; remove the redundant import and use a single alias.
mp4ff "github.com/Eyevinn/mp4ff/mp4"

machinery/src/video/mp4.go:47

  • [nitpick] Receiver name 'mp4' shadows the package alias and is non-idiomatic; consider renaming it to something like 'm' or 'p'.
func (mp4 *MP4) SetWidth(width int) {

machinery/src/capture/main.go:233

  • [nitpick] Go variable names should use CamelCase; rename 'new_name' to 'newName'.
new_name := s + "_new.mp4"

machinery/src/capture/main.go:251

  • The MP4 writer is initialized with 'new_fullName' but the muxer is writing to 'fullName'; align file paths so both write to the intended location.
mp4Video = video.NewMP4(new_fullName, spsNALUS, ppsNALUs)

machinery/src/video/mp4.go:1

  • No unit tests accompany the new MP4 logic; add tests covering initialization, sample addition, and output file writing.
package video

includePS := true
err := trak.SetAVCDescriptor("avc1", spsNALUs, ppsNALUs, includePS)
if err != nil {
panic(err)
Copy link
Preview

Copilot AI May 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid using panic for error handling in library code; return errors to the caller so they can be handled gracefully.

Suggested change
panic(err)
return nil, err

Copilot uses AI. Check for mistakes.

Comment on lines +61 to +63
// Add a video track to the MP4 file
// This is a placeholder function
// In a real implementation, this would add a video track to the MP4 file
Copy link
Preview

Copilot AI May 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is currently a stub with no implementation; either implement actual track-addition logic or remove it to avoid confusion.

Suggested change
// Add a video track to the MP4 file
// This is a placeholder function
// In a real implementation, this would add a video track to the MP4 file
// Create a new video track in the Init segment
videoTimescale := uint32(180000) // Example timescale for video
trackID := uint32(len(mp4.TrackIDs) + 1) // Generate a new track ID
err := mp4.Init.AddEmptyTrack(videoTimescale, "video", "und")
if err != nil {
panic(err)
}
// Set codec-specific parameters
trak := mp4.Init.Moov.Trak
includePS := true
err = trak.SetAVCDescriptor(codec, nil, nil, includePS)
if err != nil {
panic(err)
}
// Append the new track ID to the TrackIDs slice
mp4.TrackIDs = append(mp4.TrackIDs, trackID)

Copilot uses AI. Check for mistakes.

@@ -122,6 +125,10 @@ func HandleRecordStream(queue *packets.Queue, configDirectory string, configurat
// Write the last packet
ttime := convertPTS(pkt.TimeLegacy)
if pkt.IsVideo {

// New method using new mp4 library
mp4Video.AddSampleToTrack(0, pkt.Data, ttime, ttime-previousDuration)
Copy link
Preview

Copilot AI May 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] This sample-addition and duration logic is duplicated in several blocks; consider extracting it into a helper to reduce repetition.

Copilot uses AI. Check for mistakes.

@@ -1,6 +1,9 @@
package packets

type Stream struct {
// The ID of the stream.
Index int
Copy link
Preview

Copilot AI May 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The new 'Index' field lacks JSON/BSON struct tags; add tags if it needs to be serialized for consistency with other fields.

Suggested change
Index int
Index int `json:"index" bson:"index"`

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant