Skip to content

Commit

Permalink
Update main.py
Browse files Browse the repository at this point in the history
  • Loading branch information
giyu51 authored Oct 9, 2023
1 parent 82d8172 commit 619d2bd
Showing 1 changed file with 66 additions and 86 deletions.
152 changes: 66 additions & 86 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,39 +47,38 @@ def play(self):

# Update video frames,
def update_video(self):
print('updateVideo')
print("updateVideo")
# Get the snapshot of the current frame
self.player.video_take_snapshot(
0, '{0}.jpg'.format(self.name), 1280, 720)
self.player.video_take_snapshot(0, "{0}.jpg".format(self.name), 1280, 720)

# Return the taken snapshot to the server
return FileResponse("{0}.jpg".format(self.name))

# Get statistics about media.

def get_stats(self):
print('getStats')
print("getStats")
s = vlc.MediaStats()
m = self.player.get_media()
m.get_stats(s)

data = {
'decoded_audio': s.decoded_audio,
'decoded_video': s.decoded_video,
'demux_bitrate': int(s.demux_bitrate * 8000),
'demux_corrupted': s.demux_corrupted,
'demux_discontinuity': s.demux_discontinuity,
'demux_read_bytes': s.demux_read_bytes,
'displayed_pictures': s.displayed_pictures,
'input_bitrate': int(s.input_bitrate * 8000),
'lost_abuffers': s.lost_abuffers,
'lost_pictures': s.lost_pictures,
'played_abuffers': s.played_abuffers,
'read_bytes': s.read_bytes,
'send_bitrate': s.send_bitrate,
'sent_bytes': s.sent_bytes,
'sent_packets': s.sent_packets,
'time': int((self.player.get_time()/(1000*60)) % 60)
"decoded_audio": s.decoded_audio,
"decoded_video": s.decoded_video,
"demux_bitrate": int(s.demux_bitrate * 8000),
"demux_corrupted": s.demux_corrupted,
"demux_discontinuity": s.demux_discontinuity,
"demux_read_bytes": s.demux_read_bytes,
"displayed_pictures": s.displayed_pictures,
"input_bitrate": int(s.input_bitrate * 8000),
"lost_abuffers": s.lost_abuffers,
"lost_pictures": s.lost_pictures,
"played_abuffers": s.played_abuffers,
"read_bytes": s.read_bytes,
"send_bitrate": s.send_bitrate,
"sent_bytes": s.sent_bytes,
"sent_packets": s.sent_packets,
"time": int((self.player.get_time() / (1000 * 60)) % 60),
}
return data

Expand All @@ -94,79 +93,72 @@ def get_stats(self):

channels_list = AddChannel.channels_list

print('-------------All Channels--------------')
print("-------------All Channels--------------")
print(channels_list)
print('---------------------------------------')
print("---------------------------------------")


# Roughly said, it is Remote Control of a whole flow of fastAPI
# In each section the channel list is checked on whether it is a list of AddChannel objects (it should be like this) or is a single AddChannel object (probably is useful for testing each channel)
@app.on_event("startup")
def play():
if type(channels_list) == AddChannel: # It is a single channel
channels_list.play()
elif type(channels_list) == list: # There are few channels
for each_channel in channels_list:
each_channel.play()
for each_channel in channels_list:
each_channel.play()


@app.on_event('startup')
# You can omit these function completely if you do not use UDP
@app.on_event("startup")
async def startup_event():
asyncio.create_task(clear_buffer())
contain_udp = False

for each_channel in channels_list:
if "udp://" in each_channel.name:
contain_udp = True

if contain_udp:
asyncio.create_task(clear_buffer())


@app.get("/")
def update_video():
if type(channels_list) == AddChannel:
return channels_list.update_video()
elif type(channels_list) == list:
return [channel.update_video() for channel in channels_list]
return [channel.update_video() for channel in channels_list]


@app.get("/stats")
def get_stats():
if type(channels_list) == AddChannel:
return channels_list.get_stats()
elif type(channels_list) == list:
return [[channel.name, channel.get_stats()] for channel in channels_list]
return [[channel.name, channel.get_stats()] for channel in channels_list]


# It is a function to get stats of specific channel like ...../stats/TNT or ....../stats/DisneyTr
@app.get("/stats/{channel_name}")
def get_specific_stats(channel_name: str):
if type(channels_list) == AddChannel:
if channel_name == channels_list.name:
return channels_list.name, channels_list.get_stats()
else:
return {"ERROR": "the channel is not in the list of available channels, please update the list of channels"}

elif type(channels_list) == list:
# We iterate through the channels_list, which holds each channel object.
# If we find a channel object whose name matches the channel_name we are searching for,
# we assign that channel object to the outputOBJ variable.
# Finally, we display the name and statistics of the channel.
outputOBJ = None
for channelOBJ in channels_list:
if channelOBJ.name == channel_name:
outputOBJ = channelOBJ

if outputOBJ == None:
return {"ERROR": "the channel is not in the list of available channels, please update the list of channels"}
return outputOBJ.name, outputOBJ.get_stats()
# We iterate through the channels_list, which holds each channel object.
# If we find a channel object whose name matches the channel_name we are searching for,
# we assign that channel object to the outputOBJ variable.
# Finally, we display the name and statistics of the channel.
outputOBJ = None
for channelOBJ in channels_list:
if channelOBJ.name == channel_name:
outputOBJ = channelOBJ

if outputOBJ == None:
return {
"ERROR": f"the channel {channel_name} is not in the list of available channels, please update the list of channels"
}
return outputOBJ.name, outputOBJ.get_stats()


@app.get('/screen/{channel_name}')
@app.get("/screen/{channel_name}")
def get_channel_screen(channel_name: str):
if type(channels_list) == AddChannel:
return channels_list.update_video()
elif type(channels_list) == list:
outputOBJ = None
for channelOBJ in channels_list:
if channelOBJ.name == channel_name:
outputOBJ = channelOBJ
if outputOBJ == None:
return {"ERROR": "the channel is not in the list of available channels, please update the list of channels"}
return outputOBJ.update_video()
outputOBJ = None
for channelOBJ in channels_list:
if channelOBJ.name == channel_name:
outputOBJ = channelOBJ
if outputOBJ == None:
return {
"ERROR": f"the channel {channel_name} is not in the list of available channels, please update the list of channels"
}
return outputOBJ.update_video()


async def clear_buffer():
Expand All @@ -175,23 +167,11 @@ async def clear_buffer():
# This script runs continuously in a loop every 5 seconds.

print("_____Clearing the buffer______")
if type(channels_list) == AddChannel:
stats = channels_list.get_stats()
buffer = stats["read_bytes"]
if buffer > 1000:
# By stopping and starting the player again, we effectively reset the buffer size to 0.
# This ensures that the buffer is cleared and ready to receive new data.
channels_list.player.stop()
channels_list.player.play()

elif type(channels_list) == list:

for channelOBJ in channels_list:
channel_stats = channelOBJ.get_stats()
channel_buffer = channel_stats["read_bytes"]
if channel_buffer > 100:
channelOBJ.player.stop()
channelOBJ.player.play()
print('Buffer of {0} has been cleared'.format(
str(channelOBJ.name)))
for channelOBJ in channels_list:
channel_stats = channelOBJ.get_stats()
channel_buffer = channel_stats["read_bytes"]
if channel_buffer > 100:
channelOBJ.player.stop()
channelOBJ.player.play()
print("Buffer of {0} has been cleared".format(str(channelOBJ.name)))
await asyncio.sleep(5) # Run each 5 seconds

0 comments on commit 619d2bd

Please sign in to comment.