Check sample size in addition to container size

Also add comments and fix line length

Change-Id: Iaac8ade2b53c3cc6951dc94413bbe996e1d5e925
diff --git a/src/android/AudioPlayer_to_android.cpp b/src/android/AudioPlayer_to_android.cpp
index 5e3b94f..b8e9234 100644
--- a/src/android/AudioPlayer_to_android.cpp
+++ b/src/android/AudioPlayer_to_android.cpp
@@ -953,6 +953,7 @@
             df_representation = &df_pcm->representation;
             } // SL_ANDROID_DATAFORMAT_PCM_EX - fall through to next test.
         case SL_DATAFORMAT_PCM: {
+            // checkDataFormat() already did generic checks, now do the Android-specific checks
             const SLDataFormat_PCM *df_pcm = (const SLDataFormat_PCM *) pAudioSrc->pFormat;
             SLresult result = android_audioPlayer_validateChannelMask(df_pcm->channelMask,
                                                                       df_pcm->numChannels);
@@ -971,9 +972,19 @@
             //     upcoming check by sles_to_android_channelMaskOut are sufficient
 
             if (df_pcm->endianness != pAudioPlayer->mObject.mEngine->mEngine.mNativeEndianness) {
-                SL_LOGE("Cannot create audio player: unsupported byte order %u", df_pcm->endianness);
+                SL_LOGE("Cannot create audio player: unsupported byte order %u",
+                        df_pcm->endianness);
                 return SL_RESULT_CONTENT_UNSUPPORTED;
             }
+
+            // we don't support container size != sample depth
+            if (df_pcm->containerSize != df_pcm->bitsPerSample) {
+                SL_LOGE("Cannot create audio player: unsupported container size %u bits for "
+                        "sample depth %u bits",
+                        df_pcm->containerSize, (SLuint32)df_pcm->bitsPerSample);
+                return SL_RESULT_CONTENT_UNSUPPORTED;
+            }
+
             } //case SL_DATAFORMAT_PCM
             break;
         case SL_DATAFORMAT_MIME:
diff --git a/src/android/AudioRecorder_to_android.cpp b/src/android/AudioRecorder_to_android.cpp
index 0a205dc..add488c 100644
--- a/src/android/AudioRecorder_to_android.cpp
+++ b/src/android/AudioRecorder_to_android.cpp
@@ -179,14 +179,23 @@
             ar->mNumChannels = df_pcm->numChannels;
 
             if (df_pcm->endianness != ar->mObject.mEngine->mEngine.mNativeEndianness) {
-                SL_LOGE("Cannot create audio recorder: unsupported byte order %u", df_pcm->endianness);
+                SL_LOGE("Cannot create audio recorder: unsupported byte order %u",
+                        df_pcm->endianness);
                 return SL_RESULT_CONTENT_UNSUPPORTED;
             }
 
             ar->mSampleRateMilliHz = df_pcm->samplesPerSec; // Note: bad field name in SL ES
             SL_LOGV("AudioRecorder requested sample rate = %u mHz, %u channel(s)",
                     ar->mSampleRateMilliHz, ar->mNumChannels);
-            // FIXME validates bitsPerSample
+
+            // we don't support container size != sample depth
+            if (df_pcm->containerSize != df_pcm->bitsPerSample) {
+                SL_LOGE("Cannot create audio recorder: unsupported container size %u bits for "
+                        "sample depth %u bits",
+                        df_pcm->containerSize, (SLuint32)df_pcm->bitsPerSample);
+                return SL_RESULT_CONTENT_UNSUPPORTED;
+            }
+
             } break;
         default:
             SL_LOGE(ERROR_RECORDER_SINK_FORMAT_MUST_BE_PCM);
diff --git a/src/android/android_sles_conversions.h b/src/android/android_sles_conversions.h
index 277c471..05b4699 100644
--- a/src/android/android_sles_conversions.h
+++ b/src/android/android_sles_conversions.h
@@ -27,6 +27,9 @@
 }
 
 static inline audio_format_t sles_to_android_sampleFormat(const SLDataFormat_PCM *df_pcm) {
+    if (df_pcm->containerSize != df_pcm->bitsPerSample) {
+        return AUDIO_FORMAT_INVALID;
+    }
     switch (df_pcm->formatType) {
     case SL_DATAFORMAT_PCM:
         switch (df_pcm->containerSize) {
diff --git a/src/data.c b/src/data.c
index 069744b..a711030 100644
--- a/src/data.c
+++ b/src/data.c
@@ -401,7 +401,7 @@
                     break;
                 }
 
-                // check the container bit depth
+                // check the container bit depth and representation
                 switch (pDataFormat->mPCM.containerSize) {
                 case 8:
                     if (df_representation != NULL &&
@@ -432,8 +432,9 @@
                     break;
                 }
 
-                // container size cannot be less than sample size
-                if (pDataFormat->mPCM.containerSize < pDataFormat->mPCM.bitsPerSample) {
+                // sample size cannot be zero, and container size cannot be less than sample size
+                if (pDataFormat->mPCM.bitsPerSample == 0 ||
+                        pDataFormat->mPCM.containerSize < pDataFormat->mPCM.bitsPerSample) {
                     result = SL_RESULT_PARAMETER_INVALID;
                 }
                 if (SL_RESULT_SUCCESS != result) {