OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
DaleCurtis
2017/06/15 21:46:32
Tests should exist for the C++ variant too; althou
AndyWu
2017/08/02 01:43:40
TBD.
| |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.media; | |
6 | |
7 import static org.junit.Assert.assertArrayEquals; | |
8 | |
9 import android.media.AudioFormat; | |
10 import android.media.AudioTrack; | |
11 | |
12 import org.junit.Test; | |
13 import org.junit.runner.RunWith; | |
14 import org.robolectric.annotation.Config; | |
15 | |
16 import org.chromium.testing.local.LocalRobolectricTestRunner; | |
17 | |
18 import java.nio.ByteBuffer; | |
19 import java.util.ArrayList; | |
20 import java.util.List; | |
21 import java.util.concurrent.CountDownLatch; | |
22 import java.util.concurrent.TimeUnit; | |
23 | |
24 @RunWith(LocalRobolectricTestRunner.class) | |
25 @Config(manifest = Config.NONE) | |
26 class AudioTrackOutputStreamTest { | |
27 static class ObservableAudioTrack extends AudioTrack { | |
28 private List<Byte> mReceivedData = new ArrayList<Byte>(); | |
29 | |
30 public ObservableAudioTrack(int streamType, int sampleRateInHz, int chan nelConfig, | |
31 int audioFormat, int bufferSizeInBytes, int mode) { | |
32 super(streamType, sampleRateInHz, channelConfig, audioFormat, buffer SizeInBytes, mode); | |
33 } | |
34 | |
35 @Override | |
36 public int write(ByteBuffer audioData, int sizeInBytes, int writeMode) { | |
37 int writternSize = super.write(audioData, sizeInBytes, writeMode); | |
38 | |
39 if (writternSize > 0) { | |
40 byte[] array = new byte[writternSize]; | |
41 audioData.asReadOnlyBuffer().get(array); | |
42 recordData(array, 0, writternSize); | |
43 } | |
44 | |
45 return writternSize; | |
46 } | |
47 | |
48 @Override | |
49 public int write(byte[] audioData, int offsetInBytes, int sizeInBytes) { | |
50 int writternSize = super.write(audioData, offsetInBytes, sizeInBytes ); | |
51 | |
52 if (writternSize > 0) recordData(audioData, offsetInBytes, writternS ize); | |
53 | |
54 return writternSize; | |
55 } | |
56 | |
57 private void recordData(byte[] audioData, int offsetInBytes, int sizeInB ytes) { | |
58 for (; sizeInBytes > 0; --sizeInBytes) mReceivedData.add(audioData[o ffsetInBytes++]); | |
59 } | |
60 | |
61 public List<Byte> getReceivedData() { | |
62 return mReceivedData; | |
63 } | |
64 } | |
65 | |
66 static class DataProvider implements AudioTrackOutputStream.Callback { | |
67 private static final int MIN_BUFFER_SIZE = 1024; | |
68 private List<Byte> mGeneratedData = new ArrayList<Byte>(); | |
69 private CountDownLatch mDoneSignal; | |
70 private ObservableAudioTrack mAudioTrack; | |
71 | |
72 public DataProvider(int bufferCount) { | |
73 assert bufferCount > 0; | |
74 mDoneSignal = new CountDownLatch(bufferCount + 1); | |
75 } | |
76 | |
77 public void updateBufferCount(int bufferCount) { | |
78 assert bufferCount > 0; | |
79 mDoneSignal = new CountDownLatch(bufferCount + 1); | |
80 } | |
81 | |
82 @Override | |
83 public int getMinBufferSize(int sampleRateInHz, int channelConfig, int a udioFormat) { | |
84 return MIN_BUFFER_SIZE; | |
85 } | |
86 | |
87 @Override | |
88 public AudioTrack createAudioTrack(int streamType, int sampleRateInHz, i nt channelConfig, | |
89 int audioFormat, int bufferSizeInBytes, int mode) { | |
90 mAudioTrack = new ObservableAudioTrack(streamType, sampleRateInHz, c hannelConfig, | |
91 audioFormat, bufferSizeInBytes, mode); | |
92 return mAudioTrack; | |
93 } | |
94 | |
95 @Override | |
96 public int onMoreData(ByteBuffer audioData, int totalPlayedFrames) { | |
97 mDoneSignal.countDown(); | |
98 if (mDoneSignal.getCount() <= 1) { | |
99 return 0; | |
100 } | |
101 | |
102 final int dataSize = MIN_BUFFER_SIZE; | |
103 audioData.rewind(); | |
104 for (int i = 0; i < dataSize; ++i) { | |
105 byte data = (byte) i; | |
106 audioData.put(data); | |
107 mGeneratedData.add(data); | |
108 } | |
109 return dataSize; | |
110 } | |
111 | |
112 public void waitForOutOfData() throws InterruptedException { | |
113 mDoneSignal.await(300, TimeUnit.MILLISECONDS); | |
114 } | |
115 | |
116 public List<Byte> getGeneratedData() { | |
117 return mGeneratedData; | |
118 } | |
119 | |
120 public List<Byte> getReceivedData() { | |
121 return mAudioTrack.getReceivedData(); | |
122 } | |
123 }; | |
124 | |
125 @Test | |
126 public void playSimpleBitstream() throws InterruptedException { | |
127 DataProvider provider = new DataProvider(3); | |
128 | |
129 AudioTrackOutputStream stream = AudioTrackOutputStream.create(provider); | |
130 stream.open(2, 44100, AudioFormat.ENCODING_E_AC3); | |
131 stream.start(0); | |
132 | |
133 provider.waitForOutOfData(); | |
134 List<Byte> generatedData = provider.getGeneratedData(); | |
135 List<Byte> receivedData = provider.getReceivedData(); | |
136 | |
137 assertArrayEquals(generatedData.toArray(), receivedData.toArray()); | |
138 | |
139 stream.stop(); | |
140 } | |
141 | |
142 @Test | |
143 public void playPiecewiseBitstream() throws InterruptedException { | |
144 DataProvider provider = new DataProvider(3); | |
145 | |
146 AudioTrackOutputStream stream = AudioTrackOutputStream.create(provider); | |
147 stream.open(2, 44100, AudioFormat.ENCODING_E_AC3); | |
148 stream.start(0); | |
149 | |
150 provider.waitForOutOfData(); | |
151 | |
152 provider.updateBufferCount(3); | |
153 provider.waitForOutOfData(); | |
154 | |
155 List<Byte> generatedData = provider.getGeneratedData(); | |
156 List<Byte> receivedData = provider.getReceivedData(); | |
157 | |
158 assertArrayEquals(generatedData.toArray(), receivedData.toArray()); | |
159 | |
160 stream.stop(); | |
161 } | |
162 } | |
OLD | NEW |