Skip to content

Commit

Permalink
Can't load ffmpeghelper
Browse files Browse the repository at this point in the history
  • Loading branch information
inzi committed Oct 20, 2018
1 parent 31a2662 commit 4359b14
Show file tree
Hide file tree
Showing 125 changed files with 30,217 additions and 47 deletions.
18 changes: 18 additions & 0 deletions RtspCapture.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,38 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RtspCapture", "RtspCapture\
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RtspClientSharp", "RtspClientSharp\RtspClientSharp.csproj", "{1E3DCB5C-FA9D-4A6B-AADC-0FF971989A74}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libffmpeghelper", "libffmpeghelper\libffmpeghelper.vcxproj", "{3C96BD24-3212-4CD8-86E3-63D1E3E38155}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5DC5AECB-3072-4B82-93EF-CE0990B21004}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5DC5AECB-3072-4B82-93EF-CE0990B21004}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5DC5AECB-3072-4B82-93EF-CE0990B21004}.Debug|x86.ActiveCfg = Debug|Any CPU
{5DC5AECB-3072-4B82-93EF-CE0990B21004}.Debug|x86.Build.0 = Debug|Any CPU
{5DC5AECB-3072-4B82-93EF-CE0990B21004}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5DC5AECB-3072-4B82-93EF-CE0990B21004}.Release|Any CPU.Build.0 = Release|Any CPU
{5DC5AECB-3072-4B82-93EF-CE0990B21004}.Release|x86.ActiveCfg = Release|Any CPU
{5DC5AECB-3072-4B82-93EF-CE0990B21004}.Release|x86.Build.0 = Release|Any CPU
{1E3DCB5C-FA9D-4A6B-AADC-0FF971989A74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1E3DCB5C-FA9D-4A6B-AADC-0FF971989A74}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1E3DCB5C-FA9D-4A6B-AADC-0FF971989A74}.Debug|x86.ActiveCfg = Debug|Any CPU
{1E3DCB5C-FA9D-4A6B-AADC-0FF971989A74}.Debug|x86.Build.0 = Debug|Any CPU
{1E3DCB5C-FA9D-4A6B-AADC-0FF971989A74}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1E3DCB5C-FA9D-4A6B-AADC-0FF971989A74}.Release|Any CPU.Build.0 = Release|Any CPU
{1E3DCB5C-FA9D-4A6B-AADC-0FF971989A74}.Release|x86.ActiveCfg = Release|Any CPU
{1E3DCB5C-FA9D-4A6B-AADC-0FF971989A74}.Release|x86.Build.0 = Release|Any CPU
{3C96BD24-3212-4CD8-86E3-63D1E3E38155}.Debug|Any CPU.ActiveCfg = Debug|Win32
{3C96BD24-3212-4CD8-86E3-63D1E3E38155}.Debug|x86.ActiveCfg = Debug|Win32
{3C96BD24-3212-4CD8-86E3-63D1E3E38155}.Debug|x86.Build.0 = Debug|Win32
{3C96BD24-3212-4CD8-86E3-63D1E3E38155}.Release|Any CPU.ActiveCfg = Release|Win32
{3C96BD24-3212-4CD8-86E3-63D1E3E38155}.Release|x86.ActiveCfg = Release|Win32
{3C96BD24-3212-4CD8-86E3-63D1E3E38155}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
108 changes: 74 additions & 34 deletions RtspCapture/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Threading;
using System.Threading.Tasks;
using System.Drawing;
using RtspCapture.processor;
using RtspCapture.RawFramesReceiving;

namespace RtspCapture
{
Expand All @@ -23,28 +25,65 @@ public class Options
[Option('i', "interval", Required = false, HelpText = "Snapshots saving interval in seconds")]
public int Interval { get; set; } = 5;
}
private static readonly RTSPProcessor rTSPProcessor = new RTSPProcessor();
//private RawFramesSource _rawFramesSource;

public event EventHandler<string> StatusChanged;

