1 /*****************************************************************************
2 * chromecast_demux.cpp: Chromecast demux filter module for vlc
3 *****************************************************************************
4 * Copyright © 2015-2016 VideoLAN
6 * Authors: Steve Lhomme <robux4@videolabs.io>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_demux.h>
35 #include "chromecast_common.h"
40 static void on_paused_changed_cb(void *data
, bool paused
);
44 demux_cc(demux_t
* const demux
, chromecast_common
* const renderer
)
48 ,m_pause_date( VLC_TS_INVALID
)
49 ,m_pause_delay( VLC_TS_INVALID
)
58 vlc_meta_t
*p_meta
= vlc_meta_New();
59 if( likely(p_meta
!= NULL
) )
61 input_item_t
*p_item
= p_demux
->p_next
->p_input
?
62 input_GetItem( p_demux
->p_next
->p_input
) : NULL
;
65 /* Favor Meta from the input item of the input_thread since
66 * it's pre-processed by the meta fetcher */
67 for( int i
= 0; i
< VLC_META_TYPE_COUNT
; ++i
)
69 char *psz_meta
= input_item_GetMeta( p_item
, (vlc_meta_type_t
)i
);
72 vlc_meta_Set( p_meta
, (vlc_meta_type_t
)i
, psz_meta
);
76 if( vlc_meta_Get( p_meta
, vlc_meta_Title
) == NULL
)
78 char *psz_meta
= input_item_GetName( p_item
);
81 vlc_meta_Set( p_meta
, vlc_meta_Title
, psz_meta
);
85 p_renderer
->pf_set_meta( p_renderer
->p_opaque
, p_meta
);
87 else if (demux_Control( p_demux
->p_next
, DEMUX_GET_META
, p_meta
) == VLC_SUCCESS
)
88 p_renderer
->pf_set_meta( p_renderer
->p_opaque
, p_meta
);
90 vlc_meta_Delete( p_meta
);
93 if (demux_Control( p_demux
->p_next
, DEMUX_CAN_SEEK
, &m_can_seek
) != VLC_SUCCESS
)
95 if (demux_Control( p_demux
->p_next
, DEMUX_GET_LENGTH
, &m_length
) != VLC_SUCCESS
)
99 if( demux_Control( p_demux
->p_next
, DEMUX_GET_TITLE
,
100 &i_current_title
) == VLC_SUCCESS
)
102 input_title_t
** pp_titles
;
103 int i_nb_titles
, i_title_offset
, i_chapter_offset
;
104 if( demux_Control( p_demux
->p_next
, DEMUX_GET_TITLE_INFO
, &pp_titles
,
105 &i_nb_titles
, &i_title_offset
,
106 &i_chapter_offset
) == VLC_SUCCESS
)
108 int64_t i_longest_duration
= 0;
109 int i_longest_title
= 0;
110 bool b_is_interactive
= false;
111 for( int i
= 0 ; i
< i_nb_titles
; ++i
)
113 if( pp_titles
[i
]->i_length
> i_longest_duration
)
115 i_longest_duration
= pp_titles
[i
]->i_length
;
118 if( i_current_title
== i
&&
119 pp_titles
[i
]->i_flags
& INPUT_TITLE_INTERACTIVE
)
121 b_is_interactive
= true;
123 vlc_input_title_Delete( pp_titles
[i
] );
127 if( b_is_interactive
== true )
129 demux_Control( p_demux
->p_next
, DEMUX_SET_TITLE
,
135 es_out_Control( p_demux
->p_next
->out
, ES_OUT_RESET_PCR
);
137 p_renderer
->pf_set_on_paused_changed_cb(p_renderer
->p_opaque
,
138 on_paused_changed_cb
, p_demux
);
146 p_renderer
->pf_set_meta( p_renderer
->p_opaque
, NULL
);
147 p_renderer
->pf_set_on_paused_changed_cb( p_renderer
->p_opaque
,
153 m_start_time
= m_last_time
= -1;
154 m_start_pos
= m_last_pos
= -1.0f
;
159 if( demux_Control( p_demux
->p_next
, DEMUX_GET_TIME
, &m_start_time
) != VLC_SUCCESS
)
162 if( demux_Control( p_demux
->p_next
, DEMUX_GET_POSITION
, &m_start_pos
) != VLC_SUCCESS
)
165 m_last_time
= m_start_time
;
166 m_last_pos
= m_start_pos
;
178 p_renderer
->pf_send_input_event( p_renderer
->p_opaque
, CC_INPUT_EVENT_EOF
,
179 cc_input_arg
{ false } );
182 void setPauseState(bool paused
, mtime_t delay
)
184 p_renderer
->pf_set_pause_state( p_renderer
->p_opaque
, paused
, delay
);
189 mtime_t system
, delay
;
190 if( es_out_ControlGetPcrSystem( p_demux
->p_next
->out
, &system
, &delay
) )
191 return VLC_TS_INVALID
;
193 mtime_t cc_time
= p_renderer
->pf_get_time( p_renderer
->p_opaque
);
194 if( cc_time
!= VLC_TS_INVALID
)
195 return cc_time
- system
+ m_pause_delay
;
196 return VLC_TS_INVALID
;
201 if( m_start_time
< 0 )
204 int64_t time
= m_start_time
;
205 mtime_t cc_time
= getCCTime();
207 if( cc_time
!= VLC_TS_INVALID
)
215 if( m_length
>= 0 && m_start_pos
>= 0 )
217 m_last_pos
= ( getCCTime() / double( m_length
) ) + m_start_pos
;
224 void seekBack( mtime_t time
, double pos
)
226 es_out_Control( p_demux
->p_next
->out
, ES_OUT_RESET_PCR
);
230 int ret
= VLC_EGENERIC
;
232 ret
= demux_Control( p_demux
->p_next
, DEMUX_SET_TIME
, time
, false );
234 if( ret
!= VLC_SUCCESS
&& pos
>= 0 )
235 demux_Control( p_demux
->p_next
, DEMUX_SET_POSITION
, pos
, false );
242 return demux_Demux( p_demux
->p_next
);
244 /* The CC sout is not pacing, so we pace here */
245 int pace
= p_renderer
->pf_pace( p_renderer
->p_opaque
);
249 return VLC_DEMUXER_EGENERIC
;
250 case CC_PACE_ERR_RETRY
:
252 /* Seek back to started position */
253 seekBack(m_start_time
, m_start_pos
);
256 p_renderer
->pf_send_input_event( p_renderer
->p_opaque
,
257 CC_INPUT_EVENT_RETRY
,
258 cc_input_arg
{false} );
261 case CC_PACE_OK_WAIT
:
262 /* Yeld: return to let the input thread doing controls */
263 return VLC_DEMUXER_SUCCESS
;
265 case CC_PACE_OK_ENDED
:
268 vlc_assert_unreachable();
271 int ret
= VLC_DEMUXER_SUCCESS
;
274 ret
= demux_Demux( p_demux
->p_next
);
275 if( ret
!= VLC_DEMUXER_EGENERIC
276 && ( m_start_time
< 0 || m_start_pos
< 0.0f
) )
278 if( ret
== VLC_DEMUXER_EOF
)
284 /* Signal EOF to the sout when the es_out is empty (so when the
285 * DecoderThread fifo are empty) */
287 es_out_Control( p_demux
->p_next
->out
, ES_OUT_GET_EMPTY
, &b_empty
);
289 p_renderer
->pf_send_input_event( p_renderer
->p_opaque
,
291 cc_input_arg
{ true } );
293 /* Don't return EOF until the chromecast is not EOF. This allows
294 * this demux filter to have more controls over the sout. Indeed,
295 * we still can seek or change tracks when the input is EOF and we
296 * should continue to handle CC errors. */
297 ret
= pace
== CC_PACE_OK
? VLC_DEMUXER_SUCCESS
: VLC_DEMUXER_EOF
;
303 int Control( demux_t
*p_demux_filter
, int i_query
, va_list args
)
305 if( !m_enabled
&& i_query
!= DEMUX_FILTER_ENABLE
)
306 return demux_vaControl( p_demux_filter
->p_next
, i_query
, args
);
310 case DEMUX_GET_POSITION
:
312 double pos
= getPosition();
315 *va_arg( args
, double * ) = pos
;
322 mtime_t time
= getTime();
325 *va_arg(args
, int64_t *) = time
;
330 case DEMUX_GET_LENGTH
:
336 ret
= demux_vaControl( p_demux_filter
->p_next
, i_query
, args
);
337 if( ret
== VLC_SUCCESS
)
338 m_length
= *va_arg( ap
, int64_t * );
349 ret
= demux_vaControl( p_demux_filter
->p_next
, i_query
, args
);
350 if( ret
== VLC_SUCCESS
)
351 m_can_seek
= *va_arg( ap
, bool* );
356 case DEMUX_SET_POSITION
:
358 m_pause_delay
= m_pause_date
= VLC_TS_INVALID
;
360 double pos
= va_arg( args
, double );
361 /* Force unprecise seek */
362 int ret
= demux_Control( p_demux
->p_next
, DEMUX_SET_POSITION
, pos
, false );
363 if( ret
!= VLC_SUCCESS
)
372 m_pause_delay
= m_pause_date
= VLC_TS_INVALID
;
374 mtime_t time
= va_arg( args
, int64_t );
375 /* Force unprecise seek */
376 int ret
= demux_Control( p_demux
->p_next
, DEMUX_SET_TIME
, time
, false );
377 if( ret
!= VLC_SUCCESS
)
384 case DEMUX_SET_PAUSE_STATE
:
389 int paused
= va_arg( ap
, int );
394 if (m_pause_date
== VLC_TS_INVALID
)
395 m_pause_date
= mdate();
399 if (m_pause_date
!= VLC_TS_INVALID
)
401 m_pause_delay
+= mdate() - m_pause_date
;
402 m_pause_date
= VLC_TS_INVALID
;
406 setPauseState( paused
!= 0, m_pause_delay
);
410 /* Seek back to the last known pos when changing tracks. This will
411 * flush sout streams, make sout del/add called right away and
412 * clear CC buffers. */
413 seekBack(m_last_time
, m_last_pos
);
417 case DEMUX_FILTER_ENABLE
:
418 p_renderer
= static_cast<chromecast_common
*>(
419 var_InheritAddress( p_demux
, CC_SHARED_VAR_NAME
) );
420 assert(p_renderer
!= NULL
);
425 case DEMUX_FILTER_DISABLE
:
429 /* Seek back to last known position. Indeed we don't want to resume
430 * from the input position that can be more than 1 minutes forward
431 * (depending on the CC buffering policy). */
432 seekBack(m_last_time
, m_last_pos
);
440 return demux_vaControl( p_demux_filter
->p_next
, i_query
, args
);
444 demux_t
* const p_demux
;
445 chromecast_common
* p_renderer
;
452 mtime_t m_start_time
;
454 mtime_t m_pause_date
;
455 mtime_t m_pause_delay
;
458 static void on_paused_changed_cb( void *data
, bool paused
)
460 demux_t
*p_demux
= reinterpret_cast<demux_t
*>(data
);
462 input_thread_t
*p_input
= p_demux
->p_next
->p_input
;
464 input_Control( p_input
, INPUT_SET_STATE
, paused
? PAUSE_S
: PLAYING_S
);
467 static int Demux( demux_t
*p_demux_filter
)
469 demux_cc
*p_sys
= reinterpret_cast<demux_cc
*>(p_demux_filter
->p_sys
);
471 return p_sys
->Demux();
474 static int Control( demux_t
*p_demux_filter
, int i_query
, va_list args
)
476 demux_cc
*p_sys
= reinterpret_cast<demux_cc
*>(p_demux_filter
->p_sys
);
478 return p_sys
->Control( p_demux_filter
, i_query
, args
);
481 int Open(vlc_object_t
*p_this
)
483 demux_t
*p_demux
= reinterpret_cast<demux_t
*>(p_this
);
484 chromecast_common
*p_renderer
= static_cast<chromecast_common
*>(
485 var_InheritAddress( p_demux
, CC_SHARED_VAR_NAME
) );
486 if ( p_renderer
== NULL
)
488 msg_Warn( p_this
, "using Chromecast demuxer with no sout" );
492 demux_cc
*p_sys
= new(std::nothrow
) demux_cc( p_demux
, p_renderer
);
493 if (unlikely(p_sys
== NULL
))
496 p_demux
->p_sys
= p_sys
;
497 p_demux
->p_sys
= reinterpret_cast<demux_sys_t
*>(p_sys
);
498 p_demux
->pf_demux
= Demux
;
499 p_demux
->pf_control
= Control
;
504 void Close(vlc_object_t
*p_this
)
506 demux_t
*p_demux
= reinterpret_cast<demux_t
*>(p_this
);
507 demux_cc
*p_sys
= reinterpret_cast<demux_cc
*>(p_demux
->p_sys
);
513 set_shortname( "cc_demux" )
514 set_category( CAT_INPUT
)
515 set_subcategory( SUBCAT_INPUT_DEMUX
)
516 set_description( N_( "Chromecast demux wrapper" ) )
517 set_capability( "demux_filter", 0 )
518 add_shortcut( "cc_demux" )
519 set_callbacks( Open
, Close
)