aboutsummaryrefslogtreecommitdiff
path: root/modules/pdp_mp4videosource.cpp
blob: 5e5ef911774796facc491b4c56ff2e205da16350 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 * 
 * The Original Code is MPEG4IP.
 * 
 * The Initial Developer of the Original Code is Cisco Systems Inc.
 * Portions created by Cisco Systems Inc. are
 * Copyright (C) Cisco Systems Inc. 2000, 2001.  All Rights Reserved.
 * 
 * Contributor(s): 
 *        Dave Mackie        dmackie@cisco.com
 *        Bill May         wmay@cisco.com
 *
 * Adapted for PD/PDP by Yves Degoyon (ydegoyon@free.fr)
 */

#ifndef debug_message
#define debug_message post 
#endif

#include <sys/mman.h>

#include "m_pd.h"
#include "pdp_mp4videosource.h"
#include "video_util_rgb.h"

//#define DEBUG_TIMESTAMPS 1

int CPDPVideoSource::ThreadMain(void)
{
   // just a stub, the threaded mode is not used ( never called )
   return 0;
}

void CPDPVideoSource::DoStart(void)
{
  if (m_source) return;
  if (!Init()) return;
  m_source = true;
}

void CPDPVideoSource::DoStop(void)
{
  if (!m_source) return;
  DoStopVideo();
  m_source = false;
}

bool CPDPVideoSource::Init(void)
{

    if (m_pConfig->GetIntegerValue(CONFIG_VIDEO_SIGNAL) == VIDEO_SIGNAL_NTSC) 
    {
      m_videoSrcFrameRate = VIDEO_NTSC_FRAME_RATE;
    } 
    else 
    {
      m_videoSrcFrameRate = VIDEO_PAL_FRAME_RATE;
    }

    m_videoMbuf.frames = 2;
    m_cacheTimestamp = false;
    m_videoFrameMap = (struct video_mmap*) malloc(m_videoMbuf.frames * sizeof(struct video_mmap));
    if ( !m_videoFrameMap ) 
    {
      post("pdp_mp4live~ : video source init : failed to allocate enough memory");
      return false;
    }

    m_videoFrameMapFrame = (uint64_t *)malloc(m_videoMbuf.frames * sizeof(uint64_t));
    m_videoFrameMapTimestamp = (uint64_t *)malloc(m_videoMbuf.frames * sizeof(Timestamp));
    m_captureHead = 0;
    m_encodeHead = -1;

    m_videoFrames = 0;
    m_videoSrcFrameDuration = (Duration)(((float)TimestampTicks / m_videoSrcFrameRate) + 0.5);
    for (int i = 0; i < m_videoMbuf.frames; i++) 
    {
       // initialize frame map
       m_videoFrameMap[i].frame = i;
       m_videoFrameMap[i].width = m_pConfig->GetIntegerValue(CONFIG_VIDEO_RAW_WIDTH);
       m_videoFrameMap[i].height = m_pConfig->GetIntegerValue(CONFIG_VIDEO_RAW_HEIGHT);
       m_videoFrameMap[i].format = VIDEO_PALETTE_YUV420P;

       if (i == 0) 
       {
         m_videoCaptureStartTimestamp = GetTimestamp();
       }
       m_lastVideoFrameMapFrameLoaded = m_videoFrameMapFrame[i] = i;
       m_lastVideoFrameMapTimestampLoaded =
       m_videoFrameMapTimestamp[i] = CalculateVideoTimestampFromFrames(i);
    }

    m_pConfig->CalculateVideoFrameSize();

    InitVideo(
        (m_pConfig->m_videoNeedRgbToYuv ?
            RGBVIDEOFRAME :
            YUVVIDEOFRAME),
        true);

    SetVideoSrcSize(
        m_pConfig->GetIntegerValue(CONFIG_VIDEO_RAW_WIDTH),
        m_pConfig->GetIntegerValue(CONFIG_VIDEO_RAW_HEIGHT),
        m_pConfig->GetIntegerValue(CONFIG_VIDEO_RAW_WIDTH),
        true);

    m_maxPasses = (u_int8_t)(m_videoSrcFrameRate + 0.5);

    return true;
}

int8_t CPDPVideoSource::StartTimeStamp(Timestamp &frameTimestamp)
{
    if (m_cacheTimestamp)
      frameTimestamp = m_videoFrameMapTimestamp[m_captureHead];
    else
      frameTimestamp = GetTimestamp();

    int8_t capturedFrame = m_captureHead;
    m_captureHead = (m_captureHead + 1) % m_videoMbuf.frames;

    return capturedFrame;
}

bool CPDPVideoSource::EndTimeStamp(int8_t frameNumber)
{
  Timestamp calc = GetTimestamp();

  if (calc > m_videoSrcFrameDuration + m_lastVideoFrameMapTimestampLoaded) {
#ifdef DEBUG_TIMESTAMPS
    debug_message("pdp_mp4live~ : video frame delay past end of buffer - time is %llu should be %llu",
          calc,
          m_videoSrcFrameDuration + m_lastVideoFrameMapTimestampLoaded);
#endif
    m_videoCaptureStartTimestamp = calc;
    m_videoFrameMapFrame[frameNumber] = 0;
    m_videoFrameMapTimestamp[frameNumber] = calc;
  } else {
    m_videoFrameMapFrame[frameNumber] = m_lastVideoFrameMapFrameLoaded + 1;
    m_videoFrameMapTimestamp[frameNumber] =
      CalculateVideoTimestampFromFrames(m_videoFrameMapFrame[frameNumber]);
  }

  m_lastVideoFrameMapFrameLoaded = m_videoFrameMapFrame[frameNumber];
  m_lastVideoFrameMapTimestampLoaded = m_videoFrameMapTimestamp[frameNumber];
  return true;
}

void CPDPVideoSource::ProcessVideo(u_int8_t *pY, u_int8_t *pU, u_int8_t *pV)
{
    // for efficiency, process ~1 second before returning to check for commands
  Timestamp frameTimestamp;
    for (int pass = 0; pass < m_maxPasses; pass++) {

        // get next frame from video capture device
        m_encodeHead = StartTimeStamp(frameTimestamp);
        if (m_encodeHead == -1) {
            continue;
        }

        ProcessVideoYUVFrame(
            pY, 
            pU, 
            pV,
            m_videoSrcWidth,
            m_videoSrcWidth >> 1,
            frameTimestamp);

        // release video frame buffer back to video capture device
        if (EndTimeStamp(m_encodeHead)) {
            m_encodeHead = (m_encodeHead + 1) % m_videoMbuf.frames;
        } else {
            debug_message("pdp_mp4live~ : couldn't release capture buffer!");
        }
    }
}