]> git.sesse.net Git - vlc/blob - modules/access/sdi.cpp
8b405ee3d71eb665a4325826dea18287d1f64618
[vlc] / modules / access / sdi.cpp
1 /* BlackMagic SDI driver */
2
3 #ifdef HAVE_CONFIG_H
4 # include "config.h"
5 #endif
6
7 #ifndef INT64_C
8 #define INT64_C(c) c ## LL
9 #endif
10
11 #include <vlc_common.h>
12 #include <vlc_plugin.h>
13 #include <vlc_input.h>
14 #include <vlc_demux.h>
15 #include <vlc_access.h>
16 #include <vlc_picture.h>
17 #include <vlc_charset.h>
18 #include <vlc_fs.h>
19
20 #include "DeckLinkAPI.h"
21 #include "DeckLinkAPIDispatch.cpp"
22
23 static int  Open ( vlc_object_t * );
24 static void Close( vlc_object_t * );
25
26 #define CACHING_TEXT N_("Caching value in ms")
27 #define CACHING_LONGTEXT N_( \
28     "Caching value for SDI captures. This " \
29     "value should be set in milliseconds." )
30
31 vlc_module_begin ()
32     set_shortname( N_("SDI") )
33     set_description( N_("BlackMagic SDI input") )
34     set_category( CAT_INPUT )
35     set_subcategory( SUBCAT_INPUT_ACCESS )
36
37     add_integer( "sdi-caching", DEFAULT_PTS_DELAY / 1000, NULL,
38                  CACHING_TEXT, CACHING_LONGTEXT, true )
39
40     add_shortcut( "sdi" )
41     set_capability( "access_demux", 10 )
42     set_callbacks( Open, Close )
43 vlc_module_end ()
44
45 static int Demux  ( demux_t * );
46 static int Control( demux_t *, int, va_list );
47
48 class DeckLinkCaptureDelegate;
49
50 struct demux_sys_t
51 {
52     IDeckLink *p_card;
53     IDeckLinkInput *p_input;
54     DeckLinkCaptureDelegate *p_delegate;
55     es_out_id_t  *p_es;
56 };
57
58 class DeckLinkCaptureDelegate : public IDeckLinkInputCallback
59 {
60 public:
61     DeckLinkCaptureDelegate( demux_t *p_demux ) : p_demux_(p_demux) {}
62
63     // FIXME: These leak.
64     virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) { return E_NOINTERFACE; }
65     virtual ULONG STDMETHODCALLTYPE AddRef(void) { return 1; }
66     virtual ULONG STDMETHODCALLTYPE Release(void) { return 1; }
67
68     virtual HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(BMDVideoInputFormatChangedEvents, IDeckLinkDisplayMode*, BMDDetectedVideoInputFormatFlags);
69     virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(IDeckLinkVideoInputFrame*, IDeckLinkAudioInputPacket*);
70
71 private:
72     demux_t *p_demux_;
73 };
74
75 HRESULT DeckLinkCaptureDelegate::VideoInputFormatChanged(BMDVideoInputFormatChangedEvents events, IDeckLinkDisplayMode *mode, BMDDetectedVideoInputFormatFlags)
76 {
77     msg_Dbg( p_demux_, "Video input format changed" );    
78     return S_OK;
79 }
80
81 HRESULT DeckLinkCaptureDelegate::VideoInputFrameArrived(IDeckLinkVideoInputFrame* videoFrame, IDeckLinkAudioInputPacket* audioFrame)
82 {
83     msg_Dbg( p_demux_, "Received a frame" );
84     return S_OK;
85 }
86
87 static int Open( vlc_object_t *p_this )
88 {
89     demux_t     *p_demux = (demux_t*)p_this;
90     demux_sys_t *p_sys;
91
92     /* Only when selected */
93     if( *p_demux->psz_access == '\0' )
94         return VLC_EGENERIC;
95
96     /* Set up p_demux */
97     p_demux->pf_demux = Demux;
98     p_demux->pf_control = Control;
99     p_demux->info.i_update = 0;
100     p_demux->info.i_title = 0;
101     p_demux->info.i_seekpoint = 0;
102     p_demux->p_sys = p_sys = (demux_sys_t*)calloc( 1, sizeof( demux_sys_t ) );
103     if( !p_sys )
104         return VLC_ENOMEM;
105
106     msg_Dbg( p_demux, "hello world" );
107
108     IDeckLinkIterator *decklink_iterator = CreateDeckLinkIteratorInstance();
109     if ( !decklink_iterator ) {
110         msg_Err( p_demux, "DeckLink drivers not found." );
111         // FIXME: Leak here and several other error paths.
112         return VLC_EGENERIC;
113     }
114
115     HRESULT result;
116     result = decklink_iterator->Next( &p_sys->p_card );
117
118     if ( result != S_OK ) {
119         msg_Err( p_demux, "No DeckLink PCI cards found" );
120         return VLC_EGENERIC;
121     }
122
123     if ( p_sys->p_card->QueryInterface(IID_IDeckLinkInput, (void**)&p_sys->p_input) != S_OK ) {
124         msg_Err( p_demux, "Card has no inputs" );
125         return VLC_EGENERIC;
126     }
127
128     p_sys->p_delegate = new DeckLinkCaptureDelegate( p_demux );
129     p_sys->p_input->SetCallback( p_sys->p_delegate );
130
131     result = p_sys->p_input->EnableVideoInput(bmdModePAL, bmdFormat8BitYUV, 0);
132     if ( result != S_OK ) {
133         msg_Err( p_demux, "Failed to enable video input" );
134         return VLC_EGENERIC;
135     }
136
137     // FIXME: add audio
138     result = p_sys->p_input->StartStreams();
139     if ( result != S_OK ) {
140         msg_Err( p_demux, "Failed to start streams" );
141         return VLC_EGENERIC;
142     }
143
144     /*eDeclare elementary streams */
145     es_format_t fmt;
146     es_format_Init( &fmt, VIDEO_ES, VLC_CODEC_YUYV );
147     fmt.video.i_width = 720;
148     fmt.video.i_height = 576;
149     fmt.video.i_sar_num = 1;
150     fmt.video.i_sar_den = 1;
151
152     msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
153              (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
154     p_sys->p_es = es_out_Add( p_demux->out, &fmt );
155
156     /* Update default_pts to a suitable value for access */
157     var_Create( p_demux, "sdi-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
158
159     return VLC_SUCCESS;
160 }
161
162 static void Close( vlc_object_t *p_this )
163 {
164     demux_t     *p_demux = (demux_t *)p_this;
165     demux_sys_t *p_sys   = p_demux->p_sys;
166
167     free( p_sys );
168 }
169
170 static int Control( demux_t *p_demux, int i_query, va_list args )
171 {
172     bool *pb;
173     int64_t    *pi64;
174
175     switch( i_query )
176     {
177         /* Special for access_demux */
178         case DEMUX_CAN_PAUSE:
179         case DEMUX_CAN_SEEK:
180         case DEMUX_CAN_CONTROL_PACE:
181             pb = (bool*)va_arg( args, bool * );
182             *pb = false;
183             return VLC_SUCCESS;
184
185         case DEMUX_GET_PTS_DELAY:
186             pi64 = (int64_t*)va_arg( args, int64_t * );
187             *pi64 = var_GetInteger( p_demux, "sdi-caching" ) * 1000;
188             return VLC_SUCCESS;
189
190         case DEMUX_GET_TIME:
191             pi64 = (int64_t*)va_arg( args, int64_t * );
192             *pi64 = mdate();  // FIXME
193             return VLC_SUCCESS;
194
195         /* TODO implement others */
196         default:
197             return VLC_EGENERIC;
198     }
199
200     return VLC_EGENERIC;
201 }
202
203 static int Demux( demux_t *p_demux )
204 {
205     demux_sys_t *p_sys = p_demux->p_sys;
206
207     // FIXME
208
209     return 1;
210 }
211