This repository was archived by the owner on Feb 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathVideoStreamCaptureHandler.cs
More file actions
123 lines (108 loc) · 4.35 KB
/
Copy pathVideoStreamCaptureHandler.cs
File metadata and controls
123 lines (108 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// <copyright file="VideoStreamCaptureHandler.cs" company="Techyian">
// Copyright (c) Ian Auty and contributors. All rights reserved.
// Licensed under the MIT License. Please see LICENSE.txt for License info.
// </copyright>
using System;
using System.IO;
using MMALSharp.Common;
using MMALSharp.Processors.Motion;
namespace MMALSharp.Handlers
{
/// <summary>
/// Processes the video data to a FileStream.
/// </summary>
public class VideoStreamCaptureHandler : FileStreamCaptureHandler, IMotionVectorCaptureHandler, IVideoCaptureHandler
{
/// <summary>
/// The motion type associated with this VideoCaptureHandler
/// </summary>
public MotionType MotionType { get; set; }
/// <summary>
/// The data store for motion vectors.
/// </summary>
protected Stream MotionVectorStore { get; set; }
/// <summary>
/// Indicates whether this capture handler stores video timestamps.
/// </summary>
protected bool StoreVideoTimestamps { get; }
/// <summary>
/// Creates a new instance of the <see cref="VideoStreamCaptureHandler"/> class without provisions for writing to a file. Supports
/// subclasses in which file output is optional.
/// </summary>
public VideoStreamCaptureHandler()
: base()
{
this.StoreVideoTimestamps = false;
}
/// <summary>
/// Creates a new instance of the <see cref="VideoStreamCaptureHandler"/> class with the specified directory and filename extension.
/// </summary>
/// <param name="directory">The directory to save captured videos.</param>
/// <param name="extension">The filename extension for saving files.</param>
/// <param name="storeTimestamps">Store video timestamps.</param>
public VideoStreamCaptureHandler(string directory, string extension, bool storeTimestamps = false)
: base(directory, extension)
{
this.StoreVideoTimestamps = storeTimestamps;
}
/// <summary>
/// Creates a new instance of the <see cref="VideoStreamCaptureHandler"/> class with the specified file path.
/// </summary>
/// <param name="fullPath">The absolute full path to save captured data to.</param>
/// <param name="storeTimestamps">Store video timestamps.</param>
public VideoStreamCaptureHandler(string fullPath, bool storeTimestamps = false)
: base(fullPath)
{
this.StoreVideoTimestamps = storeTimestamps;
}
/// <inheritdoc />
public override void Process(ImageContext context)
{
if (this.CurrentStream == null)
{
return;
}
base.Process(context);
if (this.StoreVideoTimestamps && context.Pts.HasValue)
{
var str = $"{context.Pts / 1000}.{context.Pts % 1000:000}" + Environment.NewLine;
File.AppendAllText($"{this.Directory}/{this.CurrentFilename}.pts", str);
this.FileIsEmpty = false;
}
}
/// <summary>
/// Splits the current file by closing the current stream and opening a new one.
/// </summary>
public virtual void Split() => this.NewFile();
/// <summary>
/// Used to set the current working motion vector store.
/// </summary>
/// <param name="stream">The <see cref="FileStream"/> to write to.</param>
public void InitialiseMotionStore(Stream stream)
{
if (this.CurrentStream == null)
{
return;
}
this.MotionVectorStore = stream;
}
/// <summary>
/// Responsible for storing the motion vector data to an output stream.
/// </summary>
/// <param name="data">The byte array containing the motion vector data.</param>
public void ProcessMotionVectors(byte[] data)
{
if (this.MotionVectorStore != null)
{
if (this.MotionVectorStore.CanWrite)
{
this.MotionVectorStore.Write(data, 0, data.Length);
}
else
{
throw new IOException("Stream not writable.");
}
}
}
}
}