• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <source/StreamingSource.h>
20 
21 #include <memory>
22 #include <thread>
23 
24 #define SIMULATE_AUDIO          0
25 
26 namespace vsoc {
27     class RegionWorker;
28     namespace audio_data {
29         class AudioDataRegionView;
30     }
31 }
32 
33 namespace android {
34 
35 struct AudioSource : public StreamingSource {
36     using AudioDataRegionView = vsoc::audio_data::AudioDataRegionView;
37 
38     enum class Format {
39         OPUS,
40         G711_ALAW,
41         G711_ULAW,
42     };
43     // ADTS framing is only supported for AAC.
44     explicit AudioSource(Format format, bool useADTSFraming = false);
45 
46     AudioSource(const AudioSource &) = delete;
47     AudioSource &operator=(const AudioSource &) = delete;
48 
49     ~AudioSource() override;
50 
51     int32_t initCheck() const override;
52 
53     int32_t start() override;
54     int32_t stop() override;
55 
56     int32_t requestIDRFrame() override;
notifyNewStreamConsumerAudioSource57     void notifyNewStreamConsumer() override {}
notifyStreamConsumerDisconnectedAudioSource58     void notifyStreamConsumerDisconnected() override {}
59 
60     void inject(const void *data, size_t size);
61 
62 private:
63     enum State {
64         STOPPING,
65         STOPPED,
66         RUNNING,
67         PAUSED
68     };
69 
70     struct Encoder;
71     struct OPUSEncoder;
72     struct G711Encoder;
73 
74     int32_t mInitCheck;
75     State mState;
76     std::unique_ptr<Encoder> mEncoder;
77 
78     std::mutex mLock;
79     std::unique_ptr<std::thread> mThread;
80 
81 #if SIMULATE_AUDIO
82     static constexpr int32_t kSampleRate = 44100;
83     static constexpr int32_t kNumChannels = 2;
84     static constexpr size_t kNumFramesPerBuffer = 400;
85     static constexpr int32_t kFrequency = 500;
86     size_t mPhase;
87 #endif
88 };
89 
90 }  // namespace android
91 
92 
93