-
Notifications
You must be signed in to change notification settings - Fork 2
/
PKScheduledDataSlice.cpp
97 lines (76 loc) · 2.91 KB
/
PKScheduledDataSlice.cpp
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
/*
* PKScheduledDataSlice
* PlayerKit
*
* Created by Peter MacWhinnie on 9/22/09.
* Copyright 2009 Roundabout Software. All rights reserved.
*
*/
#include "PKScheduledDataSlice.h"
#include "CAComponent.h"
#include "CAComponentDescription.h"
#include "CAAudioBufferList.h"
#include "CAStreamBasicDescription.h"
#include <iostream>
#pragma mark PKScheduledDataSlice
PKScheduledDataSlice::PKScheduledDataSlice(PKAudioPlayerEngine *owner,
RBAtomicCounter *atomicCounter,
AudioBufferList *bufferList,
UInt32 numberOfFrames,
const AudioStreamBasicDescription &streamFormat) :
RBLockableObject("PKScheduledDataSlice"),
mOwner(owner),
mNumberOfActiveSlicesAtomicCounter(atomicCounter),
mBuffersStreamFormat(streamFormat),
mDataSliceProgressionNumber(0),
mNumberOfFramesToRead(numberOfFrames),
mBuffersHaveData(false),
mInvalidated(false)
{
mNumberOfActiveSlicesAtomicCounter->Retain();
bzero(&mScheduledAudioSlice, sizeof(ScheduledAudioSlice));
bzero(&mScheduledAudioSlice.mTimeStamp, sizeof(AudioTimeStamp));
mScheduledAudioSlice.mBufferList = bufferList;
mScheduledAudioSlice.mNumberFrames = numberOfFrames;
mScheduledAudioSlice.mCompletionProc = ScheduledAudioSliceCompletionProc(&PKAudioPlayerEngine::ProcessorDidFinishSlice);
mScheduledAudioSlice.mCompletionProcUserData = this;
}
PKScheduledDataSlice::~PKScheduledDataSlice()
{
AudioBufferList *buffers = mScheduledAudioSlice.mBufferList;
for (int index = 0; index < buffers->mNumberBuffers; index++)
free(buffers->mBuffers[index].mData);
CAAudioBufferList::Destroy(buffers);
mNumberOfActiveSlicesAtomicCounter->Release();
}
#pragma mark -
long PKScheduledDataSlice::CalculateBufferOffsetInFramesFromSampleTime(Float64 sampleTime) const
{
Float64 sliceTime = mScheduledAudioSlice.mTimeStamp.mSampleTime;
Float64 sliceDuration = mScheduledAudioSlice.mTimeStamp.mSampleTime + mScheduledAudioSlice.mNumberFrames;
if((sampleTime >= sliceTime) && (sampleTime <= sliceDuration))
return long(mScheduledAudioSlice.mNumberFrames - (sliceDuration - sampleTime));
return -1;
}
CFComparisonResult PKScheduledDataSlice::Comparator(PKScheduledDataSlice *left, PKScheduledDataSlice *right, PKAudioPlayerEngine *self)
{
Float64 leftSampleTime = left->mScheduledAudioSlice.mTimeStamp.mSampleTime;
Float64 rightSampleTime = right->mScheduledAudioSlice.mTimeStamp.mSampleTime;
if(leftSampleTime < rightSampleTime)
return kCFCompareLessThan;
else if(leftSampleTime > rightSampleTime)
return kCFCompareGreaterThan;
return kCFCompareEqualTo;
}
void PKScheduledDataSlice::ScheduleSliceTaskProxy(PKScheduledDataSlice *self) throw(RBException)
{
self->mOwner->ScheduleSlice(self);
}
void PKScheduledDataSlice::Reset()
{
Acquisitor lock(this);
memset(&mScheduledAudioSlice.mTimeStamp, 0, sizeof(AudioTimeStamp));
mNumberOfActiveSlicesAtomicCounter->SetValue(0);
mBuffersHaveData = false;
mInvalidated = false;
}