aboutsummaryrefslogtreecommitdiff
path: root/modules/pdp_mp4audiosource.cpp
blob: 40a8c91d89dd8696256aba350271ec6315c98990 (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
/*
 * 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-2002.  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)
 */

#include "m_pd.h"

#ifndef debug_message
#define debug_message post
#endif
#include "pdp_mp4audiosource.h"

//#define DEBUG_TIMESTAMPS 1

CPDPAudioSource::CPDPAudioSource(CLiveConfig *pConfig) : CMediaSource() 
{
  SetConfig(pConfig);

  m_pcmFrameBuffer = NULL;
  m_prevTimestamp = 0;
  m_timestampOverflowArray = NULL;
  m_timestampOverflowArrayIndex = 0;
  m_audioOssMaxBufferSize = 0;
}

int CPDPAudioSource::ThreadMain(void) 
{
  // just a stub, we don't use the threaded mode
  return 0;
}

void CPDPAudioSource::DoStart()
{
  if (m_source) {
    return;
  }

  if (!Init()) {
    return;
  }

  m_source = true;
}

void CPDPAudioSource::DoStop()
{
  if (!m_source) {
    return;
  }

  CMediaSource::DoStopAudio();

  CHECK_AND_FREE(m_timestampOverflowArray);
  free(m_pcmFrameBuffer);
  m_pcmFrameBuffer = NULL;

  m_source = false;
}

bool CPDPAudioSource::Init(void)
{
  bool rc = InitAudio(true);
  if (!rc) {
    return false;
  }

  m_channelsConfigured = m_pConfig->GetIntegerValue(CONFIG_AUDIO_CHANNELS);
  
  debug_message("pdp_mp4live~ : init audio : (m_audioDstChannels=%d m_audioDstSampleRate=%d )", 
                           m_audioDstChannels, m_audioDstSampleRate);

  rc = SetAudioSrc(PCMAUDIOFRAME,
                   m_channelsConfigured,
                   m_pConfig->GetIntegerValue(CONFIG_AUDIO_SAMPLE_RATE));

  if (!rc) {
    return false;
  }

  debug_message("pdp_mp4live~ : set audio src : (m_audioDstSamplesPerFrame=%d m_audioSrcChannels=%d)", 
                           m_audioDstSamplesPerFrame, m_audioSrcChannels);

  // for live capture we can match the source to the destination
  m_audioSrcSamplesPerFrame = m_audioDstSamplesPerFrame;
  m_pcmFrameSize = m_audioSrcSamplesPerFrame * m_audioSrcChannels * sizeof(u_int16_t);

  if (m_audioOssMaxBufferSize > 0) {
    size_t array_size;
    m_audioOssMaxBufferFrames = m_audioOssMaxBufferSize / m_pcmFrameSize;
    array_size = m_audioOssMaxBufferFrames * sizeof(*m_timestampOverflowArray);
    m_timestampOverflowArray = (Timestamp *)malloc(array_size);
    memset(m_timestampOverflowArray, 0, array_size);
  }
    
  m_pcmFrameBuffer = (u_int8_t*)malloc(m_pcmFrameSize);
  if (!m_pcmFrameBuffer) {
    goto init_failure;
  }

  // maximum number of passes in ProcessAudio, approx 1 sec.
  m_maxPasses = m_audioSrcSampleRate / m_audioSrcSamplesPerFrame;

  debug_message("pdp_mp4live~ : audio source initialization done : ( frame size=%d )", m_pcmFrameSize );

  return true;

 init_failure:
  debug_message("pdp_mp4live~ : audio initialization failed");

  free(m_pcmFrameBuffer);
  m_pcmFrameBuffer = NULL;

  return false;
}

void CPDPAudioSource::ProcessAudio(u_int8_t* pcmBuffer, u_int32_t pcmBufferSize)
{
  Timestamp currentTime = GetTimestamp();
  Timestamp timestamp;

    if ( pcmBufferSize > m_pcmFrameSize )
    {
       debug_message( "pdp_mp4live~ : too many audio samples : %d should be %d",
                      pcmBufferSize, m_pcmFrameSize );
       memcpy( m_pcmFrameBuffer, pcmBuffer, m_pcmFrameSize );
    }
    else if ( pcmBufferSize < m_pcmFrameSize )
    {
       debug_message( "pdp_mp4live~ : too little audio samples : %d should be %d",
                      pcmBufferSize, m_pcmFrameSize );
       memcpy( m_pcmFrameBuffer, pcmBuffer, pcmBufferSize );
    }
    else
    {
       memcpy( m_pcmFrameBuffer, pcmBuffer, pcmBufferSize );
    }

    timestamp = currentTime - SrcSamplesToTicks(SrcBytesToSamples(pcmBufferSize));

#ifdef DEBUG_TIMESTAMPS
    debug_message("pdp_mp4live~ : bytes=%d t=%llu timestamp=%llu delta=%llu",
                  pcmBufferSize, currentTime, timestamp, timestamp - m_prevTimestamp);
#endif

    m_prevTimestamp = timestamp;

    ProcessAudioFrame(m_pcmFrameBuffer, m_pcmFrameSize, timestamp, false);
}