aout.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*****************************************************************************
  2. * aout.c
  3. *****************************************************************************
  4. * Copyright © 2011-2012 VLC authors and VideoLAN
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  19. *****************************************************************************/
  20. #include <stdio.h>
  21. #include <assert.h>
  22. #include <string.h>
  23. #include <stdint.h>
  24. #include <jni.h>
  25. #include <vlc/vlc.h>
  26. #include "aout.h"
  27. #define LOG_TAG "VLC/JNI/aout"
  28. #include "log.h"
  29. // An audio frame will contain FRAME_SIZE samples
  30. #define FRAME_SIZE (4096*2)
  31. typedef struct
  32. {
  33. jobject j_libVlc; /// Pointer to the LibVLC Java object
  34. jmethodID play; /// Java method to play audio buffers
  35. jbyteArray buffer; /// Raw audio data to be played
  36. } aout_sys_t;
  37. /** Unique Java VM instance, as defined in libvlcjni.c */
  38. extern JavaVM *myVm;
  39. int aout_open(void **opaque, char *format, unsigned *rate, unsigned *nb_channels)
  40. {
  41. LOGI ("Opening the JNI audio output");
  42. aout_sys_t *p_sys = calloc (1, sizeof (*p_sys));
  43. if (!p_sys)
  44. return -1;
  45. p_sys->j_libVlc = *opaque; // Keep a reference to our Java object
  46. *opaque = (void*) p_sys; // The callback will need aout_sys_t
  47. LOGI ("Parameters: %u channels, FOURCC '%4.4s', sample rate: %uHz",
  48. *nb_channels, format, *rate);
  49. JNIEnv *p_env;
  50. if ((*myVm)->AttachCurrentThread (myVm, &p_env, NULL) != 0)
  51. {
  52. LOGE("Could not attach the display thread to the JVM !");
  53. return -1;
  54. }
  55. // Call the init function.
  56. jclass cls = (*p_env)->GetObjectClass (p_env, p_sys->j_libVlc);
  57. jmethodID methodIdInitAout = (*p_env)->GetMethodID (p_env, cls,
  58. "initAout", "(III)V");
  59. if (!methodIdInitAout)
  60. {
  61. LOGE ("Method initAout() could not be found!");
  62. goto error;
  63. }
  64. LOGV ("Number of channels forced to 2, number of samples to %d", FRAME_SIZE);
  65. *nb_channels = 2;
  66. (*p_env)->CallVoidMethod (p_env, p_sys->j_libVlc, methodIdInitAout,
  67. *rate, *nb_channels, FRAME_SIZE);
  68. if ((*p_env)->ExceptionCheck (p_env))
  69. {
  70. LOGE ("Unable to create audio player!");
  71. #ifndef NDEBUG
  72. (*p_env)->ExceptionDescribe (p_env);
  73. #endif
  74. (*p_env)->ExceptionClear (p_env);
  75. goto error;
  76. }
  77. /* Create a new byte array to store the audio data. */
  78. jbyteArray buffer = (*p_env)->NewByteArray (p_env,
  79. *nb_channels *
  80. FRAME_SIZE *
  81. sizeof (uint16_t) /* =2 */);
  82. if (buffer == NULL)
  83. {
  84. LOGE ("Could not allocate the Java byte array to store the audio data!");
  85. goto error;
  86. }
  87. /* Use a global reference to not reallocate memory each time we run
  88. the play function. */
  89. p_sys->buffer = (*p_env)->NewGlobalRef (p_env, buffer);
  90. /* The local reference is no longer useful. */
  91. (*p_env)->DeleteLocalRef (p_env, buffer);
  92. if (p_sys->buffer == NULL)
  93. {
  94. LOGE ("Could not create the global reference!");
  95. goto error;
  96. }
  97. // Get the play methodId
  98. p_sys->play = (*p_env)->GetMethodID (p_env, cls, "playAudio", "([BI)V");
  99. assert (p_sys->play != NULL);
  100. (*myVm)->DetachCurrentThread (myVm);
  101. return 0;
  102. error:
  103. (*myVm)->DetachCurrentThread (myVm);
  104. *opaque = NULL;
  105. free (p_sys);
  106. return -1;
  107. }
  108. /**
  109. * Play an audio frame
  110. **/
  111. void aout_play(void *opaque, const void *samples, unsigned count, int64_t pts)
  112. {
  113. aout_sys_t *p_sys = opaque;
  114. JNIEnv *p_env;
  115. /* How ugly: we constantly attach/detach this thread to/from the JVM
  116. * because it will be killed before aout_close is called.
  117. * aout_close will actually be called in an different thread!
  118. */
  119. (*myVm)->AttachCurrentThread (myVm, &p_env, NULL);
  120. (*p_env)->SetByteArrayRegion (p_env, p_sys->buffer, 0,
  121. 2 /*nb_channels*/ * count * sizeof (uint16_t),
  122. (jbyte*) samples);
  123. if ((*p_env)->ExceptionCheck (p_env))
  124. {
  125. // This can happen if for some reason the size of the input buffer
  126. // is larger than the size of the output buffer
  127. LOGE ("An exception occurred while calling SetByteArrayRegion");
  128. (*p_env)->ExceptionDescribe (p_env);
  129. (*p_env)->ExceptionClear (p_env);
  130. return;
  131. }
  132. (*p_env)->CallVoidMethod (p_env, p_sys->j_libVlc, p_sys->play,
  133. p_sys->buffer,
  134. 2 /*nb_channels*/ * count * sizeof (uint16_t),
  135. FRAME_SIZE);
  136. // FIXME: check for errors
  137. (*myVm)->DetachCurrentThread (myVm);
  138. }
  139. void aout_pause(void *opaque, int64_t pts)
  140. {
  141. LOGI ("Pausing audio output");
  142. aout_sys_t *p_sys = opaque;
  143. assert(p_sys);
  144. JNIEnv *p_env;
  145. (*myVm)->AttachCurrentThread (myVm, &p_env, NULL);
  146. // Call the pause function.
  147. jclass cls = (*p_env)->GetObjectClass (p_env, p_sys->j_libVlc);
  148. jmethodID methodIdPauseAout = (*p_env)->GetMethodID (p_env, cls, "pauseAout", "()V");
  149. if (!methodIdPauseAout)
  150. LOGE ("Method pauseAout() could not be found!");
  151. (*p_env)->CallVoidMethod (p_env, p_sys->j_libVlc, methodIdPauseAout);
  152. if ((*p_env)->ExceptionCheck (p_env))
  153. {
  154. LOGE ("Unable to pause audio player!");
  155. #ifndef NDEBUG
  156. (*p_env)->ExceptionDescribe (p_env);
  157. #endif
  158. (*p_env)->ExceptionClear (p_env);
  159. }
  160. (*myVm)->DetachCurrentThread (myVm);
  161. }
  162. void aout_close(void *opaque)
  163. {
  164. LOGI ("Closing audio output");
  165. aout_sys_t *p_sys = opaque;
  166. assert(p_sys);
  167. assert(p_sys->buffer);
  168. JNIEnv *p_env;
  169. (*myVm)->AttachCurrentThread (myVm, &p_env, NULL);
  170. // Call the close function.
  171. jclass cls = (*p_env)->GetObjectClass (p_env, p_sys->j_libVlc);
  172. jmethodID methodIdCloseAout = (*p_env)->GetMethodID (p_env, cls, "closeAout", "()V");
  173. if (!methodIdCloseAout)
  174. LOGE ("Method closeAout() could not be found!");
  175. (*p_env)->CallVoidMethod (p_env, p_sys->j_libVlc, methodIdCloseAout);
  176. if ((*p_env)->ExceptionCheck (p_env))
  177. {
  178. LOGE ("Unable to close audio player!");
  179. #ifndef NDEBUG
  180. (*p_env)->ExceptionDescribe (p_env);
  181. #endif
  182. (*p_env)->ExceptionClear (p_env);
  183. }
  184. (*p_env)->DeleteGlobalRef (p_env, p_sys->buffer);
  185. (*myVm)->DetachCurrentThread (myVm);
  186. free (p_sys);
  187. }