MP4 Writer Sample 1
The FileWriterSample1 demonstrates the simplest approach to recording a remote media stream to an MP4 file. It uses MediaSession to connect a source to a file sink — the session handles opening, stream detection, media forwarding, and lifecycle management automatically.
Overview
The FileWriterSample1 class performs the following:
- Creates a media source from a URI using SourceFactory
- Creates an IsoSink for MP4 file output
- Connects both to a MediaSession and starts recording
Implementation
VAST.Media.IMediaSource source = VAST.Media.SourceFactory.Create(sourceUri);
VAST.Media.IMediaSink fileSink = new VAST.File.ISO.IsoSink();
fileSink.Uri = filePath;
this.writerSession = new VAST.Media.MediaSession();
this.writerSession.AddSource(source);
this.writerSession.AddSink(fileSink);
this.writerSession.Error += (object sender, Media.ErrorEventArgs e) =>
{
// TODO: process error
};
this.writerSession.Start();
Creating the Source
SourceFactory creates the appropriate source implementation based on the URI protocol. For example:
| URI | Source Type |
|---|---|
rtsp://server/stream |
RTSP client source |
rtmp://server/live/stream |
RTMP client source |
srt://server:port |
SRT client source |
Creating the Sink
IsoSink writes media data to an ISO base media file format (MP4) file. The Uri property specifies the output file path.
MediaSession
MediaSession connects one or more media sources to one or more media sinks and manages the entire media flow automatically:
- Opens the source and waits for stream detection
- Forwards detected media types to the sink via
AddStream - Opens and starts the sink
- Routes all incoming media samples from the source to the sink via
PushMedia - Handles errors and lifecycle events
This eliminates the need for manual event handlers, stream registration, and sample forwarding — making it the simplest way to record a stream to a file.
Stopping
Disposing the session stops both the source and sink and closes the output file:
this.writerSession.Dispose();
Usage
// Record an RTSP stream to an MP4 file
var sample = new FileWriterSample1(
"rtsp://127.0.0.1/test",
@"C:\path\to\file.mp4");
// ... recording in progress ...
// Stop recording
sample.Dispose();
See Also
- Sample Applications — overview of all demo projects
- .NET Server Demo — parent page with setup instructions, license key configuration, and access URL reference