-
Notifications
You must be signed in to change notification settings - Fork 2.3k
PushAV Initial Implementation of Clip recorder and uploader #38998
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
Merged
chulspro
merged 9 commits into
project-chip:master
from
raveendra-karu:pushav/clip_record_and_upload
Jul 12, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
df904cd
Resolve conflicts.
raveendra-karu 1ab5fe9
fixed sign conversion warnings
raveendra-karu 403b635
Restyled by clang-format
restyled-commits 2e1452d
Use latest docker image 148
raveendra-karu ec4d88c
Use latest docker image for tests.
raveendra-karu 7766dbd
Use latest docker image for java tests.
raveendra-karu 1d09ce1
Use latest docker image for tests.
raveendra-karu 0b91805
Update camera-app README.md
raveendra-karu f5f7fbd
Fix CI spell check errors.
raveendra-karu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -573,6 +573,7 @@ FFF | |
| FFFFFFFFFFFF0102 | ||
| fffe | ||
| fffff | ||
| FFmpeg | ||
| Fi | ||
| filepath | ||
| fini | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
226 changes: 226 additions & 0 deletions
226
examples/camera-app/linux/include/pushav-clip-recorder.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,226 @@ | ||
| /* | ||
| * | ||
| * Copyright (c) 2025 Project CHIP Authors | ||
| * All rights reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| #pragma once | ||
|
|
||
| #include "pushav-uploader.h" | ||
|
|
||
| #include <algorithm> | ||
| #include <atomic> | ||
| #include <condition_variable> | ||
| #include <memory> | ||
| #include <mutex> | ||
| #include <queue> | ||
| #include <string> | ||
| #include <thread> | ||
| #include <vector> | ||
|
|
||
| // FFmpeg headers | ||
| extern "C" { | ||
| #include <libavcodec/avcodec.h> | ||
| #include <libavformat/avformat.h> | ||
| #include <libavutil/avutil.h> | ||
| #include <libavutil/error.h> | ||
| #include <libavutil/opt.h> | ||
| #include <libavutil/timestamp.h> | ||
| } | ||
|
|
||
| /** | ||
| * @struct BufferData | ||
| * @brief Contains buffer information for custom IO operations | ||
| */ | ||
| struct BufferData | ||
| { | ||
| uint8_t * mPtr; ///< Pointer to buffer data | ||
| size_t mSize; ///< Size left in the buffer | ||
| }; | ||
|
|
||
| /** | ||
| * @class PushAVClipRecorder | ||
| * @brief Manages multimedia clip recording with DASH/CMAF segmentation | ||
| * | ||
| * Handles video/audio stream processing, packet queueing, and segmented file output | ||
| */ | ||
| class PushAVClipRecorder | ||
| { | ||
| public: | ||
| /** | ||
| * @struct ClipInfoStruct | ||
| * @brief Contains clip configuration and runtime state | ||
| */ | ||
| struct ClipInfoStruct | ||
| { | ||
| bool mHasVideo; ///< Video recording enabled flag | ||
| bool mHasAudio; ///< Audio recording enabled flag | ||
| int mClipId; ///< Current clip identifier | ||
| uint32_t mMaxClipDuration; ///< Maximum clip duration in seconds | ||
| uint16_t mInitialDuration; ///< Initial clip duration in seconds | ||
| uint16_t mAugmentationDuration; ///< Duration increment on motion detect | ||
| uint16_t mChunkDuration; ///< Segment duration in seconds | ||
| uint16_t mBlindDuration; ///< Duration without recording after motion stop | ||
| std::string mRecorderId; ///< Unique recorder identifier | ||
| std::string mOutputPath; ///< Base output directory path | ||
| AVRational mInputTimeBase; ///< Input time base | ||
| std::string mUrl; ///< URL for uploading clips; | ||
| int mTriggerType; ///< Recording trigger type | ||
| std::chrono::steady_clock::time_point activationTime; ///< Time when the recording started | ||
| int mPreRollLength; ///< Pre-roll length in seconds | ||
| }; | ||
|
|
||
| /** | ||
| * @struct AudioInfoStruct | ||
| * @brief Audio stream configuration parameters | ||
| */ | ||
| struct AudioInfoStruct | ||
| { | ||
| uint64_t mChannelLayout; ///< Audio channel layout | ||
| int mChannels; ///< Number of audio channels | ||
| AVCodecID mAudioCodecId; ///< Audio codec identifier | ||
| int mSampleRate; ///< Sampling rate in Hz | ||
| int mBitRate; ///< Audio bitrate in bps | ||
| int64_t mAudioPts; ///< Audio presentation timestamp | ||
| int64_t mAudioDts; ///< Audio decoding timestamp | ||
| int mAudioStreamIndex; ///< Audio stream index | ||
| int mAudioFrameDuration; ///< Audio frame duration in samples | ||
| AVRational mAudioTimeBase; ///< Audio time base | ||
| }; | ||
|
|
||
| /** | ||
| * @struct VideoInfoStruct | ||
| * @brief Video stream configuration parameters | ||
| */ | ||
| struct VideoInfoStruct | ||
| { | ||
| AVCodecID mVideoCodecId; ///< Video codec identifier | ||
| int64_t mVideoPts; ///< Video presentation timestamp | ||
| int64_t mVideoDts; ///< Video decoding timestamp | ||
| int mWidth; ///< Video frame width | ||
| int mHeight; ///< Video frame height | ||
| int mFrameRate; ///< Video frame rate (fps) | ||
| int mVideoFrameDuration; ///< Video frame duration (μs) | ||
| AVRational mVideoTimeBase; ///< Video time base | ||
| int mVideoStreamIndex; ///< Video stream index | ||
| uint32_t mBitRate; ///< Video bitrate in bps | ||
| }; | ||
|
|
||
| /// @name Construction/Destruction | ||
| /// @{ | ||
| PushAVClipRecorder(ClipInfoStruct & aClipInfo, AudioInfoStruct & aAudioInfo, VideoInfoStruct & aVideoInfo, | ||
| PushAVUploader * aUploader); | ||
| ~PushAVClipRecorder(); | ||
| /// @} | ||
|
|
||
| /// @name Recording Control | ||
| /// @{ | ||
| void Start(); | ||
| void Stop(); | ||
| /// @} | ||
|
|
||
| /** | ||
| * @brief Enqueues media data for processing | ||
| * @param data Raw media data pointer | ||
| * @param size Data size in bytes | ||
| * @param isVideo True for video data, false for audio | ||
| */ | ||
| void PushPacket(const char * data, size_t size, bool isVideo); | ||
|
|
||
| std::atomic<bool> mDeinitializeRecorder{ false }; ///< Deinitialization flag | ||
| ClipInfoStruct mClipInfo; ///< Clip configuration parameters | ||
| void SetRecorderStatus(bool status); ///< Sets the recorder status | ||
| bool GetRecorderStatus(); ///< Gets the recorder status | ||
|
|
||
| private: | ||
| long unsigned int kMaxQueueSize = 500; ///< Maximum queue size for media packets | ||
| std::atomic<bool> mRunning{ false }; ///< Recording activity flag | ||
|
|
||
| /// @name Stream Configuration | ||
| /// @{ | ||
| AudioInfoStruct mAudioInfo; ///< Audio stream parameters | ||
| VideoInfoStruct mVideoInfo; ///< Video stream parameters | ||
| /// @} | ||
|
|
||
| AVFormatContext * mFormatContext; | ||
| AVFormatContext * mInputFormatContext; | ||
| AVStream * mVideoStream; | ||
| AVStream * mAudioStream; | ||
| AVCodecContext * mAudioEncoderContext; | ||
| std::thread mWorkerThread; | ||
| std::mutex mQueueMutex; | ||
| std::condition_variable mCondition; | ||
|
|
||
| std::queue<AVPacket *> mAudioQueue; | ||
| std::queue<AVPacket *> mVideoQueue; | ||
|
|
||
| int mAudioFragment = 1; | ||
| int mVideoFragment = 1; | ||
| int64_t mCurrentClipStartPts = AV_NOPTS_VALUE; | ||
| int64_t mFoundFirstIFramePts = -1; | ||
| int64_t currentPts = AV_NOPTS_VALUE; | ||
| bool mMetadataSet = false; | ||
| bool mUploadedInitSegment = false; | ||
| bool mUploadMPD = false; | ||
|
|
||
| PushAVUploader * mUploader; | ||
|
|
||
| /// @name Internal Methods | ||
| /// @{ | ||
| bool FileExists(const std::string & path); | ||
| bool CheckAndUploadFile(std::string path); | ||
| bool IsH264IFrame(const uint8_t * data, unsigned int length); | ||
| AVPacket * CreatePacket(const uint8_t * data, int size, bool isVideo); | ||
|
|
||
| /** | ||
| * @brief Processes queued packets and writes them to the output file. | ||
| * @return Zero if processing was successful, negative otherwise, positive for warnings. | ||
| */ | ||
| int ProcessBuffersAndWrite(); | ||
|
|
||
| /** | ||
| * @brief Configures the output format context for DASH/CMAF VoD. | ||
| * | ||
| * @param output_prefix Base path for output files. | ||
| * @param init_seg_pattern Pattern for initialization segments. | ||
| * @param media_seg_pattern Pattern for media segments. | ||
| */ | ||
| int SetupOutput(const std::string & outputPrefix, const std::string & initSegPattern, const std::string & mediaSegPattern); | ||
|
|
||
| /** | ||
| * @brief Starts the clip recording process. | ||
| * @return 0 on success, error code otherwise. | ||
| */ | ||
| int StartClipRecording(); | ||
|
|
||
| /** | ||
| * @brief Adds a video or audio stream to the output context. | ||
| * | ||
| * @param type The type of stream to add (AVMEDIA_TYPE_VIDEO or AVMEDIA_TYPE_AUDIO). | ||
| */ | ||
| int AddStreamToOutput(AVMediaType type); | ||
|
|
||
| /** | ||
| * @brief Cleans up the output context and associated resources. | ||
| */ | ||
| void CleanupOutput(); | ||
|
|
||
| /** | ||
| * @brief Finalizes the current clip and prepares for a new one. | ||
| * | ||
| * @param reason Zero for normal clip finalization or Positive number for abrupt finalization | ||
| */ | ||
| void FinalizeCurrentClip(int reason); | ||
| /// @} | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.