aboutsummaryrefslogtreecommitdiff
path: root/sys/decklink/capture.cpp
blob: c2a0bcb3d7ac4d966e122d300b73ba63de0d3932 (plain)
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
/* -LICENSE-START-
** Copyright (c) 2009 Blackmagic Design
**
** Permission is hereby granted, free of charge, to any person or organization
** obtaining a copy of the software and accompanying documentation covered by
** this license (the "Software") to use, reproduce, display, distribute,
** execute, and transmit the Software, and to prepare derivative works of the
** Software, and to permit third-parties to whom the Software is furnished to
** do so, all subject to the following:
** 
** The copyright notices in the Software and this entire statement, including
** the above license grant, this restriction and the following disclaimer,
** must be included in all copies of the Software, in whole or in part, and
** all derivative works of the Software, unless such copies or derivative
** works are solely in the form of machine-executable object code generated by
** a source language processor.
** 
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
** SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
** FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
** ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
** DEALINGS IN THE SOFTWARE.
** -LICENSE-END-
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

#include <gst/glib-compat-private.h>

#include "gstdecklinksrc.h"

#include "capture.h"

#define GST_CAT_DEFAULT gst_decklink_src_debug_category

IDeckLink *deckLink;
IDeckLinkInput *deckLinkInput;
IDeckLinkDisplayModeIterator *displayModeIterator;

static BMDTimecodeFormat g_timecodeFormat = (BMDTimecodeFormat) 0;

DeckLinkCaptureDelegate::DeckLinkCaptureDelegate ():m_refCount (0)
{
  m_mutex = g_mutex_new ();
}

DeckLinkCaptureDelegate::~DeckLinkCaptureDelegate ()
{
  g_mutex_free (m_mutex);
}

ULONG DeckLinkCaptureDelegate::AddRef (void)
{
  g_mutex_lock (m_mutex);
  m_refCount++;
  g_mutex_unlock (m_mutex);

  return (ULONG) m_refCount;
}

ULONG DeckLinkCaptureDelegate::Release (void)
{
  g_mutex_lock (m_mutex);
  m_refCount--;
  g_mutex_unlock (m_mutex);

  if (m_refCount == 0) {
    delete
        this;
    return 0;
  }

  return (ULONG) m_refCount;
}

HRESULT
    DeckLinkCaptureDelegate::VideoInputFrameArrived (IDeckLinkVideoInputFrame *
    videoFrame, IDeckLinkAudioInputPacket * audioFrame)
{
  GstDecklinkSrc *decklinksrc;
  const char *timecodeString = NULL;

  g_return_val_if_fail (priv != NULL, S_OK);
  g_return_val_if_fail (GST_IS_DECKLINK_SRC (priv), S_OK);

  decklinksrc = GST_DECKLINK_SRC (priv);

  if (videoFrame == NULL) {
    GST_WARNING_OBJECT (decklinksrc, "video frame is NULL");
    return S_OK;
  }

  if (audioFrame == NULL) {
    GST_WARNING_OBJECT (decklinksrc, "audio frame is NULL");
    return S_OK;
  }

  if (videoFrame->GetFlags () & bmdFrameHasNoInputSource) {
    GST_DEBUG_OBJECT (decklinksrc, "Frame received - No input signal detected");
    return S_OK;
  }

  if (g_timecodeFormat != 0) {
    IDeckLinkTimecode *timecode;
    if (videoFrame->GetTimecode (g_timecodeFormat, &timecode) == S_OK) {
      timecode->GetString (&timecodeString);
      CONVERT_COM_STRING (timecodeString);
    }
  }

  GST_DEBUG_OBJECT (decklinksrc, "Frame received [%s] - %s - Size: %li bytes",
      timecodeString != NULL ? timecodeString : "No timecode",
      "Valid Frame", videoFrame->GetRowBytes () * videoFrame->GetHeight ());

  if (timecodeString)
    FREE_COM_STRING (timecodeString);

  g_mutex_lock (decklinksrc->mutex);
  if (decklinksrc->video_frame != NULL) {
    decklinksrc->dropped_frames++;
    decklinksrc->video_frame->Release();
    if (decklinksrc->audio_frame) {
      decklinksrc->audio_frame->Release();
    }
  }
  videoFrame->AddRef ();
  decklinksrc->video_frame = videoFrame;
  if (audioFrame) {
    audioFrame->AddRef ();
    decklinksrc->audio_frame = audioFrame;
  }

  /* increment regardless whether frame was dropped or not */
  decklinksrc->frame_num++;

  g_cond_signal (decklinksrc->cond);
  g_mutex_unlock (decklinksrc->mutex);

  return S_OK;
}

HRESULT
DeckLinkCaptureDelegate::VideoInputFormatChanged (
    BMDVideoInputFormatChangedEvents events, IDeckLinkDisplayMode * mode,
    BMDDetectedVideoInputFormatFlags)
{
  GstDecklinkSrc *decklinksrc;

  g_return_val_if_fail (priv != NULL, S_OK);
  g_return_val_if_fail (GST_IS_DECKLINK_SRC (priv), S_OK);

  decklinksrc = GST_DECKLINK_SRC (priv);

  GST_ERROR_OBJECT (decklinksrc, "unimplemented: video input format changed");

  return S_OK;
}