Finding duration of a video using directshowlib-2005 - asp.net

My asp.net(c#) method looks as follows:
static public bool GetVideoLength(string fileName, out double length)
{
DirectShowLib.FilterGraph graphFilter = new DirectShowLib.FilterGraph();
DirectShowLib.IGraphBuilder graphBuilder;
DirectShowLib.IMediaPosition mediaPos;
length = 0.0;
try
{
graphBuilder = (DirectShowLib.IGraphBuilder)graphFilter;
graphBuilder.RenderFile(fileName, null);
mediaPos = (DirectShowLib.IMediaPosition)graphBuilder;
mediaPos.get_Duration(out length);
return true;
}
catch
{
return false;
}
finally
{
mediaPos = null;
graphBuilder = null;
graphFilter = null;
}
}
I got the duration with the above method. But my problem is i can't delete the physical file
after my operation. I used
File.Delete(FilePath);
While performing this action i got an exception as follows:
"The process cannot access the file because it is being used by another process."
My Os is windows 7(IIS 7)
Any one please help me to sort this out?

I've got no experience in coding directshow apps in C#, but plenty of experience in C++.
DirectShow is based on a technology called COM - which uses reference counting to tell it when an object is in use.
It would use a COM object to represent the IGraphBuilder for example.
In C++, we would have to deconstruct the graph, by removing all its filters, then release the graph.
I understand that C# has its own garbage collection etc., but unless you explicitly release the objects you use, they'll remain in memory.
It seems from the code you've quoted, that the graph is still opened, even though playback may have finished. In that case, it'll hold a reference to the file which you've played back, which would explain why you can't delete it - e.g. there's a read lock on the file.
Hope this points you in the right direction!

Related

Process to find camera pointer in a game using cheat engine?

I am trying to find out how to get a mouse working inside RPCS3 emulator for a game (Ghost Recon Future Soldier with patch 1.05)
There is a library that supports injecting the mouse but doesn't support the game I am trying to play. After a lot of digging, I found a library that actually implements mouse injection in a few games.
Sample implementation for the KillZone3 game to support mouse injection looks like this in C#
using KAMI.Core.Cameras;
using KAMI.Core.Utilities;
using System;
namespace KAMI.Core.Games
{
public class Killzone2PS3 : Game<HVecVACamera>
{
DerefChain m_hor;
DerefChain m_vert;
public Killzone2PS3(IntPtr ipc, string version) : base(ipc)
{
uint baseAddress = version switch
{
"01.01" => 0x117e740 + 0x234,
"01.29" => 0x11B0540 + 0x234,
_ => throw new NotImplementedException($"{nameof(Killzone2PS3)} [v'{version}'] is not implemented"),
};
var baseChain = DerefChain.CreateDerefChain(ipc, baseAddress, 0x0);
m_vert = baseChain.Chain(0x80).Chain(0x5c).Chain(0x11c).Chain(0x78);
m_hor = baseChain.Chain(0x78).Chain(0x0).Chain(0x68).Chain(0xc).Chain(0x90);
}
public override void UpdateCamera(int diffX, int diffY)
{
if (DerefChain.VerifyChains(m_hor, m_vert))
{
m_camera.HorY = IPCUtils.ReadFloat(m_ipc, (uint)m_hor.Value);
m_camera.HorX = IPCUtils.ReadFloat(m_ipc, (uint)(m_hor.Value + 4));
m_camera.Vert = IPCUtils.ReadFloat(m_ipc, (uint)m_vert.Value);
m_camera.Update(diffX * SensModifier, -diffY * SensModifier);
IPCUtils.WriteFloat(m_ipc, (uint)m_hor.Value, m_camera.HorY);
IPCUtils.WriteFloat(m_ipc, (uint)(m_hor.Value + 4), m_camera.HorX);
IPCUtils.WriteFloat(m_ipc, (uint)m_vert.Value, m_camera.Vert);
}
}
}
}
Main lines in the above program are those addresses which I believe are associated with the camera pointer stored in memory obtained mostly with Cheat Engine.
What is the process required to find these pointers for my game. I am aware that is may be different for each game but I could really use some direction here. Where do I start? How do I narrow down till I arrive at this pointer
What a coincidence, I am also doing the exact same thing on RPCS3 right now. After digging around I've found some videos that discuss into how to use Cheat Engine to find where a player's position and camera would be stored. It involves making a lot of separate scans in Cheat Engine to search for unknown values that are increasing/decreasing.
This video seems to be the closest thing to what you are looking for:
https://www.youtube.com/watch?v=yAl_6qg6ZnA
You should also make sure you set up Cheat Engine so it works with RPCS3 correctly as shown here:
https://exvsfbce.home.blog/2019/08/24/basic-cheat-engine-setup-on-rpcs3/
After you've found the correct pointer for the camera it should be fairly easy to implement it into the library by making your own class in the Games folder.

Microsoft's MPEG-2 demuxer filter - can I change an elementary stream pin's PID while the graph is running?

I'm working with multi-program UDP MPEG-2 TS streams that, -unfortunately- dynamically re-map their elementary stream PIDs at random intervals. The stream is being demuxed using Microsoft's MPEG-2 demultiplexer filter.
I'm using the PSI-Parser filter (an example filter included in the DirectShow base classes) in order to react to the PAT/PMT changes.
The code is properly reacting to the change, yet I am experiencing some odd crashes (heap memory corruption) right after I remap the Demuxer pins to their new ID's. (The re-mapping is performed inside the thread that is processing graph events, while the EC_PROGRAMCHANGED message is being processed).
The crash could be due to faulty code in my part, yet I have not found any reference that tells me if changing the pin PID mapping is safe while the graph is running.
Can anyone provide some info if this is operation is safe, and if it is not, what could I do to minimize capture disruption?
I managed to find the source code for a Windows CE version of the demuxer filter. Inspecting it, indeed, it seems that it is safe to remap a pin while the filter is running.
I also managed to find the source of my problems with the PSI-Parser filter.
When a new transport stream is detected, or the PAT version changes, the PAT is flushed, (all programs are removed, the table is re-parsed and repopulated).
There is a subtle bug within the CPATProcessor::flush() method.
//
// flush
//
// flush an array of struct: m_mpeg2_program[];
// and unmap all PMT_PIDs pids, except one: PAT
BOOL CPATProcessor::flush()
{
BOOL bResult = TRUE;
bResult = m_pPrograms->free_programs(); // CPrograms::free_programs() call
if(bResult == FALSE)
return bResult;
bResult = UnmapPmtPid();
return bResult;
}// flush
Here's the CPrograms::free_programs() implementation.
_inline BOOL free_programs()
{
for(int i= 0; i<m_ProgramCount; i++){
if(!HeapFree(GetProcessHeap(), 0, (LPVOID) m_programs[i] ))
return FALSE;
}
return TRUE;
}
The problem here is that the m_ProgramCount member is never cleared. So, -apart from reporting the wrong number of programs in the table after a flush (since it is updated incrementally for each program found in the table)-, the next time the table is flushed, it will try to release memory that was already released.
Here's my updated version that fixes the heap corruption errors:
_inline BOOL free_programs()
{
for(int i= 0; i<m_ProgramCount; i++){
if(!HeapFree(GetProcessHeap(), 0, (LPVOID) m_programs[i] ))
return FALSE;
}
m_ProgramCount = 0; // This was missing, next call will try to free memory twice
return TRUE;
}

Working with in memory FeatureSet and Feature DotSpatial

I'm a beginner with DotSpatial and I'm stuck with a problem.
I try to work with in memory feature (to keep them only in Map) until the user hit save button.
The basic idea is that the user imports some dxf files and creates featureset based on layer name if the FeatureSet doesn't all ready exist, and for each polyline from dxf creates feature which will be added into a featureset.
public IFeature AddPoligons(EntityObject polyline, List<Text> textInDxf)
{
IFeatureSet featureSet = null;
//Getting the right featureset from map
foreach (var x in appManager.Map.GetPolygonLayers())
{
if (!string.Equals(x.DataSet.Name, layerName)) continue;
featureSet = x.DataSet as FeatureSet;
break;
}
var polygon = CreatePolygon(polyline);
//Creating feature and adding to FeatureSet
var feature = featureSet.AddFeature(polygon);
//Populating DataRow with information
AddinfoIntoDataTable(feature, polygon, textInDxf);
return feature;
}
Everything thing looks fine, the map is updated with new polygons, can select them, view information from datatable BUT, when I try to delete a selected feature (polygon) it throws an IndexOutOfRange exception.
What I'm doing wrong there?
Please help! Thank you and sorry for my poor English.
P.S.
When I created the featureSet first I saved it on the disk and then loaded into map with Map.AddLayer() method.
Meanwhile I found the solution somewhere on CodePlex-DotSpatial forum, and was posted by Jany.
The idea is to call this pice of code after each action against a FeatureSet (add, remove, move)
featureLayer.DataSet.UpdateExtent();
featureLayer.DataSet.InitializeVertices();
featureLayer.LabelLayer?.CreateLabels();
if(save)
{
featureLayer.DataSet.Save();
featureLayer.DataSet.Close();
}
featureLayer.AssignFastDrawnStates();
AppManager.Map.Refresh();
AppManager.Map.ResetBuffer();
where featureLayer is IFeatureLayer.
There is one problem:
If we save the project with AppManager.Serialization.SaveProject() and then open the project AppManager.Serialization.OpenProject() the issue appear again.
As a solution for this I call my Save method after open the project and on short is look like this:
public void Save()
{
foreach(var featureLayer in AppManager.Map.GetPolygonLayers())
{
featureLayer.DataSet.Save();
}
}
Hope that helps someone.

DirectShow .Net AddSourceFilter Release file

I have an application which plays video using DirectShow.Net and also records video. When I try to record to a filename that has just been used for playback by DirectShow it fails as the file is still in use. Sometimes it will work but take anywhere from 5 - 60 seconds until the file is unlocked. Before the recording is attempted the playback graph has definitely been destroyed. The code for creating and destroying the graph is below. If I stop and start my application following playback I can record to the same filename and there are no file locking issues.
Can anyone advise how I can correctly release the source filter so the file is not locked?
Creating the graph
try
{
graphBuilder = (IGraphBuilder)new FilterGraph();
#if DEBUG
// "Connect to remote graph" in GraphEdit
rotEntry = new DsROTEntry(graphBuilder);
#endif
hr = graphBuilder.AddSourceFilter(filename, filename, out baseFilter);
DsError.ThrowExceptionForHR(hr);
vmr9 = (IBaseFilter)new VideoMixingRenderer9();
ConfigureVMR9InWindowlessMode();
hr = graphBuilder.AddFilter(vmr9, "Video Mixing Renderer 9");
DsError.ThrowExceptionForHR(hr);
FilterGraphTools.ConnectFilters(graphBuilder, baseFilter, "Output", vmr9, "VMR Input0", true);
}
Destroying the graph
if (vmr9 != null)
{
Marshal.ReleaseComObject(vmr9);
vmr9 = null;
windowlessCtrl = null;
}
if (graphBuilder != null)
{
// Remove and release all filters
FilterGraphTools.RemoveAllFilters(graphBuilder);
Marshal.ReleaseComObject(graphBuilder);
graphBuilder = null;
baseFilter = null;
}
#if DEBUG
if (rotEntry != null)
{
rotEntry.Dispose();
rotEntry = null;
}
#endif
Eventually the graph is a set of connected COM objects, and successful graph termination depends on correct release, without any leaked references. References that you might have left unreleased are leading to objects kept alive and possibly locking certain resources.
The best you can do is explicit termination/removal of individual objects:
Stop the graph
Remove all fitlers explicitly using IFilterGraph2.RemoveFilter
Use filter dependent methods calls to possibly terminate individual filters, such as by putting empty path to file source/sink filters
If even leak takes place, the graph should no longer reference resources. Note that you sometimes can also reuse filters if you are re-creating the graph.

Why is Dictionary crashing when I am sure I am accessing different value references synchronously?

During adding key to dictionary, it is crashing if I don't lock on it and giving NullReferenceException which is reasonable
Rarely sometimes it is also crashing when I am adding element to the Dictionary Value (list reference), which is weird...
I have another question as well. The files are in text format. Sometimes reading them takes 1890 ms, and other times it is taking 10 times as much as that. The runs are made consecutive. Is there a possiblity that something is busying the I/O buffer suddenly
Any recommendation to at least stablize this...
private static void ParallelReadAndCalculate(FileInfo[] files)
{
Stopwatch sw1 = Stopwatch.StartNew();
while (!Parallel.ForEach(files, (FileInfo file) => ReadFileToEnd(file)).IsCompleted) ;
Console.WriteLine(sw1.ElapsedMilliseconds);
}
private static void ReadFileToEnd(FileInfo file)
{
string key = file.Name.Split('.')[0];
lock (ListOfCompanyData)
if (!ListOfCompanyData.ContainsKey(key))
{
ListOfCompanyData.Add(key, new List<string>(19800));
}
string line = "";
using (StreamReader streamReader = (new StreamReader(file.FullName)))
{
while ((line = streamReader.ReadLine()) != null) {
// this is giving KeyNotFoundException sometimes and others, do I need to lock here given the fact that I am accessing different references synchronously
ListOfCompanyData[key].Add(line);
}
}
}
I would move the adding to the dictionary outside the parallel loop. This saves you a lock (at the cost of an extra loop), and makes it at least more practical to inspect the contents of the dictionary before the parallel processing.
Besides, it's the reading that you want done in parallel, not the adding to the dictionary.

Resources