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