Permalink
Browse files

Merge pull request #3823 from t-nelson/audiotrack_improvements

[DROID][AE] Audiotrack sink improvements
  • Loading branch information...
2 parents cdc6566 + b99d647 commit 9a87ad03237288d9d5bee1589f2379c1323268cf @t-nelson t-nelson committed Dec 19, 2013
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2013 Team XBMC
+ * http://xbmc.org
+ *
+ * This Program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This Program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with XBMC; see the file COPYING. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include "AudioFormat.h"
+#include "JNIBase.h"
+#include "jutils/jutils-details.hpp"
+
+using namespace jni;
+
+int CJNIAudioFormat::ENCODING_PCM_16BIT = 0x00000002;
+int CJNIAudioFormat::CHANNEL_OUT_STEREO = 0x0000000c;
+
+void CJNIAudioFormat::PopulateStaticFields()
+{
+ int sdk = CJNIBase::GetSDKVersion();
+ if (sdk >= 3)
+ {
+ jhclass c = find_class("android/media/AudioFormat");
+ CJNIAudioFormat::ENCODING_PCM_16BIT = get_static_field<int>(c, "ENCODING_PCM_16BIT");
+ if (sdk >= 5)
+ CJNIAudioFormat::CHANNEL_OUT_STEREO = get_static_field<int>(c, "CHANNEL_OUT_STEREO");
+ }
+}
+
@@ -0,0 +1,35 @@
+#pragma once
+/*
+ * Copyright (C) 2013 Team XBMC
+ * http://xbmc.org
+ *
+ * This Program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This Program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with XBMC; see the file COPYING. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace jni
+{
+
+class CJNIAudioFormat
+{
+ public:
+ static void PopulateStaticFields();
+
+ static int ENCODING_PCM_16BIT;
+ static int CHANNEL_OUT_STEREO;
+};
+
+};
+
@@ -33,9 +33,9 @@ class CJNIAudioManager : public CJNIBase
void setStreamVolume(int index = 0, int flags = 0);
static void PopulateStaticFields();
+ static int STREAM_MUSIC;
private:
CJNIAudioManager();
-
- static int STREAM_MUSIC;
};
+
@@ -0,0 +1,118 @@
+/*
+ * Copyright (C) 2013 Team XBMC
+ * http://xbmc.org
+ *
+ * This Program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This Program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with XBMC; see the file COPYING. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include "AudioTrack.h"
+#include "jutils/jutils-details.hpp"
+
+using namespace jni;
+
+int CJNIAudioTrack::MODE_STREAM = 0x00000001;
+int CJNIAudioTrack::PLAYSTATE_PLAYING = 0x00000003;
+
+void CJNIAudioTrack::PopulateStaticFields()
+{
+ if (CJNIBase::GetSDKVersion() >= 3)
+ {
+ jhclass c = find_class("android/media/AudioTrack");
+ CJNIAudioTrack::PLAYSTATE_PLAYING = get_static_field<int>(c, "PLAYSTATE_PLAYING");
+ if (CJNIBase::GetSDKVersion() >= 5)
+ CJNIAudioTrack::MODE_STREAM = get_static_field<int>(c, "MODE_STREAM");
+ }
+}
+
+CJNIAudioTrack::CJNIAudioTrack(int streamType, int sampleRateInHz, int channelConfig, int audioFormat, int bufferSizeInBytes, int mode)
+ : CJNIBase("android/media/AudioTrack")
+{
+ m_object = new_object(GetClassName(), "<init>", "(IIIIII)V",
+ streamType, sampleRateInHz, channelConfig,
+ audioFormat, bufferSizeInBytes, mode);
+ m_buffer = jharray(xbmc_jnienv()->NewByteArray(bufferSizeInBytes));
+
+ m_object.setGlobal();
+ m_buffer.setGlobal();
+}
+
+void CJNIAudioTrack::play()
+{
+ call_method<void>(m_object, "play", "()V");
+}
+
+void CJNIAudioTrack::stop()
+{
+ call_method<void>(m_object, "stop", "()V");
+
+ JNIEnv* jenv = xbmc_jnienv();
+
+ // might toss an exception on release so catch it.
+ jthrowable exception = jenv->ExceptionOccurred();
+ if (exception)
+ {
+ jenv->ExceptionDescribe();
+ jenv->ExceptionClear();
+ }
+}
+
+void CJNIAudioTrack::flush()
+{
+ call_method<void>(m_object, "flush", "()V");
+}
+
+void CJNIAudioTrack::release()
+{
+ call_method<void>(m_object, "release", "()V");
+
+ JNIEnv* jenv = xbmc_jnienv();
+}
+
+int CJNIAudioTrack::write(char* audioData, int offsetInBytes, int sizeInBytes)
+{
+ int written = 0;
+ JNIEnv* jenv = xbmc_jnienv();
+ char* pArray;
+
+ // Write a buffer of audio data to Java AudioTrack.
+ // Warning, no other JNI function can be called after
+ // GetPrimitiveArrayCritical until ReleasePrimitiveArrayCritical.
+ if ((pArray = (char*)jenv->GetPrimitiveArrayCritical(m_buffer, NULL)))
+ {
+ memcpy(pArray + offsetInBytes, audioData, sizeInBytes);
+ jenv->ReleasePrimitiveArrayCritical(m_buffer, pArray, 0);
+ written = call_method<int>(m_object, "write", "([BII)I", m_buffer, offsetInBytes, sizeInBytes);
+ }
+
+ return written;
+}
+
+int CJNIAudioTrack::getPlayState()
+{
+ return call_method<int>(m_object, "getPlayState", "()I");
+}
+
+int CJNIAudioTrack::getPlaybackHeadPosition()
+{
+ return call_method<int>(m_object, "getPlaybackHeadPosition", "()I");
+}
+
+int CJNIAudioTrack::getMinBufferSize(int sampleRateInHz, int channelConfig, int audioFormat)
+{
+ return call_static_method<int>( "android/media/AudioTrack", "getMinBufferSize", "(III)I",
+ sampleRateInHz, channelConfig, audioFormat);
+}
+
@@ -0,0 +1,51 @@
+#pragma once
+/*
+ * Copyright (C) 2013 Team XBMC
+ * http://xbmc.org
+ *
+ * This Program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This Program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with XBMC; see the file COPYING. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include "JNIBase.h"
+#include "ByteBuffer.h"
+
+namespace jni
+{
+
+class CJNIAudioTrack : public CJNIBase
+{
+ jharray m_buffer;
+
+ public:
+ CJNIAudioTrack(int streamType, int sampleRateInHz, int channelConfig, int audioFormat, int bufferSizeInBytes, int mode);
+
+ void play();
+ void stop();
+ void flush();
+ void release();
+ int write(char* audioData, int offsetInBytes, int sizeInBytes);
+ int getPlayState();
+ int getPlaybackHeadPosition();
+
+ static int MODE_STREAM;
+ static int PLAYSTATE_PLAYING;
+
+ static void PopulateStaticFields();
+ static int getMinBufferSize(int sampleRateInHz, int channelConfig, int audioFormat);
+};
+
+};
+
@@ -35,7 +35,9 @@
#include "PowerManager.h"
#include "Cursor.h"
#include "ConnectivityManager.h"
+#include "AudioFormat.h"
#include "AudioManager.h"
+#include "AudioTrack.h"
#include "Surface.h"
#include "MediaCodec.h"
#include "MediaCodecInfo.h"
@@ -76,7 +78,9 @@ void CJNIContext::PopulateStaticFields()
CJNICursor::PopulateStaticFields();
CJNIContentResolver::PopulateStaticFields();
CJNIConnectivityManager::PopulateStaticFields();
+ CJNIAudioFormat::PopulateStaticFields();
CJNIAudioManager::PopulateStaticFields();
+ CJNIAudioTrack::PopulateStaticFields();
CJNISurface::PopulateStaticFields();
CJNIMediaCodec::PopulateStaticFields();
CJNIMediaCodecInfoCodecProfileLevel::PopulateStaticFields();
@@ -4,7 +4,9 @@ SRCS += Context.cpp
SRCS += BroadcastReceiver.cpp
SRCS += Intent.cpp
SRCS += PackageManager.cpp
+SRCS += AudioFormat.cpp
SRCS += AudioManager.cpp
+SRCS += AudioTrack.cpp
SRCS += PowerManager.cpp
SRCS += WakeLock.cpp
SRCS += Environment.cpp
Oops, something went wrong.

0 comments on commit 9a87ad0

Please sign in to comment.