@@ -44,8 +44,8 @@ import {CmcdStreamType} from '@svta/common-media-library/cmcd/CmcdStreamType';
4444import { CmcdStreamingFormat } from '@svta/common-media-library/cmcd/CmcdStreamingFormat' ;
4545import { encodeCmcd } from '@svta/common-media-library/cmcd/encodeCmcd' ;
4646import { toCmcdHeaders } from '@svta/common-media-library/cmcd/toCmcdHeaders' ;
47-
48- const CMCD_VERSION = 1 ;
47+ import { CmcdHeaderField } from '@svta/common-media-library/cmcd/CmcdHeaderField' ;
48+ const DEFAULT_CMCD_VERSION = 1 ;
4949const DEFAULT_INCLUDE_IN_REQUESTS = 'segment' ;
5050const RTP_SAFETY_FACTOR = 5 ;
5151
@@ -64,7 +64,9 @@ function CmcdModel() {
6464 _lastMediaTypeRequest ,
6565 _isStartup ,
6666 _bufferLevelStarved ,
67- _initialMediaRequestsDone ;
67+ _initialMediaRequestsDone ,
68+ _playbackStartedTime ,
69+ _msdSent ;
6870
6971 let context = this . context ;
7072 let eventBus = EventBus ( context ) . getInstance ( ) ;
@@ -77,12 +79,19 @@ function CmcdModel() {
7779 _resetInitialSettings ( ) ;
7880 }
7981
80- function initialize ( ) {
82+ function initialize ( autoPlay ) {
8183 eventBus . on ( MediaPlayerEvents . PLAYBACK_RATE_CHANGED , _onPlaybackRateChanged , instance ) ;
8284 eventBus . on ( MediaPlayerEvents . MANIFEST_LOADED , _onManifestLoaded , instance ) ;
8385 eventBus . on ( MediaPlayerEvents . BUFFER_LEVEL_STATE_CHANGED , _onBufferLevelStateChanged , instance ) ;
8486 eventBus . on ( MediaPlayerEvents . PLAYBACK_SEEKED , _onPlaybackSeeked , instance ) ;
8587 eventBus . on ( MediaPlayerEvents . PERIOD_SWITCH_COMPLETED , _onPeriodSwitchComplete , instance ) ;
88+ if ( autoPlay ) {
89+ eventBus . on ( MediaPlayerEvents . MANIFEST_LOADING_STARTED , _onPlaybackStarted , instance ) ;
90+ }
91+ else {
92+ eventBus . on ( MediaPlayerEvents . PLAYBACK_STARTED , _onPlaybackStarted , instance ) ;
93+ }
94+ eventBus . on ( MediaPlayerEvents . PLAYBACK_PLAYING , _onPlaybackPlaying , instance ) ;
8695 }
8796
8897 function setConfig ( config ) {
@@ -124,13 +133,29 @@ function CmcdModel() {
124133 _isStartup = { } ;
125134 _initialMediaRequestsDone = { } ;
126135 _lastMediaTypeRequest = undefined ;
136+ _playbackStartedTime = undefined ;
137+ _msdSent = false ;
127138 _updateStreamProcessors ( ) ;
128139 }
129140
130141 function _onPeriodSwitchComplete ( ) {
131142 _updateStreamProcessors ( ) ;
132143 }
133144
145+ function _onPlaybackStarted ( ) {
146+ if ( ! _playbackStartedTime ) {
147+ _playbackStartedTime = Date . now ( ) ;
148+ }
149+ }
150+
151+ function _onPlaybackPlaying ( ) {
152+ if ( ! _playbackStartedTime || internalData . msd ) {
153+ return ;
154+ }
155+
156+ internalData . msd = Date . now ( ) - _playbackStartedTime ;
157+ }
158+
134159 function _updateStreamProcessors ( ) {
135160 if ( ! playbackController ) {
136161 return ;
@@ -195,7 +220,8 @@ function CmcdModel() {
195220 if ( isCmcdEnabled ( ) ) {
196221 const cmcdData = getCmcdData ( request ) ;
197222 const filteredCmcdData = _applyWhitelist ( cmcdData ) ;
198- const headers = toCmcdHeaders ( filteredCmcdData )
223+ const options = _createCmcdV2HeadersCustomMap ( ) ;
224+ const headers = toCmcdHeaders ( filteredCmcdData , options ) ;
199225
200226 eventBus . trigger ( MetricsReportingEvents . CMCD_DATA_GENERATED , {
201227 url : request . url ,
@@ -219,8 +245,8 @@ function CmcdModel() {
219245
220246 function _canBeEnabled ( cmcdParametersFromManifest ) {
221247 if ( Object . keys ( cmcdParametersFromManifest ) . length ) {
222- if ( ! cmcdParametersFromManifest . version ) {
223- logger . error ( `version parameter must be defined.` ) ;
248+ if ( parseInt ( cmcdParametersFromManifest . version ) !== 1 ) {
249+ logger . error ( `version parameter must be defined in 1 .` ) ;
224250 return false ;
225251 }
226252 if ( ! cmcdParametersFromManifest . keys ) {
@@ -257,15 +283,17 @@ function CmcdModel() {
257283
258284 function _checkAvailableKeys ( cmcdParametersFromManifest ) {
259285 const defaultAvailableKeys = Constants . CMCD_AVAILABLE_KEYS ;
286+ const defaultV2AvailableKeys = Constants . CMCD_V2_AVAILABLE_KEYS ;
260287 const enabledCMCDKeys = cmcdParametersFromManifest . version ? cmcdParametersFromManifest . keys : settings . get ( ) . streaming . cmcd . enabledKeys ;
261- const invalidKeys = enabledCMCDKeys . filter ( k => ! defaultAvailableKeys . includes ( k ) ) ;
288+ const cmcdVersion = settings . get ( ) . streaming . cmcd . version ;
289+ const invalidKeys = enabledCMCDKeys . filter ( k => ! defaultAvailableKeys . includes ( k ) && ! ( cmcdVersion === 2 && defaultV2AvailableKeys . includes ( k ) ) ) ;
262290
263291 if ( invalidKeys . length === enabledCMCDKeys . length && enabledCMCDKeys . length > 0 ) {
264- logger . error ( `None of the keys are implemented.` ) ;
292+ logger . error ( `None of the keys are implemented for CMCD version ${ cmcdVersion } .` ) ;
265293 return false ;
266294 }
267295 invalidKeys . map ( ( k ) => {
268- logger . warn ( `key parameter ${ k } is not implemented.` ) ;
296+ logger . warn ( `key parameter ${ k } is not implemented for CMCD version ${ cmcdVersion } .` ) ;
269297 } ) ;
270298
271299 return true ;
@@ -497,7 +525,7 @@ function CmcdModel() {
497525 let cid = settings . get ( ) . streaming . cmcd . cid ? settings . get ( ) . streaming . cmcd . cid : internalData . cid ;
498526 cid = cmcdParametersFromManifest . contentID ? cmcdParametersFromManifest . contentID : cid ;
499527
500- data . v = CMCD_VERSION ;
528+ data . v = settings . get ( ) . streaming . cmcd . version ?? DEFAULT_CMCD_VERSION ;
501529
502530 data . sid = settings . get ( ) . streaming . cmcd . sid ? settings . get ( ) . streaming . cmcd . sid : internalData . sid ;
503531 data . sid = cmcdParametersFromManifest . sessionID ? cmcdParametersFromManifest . sessionID : data . sid ;
@@ -520,9 +548,33 @@ function CmcdModel() {
520548 data . sf = internalData . sf ;
521549 }
522550
551+ if ( data . v === 2 ) {
552+ let ltc = playbackController . getCurrentLiveLatency ( ) * 1000 ;
553+ if ( ! isNaN ( ltc ) ) {
554+ data . ltc = ltc ;
555+ }
556+ const msd = internalData . msd ;
557+ if ( ! _msdSent && ! isNaN ( msd ) ) {
558+ data . msd = msd ;
559+ _msdSent = true ;
560+ }
561+ }
562+
563+
564+
523565 return data ;
524566 }
525567
568+ function _createCmcdV2HeadersCustomMap ( ) {
569+ const cmcdVersion = settings . get ( ) . streaming . cmcd . version ;
570+ return cmcdVersion === 1 ? { } : {
571+ customHeaderMap : {
572+ [ CmcdHeaderField . REQUEST ] : [ 'ltc' ] ,
573+ [ CmcdHeaderField . SESSION ] : [ 'msd' ]
574+ }
575+ } ;
576+ }
577+
526578 function _getBitrateByRequest ( request ) {
527579 try {
528580 return parseInt ( request . bandwidth / 1000 ) ;
@@ -658,7 +710,7 @@ function CmcdModel() {
658710 playbackRate = 1 ;
659711 }
660712 let { bandwidth, mediaType, representation, duration } = request ;
661- const mediaInfo = representation . mediaInfo
713+ const mediaInfo = representation . mediaInfo ;
662714
663715 if ( ! mediaInfo ) {
664716 return NaN ;
@@ -688,6 +740,8 @@ function CmcdModel() {
688740 eventBus . off ( MediaPlayerEvents . MANIFEST_LOADED , _onManifestLoaded , this ) ;
689741 eventBus . off ( MediaPlayerEvents . BUFFER_LEVEL_STATE_CHANGED , _onBufferLevelStateChanged , instance ) ;
690742 eventBus . off ( MediaPlayerEvents . PLAYBACK_SEEKED , _onPlaybackSeeked , instance ) ;
743+ eventBus . off ( MediaPlayerEvents . PLAYBACK_STARTED , _onPlaybackStarted , instance ) ;
744+ eventBus . off ( MediaPlayerEvents . PLAYBACK_PLAYING , _onPlaybackPlaying , instance ) ;
691745
692746 _resetInitialSettings ( ) ;
693747 }
0 commit comments