//public IVideoSource VideoSource => _realtimeVideoSource;
static void Main(string[] args)
{
Parser.Default.ParseArguments<Options>(args)
.WithParsed(options =>
{
var cancellationTokenSource = new CancellationTokenSource();

Task makeSnapshotsTask = MakeSnapshotsAsync(options, cancellationTokenSource.Token);

Console.ReadKey();
Parser.Default.ParseArguments<Options>(args)
.WithParsed(options =>
{
var cancellationTokenSource = new CancellationTokenSource();

//Task makeSnapshotsTask = MakeSnapshotsAsync(options, cancellationTokenSource.Token);
StartCapture(options);
Console.ReadKey();

cancellationTokenSource.Cancel();
makeSnapshotsTask.Wait();
})
.WithNotParsed(options =>
{
Console.WriteLine("Usage example: MjpegSnapshotsMaker.exe " +
cancellationTokenSource.Cancel();
//makeSnapshotsTask.Wait();
})
.WithNotParsed(options =>
{
Console.WriteLine("Usage example: MjpegSnapshotsMaker.exe " +
"-u rtsp://admin:[email protected]:554/ucast/11 " +
"-p S:\\Temp");
});
});

Console.WriteLine("Press any key to cancel");
Console.ReadLine();

}

private static void StartCapture(Options options)
{
//if (_rawFramesSource != null)
// return;
if (!Directory.Exists(options.Path))
Directory.CreateDirectory(options.Path);

int intervalMs = options.Interval * 1000;
int lastTimeSnapshotSaved = Environment.TickCount - intervalMs;

var connectionParameters = new ConnectionParameters(options.Uri);

RawFramesSource _rawFramesSource = new RawFramesSource(connectionParameters);
_rawFramesSource.ConnectionStatusChanged += ConnectionStatusChanged;

rTSPProcessor.SetRawFramesSource(_rawFramesSource);

_rawFramesSource.Start();
}

private static void ConnectionStatusChanged(object sender, string s)
{
//StatusChanged?.Invoke(this, s);
}

private static async Task MakeSnapshotsAsync(Options options, CancellationToken token)
{
try
Expand All @@ -56,37 +95,38 @@ private static async Task MakeSnapshotsAsync(Options options, CancellationToken
int lastTimeSnapshotSaved = Environment.TickCount - intervalMs;

var connectionParameters = new ConnectionParameters(options.Uri);
using (var rtspClient = new RtspClient(connectionParameters))
{
rtspClient.FrameReceived += (sender, frame) =>
{

//using (var rtspClient = new RtspClient(connectionParameters))
//{
// rtspClient.FrameReceived += (sender, frame) =>
// {

int ticksNow = Environment.TickCount;
// int ticksNow = Environment.TickCount;

if (Math.Abs(ticksNow - lastTimeSnapshotSaved) < intervalMs)
return;
// if (Math.Abs(ticksNow - lastTimeSnapshotSaved) < intervalMs)
// return;

lastTimeSnapshotSaved = ticksNow;
// lastTimeSnapshotSaved = ticksNow;

string snapshotName = frame.Timestamp.ToString("O").Replace(":", "_") + ".jpg";
string path = Path.Combine(options.Path, snapshotName);
// string snapshotName = frame.Timestamp.ToString("O").Replace(":", "_") + ".jpg";
// string path = Path.Combine(options.Path, snapshotName);

ArraySegment<byte> frameSegment = frame.FrameSegment;
// ArraySegment<byte> frameSegment = frame.FrameSegment;

var stream = new MemoryStream(frameSegment.Array, 0, frameSegment.Count);
// //var stream = new MemoryStream(frameSegment.Array, 0, frameSegment.Count);

Image image = Image.FromStream(stream);
// //Image image = Image.FromStream(stream);

image.Save($"[{DateTime.UtcNow}] Snapshot is saved to {snapshotName}");
// //image.Save($"[{DateTime.UtcNow}] Snapshot is saved to {snapshotName}");

Console.WriteLine($"[{DateTime.UtcNow}] Snapshot is saved to {snapshotName}");
};
// Console.WriteLine($"[{DateTime.UtcNow}] Snapshot is saved to {snapshotName}");
// };

Console.WriteLine("Connecting...");
await rtspClient.ConnectAsync(token);
Console.WriteLine("Receiving...");
await rtspClient.ReceiveAsync(token);
}
// Console.WriteLine("Connecting...");
// await rtspClient.ConnectAsync(token);
// Console.WriteLine("Receiving...");
// await rtspClient.ReceiveAsync(token);
//}
}
catch (OperationCanceledException)
{
Expand Down
16 changes: 14 additions & 2 deletions RtspCapture/RtspCapture.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,21 @@
<PackageReference Include="CommandLineParser" Version="2.3.0" />
<PackageReference Include="System.Drawing.Common" Version="4.5.1" />
</ItemGroup>

<ItemGroup>
<Content Include="x86\avcodec-58.dll" />
<Content Include="x86\avdevice-58.dll" />
<Content Include="x86\avfilter-7.dll" />
<Content Include="x86\avformat-58.dll" />
<Content Include="x86\avutil-56.dll" />
<Content Include="x86\libffmpeghelper.dll" />
<Content Include="x86\postproc-55.dll" />
<Content Include="x86\swresample-3.dll" />
<Content Include="x86\swscale-5.dll" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\RtspClientSharp\RtspClientSharp.csproj" />
</ItemGroup>

<PropertyGroup>
<PostBuildEvent>xcopy $(ProjectDir)\x86\*.dll $(TargetDir) /Y /E /C /F</PostBuildEvent>
</PropertyGroup>
</Project>
25 changes: 14 additions & 11 deletions RtspCapture/processor/RTSPProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,29 +71,32 @@ private void OnFrameReceived(object sender, RawFrame rawFrame)
if (!decoder.TryDecode(rawVideoFrame, out DecodedVideoFrameParameters decodedFrameParameters))
return;

long desiredSize = Interlocked.Read(ref _desiredSize);
//long desiredSize = (long)rawFrame. << 32 | (uint)height;

//long desiredSize = Interlocked.Read(ref _desiredSize);

int targetWidth;
int targetHeight;

int bufferSize;

if (desiredSize == 0)
{
//if (desiredSize == 0)
//{
targetWidth = decodedFrameParameters.Width;
targetHeight = decodedFrameParameters.Height;

bufferSize = decodedFrameParameters.Height *
ImageUtils.GetStride(decodedFrameParameters.Width, PixelFormat.Bgr24);
}
else
{
targetWidth = (int)(desiredSize >> 32);
targetHeight = (int)desiredSize;
//}
//else
//{
// targetWidth = (int)(desiredSize >> 32);
// targetHeight = (int)desiredSize;

// bufferSize = targetHeight *
// ImageUtils.GetStride(targetWidth, PixelFormat.Bgr24);
//}

bufferSize = targetHeight *
ImageUtils.GetStride(targetWidth, PixelFormat.Bgr24);
}

if (_decodedFrameBuffer.Length != bufferSize)
_decodedFrameBuffer = new byte[bufferSize];
Expand Down
26 changes: 26 additions & 0 deletions libffmpeghelper/dllmain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// dllmain.cpp : Defines the entry point for the DLL application.

#include "stdafx.h"

BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
avcodec_register_all();
#if defined(_DEBUG)
av_log_set_level(AV_LOG_VERBOSE);
#else
av_log_set_level(AV_LOG_PANIC);
#endif
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
17 changes: 17 additions & 0 deletions libffmpeghelper/export.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#ifdef _WINDLL
#define DllExport(rettype) extern "C" __declspec(dllexport) rettype __cdecl
#else
#define DllExport(rettype) extern "C" __attribute__((cdecl)) rettype
#endif

DllExport(int) create_video_decoder(int codec_id, void **handle);
DllExport(int) set_video_decoder_extradata(void *handle, void *extradata, int extradataLength);
DllExport(int) decode_video_frame(void *handle, void *rawBuffer, int rawBufferLength, int *frameWidth, int *frameHeight, int *framePixelFormat);
DllExport(int) scale_decoded_video_frame(void *handle, void *scalerHandle, void *scaledBuffer, int scaledBufferStride);
DllExport(void) remove_video_decoder(void *handle);

DllExport(int) create_video_scaler(int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight, int sourcePixelFormat,
int scaledWidth, int scaledHeight, int scaledPixelFormat, int quality, void **handle);
DllExport(void) remove_video_scaler(void *handle);
36 changes: 36 additions & 0 deletions libffmpeghelper/include/libavcodec/ac3_parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* AC-3 parser prototypes
* Copyright (c) 2003 Fabrice Bellard
* Copyright (c) 2003 Michael Niedermayer
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef AVCODEC_AC3_PARSER_H
#define AVCODEC_AC3_PARSER_H

#include <stddef.h>
#include <stdint.h>

/**
* Extract the bitstream ID and the frame size from AC-3 data.
*/
int av_ac3_parse_header(const uint8_t *buf, size_t size,
uint8_t *bitstream_id, uint16_t *frame_size);


#endif /* AVCODEC_AC3_PARSER_H */
37 changes: 37 additions & 0 deletions libffmpeghelper/include/libavcodec/adts_parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef AVCODEC_ADTS_PARSER_H
#define AVCODEC_ADTS_PARSER_H

#include <stddef.h>
#include <stdint.h>

#define AV_AAC_ADTS_HEADER_SIZE 7

/**
* Extract the number of samples and frames from AAC data.
* @param[in] buf pointer to AAC data buffer
* @param[out] samples Pointer to where number of samples is written
* @param[out] frames Pointer to where number of frames is written
* @return Returns 0 on success, error code on failure.
*/
int av_adts_header_parse(const uint8_t *buf, uint32_t *samples,
uint8_t *frames);

#endif /* AVCODEC_ADTS_PARSER_H */
Loading

0 comments on commit 4359b14

Please sign in to comment.