]> git.sesse.net Git - vlc/blob - modules/access/sdi.cpp
Actually receive frame data and send it on.
[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     vlc_mutex_t lock;
58     block_t *p_frame;  // protected by <lock>
59 };
60
61 class DeckLinkCaptureDelegate : public IDeckLinkInputCallback
62 {
63 public:
64     DeckLinkCaptureDelegate( demux_t *p_demux ) : p_demux_(p_demux) {}
65
66     // FIXME: These leak.
67     virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) { return E_NOINTERFACE; }
68     virtual ULONG STDMETHODCALLTYPE AddRef(void) { return 1; }
69     virtual ULONG STDMETHODCALLTYPE Release(void) { return 1; }
70
71     virtual HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(BMDVideoInputFormatChangedEvents, IDeckLinkDisplayMode*, BMDDetectedVideoInputFormatFlags);
72     virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(IDeckLinkVideoInputFrame*, IDeckLinkAudioInputPacket*);
73
74 private:
75     demux_t *p_demux_;
76 };
77
78 HRESULT DeckLinkCaptureDelegate::VideoInputFormatChanged(BMDVideoInputFormatChangedEvents events, IDeckLinkDisplayMode *mode, BMDDetectedVideoInputFormatFlags)
79 {
80     msg_Dbg( p_demux_, "Video input format changed" );    
81     return S_OK;
82 }
83
84 HRESULT DeckLinkCaptureDelegate::VideoInputFrameArrived(IDeckLinkVideoInputFrame* videoFrame, IDeckLinkAudioInputPacket* audioFrame)
85 {
86     demux_sys_t *p_sys = p_demux_->p_sys;
87
88     if (videoFrame) {
89         if (videoFrame->GetFlags() & bmdFrameHasNoInputSource) {
90             msg_Warn( p_demux_, "No input signal detected" );
91             return S_OK;
92         }
93
94         msg_Dbg( p_demux_, "Received a frame" );
95
96         block_t *p_frame;
97         p_frame = block_New( p_demux_, 720 * 576 * 3 );
98         if ( !p_frame ) {
99             msg_Err( p_demux_, "Could not allocate memory for frame" );
100             return S_OK;
101         }
102
103         void *frame_bytes;
104         videoFrame->GetBytes( &frame_bytes );
105         memcpy( p_frame->p_buffer, frame_bytes, 720 * 576 * 3 );
106
107         BMDTimeValue stream_time, frame_duration;
108         videoFrame->GetStreamTime( &stream_time, &frame_duration, 1000000 );
109         p_frame->i_pts = stream_time;
110
111         vlc_mutex_lock( &p_sys->lock );
112         p_sys->p_frame = p_frame;  // FIXME: leak
113         vlc_mutex_unlock( &p_sys->lock );
114     }
115
116     return S_OK;
117 }
118
119 static int Open( vlc_object_t *p_this )
120 {
121     demux_t     *p_demux = (demux_t*)p_this;
122     demux_sys_t *p_sys;
123
124     /* Only when selected */
125     if( *p_demux->psz_access == '\0' )
126         return VLC_EGENERIC;
127
128     /* Set up p_demux */
129     p_demux->pf_demux = Demux;
130     p_demux->pf_control = Control;
131     p_demux->info.i_update = 0;
132     p_demux->info.i_title = 0;
133     p_demux->info.i_seekpoint = 0;
134     p_demux->p_sys = p_sys = (demux_sys_t*)calloc( 1, sizeof( demux_sys_t ) );
135     if( !p_sys )
136         return VLC_ENOMEM;
137
138     vlc_mutex_init( &p_sys->lock );
139     p_sys->p_frame = NULL;
140
141     IDeckLinkIterator *decklink_iterator = CreateDeckLinkIteratorInstance();
142     if ( !decklink_iterator ) {
143         msg_Err( p_demux, "DeckLink drivers not found." );
144         // FIXME: Leak here and several other error paths.
145         return VLC_EGENERIC;
146     }
147
148     HRESULT result;
149     result = decklink_iterator->Next( &p_sys->p_card );
150
151     if ( result != S_OK ) {
152         msg_Err( p_demux, "No DeckLink PCI cards found" );
153         return VLC_EGENERIC;
154     }
155
156     if ( p_sys->p_card->QueryInterface(IID_IDeckLinkInput, (void**)&p_sys->p_input) != S_OK ) {
157         msg_Err( p_demux, "Card has no inputs" );
158         return VLC_EGENERIC;
159     }
160
161     p_sys->p_delegate = new DeckLinkCaptureDelegate( p_demux );
162     p_sys->p_input->SetCallback( p_sys->p_delegate );
163
164     result = p_sys->p_input->EnableVideoInput(bmdModePAL, bmdFormat8BitYUV, 0);
165     if ( result != S_OK ) {
166         msg_Err( p_demux, "Failed to enable video input" );
167         return VLC_EGENERIC;
168     }
169
170     // FIXME: add audio
171     result = p_sys->p_input->StartStreams();
172     if ( result != S_OK ) {
173         msg_Err( p_demux, "Failed to start streams" );
174         return VLC_EGENERIC;
175     }
176
177     /*eDeclare elementary streams */
178     es_format_t fmt;
179     es_format_Init( &fmt, VIDEO_ES, VLC_CODEC_YUYV );
180     fmt.video.i_width = 720;
181     fmt.video.i_height = 576;
182     fmt.video.i_sar_num = 1;
183     fmt.video.i_sar_den = 1;
184
185     msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
186              (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
187     p_sys->p_es = es_out_Add( p_demux->out, &fmt );
188
189     /* Update default_pts to a suitable value for access */
190     var_Create( p_demux, "sdi-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
191
192     return VLC_SUCCESS;
193 }
194
195 static void Close( vlc_object_t *p_this )
196 {
197     demux_t     *p_demux = (demux_t *)p_this;
198     demux_sys_t *p_sys   = p_demux->p_sys;
199
200     free( p_sys );
201 }
202
203 static int Control( demux_t *p_demux, int i_query, va_list args )
204 {
205     bool *pb;
206     int64_t    *pi64;
207
208     switch( i_query )
209     {
210         /* Special for access_demux */
211         case DEMUX_CAN_PAUSE:
212         case DEMUX_CAN_SEEK:
213         case DEMUX_CAN_CONTROL_PACE:
214             pb = (bool*)va_arg( args, bool * );
215             *pb = false;
216             return VLC_SUCCESS;
217
218         case DEMUX_GET_PTS_DELAY:
219             pi64 = (int64_t*)va_arg( args, int64_t * );
220             *pi64 = var_GetInteger( p_demux, "sdi-caching" ) * 1000;
221             return VLC_SUCCESS;
222
223         case DEMUX_GET_TIME:
224             pi64 = (int64_t*)va_arg( args, int64_t * );
225             *pi64 = mdate();  // FIXME
226             return VLC_SUCCESS;
227
228         /* TODO implement others */
229         default:
230             return VLC_EGENERIC;
231     }
232
233     return VLC_EGENERIC;
234 }
235
236 static int Demux( demux_t *p_demux )
237 {
238     demux_sys_t *p_sys = p_demux->p_sys;
239
240     vlc_mutex_lock( &p_sys->lock );
241     block_t *p_block = p_sys->p_frame;
242     p_sys->p_frame = NULL;
243     vlc_mutex_unlock( &p_sys->lock );
244
245     if ( p_block ) {
246         msg_Dbg( p_demux, "Sending frame" ); 
247         es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
248         es_out_Send( p_demux->out, p_sys->p_es, p_block );
249     } else {
250         usleep(50000);  // FIXME
251     }
252
253     return 1;
254 }
255