1 /*
2  * Copyright (c) 2022-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef WAV_DEMUXER_PLUGIN_H
17 #define WAV_DEMUXER_PLUGIN_H
18 
19 #include <memory>
20 #include <string>
21 #include <vector>
22 #include "plugin/interface/demuxer_plugin.h"
23 
24 namespace OHOS {
25 namespace Media {
26 namespace Plugin {
27 namespace WavPlugin {
28 struct WavHeadAttr {
29     uint8_t  chunkID[4];     // 4 byte RIFF Chunk descriptor
30     uint32_t chunkSize;      // 4 byte RIFF Chunk Size
31     uint8_t  format[4];      // 4 byte The format concern here is "WAVE",
32     // which requires two or three sub-chunks:"fmt", "fact"(optional),and "data"
33 
34     uint8_t  subChunk1ID[4]; // 4 byte The "fmt" sub-chunk describes the sound information in the data sub-chunk
35     uint32_t subChunk1Size;  // 4 byte Size of the fmt chunk  16 or 18  18 stand for has auxiliary information
36     uint16_t audioFormat;    // 2 byte Audio format 1=PCM,6=mulaw,7=alaw, 257=IBM Mu-Law, 258=IBM A-Law, 259=ADPCM
37     uint16_t numChannels;    // 2 byte Number of channels 1=Mono 2=Sterio
38     uint32_t sampleRate;     // 4 byte Sampling Frequency in Hz
39     uint32_t byteRate;       // 4 byte bytes per second
40     uint16_t blockAlign;     // 2 byte 2=16-bit mono, 4=16-bit stereo
41     uint16_t bitsPerSample;  // 2 byte Number of bits per sample
42 
43     uint8_t  subChunk2ID[4]; // 4 byte (optional) The "fact" sub-chunk exists in non-PCM format
44     uint32_t subChunk2Size;  // 4 byte (optional)
45     uint32_t dataFactSize;   // 4 byte (optional)
46 
47     uint8_t  subChunk3ID[4]; // 4 byte The "data" sub-chunk indicates the size of the sound information
48     uint32_t subChunk3Size;  // 4 byte Sampled data length
49 }; // 根据wav协议构建的结构体,轻易勿动
50 
51 class WavDemuxerPlugin : public DemuxerPlugin {
52 public:
53     explicit WavDemuxerPlugin(std::string name);
54     ~WavDemuxerPlugin() override;
55 
56     Status SetDataSource(const std::shared_ptr<DataSource>& source) override;
57     Status GetMediaInfo(MediaInfo& mediaInfo) override;
58     Status ReadFrame(Buffer& outBuffer, int32_t timeOutMs) override;
59     Status SeekTo(int32_t trackId, int64_t seekTime, SeekMode mode, int64_t& realSeekTime) override;
60     Status Reset() override;
61     Status GetParameter(Tag tag, ValueType &value) override;
62     Status SetParameter(Tag tag, const ValueType &value) override;
63     std::shared_ptr<Allocator> GetAllocator() override;
64     Status SetCallback(Callback* cb) override;
65     size_t GetTrackCount() override;
66     Status SelectTrack(int32_t trackId) override;
67     Status UnselectTrack(int32_t trackId) override;
68     Status GetSelectedTracks(std::vector<int32_t>& trackIds) override;
69     Status GetDataFromSource();
70 private:
71     struct IOContext {
72         std::shared_ptr<DataSource> dataSource {nullptr};
73         int64_t offset {0};
74         bool eos {false};
75     };
76     uint64_t            fileSize_;
77     IOContext           ioContext_;
78     uint32_t            dataOffset_;
79     Seekable            seekable_;
80     uint32_t            wavHeadLength_;
81     WavHeadAttr         wavHeader_ {};
82 };
83 } // namespace WavPlugin
84 } // namespace Plugin
85 } // namespace Media
86 } // namespace OHOS
87 
88 #endif // WAV_DEMUXER_PLUGIN_H
89