OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
1 #include "jni/sound_service.h" | 5 #include "jni/sound_service.h" |
2 | 6 |
3 #include "bin/log.h" | 7 #include "jni/log.h" |
4 #include "jni/resource.h" | 8 #include "jni/resource.h" |
5 | 9 |
6 SoundService::SoundService(android_app* application) | 10 SoundService::SoundService(android_app* application) |
7 : application_(application), | 11 : application_(application), |
8 engine_(NULL), | 12 engine_(NULL), |
9 engine_if_(NULL), | 13 engine_if_(NULL), |
10 output_mix_(NULL), | 14 output_mix_(NULL), |
11 background_player_(NULL), | 15 background_player_(NULL), |
12 background_player_if_(NULL), | 16 background_player_if_(NULL), |
13 background_player_seek_if_(NULL) { | 17 background_player_seek_if_(NULL) { |
14 } | 18 } |
15 | 19 |
16 int32_t SoundService::Start() { | 20 int32_t SoundService::Start() { |
17 Log::Print("Starting SoundService"); | 21 LOGI("Starting SoundService"); |
18 | 22 |
19 const SLInterfaceID k_engine_mix_IIDs[] = { SL_IID_ENGINE }; | 23 const SLInterfaceID k_engine_mix_IIDs[] = { SL_IID_ENGINE }; |
20 const SLboolean k_engine_mix_reqs[] = { SL_BOOLEAN_TRUE }; | 24 const SLboolean k_engine_mix_reqs[] = { SL_BOOLEAN_TRUE }; |
21 const SLInterfaceID k_output_mix_IIDs[] = {}; | 25 const SLInterfaceID k_output_mix_IIDs[] = {}; |
22 const SLboolean k_output_mix_reqs[] = {}; | 26 const SLboolean k_output_mix_reqs[] = {}; |
23 if (slCreateEngine(&engine_, 0, NULL, 1, k_engine_mix_IIDs, k_engine_mix_reqs) | 27 if (slCreateEngine(&engine_, 0, NULL, 1, k_engine_mix_IIDs, k_engine_mix_reqs) |
24 == SL_RESULT_SUCCESS && | 28 == SL_RESULT_SUCCESS && |
25 (*engine_)->Realize(engine_, SL_BOOLEAN_FALSE) == SL_RESULT_SUCCESS && | 29 (*engine_)->Realize(engine_, SL_BOOLEAN_FALSE) == SL_RESULT_SUCCESS && |
26 (*engine_)->GetInterface(engine_, SL_IID_ENGINE, &engine_if_) == | 30 (*engine_)->GetInterface(engine_, SL_IID_ENGINE, &engine_if_) == |
27 SL_RESULT_SUCCESS && | 31 SL_RESULT_SUCCESS && |
28 (*engine_if_)->CreateOutputMix(engine_if_, &output_mix_, 0, | 32 (*engine_if_)->CreateOutputMix(engine_if_, &output_mix_, 0, |
29 k_output_mix_IIDs, k_output_mix_reqs) == | 33 k_output_mix_IIDs, k_output_mix_reqs) == |
30 SL_RESULT_SUCCESS && | 34 SL_RESULT_SUCCESS && |
31 (*output_mix_)->Realize(output_mix_, SL_BOOLEAN_FALSE) == | 35 (*output_mix_)->Realize(output_mix_, SL_BOOLEAN_FALSE) == |
32 SL_RESULT_SUCCESS) { | 36 SL_RESULT_SUCCESS) { |
33 return 0; | 37 return 0; |
34 } | 38 } |
35 Log::Print("Failed to start SoundService"); | 39 LOGI("Failed to start SoundService"); |
36 Stop(); | 40 Stop(); |
37 return -1; | 41 return -1; |
38 } | 42 } |
39 | 43 |
40 void SoundService::Stop() { | 44 void SoundService::Stop() { |
41 StopBackground(); | 45 StopBackground(); |
42 if (output_mix_ != NULL) { | 46 if (output_mix_ != NULL) { |
43 (*output_mix_)->Destroy(output_mix_); | 47 (*output_mix_)->Destroy(output_mix_); |
44 output_mix_ = NULL; | 48 output_mix_ = NULL; |
45 } | 49 } |
46 if (engine_ != NULL) { | 50 if (engine_ != NULL) { |
47 (*engine_)->Destroy(engine_); | 51 (*engine_)->Destroy(engine_); |
48 engine_ = NULL; | 52 engine_ = NULL; |
49 engine_if_ = NULL; | 53 engine_if_ = NULL; |
50 } | 54 } |
51 } | 55 } |
52 | 56 |
53 int32_t SoundService::PlayBackground(const char* path) { | 57 int32_t SoundService::PlayBackground(const char* path) { |
54 Resource resource(application_, path); | 58 Resource resource(application_, path); |
55 if (resource.open() < 0) { | 59 if (resource.open() < 0) { |
56 Log::Print("Could not open file %s", path); | 60 LOGI("Could not open file %s", path); |
57 return -1; | 61 return -1; |
58 } | 62 } |
59 Log::Print("Saving FD data"); | 63 LOGI("Saving FD data"); |
60 SLDataLocator_AndroidFD data_locator_in; | 64 SLDataLocator_AndroidFD data_locator_in; |
61 data_locator_in.locatorType = SL_DATALOCATOR_ANDROIDFD; | 65 data_locator_in.locatorType = SL_DATALOCATOR_ANDROIDFD; |
62 data_locator_in.fd = resource.descriptor(); | 66 data_locator_in.fd = resource.descriptor(); |
63 data_locator_in.offset = resource.start(); | 67 data_locator_in.offset = resource.start(); |
64 data_locator_in.length = resource.length(); | 68 data_locator_in.length = resource.length(); |
65 resource.close(); | 69 resource.close(); |
66 | 70 |
67 Log::Print("Init data format"); | 71 LOGI("Init data format"); |
68 SLDataFormat_MIME data_format; | 72 SLDataFormat_MIME data_format; |
69 data_format.formatType = SL_DATAFORMAT_MIME; | 73 data_format.formatType = SL_DATAFORMAT_MIME; |
70 data_format.mimeType = NULL; | 74 data_format.mimeType = NULL; |
71 data_format.containerType = SL_CONTAINERTYPE_UNSPECIFIED; | 75 data_format.containerType = SL_CONTAINERTYPE_UNSPECIFIED; |
72 | 76 |
73 Log::Print("Init data source"); | 77 LOGI("Init data source"); |
74 SLDataSource data_source; | 78 SLDataSource data_source; |
75 data_source.pLocator = &data_locator_in; | 79 data_source.pLocator = &data_locator_in; |
76 data_source.pFormat = &data_format; | 80 data_source.pFormat = &data_format; |
77 | 81 |
78 Log::Print("Init out locator"); | 82 LOGI("Init out locator"); |
79 SLDataLocator_OutputMix data_locator_out; | 83 SLDataLocator_OutputMix data_locator_out; |
80 data_locator_out.locatorType = SL_DATALOCATOR_OUTPUTMIX; | 84 data_locator_out.locatorType = SL_DATALOCATOR_OUTPUTMIX; |
81 data_locator_out.outputMix = output_mix_; | 85 data_locator_out.outputMix = output_mix_; |
82 | 86 |
83 Log::Print("Init data sink"); | 87 LOGI("Init data sink"); |
84 SLDataSink data_sink; | 88 SLDataSink data_sink; |
85 data_sink.pLocator = &data_locator_out; | 89 data_sink.pLocator = &data_locator_out; |
86 data_sink.pFormat = NULL; | 90 data_sink.pFormat = NULL; |
87 | 91 |
88 const SLInterfaceID k_background_player_IIDs[] = { SL_IID_PLAY, SL_IID_SEEK }; | 92 const SLInterfaceID k_background_player_IIDs[] = { SL_IID_PLAY, SL_IID_SEEK }; |
89 const SLboolean k_background_player_reqs[] = | 93 const SLboolean k_background_player_reqs[] = |
90 { SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE }; | 94 { SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE }; |
91 | 95 |
92 Log::Print("Creating audio player"); | 96 LOGI("Creating audio player"); |
93 if ((*engine_if_)-> | 97 if ((*engine_if_)-> |
94 CreateAudioPlayer(engine_if_, &background_player_, | 98 CreateAudioPlayer(engine_if_, &background_player_, |
95 &data_source, &data_sink, 2, | 99 &data_source, &data_sink, 2, |
96 k_background_player_IIDs, | 100 k_background_player_IIDs, |
97 k_background_player_reqs) != SL_RESULT_SUCCESS) { | 101 k_background_player_reqs) != SL_RESULT_SUCCESS) { |
98 Log::PrintErr("Couldn't create audio player"); | 102 LOGE("Couldn't create audio player"); |
99 return -1; | 103 return -1; |
100 } | 104 } |
101 Log::Print("Created audio player"); | 105 LOGI("Created audio player"); |
102 if ((*background_player_)-> | 106 if ((*background_player_)-> |
103 Realize(background_player_, SL_BOOLEAN_FALSE) != SL_RESULT_SUCCESS) { | 107 Realize(background_player_, SL_BOOLEAN_FALSE) != SL_RESULT_SUCCESS) { |
104 Log::PrintErr("Couldn't realize audio player"); | 108 LOGE("Couldn't realize audio player"); |
105 return -1; | 109 return -1; |
106 } | 110 } |
107 Log::Print("Realized audio player"); | 111 LOGI("Realized audio player"); |
108 if ((*background_player_)-> | 112 if ((*background_player_)-> |
109 GetInterface(background_player_, SL_IID_PLAY, | 113 GetInterface(background_player_, SL_IID_PLAY, |
110 &background_player_if_) != SL_RESULT_SUCCESS) { | 114 &background_player_if_) != SL_RESULT_SUCCESS) { |
111 Log::PrintErr("Couldn't get player interface"); | 115 LOGE("Couldn't get player interface"); |
112 return -1; | 116 return -1; |
113 } | 117 } |
114 Log::Print("Got player interface"); | 118 LOGI("Got player interface"); |
115 if ((*background_player_)-> | 119 if ((*background_player_)-> |
116 GetInterface(background_player_, SL_IID_SEEK, | 120 GetInterface(background_player_, SL_IID_SEEK, |
117 &background_player_seek_if_) != SL_RESULT_SUCCESS) { | 121 &background_player_seek_if_) != SL_RESULT_SUCCESS) { |
118 Log::PrintErr("Couldn't get seek interface"); | 122 LOGE("Couldn't get seek interface"); |
119 return -1; | 123 return -1; |
120 } | 124 } |
121 Log::Print("Got seek interface"); | 125 LOGI("Got seek interface"); |
122 if ((*background_player_seek_if_)-> | 126 if ((*background_player_seek_if_)-> |
123 SetLoop(background_player_seek_if_, SL_BOOLEAN_TRUE, 0, | 127 SetLoop(background_player_seek_if_, SL_BOOLEAN_TRUE, 0, |
124 SL_TIME_UNKNOWN) != SL_RESULT_SUCCESS) { | 128 SL_TIME_UNKNOWN) != SL_RESULT_SUCCESS) { |
125 Log::PrintErr("Couldn't set loop"); | 129 LOGE("Couldn't set loop"); |
126 return -1; | 130 return -1; |
127 } | 131 } |
128 Log::Print("Set loop"); | 132 LOGI("Set loop"); |
129 if ((*background_player_if_)-> | 133 if ((*background_player_if_)-> |
130 SetPlayState(background_player_if_, SL_PLAYSTATE_PLAYING) != | 134 SetPlayState(background_player_if_, SL_PLAYSTATE_PLAYING) != |
131 SL_RESULT_SUCCESS) { | 135 SL_RESULT_SUCCESS) { |
132 Log::PrintErr("Couldn't start playing"); | 136 LOGE("Couldn't start playing"); |
133 return -1; | 137 return -1; |
134 } | 138 } |
135 Log::Print("Started playing"); | 139 LOGI("Started playing"); |
136 return 0; | 140 return 0; |
137 } | 141 } |
138 | 142 |
139 void SoundService::StopBackground() { | 143 void SoundService::StopBackground() { |
140 if (background_player_if_ != NULL) { | 144 if (background_player_if_ != NULL) { |
141 SLuint32 state; | 145 SLuint32 state; |
142 (*background_player_)->GetState(background_player_, &state); | 146 (*background_player_)->GetState(background_player_, &state); |
143 if (state == SL_OBJECT_STATE_REALIZED) { | 147 if (state == SL_OBJECT_STATE_REALIZED) { |
144 (*background_player_if_)->SetPlayState(background_player_if_, | 148 (*background_player_if_)->SetPlayState(background_player_if_, |
145 SL_PLAYSTATE_PAUSED); | 149 SL_PLAYSTATE_PAUSED); |
146 | 150 |
147 (*background_player_)->Destroy(background_player_); | 151 (*background_player_)->Destroy(background_player_); |
148 background_player_ = NULL; | 152 background_player_ = NULL; |
149 background_player_if_ = NULL; | 153 background_player_if_ = NULL; |
150 background_player_seek_if_ = NULL; | 154 background_player_seek_if_ = NULL; |
151 } | 155 } |
152 } | 156 } |
153 } | 157 } |
154 | |
OLD | NEW |