• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2008, The Android Open Source Project
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *  * Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  *  * Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 // must include config.h first for webkit to fiddle with new/delete
27 #include "config.h"
28 #include "android_npapi.h"
29 
30 #include "media/AudioTrack.h"
31 
32 struct ANPAudioTrack {
33     void*                mUser;
34     ANPAudioCallbackProc mProc;
35     android::AudioTrack* mTrack;
36 };
37 
toANPFormat(int fm)38 static ANPSampleFormat toANPFormat(int fm) {
39     switch (fm) {
40         case android::AudioSystem::PCM_16_BIT:
41             return kPCM16Bit_ANPSampleFormat;
42         case android::AudioSystem::PCM_8_BIT:
43             return kPCM8Bit_ANPSampleFormat;
44         default:
45             return kUnknown_ANPSamleFormat;
46     }
47 }
48 
fromANPFormat(ANPSampleFormat fm)49 static android::AudioSystem::audio_format fromANPFormat(ANPSampleFormat fm) {
50     switch (fm) {
51         case kPCM16Bit_ANPSampleFormat:
52             return android::AudioSystem::PCM_16_BIT;
53         case kPCM8Bit_ANPSampleFormat:
54             return android::AudioSystem::PCM_8_BIT;
55         default:
56             return android::AudioSystem::INVALID_FORMAT;
57     }
58 }
59 
callbackProc(int event,void * user,void * info)60 static void callbackProc(int event, void* user, void* info) {
61     ANPAudioTrack* track = reinterpret_cast<ANPAudioTrack*>(user);
62 
63     switch (event) {
64         case android::AudioTrack::EVENT_MORE_DATA: {
65             ANPAudioBuffer dst;
66             android::AudioTrack::Buffer* src;
67 
68             src = reinterpret_cast<android::AudioTrack::Buffer*>(info);
69             dst.bufferData      = src->raw;
70             dst.channelCount    = src->channelCount;
71             dst.format          = toANPFormat(src->format);
72             dst.size            = src->size;
73             track->mProc(kMoreData_ANPAudioEvent, track->mUser, &dst);
74             // return the updated size field
75             src->size = dst.size;
76             break;
77         }
78         case android::AudioTrack::EVENT_UNDERRUN:
79             track->mProc(kUnderRun_ANPAudioEvent, track->mUser, NULL);
80             break;
81         default:
82             SkDebugf("------ unknown audio event for plugin %d\n", event);
83             break;
84     }
85 }
86 
ANPCreateTrack(uint32_t sampleRate,ANPSampleFormat format,int channelCount,ANPAudioCallbackProc proc,void * user)87 static ANPAudioTrack* ANPCreateTrack(uint32_t sampleRate,
88                                      ANPSampleFormat format,
89                                      int channelCount,
90                                      ANPAudioCallbackProc proc,
91                                      void* user) {
92 
93     ANPAudioTrack* track = new ANPAudioTrack;
94 
95     track->mUser = user;
96     track->mProc = proc;
97     track->mTrack = new android::AudioTrack(android::AudioSystem::MUSIC,
98                                             sampleRate,
99                                             fromANPFormat(format),
100                                             channelCount,
101                                             0,  // frameCount
102                                             0,  // flags
103                                             callbackProc,
104                                             track,
105                                             0);
106 
107     if (track->mTrack->initCheck() != 0) {  // failure
108         delete track->mTrack;
109         delete track;
110         track = NULL;
111     }
112     return track;
113 }
114 
ANPDeleteTrack(ANPAudioTrack * track)115 static void ANPDeleteTrack(ANPAudioTrack* track) {
116     if (track) {
117         delete track->mTrack;
118         delete track;
119     }
120 }
121 
ANPTrackStart(ANPAudioTrack * track)122 static void ANPTrackStart(ANPAudioTrack* track) {
123     track->mTrack->start();
124 }
125 
ANPTrackPause(ANPAudioTrack * track)126 static void ANPTrackPause(ANPAudioTrack* track) {
127     track->mTrack->pause();
128 }
129 
ANPTrackStop(ANPAudioTrack * track)130 static void ANPTrackStop(ANPAudioTrack* track) {
131     track->mTrack->stop();
132 }
133 
ANPTrackIsStopped(ANPAudioTrack * track)134 static bool ANPTrackIsStopped(ANPAudioTrack* track) {
135     return track->mTrack->stopped();
136 }
137 
138 ///////////////////////////////////////////////////////////////////////////////
139 
ANPAudioTrackInterfaceV0_Init(ANPInterface * value)140 void ANPAudioTrackInterfaceV0_Init(ANPInterface* value) {
141     ANPAudioTrackInterfaceV0* si = reinterpret_cast<ANPAudioTrackInterfaceV0*>(value);
142     si->newTrack    = ANPCreateTrack;
143     si->deleteTrack = ANPDeleteTrack;
144     si->start       = ANPTrackStart;
145     si->pause       = ANPTrackPause;
146     si->stop        = ANPTrackStop;
147     si->isStopped   = ANPTrackIsStopped;
148 }
149 
150