HTTPStreamingF4FIndexHandler.as 75.3 KB
Newer Older
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
/*****************************************************
*  
*  Copyright 2009 Adobe Systems Incorporated.  All Rights Reserved.
*  
*****************************************************
*  The contents of this file are subject to the Mozilla Public License
*  Version 1.1 (the "License"); you may not use this file except in
*  compliance with the License. You may obtain a copy of the License at
*  http://www.mozilla.org/MPL/
*   
*  Software distributed under the License is distributed on an "AS IS"
*  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
*  License for the specific language governing rights and limitations
*  under the License.
*   
*  
*  The Initial Developer of the Original Code is Adobe Systems Incorporated.
*  Portions created by Adobe Systems Incorporated are Copyright (C) 2009 Adobe Systems 
*  Incorporated. All Rights Reserved. 
*  
*****************************************************/
package org.osmf.net.httpstreaming.f4f
{
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.TimerEvent;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	import flash.utils.IDataInput;
	import flash.utils.Timer;
	
	import org.osmf.elements.f4mClasses.BootstrapInfo;
	import org.osmf.events.DVRStreamInfoEvent;
	import org.osmf.events.HTTPStreamingEvent;
	import org.osmf.events.HTTPStreamingEventReason;
	import org.osmf.events.HTTPStreamingFileHandlerEvent;
	import org.osmf.events.HTTPStreamingIndexHandlerEvent;
	import org.osmf.net.dvr.DVRUtils;
	import org.osmf.net.httpstreaming.HTTPStreamDownloader;
	import org.osmf.net.httpstreaming.HTTPStreamRequest;
	import org.osmf.net.httpstreaming.HTTPStreamRequestKind;
	import org.osmf.net.httpstreaming.HTTPStreamingFileHandlerBase;
	import org.osmf.net.httpstreaming.HTTPStreamingIndexHandlerBase;
	import org.osmf.net.httpstreaming.HTTPStreamingUtils;
	import org.osmf.net.httpstreaming.dvr.DVRInfo;
	import org.osmf.net.httpstreaming.flv.FLVTagScriptDataMode;
	import org.osmf.net.httpstreaming.flv.FLVTagScriptDataObject;
	import org.osmf.utils.OSMFSettings;
	import org.osmf.utils.URL;

	CONFIG::LOGGING 
	{	
		import org.osmf.logging.Logger;
	}

	[ExcludeClass]
	
	/**
	 * @private
	 * 
	 * The actual implementation of HTTPStreamingFileIndexHandlerBase.  It
	 * handles the indexing scheme of an F4V file.
	 */	
	public class HTTPStreamingF4FIndexHandler extends HTTPStreamingIndexHandlerBase
	{
		/*
		AdobePatentID="2390US01"
		*/

		/**
		 * Default Constructor.
		 *
		 * @param fileHandler The associated file handler object which is responsable for processing the actual data.
		 * 					  We need this object as it may process bootstrap information found into the stream.
		 * @param fragmentsThreshold The default threshold for fragments.   
		 */
		public function HTTPStreamingF4FIndexHandler(fileHandler:HTTPStreamingFileHandlerBase, fragmentsThreshold:uint = DEFAULT_FRAGMENTS_THRESHOLD)
		{
			super();
			
			// listen for any bootstrap box information dispatched by file handler
			fileHandler.addEventListener(HTTPStreamingFileHandlerEvent.NOTIFY_BOOTSTRAP_BOX, onBootstrapBox);
			
			_bestEffortF4FHandler.addEventListener(HTTPStreamingFileHandlerEvent.NOTIFY_BOOTSTRAP_BOX, onBestEffortF4FHandlerNotifyBootstrapBox);
		}
		
		/**
		 * @private
		 */
		override public function dvrGetStreamInfo(indexInfo:Object):void
		{
			_invokedFromDvrGetStreamInfo = true;
			playInProgress = false;
			initialize(indexInfo);
		} 
		
		/**
		 * Initializes the index handler.
		 * 
		 * @param indexInfor The index information.
		 * 
		 * @langversion 3.0
		 * @playerversion Flash 10
		 * @playerversion AIR 1.5
		 * @productversion OSMF 1.6
		 */
		override public function initialize(indexInfo:Object):void
		{
			// Make sure we have an info object of the expected type.
			_f4fIndexInfo = indexInfo as HTTPStreamingF4FIndexInfo;
			if (_f4fIndexInfo == null || _f4fIndexInfo.streamInfos == null || _f4fIndexInfo.streamInfos.length <= 0)
			{
				CONFIG::LOGGING
				{			
					logger.error("indexInfo object wrong or contains insufficient information!");
				}
				
				dispatchEvent(new HTTPStreamingEvent(HTTPStreamingEvent.INDEX_ERROR));
				return;					
			}
			
			_indexUpdating = false;
			_pendingIndexLoads = 0;
			_pendingIndexUpdates = 0;
			_pendingIndexUrls = new Object();
			
			playInProgress = false;
			_pureLiveOffset = NaN;

			_serverBaseURL = _f4fIndexInfo.serverBaseURL;			
			_streamInfos = _f4fIndexInfo.streamInfos;

			var bootstrapBox:AdobeBootstrapBox;
			var streamCount:int = _streamInfos.length;
			
			_streamQualityRates = [];
			_streamNames = [];
			
			_bootstrapBoxesURLs = new Vector.<String>(streamCount);
			_bootstrapBoxes = new Vector.<AdobeBootstrapBox>(streamCount);
			for (var quality:int = 0; quality < streamCount; quality++)
			{
				var streamInfo:HTTPStreamingF4FStreamInfo = _streamInfos[quality];
				if (streamInfo != null)
				{
					_streamQualityRates[quality]	= streamInfo.bitrate;
					_streamNames[quality] 			= streamInfo.streamName;
					
					var bootstrap:BootstrapInfo = streamInfo.bootstrapInfo;
					
					if (bootstrap == null || (bootstrap.url == null && bootstrap.data == null))
					{
						CONFIG::LOGGING
						{			
							logger.error("Bootstrap(" + quality + ")  null or contains inadequate information!");
						}
						
						dispatchEvent(new HTTPStreamingEvent(HTTPStreamingEvent.INDEX_ERROR));
						return;					
					}
					
					if (bootstrap.data != null)
					{
						bootstrapBox = processBootstrapData(bootstrap.data, quality);
						if (bootstrapBox == null)
						{
							CONFIG::LOGGING
							{			
								logger.error("BootstrapBox(" + quality + ") is null, potentially from bad bootstrap data!");
							}
							
							dispatchEvent(new HTTPStreamingEvent(HTTPStreamingEvent.INDEX_ERROR));
							return;					
						}
						_bootstrapBoxes[quality] = bootstrapBox;
					}
					else
					{
						_bootstrapBoxesURLs[quality] 	= HTTPStreamingUtils.normalizeURL(bootstrap.url);
						
						_pendingIndexLoads++;
						dispatchIndexLoadRequest(quality);
					}
				}
			}
			
			if (_pendingIndexLoads == 0)
			{
				notifyRatesReady();
				notifyIndexReady(0);
			}
		}
		
		/**
		 * @inheritDoc
		 */
		override public function dispose():void
		{
			destroyBootstrapUpdateTimer();
			
			_pendingIndexLoads = 0;
			_pendingIndexUpdates = 0;
			_pendingIndexUrls = new Object();
			
			_bestEffortNeedsToFireFragmentDuration = false;
			_bestEffortEnabled = true;
			if (_bestEffortNotifyBootstrapBoxInfo != null && _bestEffortNotifyBootstrapBoxInfo.hasOwnProperty("downloader"))
			{
				var downloader:HTTPStreamDownloader = _bestEffortNotifyBootstrapBoxInfo.downloader as HTTPStreamDownloader;
				if (downloader != null)
				{
					downloader.close(true);
				}
			}
		}
		
		/**
		 * Called when the index file has been loaded and is ready to be processed.
		 * 
		 * @param data The data from the loaded index file.
		 * @param indexContext An arbitrary context object which describes the loaded
		 * index file.  Useful for index handlers which load multiple index files
		 * (and thus need to know which one to process).
		 *  
		 *  @langversion 3.0
		 *  @playerversion Flash 10
		 *  @playerversion AIR 1.5
		 *  @productversion OSMF 1.0
		 */
		override public function processIndexData(data:*, indexContext:Object):void
		{
			var quality:int = indexContext as int;
			var bootstrapBox:AdobeBootstrapBox = processBootstrapData(data, quality);

			if (bootstrapBox == null)
			{
				CONFIG::LOGGING
				{			
					logger.error("BootstrapBox(" + quality + ") is null when attempting to process index data during a bootstrap update.");
				}
				
				dispatchEvent(new HTTPStreamingEvent(HTTPStreamingEvent.INDEX_ERROR));
				return;					
			}

			if (!_indexUpdating) 
			{
				// we are processing an index initialization
				_pendingIndexLoads--;

				CONFIG::LOGGING
				{			
					logger.debug("Pending index loads: " + _pendingIndexLoads);
				}
			}
			else
			{
				// we are processing an index update
				_pendingIndexUpdates--;

				CONFIG::LOGGING
				{			
					logger.debug("Pending index updates: " + _pendingIndexUpdates);
				}

				var requestedUrl:String = _bootstrapBoxesURLs[quality];
				if (requestedUrl != null && _pendingIndexUrls.hasOwnProperty(requestedUrl))
				{
					_pendingIndexUrls[requestedUrl].active = false;
				}
				
				if (_pendingIndexUpdates == 0)
				{
					_indexUpdating = false;
					// FM-924, onMetadata is called twice on http streaming live/dvr content 
					// It is really unnecessary to call onMetadata multiple times. The change of
					// media length is fixed for VOD, and is informed by the call dispatchDVRStreamInfo
					// for DVR. For "pure live", it does not really matter. Whenever MBR switching
					// happens, onMetadata will be called by invoking checkMetadata method.
					// 
					//notifyTotalDuration(bootstrapBox.totalDuration / bootstrapBox.timeScale, indexContext as int);
				}
			}
			
			CONFIG::LOGGING
			{			
				logger.debug("BootstrapBox(" + quality + ") loaded successfully." + 
					"[version:" + bootstrapBox.bootstrapVersion + 
					", fragments from frt:" + bootstrapBox.totalFragments +
					", fragments from srt:" + bootstrapBox.segmentRunTables[0].totalFragments + "]"
				);
			}
			updateBootstrapBox(quality, bootstrapBox, true /* sourceIsIndex */);
			
			if (_pendingIndexLoads == 0 && !_indexUpdating)
			{
				notifyRatesReady();
				notifyIndexReady(quality);
			}
		}	
		
		/**
		 * Returns the HTTPStreamRequest which encapsulates the file for the given
		 * playback time and quality.  If no such file exists for the specified time
		 * or quality, then this method should return null. 
		 * 
		 * @param time The time for which to retrieve a request object.
		 * @param quality The quality of the requested stream.
		 *  
		 * @langversion 3.0
		 * @playerversion Flash 10
		 * @playerversion AIR 1.5
		 * @productversion OSMF 1.0
		 */
		override public function getFileForTime(time:Number, quality:int):HTTPStreamRequest
		{
			if (   quality < 0 
				|| quality >= _streamInfos.length 
				|| time < 0)
			{
				CONFIG::LOGGING
				{
					logger.warn("Invalid parameters for getFileForTime(time=" + time + ", quality=" + quality + ").");	
				}
				return new HTTPStreamRequest(HTTPStreamRequestKind.DONE);
			}
			
			// best effort fetch accounting. initially assume seeks are not best effort.
			_bestEffortState = BEST_EFFORT_STATE_OFF;
			
			var bootstrapBox:AdobeBootstrapBox = _bootstrapBoxes[quality];
			if (bootstrapBox == null)
				return new HTTPStreamRequest(HTTPStreamRequestKind.DONE);
			
			if (!playInProgress && isStopped(bootstrapBox))
			{
				destroyBootstrapUpdateTimer();
				return new HTTPStreamRequest(HTTPStreamRequestKind.DONE);
			}
							
			updateMetadata(quality);
			
			var streamRequest:HTTPStreamRequest;
			var desiredTime:Number = time * bootstrapBox.timeScale;

			// we should know the segment and fragment containing the desired time
			if(_bestEffortEnabled)
			{
				streamRequest = getFirstRequestForBestEffortSeek(desiredTime, quality, bootstrapBox);
			}
			else
			{
				streamRequest = getSeekRequestForNormalFetch(desiredTime, quality, bootstrapBox);
			}
			
			CONFIG::LOGGING
			{
				logger.debug("The url for ( time=" + time + ", quality=" + quality + ") = " + streamRequest.toString());
			}
			
			return streamRequest;
		}
				
		/**
		 * @private
		 * 
		 * helper for getFileForTime, called when best effort fetch is disabled.
		 * 
		 * @return the action to take, expressed as an HTTPStreamRequest
		 **/
		private function getSeekRequestForNormalFetch(
			desiredTime:Number,
			quality:int,
			bootstrapBox:AdobeBootstrapBox):HTTPStreamRequest
		{
			var streamRequest:HTTPStreamRequest = null;
			var refreshNeeded:Boolean = false;
			var currentTime:Number = bootstrapBox.currentMediaTime;
			var contentComplete:Boolean = bootstrapBox.contentComplete();
			var frt:AdobeFragmentRunTable = getFragmentRunTable(bootstrapBox);
			CONFIG::LOGGING
			{
				if (contentComplete)
				{
					logger.debug("Bootstrap reports that content is complete. If this is a live stream, then the publisher stopped it.");
				}
			}
			if (desiredTime <= currentTime)
			{
				if(frt != null)
				{
					_currentFAI = frt.findFragmentIdByTime(desiredTime, currentTime, contentComplete ? false : bootstrapBox.live);
				}
				if (_currentFAI == null || fragmentOverflow(bootstrapBox, _currentFAI.fragId))
				{
					// we're beyond the end of the bootstrap
					if (!bootstrapBox.live || contentComplete)
					{
						// we're either:
						// - vod, in which case we should stop playback
						// - live/DVR, and there's a null term, meaning the content is done
						return new HTTPStreamRequest(HTTPStreamRequestKind.DONE);
					}
					else
					{
						// we're in live and we reached the end of content, but we're not done
						return initiateLivenessFailure(quality);
					}
				}
				else
				{
					// normal case: we found the fragment we were looking for. initiate a download request
					return initiateNormalDownload(bootstrapBox, quality);
				}
			}
			else if(bootstrapBox.live)
			{
				// we are trying to seek beyond the "live" point in the live scenario
				return initiateBootstrapRefresh(quality);
			}
			else
			{
				// we are trying to seek beyond the "live" point in the vod scenario
				return new HTTPStreamRequest(HTTPStreamRequestKind.DONE);
			}
		}
		
		/**
		 * @private
		 */
		override public function getNextFile(quality:int):HTTPStreamRequest
		{
			if (quality < 0 || quality >= _streamInfos.length)
			{
				CONFIG::LOGGING
				{
					logger.warn("Invalid parameters for getNextFile(quality=" + quality + ").");	
				}
				return new HTTPStreamRequest(HTTPStreamRequestKind.DONE);
			}
			
			var bootstrapBox:AdobeBootstrapBox = _bootstrapBoxes[quality];
			if (bootstrapBox == null)
				return new HTTPStreamRequest(HTTPStreamRequestKind.DONE);
			
			if (!playInProgress && isStopped(bootstrapBox))
			{
				destroyBootstrapUpdateTimer();
				return new HTTPStreamRequest(HTTPStreamRequestKind.DONE);
			}
			
			updateMetadata(quality);
			var streamRequest:HTTPStreamRequest = null;
			
			if(_bestEffortEnabled)
			{
				// best effort case
				if(_bestEffortState == BEST_EFFORT_STATE_OFF ||
					_bestEffortState == BEST_EFFORT_STATE_PLAY)
				{
					streamRequest = getNextRequestForBestEffortPlay(quality, bootstrapBox);
				}
				else
				{
					streamRequest = getNextRequestForBestEffortSeek(quality, bootstrapBox);
				}
			}
			else
			{
				streamRequest = getNextRequestForNormalPlay(quality, bootstrapBox);
			}
			
			CONFIG::LOGGING
			{
				logger.debug("Next url for (quality=" + quality + ") = " + streamRequest.toString());
			}
			
			return streamRequest;
		}
		
		/**
		 * @private
		 * 
		 * helper for getNextFile, called when best effort fetch is disabled.
		 * 
		 * @return the action to take, expressed as an HTTPStreamRequest
		 **/
		private function getNextRequestForNormalPlay(
			quality:int,
			bootstrapBox:AdobeBootstrapBox):HTTPStreamRequest
		{
			var streamRequest:HTTPStreamRequest;
			var currentTime:Number = bootstrapBox.currentMediaTime;
			
			var contentComplete:Boolean = bootstrapBox.contentComplete();
			CONFIG::LOGGING
			{
				if (contentComplete)
				{
					logger.debug("Bootstrap reports that content is complete. If this is a live stream, then the publisher stopped it.");
				}
			}
			
			var oldCurrentFAI:FragmentAccessInformation = _currentFAI;
			var frt:AdobeFragmentRunTable = getFragmentRunTable(bootstrapBox);
			if (oldCurrentFAI == null)
			{
				_currentFAI = null;
			}
			if(frt != null)
			{
				_currentFAI = frt.validateFragment(
					oldCurrentFAI.fragId + 1, // fragId
					currentTime, // totalDuration
					contentComplete ? false : bootstrapBox.live);
			}
			if (_currentFAI == null || fragmentOverflow(bootstrapBox, _currentFAI.fragId))
			{
				// we're beyond the end of the bootstrap
				_currentFAI = oldCurrentFAI;
				if (!bootstrapBox.live || contentComplete)
				{
					// we're either:
					// - vod, in which case we should stop playback
					// - live/DVR, and there's a null term, meaning the content is done
					return new HTTPStreamRequest(HTTPStreamRequestKind.DONE);
				}
				else
				{
					// we're in live and we reached the end of content, but we're not done
					_currentFAI = oldCurrentFAI;
					return initiateLivenessFailure(quality);
				}
			}
			else
			{
				// normal case: we found the fragment we were looking for. initiate a download request
				return initiateNormalDownload(bootstrapBox, quality);
			}
		}
		
		
		/**
		 * @private
		 * 
		 * Initiates a live failed request (from getNextFile or getFileForTime).
		 * 
		 * @return the action to take, expressed as an HTTPStreamRequest
		 **/
		private function initiateLivenessFailure(quality:int):HTTPStreamRequest
		{
			adjustDelay();
			refreshBootstrapBox(quality);
			
			var livenessDelay:Number;
			if(_bestEffortEnabled)
			{
				// after half a fragment duration the bootstrap should be updated
				// but at least a second
				livenessDelay = Math.max((_f4fIndexInfo.bestEffortFetchInfo.fragmentDuration / 2) / 1000.0, 1);
			}
			else
			{
				// otherwise, just use the default delay
				livenessDelay = _delay;
			}
			return new HTTPStreamRequest(
				HTTPStreamRequestKind.LIVE_STALL,
				null, // url
				livenessDelay); // retryAfter
		}
		
		/**
		 * @private
		 * 
		 * Initiates a refresh request (from getNextFile or getFileForTime).
		 * 
		 * @return the action to take, expressed as an HTTPStreamRequest
		 **/
		private function initiateBootstrapRefresh(quality:int):HTTPStreamRequest
		{
			adjustDelay();
			refreshBootstrapBox(quality);
			return new HTTPStreamRequest(
				HTTPStreamRequestKind.RETRY,
				null, // url
				_delay); // retryAfter
		}
		
		/**
		 * @private
		 * 
		 * Initiates a normal download request (from getNextFile or getFileForTime).
		 * 
		 * @return the action to take, expressed as an HTTPStreamRequest
		 **/
		private function initiateNormalDownload(bootstrapBox:AdobeBootstrapBox, quality:int):HTTPStreamRequest
		{
			// if we had a pending BEF download, invalidate it
			stopListeningToBestEffortDownload();

			// if we encounter liveness in the future, restart BEFs
			// after the current fragment
			_bestEffortLivenessRestartPoint = _currentFAI.fragId;
			CONFIG::LOGGING
			{
				logger.debug("Setting _bestEffortLivenessRestartPoint to "+_bestEffortLivenessRestartPoint+" because of normal download.");
			}
			
			// remember that we started a download now
			_bestEffortLastGoodFragmentDownloadTime = new Date();
			
			playInProgress = true;
			updateQuality(quality);
			notifyFragmentDuration(_currentFAI.fragDuration / bootstrapBox.timeScale);
			return new HTTPStreamRequest(
				HTTPStreamRequestKind.DOWNLOAD,
				getFragmentUrl(quality, _currentFAI)); // url
		}
		
		/**
		 * Checks if specified fragment identifier overflows the actual 
		 * fragments contained into the bootstrap.
		 * 
		 * @param  bootstrapBox The bootstrap which contains the fragment run table.
		 * @param fragId Specified fragment identifier which must be checked.
		 * 
		 * @langversion 3.0
		 * @playerversion Flash 10
		 * @playerversion AIR 1.5
		 * @productversion OSMF 1.0
		 */
		private function fragmentOverflow(bootstrapBox:AdobeBootstrapBox, fragId:uint):Boolean
		{
			var fragmentRunTable:AdobeFragmentRunTable = bootstrapBox.fragmentRunTables[0];
			var fdp:FragmentDurationPair = fragmentRunTable.fragmentDurationPairs[0];
			var segmentRunTable:AdobeSegmentRunTable = bootstrapBox.segmentRunTables[0];
			return ((segmentRunTable == null) || ((segmentRunTable.totalFragments + fdp.firstFragment - 1) < fragId));
		}

		/**
		 * Checks if there is no more data available for a specified
		 * bootstrap and if we should stop playback.
		 * 
		 * @langversion 3.0
		 * @playerversion Flash 10
		 * @playerversion AIR 1.5
		 * @productversion OSMF 1.0
		 */
		private function isStopped(bootstrapBox:AdobeBootstrapBox):Boolean
		{
			var result:Boolean = false;
			
			if (_f4fIndexInfo.dvrInfo != null)
			{
				// in DVR scenario, the content is considered stopped once the dvr 
				// data is taken offline
				result = _f4fIndexInfo.dvrInfo.offline;
			}
			else if (bootstrapBox != null && bootstrapBox.live)
			{
				// in pure live, the content is considered stopped once the 
				// fragment run table reports complete flag is set
				var frt:AdobeFragmentRunTable = getFragmentRunTable(bootstrapBox);
				if (frt != null)
				{
					result = frt.tableComplete();
				}
			}
						
			return result;
		}
		
		/**
		 * Gets the url for specified fragment and quality.
		 * 
		 * @langversion 3.0
		 * @playerversion Flash 10
		 * @playerversion AIR 1.5
		 * @productversion OSMF 1.6
		 */
		private function getFragmentUrl(quality:int, fragment:FragmentAccessInformation):String
		{
			var bootstrapBox:AdobeBootstrapBox = _bootstrapBoxes[quality];
			var frt:AdobeFragmentRunTable = getFragmentRunTable(bootstrapBox);
			var fdp:FragmentDurationPair = frt.fragmentDurationPairs[0];
			var segId:uint = bootstrapBox.findSegmentId(fragment.fragId - fdp.firstFragment + 1);
			
			return constructFragmentRequest(_serverBaseURL, _streamNames[quality], segId, fragment.fragId);
		}
		
		/**
		 * Constructs the url for specified parameters.
		 * 		  
		 * @langversion 3.0
		 * @playerversion Flash 10
		 * @playerversion AIR 1.5
		 * @productversion OSMF 1.6
		 */
		protected function constructFragmentRequest(serverBaseURL:String, streamName:String, segmentId:uint, fragmentId:uint):String
		{
			var requestUrl:String = "";
			if (streamName != null && streamName.indexOf("http") != 0)
			{
				requestUrl = serverBaseURL + "/" ;
			}
			requestUrl += streamName;
			
			var tempURL:URL = new URL(requestUrl);
			tempURL.path += "Seg" + segmentId + "-Frag" + fragmentId;
			
			requestUrl = tempURL.protocol + "://" + tempURL.host;
			if (tempURL.port != null && tempURL.port.length > 0)
			{
				requestUrl += ":" + tempURL.port;
			}
			requestUrl += "/" + tempURL.path;
			if (tempURL.query != null && tempURL.query.length > 0)
			{
				requestUrl += "?" + tempURL.query;
			}
			if (tempURL.fragment != null && tempURL.fragment.length > 0)
			{
				requestUrl += "#" + tempURL.fragment;
			}
			
			return requestUrl;
		}
		
		/**
		 * Returns the fragment run table from the specified bootstrap box.
		 * It assumes that there is only one fragment run table.
		 * 
		 * @langversion 3.0
		 * @playerversion Flash 10
		 * @playerversion AIR 1.5
		 * @productversion OSMF 1.0
		 */
		private function getFragmentRunTable(bootstrapBox:AdobeBootstrapBox):AdobeFragmentRunTable
		{
			if (bootstrapBox == null)
				return null;
			
			return bootstrapBox.fragmentRunTables[0];
		}

		/**
		 * Adjusts the delay for future inquires from clients.
		 * When the index handler needs more time to obtain data in order to
		 * respond to a request from its clients, it will return a response 
		 * requesting more time. This method varies the delay.
		 * 
		 * @langversion 3.0
		 * @playerversion Flash 10
		 * @playerversion AIR 1.5
		 * @productversion OSMF 1.6
		 */
		private function adjustDelay():void
		{
			if (_delay < 1.0)
			{
				_delay = _delay * 2.0;
				if (_delay > 1.0)
				{
					_delay = 1.0;
				} 
			}
		}

		/**
		 * Issues a request for refreshing the specified quality bootstrap.
		 *
		 * @param quality Quality level for which a refresh should be requested.
		 * 
		 * @langversion 3.0
		 * @playerversion Flash 10
		 * @playerversion AIR 1.5
		 * @productversion OSMF 1.0
		 */
		private function refreshBootstrapBox(quality:uint):void
		{
			var requestedUrl:String = _bootstrapBoxesURLs[quality];
			if (requestedUrl == null)
				return;
			
			var pendingIndexUrl:Object = null;
			if (_pendingIndexUrls.hasOwnProperty(requestedUrl))
			{
				pendingIndexUrl = _pendingIndexUrls[requestedUrl];
			}
			else
			{
				pendingIndexUrl = new Object();
				pendingIndexUrl["active"] = false;
				pendingIndexUrl["date"] = null;
				_pendingIndexUrls[requestedUrl] = pendingIndexUrl;
			}

			var ignoreRefreshRequest:Boolean = pendingIndexUrl.active;
			var newRequestDate:Date = new Date();
			var elapsedTime:Number = 0;
			
			if (!ignoreRefreshRequest && OSMFSettings.hdsMinimumBootstrapRefreshInterval > 0)
			{
				var previousRequestDate:Date = pendingIndexUrl["date"];
				elapsedTime = Number.MAX_VALUE;
				if (previousRequestDate != null)
				{
					elapsedTime = newRequestDate.valueOf() - previousRequestDate.valueOf();
				}
				
				ignoreRefreshRequest = elapsedTime < OSMFSettings.hdsMinimumBootstrapRefreshInterval;
			}
			
			if (!ignoreRefreshRequest)
			{
				_pendingIndexUrls[requestedUrl].date = newRequestDate;
				_pendingIndexUrls[requestedUrl].active = true;
				_pendingIndexUpdates++;
				_indexUpdating = true;
				
				CONFIG::LOGGING
				{
					logger.debug("Refresh (quality=" + quality + ") using " + requestedUrl + ". [active=" + pendingIndexUrl.active + ", elapsedTime=" + elapsedTime.toFixed(2) + "]");
				}
				
				dispatchIndexLoadRequest(quality);
			}
			else
			{
				CONFIG::LOGGING
				{
					logger.debug("Refresh (quality=" + quality + ") ignored. [active=" + pendingIndexUrl.active + ", elapsedTime=" + elapsedTime.toFixed(2) + "]");
				}
			}
		}

		/**
		 * Updates the specified bootstrap box if the specified information
		 * is newer than the current one. If the updated box if the current one, 
		 * it also refreshes the associated DVR information.
		 *  
		 * @langversion 3.0
		 * @playerversion Flash 10
		 * @playerversion AIR 1.5
		 * @productversion OSMF 1.6
		 */
		private function updateBootstrapBox(
			quality:int, 
			bootstrapBox:AdobeBootstrapBox,
			sourceIsIndex:Boolean):void
		{
			
			if (shouldAcceptBootstrapBox(quality, bootstrapBox, sourceIsIndex))
			{
				CONFIG::LOGGING
				{
					logger.debug("Bootstrap information for quality[" + quality + "] updated. ("
						+ ", live=" + bootstrapBox.live
						+ ", time=" + bootstrapBox.currentMediaTime
						+ ", first="+ bootstrapBox.fragmentRunTables[0].firstFragmentId
						+ ", gapCount="+ bootstrapBox.fragmentRunTables[0].countGapFragments()
						+ ", done="+ bootstrapBox.contentComplete() +")");
				}
				_bootstrapBoxes[quality] = bootstrapBox;
				_delay = 0.05;
				if (quality == _currentQuality)
				{
					dispatchDVRStreamInfo(bootstrapBox);
				}
			}
		}
		
		/**
		 * @return true if the specified bootstrap should be accepted over the existing bootstrap
		 */
		private function shouldAcceptBootstrapBox(quality:int,
												  newBootstrap:AdobeBootstrapBox,
												  sourceIsIndex:Boolean):Boolean
		{
			var existingBootstrap:AdobeBootstrapBox = _bootstrapBoxes[quality];
			if(newBootstrap == null ||
				newBootstrap.fragmentRunTables.length == 0 ||
				newBootstrap.segmentRunTables.length == 0)
			{
				// reject invalid
				return false;
			}
			var newFrt:AdobeFragmentRunTable = newBootstrap.fragmentRunTables[0];
			var newSrt:AdobeSegmentRunTable = newBootstrap.segmentRunTables[0];
			if(newFrt == null || newSrt == null)
			{
				// reject invalid
				return false;
			}
			if(newFrt.firstFragmentId == 0)
			{
				// don't accept bootstraps that have no valid entries
				return false;
			}
			if(existingBootstrap == null)
			{
				// accept if we don't have a bootstrap yet
				return true;
			}
			if(existingBootstrap.live != newBootstrap.live)
			{
				// bootstrap went from live to vod or vod to live
				// this is not good. reject the bootstrap.
				return false;
			}
			if(!existingBootstrap.live)
			{
				// VOD
				
				// accept newer bootstrap if the version is newer
				if(newBootstrap.version != existingBootstrap.version)
				{
					return newBootstrap.version > existingBootstrap.version;
				}
				
				// tie breaker is the media time
				return newBootstrap.currentMediaTime > existingBootstrap.currentMediaTime;
			}
			else
			{
				// LIVE
				
				if(!sourceIsIndex)
				{
					// do not accept bootstraps that are internal to the fragment since they lie.
					return false;
				}
			
				// historical note: we do not use the version anymore. with multiple packagers,
				// the version can become out of sync when there are discontinuities. fixing
				// this would require synchronization across packagers of bootstrap version which
				// would be done by deriving version from the common clock (currentMediaTime). thus
				// it makes sense to accept changes based on currentMediaTime instead and ignore
				// the version.
				
				var existingFrt:AdobeFragmentRunTable = existingBootstrap.fragmentRunTables[0];
				var existingSrt:AdobeSegmentRunTable = existingBootstrap.segmentRunTables[0];
				
				if(newBootstrap.currentMediaTime != existingBootstrap.currentMediaTime)
				{
					// prefer newer bootstraps
					return newBootstrap.currentMediaTime > existingBootstrap.currentMediaTime;
				}
				
				if(newFrt.firstFragmentId != existingFrt.firstFragmentId)
				{
					// prefer bootstraps with an earlier starting point
					return newFrt.firstFragmentId < existingFrt.firstFragmentId;
				}
				
				var newGapCount:uint = newFrt.countGapFragments();
				var existingGapCount:uint = existingFrt.countGapFragments();
				if(newGapCount != existingGapCount)
				{
					// prefer bootstraps with fewer gap fragments
					return newGapCount < existingGapCount;
				}
				
				if(newBootstrap.contentComplete() && !existingBootstrap.contentComplete())
				{
					// prefer done bootstraps
					return true;
				}
				
				// otherwise reject bootstraps
				return false;
			}
		}
		
		/**
		 * Processes bootstrap raw information and returns an AdobeBootstrapBox object.
		 * 
		 * @param data The raw representation of bootstrap.
		 * @param indexContext The index context used while processing bootstrap.
		 * 
		 * @langversion 3.0
		 * @playerversion Flash 10
		 * @playerversion AIR 1.5
		 * @productversion OSMF 1.6
		 */
		private function processBootstrapData(data:*, indexContext:Object):AdobeBootstrapBox
		{
			var parser:BoxParser = new BoxParser();
			data.position = 0;
			parser.init(data);
			try
			{
				var boxes:Vector.<Box> = parser.getBoxes();
			}
			catch (e:Error)
			{
				boxes = null;
			}
			
			if (boxes == null || boxes.length < 1)
			{
				return null;
			}
			
			var bootstrapBox:AdobeBootstrapBox = boxes[0] as AdobeBootstrapBox;
			if (bootstrapBox == null)
			{
				return null;
			}
			
			if (_serverBaseURL == null || _serverBaseURL.length <= 0)
			{
				if (bootstrapBox.serverBaseURLs == null || bootstrapBox.serverBaseURLs.length <= 0)
				{
					// If serverBaseURL is not set from the external, we need to pick 
					// a server base URL from the bootstrap box. For now, we just
					// pick the first one. It is an error if the server base URL is null 
					// under this circumstance.
					return null;
				}
				
				_serverBaseURL = bootstrapBox.serverBaseURLs[0];
			}
			
			return bootstrapBox;
		}	

		/**
		 * Updates the current quality index. 
		 * 
		 * Also in MBR scenarios with protected content we need to append the additionalHeader 
		 * that contains the DRM metadata to the Flash Player for that fragment before any 
		 * additional TCMessage.
		 *  
		 * @langversion 3.0
		 * @playerversion Flash 10
		 * @playerversion AIR 1.5
		 * @productversion OSMF 1.6
		 */
		private function updateQuality(quality:int):void
		{
			if (quality != _currentQuality)
			{
				// we preserve this for later comparison
				var prevAdditionalHeader:ByteArray = _currentAdditionalHeader;
				var newAdditionalHeader:ByteArray = _streamInfos[quality].additionalHeader;

				CONFIG::LOGGING
				{
					logger.debug("Quality changed from " + _currentQuality + " to " +  quality + ".");
				}
				_currentQuality = quality;
				_currentAdditionalHeader = newAdditionalHeader;
				
				// We compare the two DRM headers and if they are different we inject
				// the new one as script data into the underlying objects.
				// Strictly speaking, the != comparison of additional header is not enough. 
				// Ideally, we need to do bytewise comparison, however there might be a performance
				// hit considering the size of the additional header.
				if (newAdditionalHeader != null && newAdditionalHeader != prevAdditionalHeader)
				{
					CONFIG::LOGGING
					{
						logger.debug("Update of DRM header is required.");
					}
					dispatchAdditionalHeader(newAdditionalHeader);
				}
			}
		}

		/**
		 * Updates the metadata for the current quality.
		 * 
		 * @langversion 3.0
		 * @playerversion Flash 10
		 * @playerversion AIR 1.5
		 * @productversion OSMF 1.6
		 */
		private function updateMetadata(quality:int):void
		{
			if (quality != _currentQuality)
			{
                var bootstrapBox:AdobeBootstrapBox = _bootstrapBoxes[quality];
				if (bootstrapBox != null)
				{
                    //#136 when we are streaming live and not in dvr mode set the duration to zero.
					notifyTotalDuration(bootstrapBox.live && !_f4fIndexInfo.dvrInfo ? 0 : bootstrapBox.totalDuration / bootstrapBox.timeScale, quality);
				}
			}
		}

		/**
		 * Dispatches the protected content header.
		 *  
		 * @langversion 3.0
		 * @playerversion Flash 10
		 * @playerversion AIR 1.5
		 * @productversion OSMF 1.6
		 */
		private function dispatchAdditionalHeader(additionalHeader:ByteArray):void
		{
			var flvTag:FLVTagScriptDataObject = new FLVTagScriptDataObject();
			flvTag.data = additionalHeader;
			
			dispatchEvent(
				new HTTPStreamingEvent(
					HTTPStreamingEvent.SCRIPT_DATA
					, false
					, false
					, 0
					, flvTag
					, FLVTagScriptDataMode.FIRST
				)
			);
		}
		
		/**
		 * Dispatches the DVR information extracted from the specified bootstrap.
		 *  
		 * @param bootstrapBox The bootstrap box containing the DVR information.
		 * 
		 * @langversion 3.0
		 * @playerversion Flash 10
		 * @playerversion AIR 1.5
		 * @productversion OSMF 1.6
		 */
		private function dispatchDVRStreamInfo(bootstrapBox:AdobeBootstrapBox):void
		{
			var frt:AdobeFragmentRunTable = getFragmentRunTable(bootstrapBox);
			
			var dvrInfo:DVRInfo = _f4fIndexInfo.dvrInfo;
			if (dvrInfo != null)
			{
				// update recording status from fragment runt table
				dvrInfo.isRecording = !frt.tableComplete();
				
				// calculate current duration
				var currentDuration:Number = bootstrapBox.totalDuration/bootstrapBox.timeScale;
				var currentTime:Number = bootstrapBox.currentMediaTime/bootstrapBox.timeScale;
				
				// update start time for the first time
				if (isNaN(dvrInfo.startTime))
				{
					if (!dvrInfo.isRecording)
					{
						dvrInfo.startTime = 0;
					}
					else
					{
						var beginOffset:Number = ((dvrInfo.beginOffset < 0) || isNaN(dvrInfo.beginOffset)) ? 0 : dvrInfo.beginOffset;
						var endOffset:Number = ((dvrInfo.endOffset < 0) || isNaN(dvrInfo.endOffset))? 0 : dvrInfo.endOffset;
						
						dvrInfo.startTime = DVRUtils.calculateOffset(beginOffset, endOffset, currentDuration);  
					}
					
					dvrInfo.startTime += (frt.fragmentDurationPairs)[0].durationAccrued/bootstrapBox.timeScale;
					if (dvrInfo.startTime > currentTime)
					{
						dvrInfo.startTime = currentTime;
					}
				}
				
				// update current length of the DVR window 
				dvrInfo.curLength = currentTime - dvrInfo.startTime;	
				
				// adjust the start time if we have a DVR rooling window active
				if ((dvrInfo.windowDuration != -1) && (dvrInfo.curLength > dvrInfo.windowDuration))
				{
					dvrInfo.startTime += dvrInfo.curLength - dvrInfo.windowDuration;
					dvrInfo.curLength = dvrInfo.windowDuration;
				}
				
				dispatchEvent(
					new DVRStreamInfoEvent(
						DVRStreamInfoEvent.DVRSTREAMINFO, 
						false, 
						false, 
						dvrInfo
					)
				); 								
			}	
		}

		
		/**
		 * Dispatches an event requesting loading/refreshing of the specified quality.
		 * 
		 * @param quality The quality level for which the request should be made.
		 *  
		 * @langversion 3.0
		 * @playerversion Flash 10
		 * @playerversion AIR 1.5
		 * @productversion OSMF 1.6
		 */
		private function dispatchIndexLoadRequest(quality:int):void
		{
			dispatchEvent(
				new HTTPStreamingIndexHandlerEvent( 
					HTTPStreamingIndexHandlerEvent.REQUEST_LOAD_INDEX 
					, false
					, false
					, false
					, NaN
					, null
					, null
					, new URLRequest(_bootstrapBoxesURLs[quality])
					, quality
					, true
				)
			);
		}
		
		/**
		 * Notifies clients that rates are ready.
		 *  
		 * @langversion 3.0
		 * @playerversion Flash 10
		 * @playerversion AIR 1.5
		 * @productversion OSMF 1.6
		 */
		private function notifyRatesReady():void
		{
			dispatchEvent( 
				new HTTPStreamingIndexHandlerEvent( 
					HTTPStreamingIndexHandlerEvent.RATES_READY
					, false
					, false
					, false
					, NaN
					, _streamNames
					, _streamQualityRates
				)
			);
		}
		
		/**
		 * Notifies clients that index is ready.
		 *  
		 * @langversion 3.0
		 * @playerversion Flash 10
		 * @playerversion AIR 1.5
		 * @productversion OSMF 1.6
		 */
		private function notifyIndexReady(quality:int):void
		{
			var bootstrapBox:AdobeBootstrapBox = _bootstrapBoxes[quality];
			var frt:AdobeFragmentRunTable = getFragmentRunTable(bootstrapBox);
			
			if(!_bestEffortInited)
			{
				// initialize _bestEffortEnabled
				// best effort is only enabled if
				// - enabled in the f4m
				// - the stream is live (the first bootstrap is marked as live)
				// this codepath is executed via DVRGetStreamInfo
				// after the first bootstrap fetch, but
				// before the load state is ready.
				_bestEffortEnabled =_f4fIndexInfo.bestEffortFetchInfo != null && bootstrapBox.live;
				_bestEffortInited = true;
			}
			
			dispatchDVRStreamInfo(bootstrapBox);
			
			if (!_invokedFromDvrGetStreamInfo)
			{
				// in pure live scenario, update the "closest" position to live we want
				if (bootstrapBox.live && _f4fIndexInfo.dvrInfo == null && isNaN(_pureLiveOffset))
				{
					_pureLiveOffset = bootstrapBox.currentMediaTime - OSMFSettings.hdsPureLiveOffset * bootstrapBox.timeScale;
					if (_pureLiveOffset < 0)
					{
						_pureLiveOffset = NaN;
					}
					else
					{
						_pureLiveOffset = _pureLiveOffset / bootstrapBox.timeScale;
					}
				}
				
				// If the stream is live, initialize the bootstrap update timer
				// if we are in a live stream with rolling window feature activated
				if (bootstrapBox.live && _f4fIndexInfo.dvrInfo != null)
				{
					initializeBootstrapUpdateTimer();
				}
				
				if (frt.tableComplete())
				{
					// Destroy the timer if the stream is no longer recording
					destroyBootstrapUpdateTimer();
				}
				
				dispatchEvent(
					new HTTPStreamingIndexHandlerEvent(
						HTTPStreamingIndexHandlerEvent.INDEX_READY
						, false
						, false
						, bootstrapBox.live
						, _pureLiveOffset 
					)
				);
			}
			_invokedFromDvrGetStreamInfo = false;
		}

		/**
		 * Notifies clients that total duration is available through onMetadata message.
		 *  
		 * @langversion 3.0
		 * @playerversion Flash 10
		 * @playerversion AIR 1.5
		 * @productversion OSMF 1.0
		 */
		private function notifyTotalDuration(duration:Number, quality:int):void
		{
			var metaInfo:Object = _streamInfos[quality].streamMetadata;
			if (metaInfo == null)
			{
				metaInfo = new Object();
			}


			metaInfo.duration = duration;
			
			var sdo:FLVTagScriptDataObject = new FLVTagScriptDataObject();
			sdo.objects = ["onMetaData", metaInfo];
			dispatchEvent(
				new HTTPStreamingEvent(
					HTTPStreamingEvent.SCRIPT_DATA
					, false
					, false
					, 0
					, sdo
					, FLVTagScriptDataMode.IMMEDIATE
				)
			);
		}

		/**
		 * Notifies clients that total duration is available through onMetadata message.
		 *  
		 * @langversion 3.0
		 * @playerversion Flash 10
		 * @playerversion AIR 1.5
		 * @productversion OSMF 1.0
		 */
		private function notifyFragmentDuration(duration:Number):void
		{
			// Update the bootstrap update interval; we set its value to the fragment duration
			bootstrapUpdateInterval = duration * 1000;
			if (bootstrapUpdateInterval < OSMFSettings.hdsMinimumBootstrapRefreshInterval)
			{
				bootstrapUpdateInterval = OSMFSettings.hdsMinimumBootstrapRefreshInterval;
			}
			
			dispatchEvent(
				new HTTPStreamingEvent( 
					HTTPStreamingEvent.FRAGMENT_DURATION 
					, false
					, false
					, duration
					, null
					, null
				)
			);				
		}

		private function initializeBootstrapUpdateTimer():void
		{
			if (bootstrapUpdateTimer == null)
			{
				// This will regularly update the bootstrap information;
				// We just initialize the timer here; we'll start it in the first call of the getFileForTime method
				// or in the first call of getNextFile
				// The initial delay is 4000 (recommended fragment duration)
				bootstrapUpdateTimer = new Timer(bootstrapUpdateInterval);
				bootstrapUpdateTimer.addEventListener(TimerEvent.TIMER, onBootstrapUpdateTimer);
				bootstrapUpdateTimer.start();
			}
		}
		
		private function destroyBootstrapUpdateTimer():void
		{
			if (bootstrapUpdateTimer != null)
			{
				bootstrapUpdateTimer.removeEventListener(TimerEvent.TIMER, onBootstrapUpdateTimer);
				bootstrapUpdateTimer = null;
			}
		}

		/// Event handlers
		/**
		 * Handler called when bootstrap information is available from external objects
		 * (for exemple: the stream packager can insert bootstrap information into
		 * the stream itself, and this information is processed by file handler).
		 * 
		 * We will use it to update the bootstrap information for current quality.
		 * 
		 * @langversion 3.0
		 * @playerversion Flash 10
		 * @playerversion AIR 1.5
		 * @productversion OSMF 1.6
		 */ 
		private function onBootstrapBox(event:HTTPStreamingFileHandlerEvent):void
		{
			CONFIG::LOGGING
			{
				logger.debug("Bootstrap box inside media stream found. Trying to update the bootstrap");
			}
			updateBootstrapBox(_currentQuality, event.bootstrapBox, false /* sourceIsIndex */);			
			
			// for best effort fetches, we will trigger the FRAGMENT_DURATION event after we finish the download.
			notifyFragmentDurationForBestEffort(event.bootstrapBox);
		}
		
		private function onBootstrapUpdateTimer(event:TimerEvent):void
		{ 
			if (_currentQuality != -1)
			{
				refreshBootstrapBox(_currentQuality);
				bootstrapUpdateTimer.delay = bootstrapUpdateInterval;
			}
		}
		
		/**
		 * @private
		 * 
		 * helper for getFileForTime, called when best effort fetch is enabled.
		 * 
		 * @return the action to take, expressed as an HTTPStreamRequest
		 **/
		private function getFirstRequestForBestEffortSeek(
			desiredTime:Number,
			quality:int,
			bootstrapBox:AdobeBootstrapBox):HTTPStreamRequest
		{
			bestEffortLog("Initiating best effort seek "+desiredTime);
			
			_bestEffortState = BEST_EFFORT_STATE_SEEK_BACKWARD;
			_bestEffortSeekTime = desiredTime;
			_bestEffortFailedFetches = 0;
			_bestEffortLastGoodFragmentDownloadTime = null;

			return getNextRequestForBestEffortSeek(quality, bootstrapBox);
		}
		
		/**
		 * @private
		 * 
		 * helper. called when best effort fetch is enabled during the initial seek
		 * (getFileForTime) as well as in subsequent iterations (getNextFile).
		 * 
		 * @return the action to take, expressed as an HTTPStreamRequest
		 **/
		private function getNextRequestForBestEffortSeek(
			quality:int,
			bootstrapBox:AdobeBootstrapBox):HTTPStreamRequest
		{
			var frt:AdobeFragmentRunTable = getFragmentRunTable(bootstrapBox);
			if(frt == null)
			{
				// rare case: there were no fragment run table entries
				bestEffortLog("Best effort done because the bootstrap box was invalid");
				return new HTTPStreamRequest(HTTPStreamRequestKind.DONE);
			}
			
			// if we had a pending BEF download, invalidate it
			stopListeningToBestEffortDownload();
			
			_currentFAI = null;
			
			var bestEffortSeekResult:uint = doBestEffortSeek(bootstrapBox, frt);
			if(bestEffortSeekResult != 0)
			{
				// doing best effort
				// initialize stuff related to reading the f4f
				bestEffortLog("Best effort seek fetch for fragment "+bestEffortSeekResult);
				_bestEffortF4FHandler.beginProcessFile(true, _bestEffortSeekTime);
				_bestEffortSeekBuffer.length = 0;
				_bestEffortSeekBuffer.position = 0;
				return initiateBestEffortRequest(bestEffortSeekResult, quality);
			}
			else
			{
				// revert to normal fetch, usually because
				// - the desired time is present in the FRT.
				// - we're seeking to a time >= currentMediaTime (this occurs when
				//   when we're initially playing a live stream with no live offset.)
				// - or, all best effort fetches failed
				//
				// we are not calling findFragmentIdByTime here because there are edge cases
				// where findFragmentIdByTime will return null or invalid results. instead,
				// use the well-defined getFragmentWithTimeGreq.
				_bestEffortState = BEST_EFFORT_STATE_OFF;
				_currentFAI = frt.getFragmentWithTimeGreq(_bestEffortSeekTime);
				if (_currentFAI == null)
				{
					// we didn't find the time in the FRT.
					// this is usually because we're seeking to a time >= currentMediaTime,
					// or there are no FRT entries (rare).
					// in any case, initiate playback from the specified time.
					bestEffortLog("Best effort done because there were no bootstrap entries");
					_bestEffortState = BEST_EFFORT_STATE_OFF;
					_bestEffortLivenessRestartPoint = Math.max(guessFragmentIdForTime(_bestEffortSeekTime), 1) - 1; 
					_currentFAI = new FragmentAccessInformation();
					_currentFAI.fragId = _bestEffortLivenessRestartPoint;
					return getNextFile(quality);
				}
				else
				{
					// normal case. we found something, play it out.
					bestEffortLog("Normal seek request for fragment "+_currentFAI.fragId);
					return initiateNormalDownload(bootstrapBox, quality);
				}
			}
		}
		
		/**
		 * @private
		 * 
		 * @return the expected fragment number for the desired seek time
		 **/
		private function guessFragmentIdForTime(time:Number):uint
		{
			return uint(Math.floor(time / _f4fIndexInfo.bestEffortFetchInfo.fragmentDuration)) + 1;
		}
		
		/**
		 * @private
		 * 
		 * helper for getNextRequestForBestEffortSeek. figures out the next fragment number to best-effort-fetch for the current seek.
		 * 
		 * @return the fragment number to best effort fetch, or 0 to revert to normal fetch
		 **/
		private function doBestEffortSeek(
			bootstrapBox:AdobeBootstrapBox,
			frt:AdobeFragmentRunTable):uint
		{
			if(_bestEffortSeekTime >= bootstrapBox.currentMediaTime)
			{
				// don't do best effort if we're looking for a future time
				bestEffortLog("Seek time greter than current media time.");
				return 0;
			}

			if(!frt.isTimeInGap(_bestEffortSeekTime, _f4fIndexInfo.bestEffortFetchInfo.fragmentDuration))
			{
				// don't do best effort if the time is present in the frt
				bestEffortLog("Found seek time in FRT");
				return 0;
			}

			var firstFragment:FragmentAccessInformation = frt.getFragmentWithIdGreq(0);
			if(firstFragment != null)
			{
				// don't do best effort if we're looking for a time before the first bootstrap entry
				var firstFragmentStartTime:Number = firstFragment.fragmentEndTime - firstFragment.fragDuration;
				if(_bestEffortSeekTime < firstFragmentStartTime)
				{
					bestEffortLog("Seek time before first bootstrap entry time.");
					return 0;
				}
			}

			if(_bestEffortState == BEST_EFFORT_STATE_SEEK_BACKWARD)
			{
				var backwardSeekResult:uint = doBestEffortSeekBackward(frt);
				if(backwardSeekResult != 0)
				{
					return backwardSeekResult;
				}
				else
				{
					// backward seek failed
					_bestEffortState = BEST_EFFORT_STATE_SEEK_FORWARD;
					_bestEffortFailedFetches = 0;
				}
			}
			// _bestEffortState == BEST_EFFORT_STATE_SEEK_FORWARD
			var forwardSeekResult:uint = doBestEffortSeekForward(frt);
			if(forwardSeekResult != 0)
			{
				return forwardSeekResult;
			}
			else
			{
				// forward seek failed
				return 0;
			}
		}
		
		/**
		 * @private
		 * 
		 * helper for doBestEffortSeek. figures out the next fragment number to best-effort-fetch for the current seek
		 * while in backward fetch state.
		 * 
		 * @return the fragment number to best effort fetch, or 0 if we should not perform backward fetch.
		 **/
		private function doBestEffortSeekBackward(frt:AdobeFragmentRunTable):uint
		{
			// guess a fragment number for _bestEffortSeekTime
			if(_bestEffortFailedFetches >= _f4fIndexInfo.bestEffortFetchInfo.maxBackwardFetches)
			{
				// we exhausted all our best effort backward fetches. move to forwards search.
				bestEffortLog("Best effort seek backward failing due to too many failures");
				return 0;
			}
			var guessFragmentId:uint = guessFragmentIdForTime(_bestEffortSeekTime);
			if(guessFragmentId <= _bestEffortFailedFetches + 1)
			{
				// going backwards would hit fragment number 0
				bestEffortLog("Best effort seek backward hit fragment 0");
				return 0;
			}
			var nextFragmentId:uint = guessFragmentId - (_bestEffortFailedFetches + 1);
					
			// search the fragment run table to see if the desired fragment is already available
			if(!frt.isFragmentInGap(nextFragmentId))
			{
				// special case: the fragment we were going to request already exists in
				// the fragment run table, and already know from our findFragmentIdByTime
				// call that it doesn't contain the desired time. move to forwards search.
				bestEffortLog("Best effort seek backward hit an existing fragment "+nextFragmentId);
				return 0;
			}
			
			bestEffortLog("Best effort seek backward fetch "+nextFragmentId);
			return nextFragmentId; // true because this is a best effort fetch
		}
		
		/**
		 * @private
		 * 
		 * helper for doBestEffortSeek. figures out the next fragment number to best-effort-fetch for the current seek
		 * while in forward fetch state.
		 * 
		 * @return the fragment number to best effort fetch, or 0 if we should not perform forward fetch.
		 **/
		private function doBestEffortSeekForward(frt:AdobeFragmentRunTable):uint
		{
			if (_bestEffortFailedFetches >= _f4fIndexInfo.bestEffortFetchInfo.maxForwardFetches)
			{
				// all best effort fetches failed.
				// give up and just perform a normal seek.
				bestEffortLog("Best effort seek failing due to too many failures");
				return 0;
			}
			var nextFragmentId:uint = guessFragmentIdForTime(_bestEffortSeekTime) + _bestEffortFailedFetches;
			
			// search the fragment run table to see if the desired fragment is already available
			if(!frt.isFragmentInGap(nextFragmentId))
			{
				// special case: the fragment we were going to request already exists in
				// the fragment run table. just use that fragment.
				bestEffortLog("Best effort seek forward hit an existing fragment "+nextFragmentId);
				return 0;
			}
				
			// normal case: perform a best effort forward fetch
			bestEffortLog("Best effort seek forward fetch "+nextFragmentId);
			return nextFragmentId; // true because this is a best effort fetch
		}
		
		/**
		 * @private
		 * 
		 * helper for getNextFile. called when best effort fetch is enabled
		 * but we're not seeking.
		 * 
		 * @return the action to take, expressed as an HTTPStreamRequest
		 **/
		private function getNextRequestForBestEffortPlay(
			quality:int, 
			bootstrapBox:AdobeBootstrapBox):HTTPStreamRequest
		{
			// parse out the FRT and SRT
			var frt:AdobeFragmentRunTable = getFragmentRunTable(bootstrapBox);
			if(_currentFAI == null ||
				frt == null ||
				bootstrapBox == null ||
				bootstrapBox.segmentRunTables.length < 1 ||
				bootstrapBox.segmentRunTables[0].segmentFragmentPairs.length < 1)
			{
				// rare case #1: _currentFAI shouldn't ever be null.
				// rare case #2: FRT was invalid
				// rare case #3: SRT was invalid
				bestEffortLog("Best effort in a weird state.");
				return new HTTPStreamRequest(HTTPStreamRequestKind.DONE);
			}
			var srt:AdobeSegmentRunTable = bootstrapBox.segmentRunTables[0];
			
			// figure out the next fragment to fetch
			var nextFragmentId : uint = _currentFAI.fragId + 1;
			
			// figure out the live point. the SRT is the best source for
			// how many fragments are available. this is roughly equivalent to the
			// check happening in fragmentOverflow
			var firstFragmentId:uint = frt.firstFragmentId;
			if(firstFragmentId == 0)
			{
				// rare case: FRT didn't contain any valid entries
				bestEffortLog("Best effort in a weird state.");
				return new HTTPStreamRequest(HTTPStreamRequestKind.DONE);
			}
			var livePoint : uint = firstFragmentId + srt.totalFragments;
			
			// figure out what kind of situation we're in
			// liveness, dropout, done, or normal
			var situation:String;
			if(nextFragmentId >= livePoint)
			{
				if(bootstrapBox.contentComplete())
				{
					situation = BEST_EFFORT_PLAY_SITUAUTION_DONE;
				}
				else
				{
					situation = BEST_EFFORT_PLAY_SITUAUTION_LIVENESS;
				}
			}
			else
			{
				if(frt.isFragmentInGap(nextFragmentId))
				{
					situation = BEST_EFFORT_PLAY_SITUAUTION_DROPOUT;
				}
				else
				{ 
					situation = BEST_EFFORT_PLAY_SITUAUTION_NORMAL; 
				}
			}
			
			if(situation == BEST_EFFORT_PLAY_SITUAUTION_DROPOUT ||
				situation == BEST_EFFORT_PLAY_SITUAUTION_LIVENESS)
			{
				// try to do BEF, we can still bail if there were too many failures.
				
				bestEffortLog("Best effort in "+situation);
				
				if(situation == BEST_EFFORT_PLAY_SITUAUTION_LIVENESS &&
					_bestEffortLastGoodFragmentDownloadTime != null)
				{
					var now:Date = new Date();
					var nextPossibleBEFTime:Number =
						_bestEffortLastGoodFragmentDownloadTime.valueOf() + Math.max(_f4fIndexInfo.bestEffortFetchInfo.fragmentDuration, 1000);
					if(now.valueOf() < nextPossibleBEFTime)
					{
						// in order to prevent too many spurious liveness BEFs,
						// don't perform liveness BEF unless its been at least half a fragment interval since the last
						// successful download
						return initiateBootstrapRefresh(quality);
					}
				}
				
				if(_bestEffortState == BEST_EFFORT_STATE_OFF)
				{
					// we're beginning best effort fetch
					bestEffortLog("Best effort play start");
					_bestEffortState = BEST_EFFORT_STATE_PLAY;
					_bestEffortFailedFetches = 0;
				}
				
				if(_bestEffortFailedFetches < _f4fIndexInfo.bestEffortFetchInfo.maxForwardFetches)
				{
					// perform a best effort fetch
					return initiateBestEffortRequest(nextFragmentId, quality);
				}
				else
				{
					// oops, all best effort fetches failed.
					// give up and just perform a normal next
					bestEffortLog("Best effort play failing due to too many failures");
					// fall through to do non-BEF behavior
				}
			}
			
			// we're going to do the non-BEF behavior
			if(situation == BEST_EFFORT_PLAY_SITUAUTION_LIVENESS)
			{
				bestEffortLog("Best effort in liveness");
				
				// the next time through, restart just after the last good download point 
				_bestEffortState = BEST_EFFORT_STATE_OFF;
				_currentFAI.fragId = _bestEffortLivenessRestartPoint;
				
				return initiateLivenessFailure(quality);
			}
			else if(situation == BEST_EFFORT_PLAY_SITUAUTION_DONE)
			{
				bestEffortLog("Best effort done");
				return new HTTPStreamRequest(HTTPStreamRequestKind.DONE);
			}
			else // implicit: situation is 'normal' or 'dropout'
			{			
				// figure out the next fragment id after the gap
				// we're not using validateFragment because it has incompatible behavior close to the live point
				var oldCurrentFAI:FragmentAccessInformation = _currentFAI;
				_currentFAI = frt.getFragmentWithIdGreq(nextFragmentId);
				if(_currentFAI == null)
				{
					// very rare case: couldn't get the fragment because there were no valid bootstrap entries.
					_currentFAI = oldCurrentFAI;
					bestEffortLog("Best effort done because there were no bootstrap entries");
					return initiateBootstrapRefresh(quality);
				}
				
				_bestEffortState = BEST_EFFORT_STATE_OFF; // make sure to set state to off in case we were in play state
				bestEffortLog("Normal play request for fragment "+_currentFAI.fragId);
				return initiateNormalDownload(bootstrapBox, quality);
			}
		}
		
		/**
		 * @private
		 * 
		 * Initiates a best effort request (from getNextFile or getFileForTime) and constructs an HTTPStreamRequest.
		 * 
		 * @return the action to take, expressed as an HTTPStreamRequest
		 **/
		private function initiateBestEffortRequest(nextFragmentId:uint, quality:int):HTTPStreamRequest
		{
			// if we had a pending BEF download, invalidate it
			stopListeningToBestEffortDownload();
			
			// update state
			_currentFAI = new FragmentAccessInformation();
			_currentFAI.fragId = nextFragmentId;
			_currentFAI.fragDuration = 0; // the only code that cares about this value is notifyFragmentDuration
			_currentFAI.fragmentEndTime = 0; // currently nobody cares about this value
			playInProgress = true;
			updateQuality(quality);
			
			// we don't know the fragment duration, so update the bootstrap frequently
			bootstrapUpdateInterval = OSMFSettings.hdsMinimumBootstrapRefreshInterval;
			
			// figure out the url to fetch
			var guessSegmentNumber:uint = uint(Math.ceil(Number(nextFragmentId) /
				(_f4fIndexInfo.bestEffortFetchInfo.segmentDuration/_f4fIndexInfo.bestEffortFetchInfo.fragmentDuration)));
			var guessUrl:String = constructFragmentRequest(
				_serverBaseURL, // serverBaseURL
				_streamNames[quality], // streamName
				guessSegmentNumber, // segmentId
				nextFragmentId); //fragmentId
			bestEffortLog("Best effort fetch for fragment "+nextFragmentId+" with url "+guessUrl+". State is "+_bestEffortState);
			
			// clean up best effort state
			_bestEffortDownloadReply = null;
			_bestEffortNeedsToFireFragmentDuration = false;
			
			// recreate the best effort download monitor
			// this protects us against overlapping best effort downloads
			_bestEffortDownloaderMonitor = new EventDispatcher();
			_bestEffortDownloaderMonitor.addEventListener(HTTPStreamingEvent.DOWNLOAD_COMPLETE, onBestEffortDownloadComplete);
			_bestEffortDownloaderMonitor.addEventListener(HTTPStreamingEvent.DOWNLOAD_ERROR, onBestEffortDownloadError);
			
			var streamRequest:HTTPStreamRequest =  new HTTPStreamRequest(
				HTTPStreamRequestKind.BEST_EFFORT_DOWNLOAD,
				guessUrl, // url
				-1, // retryAfter
				_bestEffortDownloaderMonitor); // bestEffortDownloaderMonitor
			
			// trigger a refresh
			adjustDelay();
			refreshBootstrapBox(quality);
			
			return streamRequest;
		}
		
		/**
		 * @private
		 *
		 * if we had a pending BEF download, invalid it
		 **/
		private function stopListeningToBestEffortDownload():void
		{
			if(_bestEffortDownloaderMonitor != null)
			{
				_bestEffortDownloaderMonitor.removeEventListener(HTTPStreamingEvent.DOWNLOAD_COMPLETE, onBestEffortDownloadComplete);
				_bestEffortDownloaderMonitor.removeEventListener(HTTPStreamingEvent.DOWNLOAD_ERROR, onBestEffortDownloadError);
				_bestEffortDownloaderMonitor = null;
			}
		}
		
		/**
		 * @private
		 * 
		 * Best effort backward seek needs to pre-parse the fragment in order to determine if the
		 * downloaded fragment actually contains the desired seek time. This method performs that parse.
		 **/
		private function bufferAndParseDownloadedBestEffortBytes(url:String, downloader:HTTPStreamDownloader):void
		{
			if(_bestEffortDownloadReply != null)
			{
				// if we already decided to skip or continue, don't parse new bytes
				return;
			}
			// annoying way to pass argument into onBestEffortF4FHandlerNotifyBootstrapBox
			_bestEffortNotifyBootstrapBoxInfo = {
				downloader: downloader,
				url: url
			};
			try
			{
				var downloaderAvailableBytes:uint = downloader.totalAvailableBytes;
				if(downloaderAvailableBytes > 0)
				{
					// buffer the downloaded bytes
					var downloadInput:IDataInput = downloader.getBytes(downloaderAvailableBytes);
					if(downloadInput != null)
					{
						downloadInput.readBytes(_bestEffortSeekBuffer, _bestEffortSeekBuffer.length, downloaderAvailableBytes);
					}
					
					// feed the bytes to our f4f handler in order to parse out the bootstrap box.
					while(_bestEffortF4FHandler.inputBytesNeeded > 0 &&
						_bestEffortF4FHandler.inputBytesNeeded <= _bestEffortSeekBuffer.bytesAvailable &&
						_bestEffortDownloadReply == null) // changes to non-null once we parse out the bootstrap box
					{
						_bestEffortF4FHandler.processFileSegment(_bestEffortSeekBuffer); 
					}
					if(_bestEffortDownloadReply == HTTPStreamingEvent.DOWNLOAD_CONTINUE)
					{
						// we're done parsing and the HTTPStreamSource is going to process the file,
						// restore the contents of the downloader
						downloader.clearSavedBytes();
						_bestEffortSeekBuffer.position = 0;
						downloader.appendToSavedBytes(_bestEffortSeekBuffer, _bestEffortSeekBuffer.length);
						_bestEffortSeekBuffer.length = 0; // release the buffer
					}
				}
			}
			finally
			{
				_bestEffortNotifyBootstrapBoxInfo = null;
			}
		}
		
		/**
		 * @private
		 * 
		 * Fired when the _bestEffortF4FHandler fires a NOTIFY_BOOTSTRAP_BOX event.
		 * This occurs during the pre-parsing that occurs in best effort backward fetch.
		 **/
		private function onBestEffortF4FHandlerNotifyBootstrapBox(event:HTTPStreamingFileHandlerEvent):void
		{
			var url:String = _bestEffortNotifyBootstrapBoxInfo.url as String;
			var downloader:HTTPStreamDownloader = _bestEffortNotifyBootstrapBoxInfo.downloader as HTTPStreamDownloader;
			
			if(_bestEffortDownloadReply != null)
			{
				bestEffortLog("Best effort found a bootstrap box in the downloaded fragment, but we already replied.");
				return;
			}
			
			var bootstrapBox:AdobeBootstrapBox = event.bootstrapBox;
			var frt:AdobeFragmentRunTable = getFragmentRunTable(bootstrapBox);
			if(frt == null)
			{
				bestEffortLog("Best effort download contained an invalid bootstrap box.");
				skipBestEffortFetch(url, downloader);
				return;
			}
			
			if(frt.fragmentDurationPairs.length != 1)
			{
				bestEffortLog("Best effort download has an FRT with more than 1 entry.");
				skipBestEffortFetch(url, downloader);
				return;
			}
			var fdp:FragmentDurationPair = frt.fragmentDurationPairs[0];
			if(fdp.duration == 0)
			{
				bestEffortLog("Best effort download FDP was a discontinuity.");
				skipBestEffortFetch(url, downloader);
				return;
			}
			var fragmentEndTime:Number = fdp.durationAccrued + fdp.duration;
			if(_bestEffortSeekTime < fragmentEndTime)
			{
				bestEffortLog("Best effort found the desired time within the downloaded fragment.");
				continueBestEffortFetch(url, downloader);
			}
			else
			{
				bestEffortLog("Best effort didn't find the desired time within the downloaded fragment.");
				skipBestEffortFetch(url, downloader);
				_bestEffortState = BEST_EFFORT_STATE_SEEK_FORWARD;
				_bestEffortFailedFetches = 0;
			}
		}
		
		/**
		 * @private
		 * 
		 * Invoked on HTTPStreamingEvent.DOWNLOAD_COMPLETE for best effort downloads
		 */
		private function onBestEffortDownloadComplete(event:HTTPStreamingEvent):void
		{
			if(_bestEffortDownloaderMonitor == null ||
				_bestEffortDownloaderMonitor != event.target as IEventDispatcher)
			{
				// we're receiving an event for a download we abandoned
				return;
			}
			bestEffortLog("Best effort download complete");
			
			// unregister the listeners
			stopListeningToBestEffortDownload();
			
			// forward the DOWNLOAD_COMPLETE to HTTPStreamSource, but change the reason
			var clone:HTTPStreamingEvent = new HTTPStreamingEvent(
				event.type,
				event.bubbles,
				event.cancelable,
				event.fragmentDuration,
				event.scriptDataObject,
				event.scriptDataMode,
				event.url,
				event.bytesDownloaded,
				HTTPStreamingEventReason.BEST_EFFORT,
				event.downloader);
			dispatchEvent(clone);
			
			if(_bestEffortDownloadReply != null)
			{
				// we already said "continue" or "skip"
				return;
			}
			
			switch(_bestEffortState)
			{
				case BEST_EFFORT_STATE_PLAY:
				case BEST_EFFORT_STATE_SEEK_FORWARD:
					// we successfully got a fragment.
					// tell HTTPStreamSource to go ahead and process this fragment.
					continueBestEffortFetch(event.url, event.downloader);
					break;
				case BEST_EFFORT_STATE_SEEK_BACKWARD:
					// in backward seek state we want to parse the fragment in order to see if it
					// has the time we want. parse any remaining bytes
					bufferAndParseDownloadedBestEffortBytes(event.url, event.downloader);
					if(_bestEffortDownloadReply == null)
					{
						// rare case: we downloaded the fragment, but we never found a bootstrap box.
						// just keep going
						skipBestEffortFetch(event.url, event.downloader);
					}
					break;
				default:
					bestEffortLog("Best effort download complete received while in unexpected state ("+_bestEffortState+")");
					break;
			}
		}
		
		/**
		 * @private
		 * 
		 * Invoked on HTTPStreamingEvent.DOWNLOAD_ERROR for best effort downloads
		 */
		private function onBestEffortDownloadError(event:HTTPStreamingEvent):void
		{
			if(_bestEffortDownloaderMonitor == null ||
				_bestEffortDownloaderMonitor != event.target as IEventDispatcher)
			{
				// we're receiving an event for a download we abandoned
				return;
			}
			// unregister our listeners
			stopListeningToBestEffortDownload();
			
			if(_bestEffortDownloadReply != null)
			{
				// special case: if we received some bytes and said "continue", but then the download failed.
				// there means there was a connection problem mid-download
				bestEffortLog("Best effort download error after we already decided to skip or continue.");
				dispatchEvent(event); // this stops playback
			}
			else if(event.reason == HTTPStreamingEventReason.TIMEOUT)
			{
				// special case: the download took too long and all the retries failed
				bestEffortLog("Best effort download timed out");
				dispatchEvent(event); // this stops playback
			}
			else
			{
				// failure due to http status code, or some other reason. resume best effort fetch
				bestEffortLog("Best effort download error.");
				++_bestEffortFailedFetches;
				skipBestEffortFetch(event.url, event.downloader);
			}
		}
		
		/**
		 * @private
		 * 
		 * After initiating a best effort fetch, call this function to tell the
		 * HTTPStreamSource that it should not continue processing the downloaded
		 * fragment.
		 * 
		 **/
		private function skipBestEffortFetch(url:String, downloader:HTTPStreamDownloader):void
		{
			if(_bestEffortDownloadReply != null)
			{
				bestEffortLog("Best effort wanted to skip fragment, but we're already replied with "+_bestEffortDownloadReply);
				return;
			}
			bestEffortLog("Best effort skipping fragment.");
			var event:HTTPStreamingEvent = new HTTPStreamingEvent(HTTPStreamingEvent.DOWNLOAD_SKIP,
				false, // bubbles
				false, // cancelable
				0, // fragmentDuration
				null, // scriptDataObject
				FLVTagScriptDataMode.NORMAL, // scriptDataMode 
				url, // url
				0, // bytesDownloaded
				HTTPStreamingEventReason.BEST_EFFORT, // reason
				downloader); // downloader
			dispatchEvent(event);
			
			_bestEffortDownloadReply = HTTPStreamingEvent.DOWNLOAD_SKIP;
			_bestEffortNeedsToFireFragmentDuration = false;
		}
		
		/**
		 * @private
		 * 
		 * After initiating a best effort fetch, call this function to tell the
		 * HTTPStreamSource that it may continue processing the downloaded fragment.
		 * A continue event is assumed to mean that best effort fetch is complete.
		 **/
		private function continueBestEffortFetch(url:String, downloader:HTTPStreamDownloader):void
		{
			if(_bestEffortDownloadReply != null)
			{
				bestEffortLog("Best effort wanted to continue, but we're already replied with "+_bestEffortDownloadReply);
				return;
			}
			bestEffortLog("Best effort received a desirable fragment.");
			
			var event:HTTPStreamingEvent = new HTTPStreamingEvent(HTTPStreamingEvent.DOWNLOAD_CONTINUE,
				false, // bubbles
				false, // cancelable
				0, // fragmentDuration
				null, // scriptDataObject
				FLVTagScriptDataMode.NORMAL, // scriptDataMode 
				url, // url
				0, // bytesDownloaded
				HTTPStreamingEventReason.BEST_EFFORT, // reason
				downloader); // downloader
			
			// if we encounter liveness in the future, restart BEFs
			// after the current fragment
			_bestEffortLivenessRestartPoint = _currentFAI.fragId;
			CONFIG::LOGGING
			{
				logger.debug("Setting _bestEffortLivenessRestartPoint to "+_bestEffortLivenessRestartPoint+" because of successful BEF download.");
			}
			
			// remember that we started a download now
			_bestEffortLastGoodFragmentDownloadTime = new Date();
			
			dispatchEvent(event);
			_bestEffortDownloadReply = HTTPStreamingEvent.DOWNLOAD_CONTINUE;
			_bestEffortNeedsToFireFragmentDuration = true;
			_bestEffortState = BEST_EFFORT_STATE_OFF;
		}
		
		/**
		 * @private
		 * 
		 * After initiating a best effort fetch, call this function to tell the
		 * HTTPStreamSource that a bad download error occurred. This causes HTTPStreamSource
		 * to stop playback with an error.
		 **/
		private function errorBestEffortFetch(url:String, downloader:HTTPStreamDownloader):void
		{
			bestEffortLog("Best effort fetch error.");
			var event:HTTPStreamingEvent = new HTTPStreamingEvent(HTTPStreamingEvent.DOWNLOAD_ERROR,
				false, // bubbles
				false, // cancelable
				0, // fragmentDuration
				null, // scriptDataObject
				FLVTagScriptDataMode.NORMAL, // scriptDataMode 
				url, // url
				0, // bytesDownloaded
				HTTPStreamingEventReason.BEST_EFFORT, // reason
				downloader); // downloader
			dispatchEvent(event);
			_bestEffortDownloadReply = HTTPStreamingEvent.DOWNLOAD_ERROR;
			_bestEffortNeedsToFireFragmentDuration = false;
		}
		
		/**
		 * @private
		 * 
		 * Fires FRAGMENT_DURATION in response to best effort fetch.
		 * 
		 * Normal downloads fire FRAGMENT_DURATION before the download starts.
		 * In the best effort case, the duration is unavailable at request time and only
		 * becomes available after the fragment downloaded and parsed (the duration is
		 * passed along with the fragment's boostrap box). Thus, in the best effor case,
		 * FRAGMENT_DURATION is fired after the fragment download completes. 
		 **/
		private function notifyFragmentDurationForBestEffort(bootstrapBox:AdobeBootstrapBox):void
		{
			if(!_bestEffortNeedsToFireFragmentDuration || // we only care if we haven't fired an event yet
				bootstrapBox == null) // safety check
			{
				return;
			}
			_bestEffortNeedsToFireFragmentDuration = false; // don't invoke again
			
			// look up the fragment duration in the fragment run table.
			var frt:AdobeFragmentRunTable = getFragmentRunTable(bootstrapBox);
			if(frt == null)
			{
				return;
			}
			var fragmentDuration:Number = frt.getFragmentDuration(_currentFAI.fragId);
			if(fragmentDuration == 0) // missing
			{
				return;
			}
			
			// unlike notifyFragmentDuration, we do not change bootstrapUpdateInterval since
			// we're getting the information at a later time (after we've downloaded the fragment)
			
			// fire the event
			bestEffortLog("Best effort fetch firing the fragment duration.");
			dispatchEvent(new HTTPStreamingEvent( 
				HTTPStreamingEvent.FRAGMENT_DURATION, // type
				false, // bubbles
				false, // cancelable
				fragmentDuration / bootstrapBox.timeScale, // fragmentDuration
				null, // scriptDataObject
				null)); // scriptDataMode
		}
		
		/**
		 * @private
		 * logging related to best effort fetch
		 **/
		private function bestEffortLog(s:String):void
		{
			CONFIG::LOGGING
			{
				logger.debug("BEST EFFORT: "+s);
			}
		}
		
		
		/**
		 * @inheritDoc
		 */	
		public override function get isBestEffortFetchEnabled():Boolean
		{
			return _bestEffortEnabled;
		}
		
//		/**
//		 * @private
//		 * 
//		 * Given timeBias, calculates the corresponding segment duration.
//		 */
//		internal function calculateSegmentDuration(abst:AdobeBootstrapBox, timeBias:Number):Number
//		{
//			var fragmentDurationPairs:Vector.<FragmentDurationPair> = (abst.fragmentRunTables)[0].fragmentDurationPairs;
//			var fragmentId:uint = currentFAI.fragId;
//			
//			var index:int =  fragmentDurationPairs.length - 1;
//			while (index >= 0)
//			{
//				var fragmentDurationPair:FragmentDurationPair = fragmentDurationPairs[index];
//				if (fragmentDurationPair.firstFragment <= fragmentId)
//				{
//					var duration:Number = fragmentDurationPair.duration;
//					var durationAccrued:Number = fragmentDurationPair.durationAccrued;
//					durationAccrued += (fragmentId - fragmentDurationPair.firstFragment) * fragmentDurationPair.duration;
//					if (timeBias > 0)
//					{
//						duration -= (timeBias - durationAccrued);
//					}
//					
//					return duration;
//				}
//				else
//				{
//					index--;
//				}
//			}
//			
//			return 0;
//		}
//
//		override public function getFragmentDurationFromUrl(fragmentUrl:String):Number
//		{
//			// we assume that there is only one afrt in bootstrap
//			
//			var tempFragmentId:String = fragmentUrl.substr(fragmentUrl.indexOf("Frag")+4, fragmentUrl.length);
//			var fragId:uint = uint(tempFragmentId);
//			var abst:AdobeBootstrapBox = bootstrapBoxes[_currentQuality];
//			var afrt:AdobeFragmentRunTable = abst.fragmentRunTables[0];
//			return afrt.getFragmentDuration(fragId);
//		}

		
		/// Internals		
		private var _currentQuality:int = -1;
		private var _currentAdditionalHeader:ByteArray = null;
		private var _currentFAI:FragmentAccessInformation = null;						// annoying way to pass argument into onBestEffortF4FHandlerNotifyBootstrapBox
		
		private var _pureLiveOffset:Number = NaN;
		
		private var _f4fIndexInfo:HTTPStreamingF4FIndexInfo = null;
		private var _bootstrapBoxes:Vector.<AdobeBootstrapBox> = null;
		private var _bootstrapBoxesURLs:Vector.<String> = null;
		private var _streamInfos:Vector.<HTTPStreamingF4FStreamInfo> = null;
		private var _streamNames:Array = null;
		private var _streamQualityRates:Array = null;
		private var _serverBaseURL:String = null;
		
		private var _delay:Number = 0.05;
		
		private var _indexUpdating:Boolean = false;
		private var _pendingIndexLoads:int = 0;
		private var _pendingIndexUpdates:int = 0;
		private var _pendingIndexUrls:Object = new Object();

		private var _invokedFromDvrGetStreamInfo:Boolean = false;
		
		
		private var playInProgress:Boolean;
		
		private var bootstrapUpdateTimer:Timer;
		private var bootstrapUpdateInterval:Number = 4000;
		public static const DEFAULT_FRAGMENTS_THRESHOLD:uint = 5;
		
		// _bestEffortState values
		private static const BEST_EFFORT_STATE_OFF:String = "off"; 											// not performing best effort fetch
		private static const BEST_EFFORT_STATE_PLAY:String = "play"; 										// doing best effort for liveness or dropout
		private static const BEST_EFFORT_STATE_SEEK_BACKWARD:String = "seekBackward";						// in the backward fetch phase of best effort seek
		private static const BEST_EFFORT_STATE_SEEK_FORWARD:String = "seekForward";							// in the forward fetch phase of best effort seek
		
		private var _bestEffortInited:Boolean = false;														// did we initialize _bestEffortEnabled?
		private var _bestEffortEnabled:Boolean = false;														// is BEF enabled at all?
		private var _bestEffortState:String =  BEST_EFFORT_STATE_OFF;										// the current state of best effort
		private var _bestEffortSeekTime:Number = 0;															// the time we're seeking to
		private var _bestEffortDownloaderMonitor:EventDispatcher = new EventDispatcher(); 					// Special dispatcher to handler the results of best-effort downloads.
		private var _bestEffortFailedFetches:uint = 0; 														// The number of fetches that have failed so far.
		private var _bestEffortDownloadReply:String = null;													// After initiating a download, this is the DOWNLOAD_CONTINUE or DOWNLOAD_SKIP reply that we sent
		private var _bestEffortNeedsToFireFragmentDuration:Boolean = false;									// Do we need to fire the FRAGMENT_DURATION event for the next fragment?
		private var _bestEffortF4FHandler:HTTPStreamingF4FFileHandler = new HTTPStreamingF4FFileHandler();	// used to pre-parse backward seeks
		private var _bestEffortSeekBuffer:ByteArray = new ByteArray();										// buffer for saving bytes when pre-parsing backward seek
		private var _bestEffortNotifyBootstrapBoxInfo:Object = null;										// hacky way to pass arguments to helper functions
		private var _bestEffortLivenessRestartPoint:uint = 0;												// if we have to restart BEF for liveness, the fragment number of the first BEF - 1
		private var _bestEffortLastGoodFragmentDownloadTime:Date = null;
		
		// constants used by getNextRequestForBestEffortPlay:
		private static const BEST_EFFORT_PLAY_SITUAUTION_NORMAL:String = "normal";
		private static const BEST_EFFORT_PLAY_SITUAUTION_DROPOUT:String = "dropout";
		private static const BEST_EFFORT_PLAY_SITUAUTION_LIVENESS:String = "liveness";
		private static const BEST_EFFORT_PLAY_SITUAUTION_DONE:String = "done";

		CONFIG::LOGGING
		{
			private static const logger:org.osmf.logging.Logger = org.osmf.logging.Log.getLogger("org.osmf.net.httpstreaming.f4f.HTTPStreamF4FIndexHandler");
		}
	}
}