Created
August 14, 2020 00:03
-
-
Save fernandoc1/93ce154a42b969119c898c9d5c962a7b to your computer and use it in GitHub Desktop.
Read H264 content from a directory and store it in a internal buffer.
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
| #include "channelwriter.hpp" | |
| #include <cstdint> | |
| #include <cstdio> | |
| #include <cstdlib> | |
| #include <cstring> | |
| #include <dirent.h> | |
| #include <fcntl.h> | |
| #include <iostream> | |
| #include <libgen.h> | |
| #include <signal.h> | |
| #include <sys/ioctl.h> | |
| #include <unistd.h> | |
| std::vector<uint8_t> buffer; | |
| int libSetVideoStreamData(int iVideoIndex, char* VideoData, int iDataLen) | |
| { | |
| buffer.resize(iDataLen); | |
| memcpy(buffer.data(), (uint8_t*)VideoData, iDataLen); | |
| ChannelWriter* writer = ChannelWriter::getChannelWriter(0); | |
| writer->writeFrame(buffer); | |
| return 0; | |
| } | |
| int readDirectory(const char* dirPath) | |
| { | |
| DIR* videoDir; | |
| struct dirent** namelist; | |
| int numberOfFiles = scandir(dirPath, &namelist, 0, alphasort); | |
| if(numberOfFiles < 0) | |
| { | |
| printf("Error reading directory\n"); | |
| return -1; | |
| } | |
| char* filepath = (char*)malloc(512); | |
| size_t bufferSize = 2048; | |
| uint8_t* buffer = (uint8_t*)malloc(bufferSize); | |
| for(int i = 0; i < numberOfFiles; i++) | |
| { | |
| snprintf(filepath, 512, "%s/%s", dirPath, namelist[i]->d_name); | |
| int filepathSize = strlen(filepath); | |
| printf("%s\n", filepath); | |
| if( (filepathSize > 8) | |
| && (filepath[filepathSize-5]=='.') | |
| && (filepath[filepathSize-4]=='h') | |
| && (filepath[filepathSize-3]=='2') | |
| && (filepath[filepathSize-2]=='6') | |
| && (filepath[filepathSize-1]=='4') | |
| ) | |
| { | |
| printf("Found h264 chunk\n"); | |
| } | |
| else | |
| { | |
| continue; | |
| } | |
| FILE* f = fopen(filepath, "rb"); | |
| if(!f) | |
| { | |
| printf("File error: %s\n", filepath); | |
| fclose(f); | |
| continue; | |
| } | |
| fseek(f, 0, SEEK_END); | |
| long fsize = ftell(f); | |
| fseek(f, 0, SEEK_SET); | |
| if(fsize > bufferSize) | |
| { | |
| buffer = (uint8_t*)realloc(buffer, fsize); | |
| bufferSize = fsize; | |
| } | |
| int amountRead = fread(buffer, 1, fsize, f); | |
| printf("Read: %d\n", amountRead); | |
| libSetVideoStreamData(0, (char*)buffer, fsize); | |
| //libSetVideoStreamData(1, (char*)buffer, fsize); | |
| //libSetVideoStreamData(2, (char*)buffer, fsize); | |
| //usleep(50000); | |
| usleep(30000); | |
| fclose(f); | |
| } | |
| printf("BufferSize: %ld\n", bufferSize); | |
| for(int i = 0; i < numberOfFiles; i++) | |
| { | |
| free(namelist[i]); | |
| } | |
| free(namelist); | |
| free(buffer); | |
| free(filepath); | |
| } | |
| int main(int argc, char** argv) | |
| { | |
| if(argc < 2) | |
| { | |
| printf("Usage: %s <folder path>\n", argv[0]); | |
| return 1; | |
| } | |
| while(true) | |
| { | |
| readDirectory(argv[1]); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment