-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathfixed_env_hotdash.py
486 lines (392 loc) · 21.3 KB
/
fixed_env_hotdash.py
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
import load_trace
import numpy as np
import requests
import json
import os
import pickle
import a3c
from collections import Counter
import tensorflow as tf
######################################################################
S_INFO = 6 # bit_rate, buffer_size, next_chunk_size, bandwidth_measurement(throughput and time), chunk_til_video_end
S_LEN = 8 # take how many frames in the past
A_DIM = 6
ACTOR_LR_RATE = 0.0001
CRITIC_LR_RATE = 0.001
VIDEO_BIT_RATE = [300, 750, 1200, 1850, 2850, 4300] # Kbps
BUFFER_NORM_FACTOR = 10.0
CHUNK_TIL_VIDEO_END_CAP = 48.0
M_IN_K = 1000.0
REBUF_PENALTY = 4.3 # 1 sec rebuffering -> 3 Mbps
SMOOTH_PENALTY = 1
DEFAULT_QUALITY = 1 # default video quality without agent
RANDOM_SEED = 42
RAND_RANGE = 1000
LOG_FILE = './results/log_sim_rl'
TEST_TRACES = './old_traces/cooked_norway_traces/'
# log in format of time_stamp bit_rate buffer_size rebuffer_time chunk_size download_time reward
# NN_MODEL = sys.argv[1]
ABR_NN_MODEL = './models/pretrain_linear_reward.ckpt'
# HOTSPOTS_FILE = './current_hotspots.pkl'
######################################################################
NUM_HOTSPOT_CHUNKS = 5
# NUM_HOTSPOT_CHUNKS = 7
MILLISECONDS_IN_SECOND = 1000.0
B_IN_MB = 1000000.0
BITS_IN_BYTE = 8.0
VIDEO_CHUNK_LEN = 4000.0 # millisec, every time add this amount to buffer
BITRATE_LEVELS = 6
TOTAL_VIDEO_CHUNK = 49
BUFFER_THRESH = 60.0 * MILLISECONDS_IN_SECOND # millisec, max buffer limit
DRAIN_BUFFER_SLEEP_TIME = 500.0 # millisec
PACKET_PAYLOAD_PORTION = 0.95
LINK_RTT = 80 # millisec
PACKET_SIZE = 1500 # bytes
VIDEO_SIZE_FILE = 'video/video_size_'
HOTSPOT_CHUNKS_PERCENT = 10 # percentage of chunks which are hotspot
######################################################################
class Environment:
prefetch_decisions = []
def __init__(self, all_cooked_time, all_cooked_bw, all_file_names, random_seed=RANDOM_SEED):
assert len(all_cooked_time) == len(all_cooked_bw)
self.all_file_names = all_file_names
self.pensieve_returned = []
np.random.seed(random_seed)
self.all_cooked_time = all_cooked_time
self.all_cooked_bw = all_cooked_bw
self.current_chunk_sequence_num = 0
self.reached_end_of_hotspots = False
self.last_hotspot_waiting = True
# Keeps track of number of chunks downloaded so far
self.video_chunk_counter = 0
# Keeps track of normal chunks head
self.normal_chunk_counter = 0
self.play_buffer_size = 0.0
self.total_buffer_size = 0.0
self.out_of_sync_buffer = []
# pick a random trace file
self.trace_idx = 0
# print(self.all_cooked_time)
self.cooked_time = self.all_cooked_time[self.trace_idx]
self.cooked_bw = self.all_cooked_bw[self.trace_idx]
self.mahimahi_start_ptr = 1
# randomize the start point of the trace
# note: trace file starts with time 0
self.mahimahi_ptr = 1
self.last_mahimahi_time = self.cooked_time[self.mahimahi_ptr - 1]
self.video_size = {} # in bytes
for bitrate in range(BITRATE_LEVELS):
self.video_size[bitrate] = []
with open(VIDEO_SIZE_FILE + str(bitrate)) as f:
for line in f:
self.video_size[bitrate].append(int(line.split()[0]))
# Hotspot related fields
# self.hotspot_chunks = [8, 17, 25, 33, 46]
# Normal bitrate reward: 5910.0
# Hotspot bitrate reward: 4623.0
# Rebuffering reward: 3601.91988334
# Smoothness reward: 1113.9
# Total reward: 5817.18011666
# self.hotspot_chunks = [9, 16, 26, 32, 47]
# Normal bitrate reward: 5893.75
# Hotspot bitrate reward: 4604.0
# Rebuffering reward: 3585.94404787
# Smoothness reward: 1140.6
# Total reward: 5771.20595213
# self.hotspot_chunks = [5, 12, 22, 37, 42]
# Normal bitrate reward: 5897.65
# Hotspot bitrate reward: 4644.0
# Rebuffering reward: 3690.50150514
# Smoothness reward: 1123.65
# Total reward: 5727.49849486
# with open(HOTSPOTS_FILE, "r") as hotspots_file:
# self.hotspot_chunks = pickle.load(hotspots_file)
self.hotspot_chunks = [8, 17, 25, 33, 46]
self.last_hotspot_chunk_considered = self.hotspot_chunks[0]
self.num_hotspot_chunks = len(self.hotspot_chunks)
self.last_hotspot_chunk = self.hotspot_chunks[0]
self.next_hotspot_chunk = self.hotspot_chunks[0]
self.hotspot_chunk_counter = 0
self.total_buffer_size = 0 # play buffer + out-of-order buffer
self.next_normal_bitrate = DEFAULT_QUALITY
self.next_hotspot_bitrate = DEFAULT_QUALITY
self.last_normal_bitrate = DEFAULT_QUALITY
self.last_hotspot_bitrate = DEFAULT_QUALITY
self.current_bit_rate_decision = DEFAULT_QUALITY
self.last_bit_rate_decision = DEFAULT_QUALITY
self.last_hotspot_downloaded = -1
self.last_normal_downloaded = -1
# Smoothness eval
self.smoothness_eval_start_chunk = 0
self.all_bitrate_decisions = []
for i in range(TOTAL_VIDEO_CHUNK):
self.all_bitrate_decisions.append(-1)
self.bitrate_selected = []
######################################################################
def reset(self):
self.current_chunk_sequence_num = 0
self.reached_end_of_hotspots = False
self.last_hotspot_waiting = True
self.video_chunk_counter = 0
self.normal_chunk_counter = 0
self.play_buffer_size = 0.0
self.total_buffer_size = 0.0
self.out_of_sync_buffer = []
# pick a random trace file
self.trace_idx += 1
if self.trace_idx >= len(self.all_cooked_time):
self.trace_idx = 0
self.cooked_time = self.all_cooked_time[self.trace_idx]
self.cooked_bw = self.all_cooked_bw[self.trace_idx]
# randomize the start point of the trace
# note: trace file starts with time 0
self.mahimahi_ptr = self.mahimahi_start_ptr
self.last_mahimahi_time = self.cooked_time[self.mahimahi_ptr - 1]
## Hotspot related fields
# with open(HOTSPOTS_FILE, "r") as hotspots_file:
# self.hotspot_chunks = pickle.load(hotspots_file)
self.hotspot_chunks = [8, 17, 25, 33, 46]
self.last_hotspot_chunk_considered = self.hotspot_chunks[0]
self.num_hotspot_chunks = len(self.hotspot_chunks)
self.last_hotspot_chunk = self.hotspot_chunks[0]
self.next_hotspot_chunk = self.hotspot_chunks[0]
self.hotspot_chunk_counter = 0
self.total_buffer_size = 0 # play buffer + out-of-order buffer
self.next_normal_bitrate = DEFAULT_QUALITY
self.next_hotspot_bitrate = DEFAULT_QUALITY
self.last_normal_bitrate = DEFAULT_QUALITY
self.last_hotspot_bitrate = DEFAULT_QUALITY
self.current_bit_rate_decision = DEFAULT_QUALITY
self.last_bit_rate_decision = DEFAULT_QUALITY
self.last_hotspot_downloaded = -1
self.last_normal_downloaded = -1
self.smoothness_eval_start_chunk = 0
for i in self.all_bitrate_decisions:
self.bitrate_selected.append(i)
# print "Counter: {}".format(Counter(self.pensieve_returned))
# print "Prefetch decisions: {}".format(Counter(self.prefetch_decisions))
self.all_bitrate_decisions = []
for i in range(TOTAL_VIDEO_CHUNK):
self.all_bitrate_decisions.append(-1)
def execute_action(self, prefetch_decision, next_normal_bitrate_predict, next_hotspot_bitrate_predict):
assert prefetch_decision >= 0 and prefetch_decision <= 1
if self.hotspot_chunk_counter == NUM_HOTSPOT_CHUNKS:
prefetch_decision = 0
Environment.prefetch_decisions.append(prefetch_decision)
# print "Prefetch decisions: {}".format(Counter(Environment.prefetch_decisions))
# increment video chunk counter
self.video_chunk_counter += 1
# print "Video chunk counter: {}".format(self.video_chunk_counter)
self.next_normal_bitrate = next_normal_bitrate_predict
self.next_hotspot_bitrate = next_hotspot_bitrate_predict
was_hotspot_chunk = 0
self.last_bit_rate_decision = self.current_bit_rate_decision
if prefetch_decision == 1:
self.current_bit_rate_decision = self.next_hotspot_bitrate
# if self.hotspot_chunk_counter < len(self.hotspot_chunks):
# if self.last_hotspot_chunk_considered < self.hotspot_chunks[self.hotspot_chunk_counter]:
# was_hotspot_chunk = 1
# self.last_hotspot_chunk_considered = self.hotspot_chunks[self.hotspot_chunk_counter]
else:
self.current_bit_rate_decision = self.next_normal_bitrate
if self.hotspot_chunk_counter < len(self.hotspot_chunks):
if self.normal_chunk_counter in self.hotspot_chunks:
self.hotspot_chunk_counter += 1
if self.hotspot_chunk_counter < len(self.hotspot_chunks):
self.next_hotspot_chunk = self.hotspot_chunks[self.hotspot_chunk_counter]
# if self.last_hotspot_chunk_considered < self.hotspot_chunks[self.hotspot_chunk_counter]:
# was_hotspot_chunk = 1
# self.last_hotspot_chunk_considered = self.hotspot_chunks[self.hotspot_chunk_counter]
video_chunk_size = 0
if prefetch_decision == 0:
video_chunk_size = self.video_size[self.next_normal_bitrate][self.normal_chunk_counter]
# video_chunk_size = self.video_size[0][self.normal_chunk_counter]
self.all_bitrate_decisions[self.normal_chunk_counter] = self.next_normal_bitrate
# self.current_chunk_sequence_num = self.normal_chunk_counter
self.last_normal_bitrate = self.next_normal_bitrate
self.last_normal_downloaded = self.normal_chunk_counter
self.normal_chunk_counter += 1
else:
video_chunk_size = self.video_size[self.next_hotspot_bitrate][self.next_hotspot_chunk]
self.all_bitrate_decisions[self.next_hotspot_chunk] = self.next_hotspot_bitrate
# print "Next hotspot chunk: {}".format(self.next_hotspot_chunk)
# self.current_chunk_sequence_num = self.hotspot_chunks[self.hotspot_chunk_counter]
self.last_hotspot_chunk = self.next_hotspot_chunk
self.last_hotspot_downloaded = self.last_hotspot_chunk
if self.hotspot_chunk_counter < NUM_HOTSPOT_CHUNKS:
self.hotspot_chunk_counter += 1
if self.hotspot_chunk_counter == NUM_HOTSPOT_CHUNKS:
self.next_hotspot_chunk = self.hotspot_chunks[self.hotspot_chunk_counter-1]
self.reached_end_of_hotspots = True
else:
self.next_hotspot_chunk = self.hotspot_chunks[self.hotspot_chunk_counter]
self.last_hotspot_bitrate = self.next_hotspot_bitrate
# Added now
if self.hotspot_chunk_counter < len(self.hotspot_chunks):
if self.last_hotspot_chunk_considered < self.hotspot_chunks[self.hotspot_chunk_counter]:
was_hotspot_chunk = 1
self.last_hotspot_chunk_considered = self.hotspot_chunks[self.hotspot_chunk_counter]
elif self.hotspot_chunk_counter == len(self.hotspot_chunks) and self.reached_end_of_hotspots and self.last_hotspot_waiting:
was_hotspot_chunk = 1
self.last_hotspot_waiting = False
# print "Normal chunk counter: {}".format(self.normal_chunk_counter)
# print "Video chunk size: {}".format(video_chunk_size)
# print "All bitrate decisions: {}".format(self.all_bitrate_decisions)
# print "Last normal bitrate: {}".format(self.last_normal_bitrate)
# print "Next normal bitrate: {}".format(self.next_normal_bitrate)
# print "Last hotspot chunk: {}".format(self.last_hotspot_chunk)
# print "Next hotspot chunk: {}".format(self.next_hotspot_chunk)
# print "Hotspot chunk counter: {}".format(self.hotspot_chunk_counter)
# print "Last hotspot bitrate: {}".format(self.last_hotspot_bitrate)
# print "Next hotspot bitrate: {}".format(self.next_hotspot_bitrate)
# use the delivery opportunity in mahimahi
delay = 0.0 # in ms
video_chunk_counter_sent = 0 # in bytes
while True: # download video chunk over mahimahi
throughput = self.cooked_bw[self.mahimahi_ptr] \
* B_IN_MB / BITS_IN_BYTE
duration = self.cooked_time[self.mahimahi_ptr] \
- self.last_mahimahi_time
packet_payload = throughput * duration * PACKET_PAYLOAD_PORTION
if video_chunk_counter_sent + packet_payload > video_chunk_size:
fractional_time = (video_chunk_size - video_chunk_counter_sent) / \
throughput / PACKET_PAYLOAD_PORTION
delay += fractional_time
self.last_mahimahi_time += fractional_time
break
video_chunk_counter_sent += packet_payload
delay += duration
self.last_mahimahi_time = self.cooked_time[self.mahimahi_ptr]
self.mahimahi_ptr += 1
if self.mahimahi_ptr >= len(self.cooked_bw):
# loop back in the beginning
# note: trace file starts with time 0
self.mahimahi_ptr = 1
self.last_mahimahi_time = 0
delay *= MILLISECONDS_IN_SECOND
delay += LINK_RTT
# rebuffer time
# print "Delay: {}".format(delay)
rebuf = np.maximum(delay - self.play_buffer_size, 0.0)
# print "Rebuffering time: {}".format(rebuf)
# update the play buffer
self.play_buffer_size = np.maximum(self.play_buffer_size - delay, 0.0)
# print "Play buffer size: {}".format(self.play_buffer_size)
# add in the new chunk
if prefetch_decision == 0:
self.play_buffer_size += VIDEO_CHUNK_LEN
# print "Play buffer size: {}".format(self.play_buffer_size)
else:
self.out_of_sync_buffer.append(self.last_hotspot_chunk)
# print "Out of sync buffer: {}".format(self.out_of_sync_buffer)
# reconcile buffer with out-of-sync chunks
while len(self.out_of_sync_buffer) > 0:
if self.out_of_sync_buffer[0] == self.normal_chunk_counter:
self.play_buffer_size += VIDEO_CHUNK_LEN
self.normal_chunk_counter += 1
# self.current_chunk_sequence_num += 1
self.out_of_sync_buffer = self.out_of_sync_buffer[1:]
else:
break
# print "Normal chunk counter: {}".format(self.normal_chunk_counter)
# print "Play buffer size: {}".format(self.play_buffer_size)
# print "Out of sync buffer: {}".format(self.out_of_sync_buffer)
# compute bitrate decision array for smoothness calculation
if self.normal_chunk_counter <= 2:
self.smoothness_eval_start_chunk = 0
last_in_sync_chunk = self.normal_chunk_counter
# print "Last downloaded normal chunk: {}".format(self.smoothness_eval_start_chunk)
# print "Last in sync chunk: {}".format(last_in_sync_chunk)
# compute total buffer size
self.total_buffer_size = self.play_buffer_size \
+ (VIDEO_CHUNK_LEN * len(self.out_of_sync_buffer))
# print "Total buffer size: {}".format(self.total_buffer_size)
# sleep if buffer gets too large
sleep_time = 0
if self.total_buffer_size > BUFFER_THRESH:
# exceed the buffer limit
# we need to skip some network bandwidth here
# but do not add up the delay
drain_buffer_time = self.total_buffer_size - BUFFER_THRESH
sleep_time = np.ceil(drain_buffer_time / DRAIN_BUFFER_SLEEP_TIME) * \
DRAIN_BUFFER_SLEEP_TIME
self.play_buffer_size -= sleep_time
self.total_buffer_size -= sleep_time
while True:
duration = self.cooked_time[self.mahimahi_ptr] \
- self.last_mahimahi_time
if duration > sleep_time / MILLISECONDS_IN_SECOND:
self.last_mahimahi_time += sleep_time / MILLISECONDS_IN_SECOND
break
sleep_time -= duration * MILLISECONDS_IN_SECOND
self.last_mahimahi_time = self.cooked_time[self.mahimahi_ptr]
self.mahimahi_ptr += 1
if self.mahimahi_ptr >= len(self.cooked_bw):
# loop back in the beginning
# note: trace file starts with time 0
self.mahimahi_ptr = 1
self.last_mahimahi_time = 0
# the "last buffer size" return to the controller
# Note: in old version of dash the lowest buffer is 0.
# In the new version the buffer always have at least
# one chunk of video
return_buffer_size = self.play_buffer_size
video_chunk_remain = TOTAL_VIDEO_CHUNK - self.video_chunk_counter
# print "Video chunks remaining: {}".format(video_chunk_remain)
smoothness_eval_bitrates = self.all_bitrate_decisions[self.smoothness_eval_start_chunk:last_in_sync_chunk]
end_of_video = False
if self.video_chunk_counter >= TOTAL_VIDEO_CHUNK-1: # or self.hotspot_chunk_counter == NUM_HOTSPOT_CHUNKS:
end_of_video = True
# print "End of video: {}".format(end_of_video)
self.reset()
# print "Prefetch decisions: {}".format(Counter(Environment.prefetch_decisions))
next_video_chunk_sizes = []
for i in range(BITRATE_LEVELS):
next_video_chunk_sizes.append(self.video_size[i][self.normal_chunk_counter])
# print "Next video chunk sizes: {}".format(next_video_chunk_sizes)
next_hotspot_chunk_sizes = []
for i in range(BITRATE_LEVELS):
next_hotspot_chunk_sizes.append(self.video_size[i][self.next_hotspot_chunk])
# print "Next hotspot chunk sizes: {}".format(next_hotspot_chunk_sizes)
dist_from_hotspot_chunks = []
for i in range(self.num_hotspot_chunks):
dist_from_hotspot_chunks.append(self.hotspot_chunks[i] - self.next_hotspot_chunk)
# print "Distance from hotspot chunks: {}".format(dist_from_hotspot_chunks)
# print "Smoothness eval bitrates: {}".format(smoothness_eval_bitrates)
self.last_prefetch_decision = prefetch_decision
# print "Bitrate decisions: {}".format(self.all_bitrate_decisions)
if prefetch_decision == 0:
self.current_chunk_sequence_num = self.last_normal_downloaded
else:
self.current_chunk_sequence_num = self.last_hotspot_downloaded
state_data_for_action = {}
# normal chunk state information
state_data_for_action['delay'] = delay
state_data_for_action['sleep_time'] = sleep_time
if prefetch_decision == 0:
state_data_for_action['last_bit_rate'] = self.last_normal_bitrate
else:
state_data_for_action['last_bit_rate'] = self.last_hotspot_bitrate
state_data_for_action['play_buffer_size'] = self.play_buffer_size / MILLISECONDS_IN_SECOND
state_data_for_action['rebuf'] = rebuf / MILLISECONDS_IN_SECOND
state_data_for_action['video_chunk_size'] = video_chunk_size
state_data_for_action['next_video_chunk_sizes'] = next_video_chunk_sizes
state_data_for_action['end_of_video'] = end_of_video
state_data_for_action['video_chunk_remain'] = video_chunk_remain
state_data_for_action['current_seq_no'] = self.current_chunk_sequence_num
state_data_for_action['log_prefetch_decision'] = prefetch_decision
# hotspot chunk state information
state_data_for_action['was_hotspot_chunk'] = was_hotspot_chunk
state_data_for_action['hotspot_chunks_remain'] = self.num_hotspot_chunks - self.hotspot_chunk_counter
state_data_for_action['chunks_till_played'] = self.next_hotspot_chunk - self.normal_chunk_counter
state_data_for_action['total_buffer_size'] = self.total_buffer_size / MILLISECONDS_IN_SECOND
state_data_for_action['last_hotspot_bit_rate'] = self.last_hotspot_bitrate
state_data_for_action['next_hotspot_chunk_sizes'] = next_hotspot_chunk_sizes
state_data_for_action['dist_from_hotspot_chunks'] = dist_from_hotspot_chunks
state_data_for_action['smoothness_eval_bitrates'] = smoothness_eval_bitrates
self.smoothness_eval_start_chunk = last_in_sync_chunk - 1
# abr decision state information
state_data_for_action['normal_bitrate_pensieve'] = next_normal_bitrate_predict
state_data_for_action['hotspot_bitrate_pensieve'] = next_hotspot_bitrate_predict
return state_data_for_action
######################################################################