I am trying to use Windows Media .Net library to copy an audio/video file that's an asf file. I'm relatively new to the Windows Media Format SDK, so I'm not sure if I'm doing this right. I have looked through some of the sample projects that are included with the download and looked through the documentation for the C++ version, but I can't seem to figure out why it's crashing when I call EndWriting. So if someone could help point out what I'm doing wrong here I would gratefully appreciate it.
I have a WinForm that will have a button on it to start the recording, and another button to end the recording. My class implements the IWMReaderCallback and uses a private class called SampleProp to be used to hold the values for the pointers and such that will be returned in the OnSample method. So in the OnSample method I'm filling a byte[] member of the SampleProp instance with a copy of the sample being returned. I then add the instance of SampleProp to a collection that will be used in another method called ProcessReaderSample. In this other method I am creating the IWMReader and IWMWriter objects, and has a while loop that will call BeginWriting, AllocateSample, WriteSample, then EndWriting. And that's where it crashes. Here is my code that I'm using...
public class MyClass : IWMReaderCallback
{
static readonly string _streamingFileName = "C:\\tmpStream.asf";
static readonly string _streamingURL = @"http://localhost:8080";
static readonly string _recordingFileName = "C:\\tmpRecording.asf";
IWMReader _reader = null;
IWMReaderAdvanced _readerAdvanced = null;
IWMHeaderInfo _readerHeaderInfo = null;
IWMProfile _readerProfile = null;
IWMWriter _writer = null;
IWMWriterAdvanced _writerAdvanced = null;
IWMHeaderInfo _writerHeaderInfo = null;
int _streamCount = 0;
Guid[] _guidStreamType = null;
short[] _streamNumber = null;
void GetReader()
{
WMUtils.WMCreateReader(IntPtr.Zero, Rights.Playback, out _reader);
_readerAdvanced = _reader as IWMReaderAdvanced;
_readerHeaderInfo = _reader as IWMHeaderInfo;
_reader.Open(_streamingFileName, this, IntPtr.Zero);
_readerAdvanced.SetUserProvidedClock(true);
_readerAdvanced.SetManualStreamSelection(true);
}
void GetWritter()
{
WMUtils.WMCreateWriter(IntPtr.Zero, out _writer);
_writerAdvance开发者_运维知识库d = _writer as IWMWriterAdvanced;
_writerHeaderInfo = _writer as IWMHeaderInfo;
_writer.SetProfile(_readerProfile);
_writer.SetOutputFilename(_recordingFileName);
int inputCount = 0;
_writer.GetInputCount(out inputCount);
for (int i = 0; i < inputCount; i++)
{
_writer.SetInputProps(i, null);
}
}
class SampleProp
{
public int OutputNum { get; private set; }
public long SampleTime { get; private set; }
public SampleFlag Flag { get; private set; }
public byte[] Sample;
public int Size { get; private set; }
public SampleProp(int size, int outputNum, long sampleTime, SampleFlag flag)
{
Size = size;
OutputNum = outputNum;
SampleTime = sampleTime;
Flag = flag;
Sample = new byte[size];
}
}
List<SampleProp> _writableSamples = null;
public void OnSample(int dwOutputNum, long cnsSampleTime, long cnsSampleDuration, SampleFlag dwFlags, INSSBuffer pSample, IntPtr pvContext)
{
int size = 0;
pSample.GetLength(out size);
var prop = new SampleProp(size, dwOutputNum, cnsSampleTime, dwFlags);
IntPtr ptr = IntPtr.Zero;
pSample.GetBuffer(out ptr);
Marshal.Copy(ptr, prop.Sample, 0, size);
_writableSamples.Add(prop);
}
void ProcessReaderSample()
{
_event.Reset();
GetReader();
GetProfileInfo();
GetWritter();
_reader.Start(0, 0, 1.0f, IntPtr.Zero);
_isRecording = true;
var hasStarted = false;
while (_isRecording || _writableSamples.Count > 0)
{
if (_writableSamples.Count > 0)
{
_writer.BeginWriting();
INSSBuffer buffer;
_writer.AllocateSample(_writableSamples[0].Size, out buffer);
IntPtr ptr = IntPtr.Zero;
buffer.GetBuffer(out ptr);
Marshal.Copy(_writableSamples[0].Sample, 0, ptr, _writableSamples[0].Size);
buffer.SetLength(_writableSamples[0].Size);
_writer.WriteSample(_writableSamples[0].OutputNum, _writableSamples[0].SampleTime, _writableSamples[0].Flag, buffer);
Marshal.ReleaseComObject(buffer);
_writableSamples.RemoveAt(0);
_writer.EndWriting();
}
}
}
bool _isRecording = false;
public void StartRecording()
{
if (_isRecording) return;
_writableSamples = new List<SampleProp>();
Thread writingThread = new Thread(new ThreadStart(ProcessReaderSample));
writingThread.Start();
}
public void StopRecording()
{
if (!_isRecording) return;
_isRecording = false;
}
So if someone could please help me with this. Thank you in advance.
I went a totally different direction for the solution to this issue. I decided to not use solely the WM .Net Framework and instead used the DirectShow.Net Framework implementing the WM Asf Reader and Writer. Here is a link to my question and answer.
Is it possible to use an http url as your source location of a Source Filter in DirectShow .Net?
精彩评论