-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBufferManager.cs
More file actions
145 lines (132 loc) · 4.97 KB
/
BufferManager.cs
File metadata and controls
145 lines (132 loc) · 4.97 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Purpose: This script is used to manage the buffer of the video player. It keeps track of the segments that are currently in the buffer and removes the oldest segment when the buffer is full. The buffer size can be set to a specific value (N) and the current buffer size can be retrieved. The buffer queue can also be retrieved if needed. This script is a singleton and should be attached to an empty GameObject in the scene.
//Imports:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;
public class BufferManager : MonoBehaviour
{
private static BufferManager instance;
public static BufferManager Instance
{
get
{
if (instance == null)
{
GameObject bufferManagerObject = new GameObject("BufferManager");
instance = bufferManagerObject.AddComponent<BufferManager>();
}
return instance;
}
}
private Queue<string> bufferQueue = new Queue<string>();
private string buffermanagerLog = "";
private int bufferCapacity = 2; // Default buffer capacity (changeable)
private DateTime startTime;
void Awake()
{
// Singleton pattern to keep only one instance of BufferManager
if (instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
startTime = DateTime.Now;
}
else
{
Destroy(gameObject);
}
buffermanagerLog = Path.Combine(Application.persistentDataPath, "BufferManagerLog.txt");
}
// Set buffer size to a specific value (N)
public void SetBufferSize(int size)
{
File.AppendAllText(buffermanagerLog, $"{GetTimestamp()} Buffer size set to: {size}\n");
bufferCapacity = size;
}
public int GetMaxBufferSize()
{
return bufferCapacity;
}
// Return the current buffer size (number of elements in the buffer)
public int GetBufferSize()
{
return bufferQueue.Count;
}
// Add a segment to the buffer
public void AddToBuffer(string segmentPath)
{
if (bufferQueue.Count >= bufferCapacity)
{
File.AppendAllText(buffermanagerLog, $"{GetTimestamp()} Buffer is full. Cannot add segment: {segmentPath}\n");
return;
}
bufferQueue.Enqueue(segmentPath);
File.AppendAllText(buffermanagerLog, $"{GetTimestamp()} Segment added to buffer: {segmentPath}\n");
File.AppendAllText(buffermanagerLog, $"{GetTimestamp()} Current buffer size: {bufferQueue.Count}\n");
Debug.Log("Segment added to buffer. Current buffer size: " + bufferQueue.Count);
}
// Remove the oldest segment from the buffer
public void RemoveFromBuffer( )
{
if (bufferQueue.Count > 0)
{
string removedSegment = bufferQueue.Dequeue();
File.AppendAllText(buffermanagerLog, $"{GetTimestamp()} Removed segment from buffer: {removedSegment}\n");
File.AppendAllText(buffermanagerLog, $"{GetTimestamp()} Current buffer size: {bufferQueue.Count}\n");
Debug.Log("Removed segment from buffer: " + removedSegment);
}
else
{
File.AppendAllText(buffermanagerLog, $"{GetTimestamp()} Buffer is empty. Cannot remove segment.\n");
Debug.Log("Buffer is empty. Cannot remove segment.");
}
}
// Get the current buffer queue (optional method)
public Queue<string> GetBufferQueue()
{
return bufferQueue;
}
// Get timestamp in seconds since start
private string GetTimestamp()
{
TimeSpan elapsedTime = DateTime.Now - startTime;
return $"[{elapsedTime.TotalSeconds:F2}s]";
}
public bool isSegmentFirstInBuffer(string path)
{
if (bufferQueue.Count > 0)
{
string firstSegment = bufferQueue.Peek();
if(firstSegment.Contains(path))
{
File.AppendAllText(buffermanagerLog, $"{GetTimestamp()} Segment {path} is first in buffer\n");
}
else{
File.AppendAllText(buffermanagerLog, $"{GetTimestamp()} Segment {path} is not first in buffer\n");
}
return firstSegment.Contains(path);
}
return false;
}
public string GetFirstSegmentInBuffer()
{
if (bufferQueue.Count > 0)
{
return bufferQueue.Peek();
}
return "";
}
public void printQueue()
{
Debug.Log("-->>>> debug 19 Printing buffer queue");
// create new queue to iterate over
Queue<string> tempQueue = new Queue<string>(bufferQueue);
while(tempQueue.Count > 0)
{
string segment = tempQueue.Dequeue();
Debug.Log("---->>> debug 19 printing buffer : segment: " + segment);
}
}
}