• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
23 #include "core/plugin_register.h"
24 #include "plugin/interface/demuxer_plugin.h"
25 #include "foundation/osal/thread/mutex.h"
26 
27 namespace OHOS {
28 namespace Media {
29 namespace Plugin {
30 namespace WavPlugin {
31 struct WavHeadAttr {
32     uint8_t  chunkID[4];     // 4 byte RIFF Chunk descriptor
33     uint32_t chunkSize;      // 4 byte RIFF Chunk Size
34     uint8_t  format[4];      // 4 byte The format concern here is "WAVE",
35     // which requires two or three sub-chunks:"fmt", "fact"(optional),and "data"
36 
37     uint8_t  subChunk1ID[4]; // 4 byte The "fmt" sub-chunk describes the sound information in the data sub-chunk
38     uint32_t subChunk1Size;  // 4 byte Size of the fmt chunk  16 or 18  18 stand for has auxiliary information
39     uint16_t audioFormat;    // 2 byte Audio format 1=PCM,6=mulaw,7=alaw, 257=IBM Mu-Law, 258=IBM A-Law, 259=ADPCM
40     uint16_t numChannels;    // 2 byte Number of channels 1=Mono 2=Sterio
41     uint32_t sampleRate;     // 4 byte Sampling Frequency in Hz
42     uint32_t byteRate;       // 4 byte bytes per second
43     uint16_t blockAlign;     // 2 byte 2=16-bit mono, 4=16-bit stereo
44     uint16_t bitsPerSample;  // 2 byte Number of bits per sample
45 
46     uint8_t  subChunk2ID[4]; // 4 byte (optional) The "fact" sub-chunk exists in non-PCM format
47     uint32_t subChunk2Size;  // 4 byte (optional)
48     uint32_t dataFactSize;   // 4 byte (optional)
49 
50     uint8_t  subChunk3ID[4]; // 4 byte The "data" sub-chunk indicates the size of the sound information
51     uint32_t subChunk3Size;  // 4 byte Sampled data length
52 }; // 根据wav协议构建的结构体,轻易勿动
53 
54 class WavDemuxerPlugin : public DemuxerPlugin {
55 public:
56     explicit WavDemuxerPlugin(std::string name);
57     ~WavDemuxerPlugin() override;
58 
59     Status SetDataSource(const std::shared_ptr<DataSource>& source) override;
60     Status GetMediaInfo(MediaInfo& mediaInfo) override;
61     Status ReadFrame(Buffer& outBuffer, int32_t timeOutMs) override;
62     Status SeekTo(int32_t trackId, int64_t hstTime, SeekMode mode) override;
63     Status Reset() override;
64     Status GetParameter(Tag tag, ValueType &value) override;
65     Status SetParameter(Tag tag, const ValueType &value) override;
66     std::shared_ptr<Allocator> GetAllocator() override;
67     Status SetCallback(Callback* cb) override;
68     size_t GetTrackCount() override;
69     Status SelectTrack(int32_t trackId) override;
70     Status UnselectTrack(int32_t trackId) override;
71     Status GetSelectedTracks(std::vector<int32_t>& trackIds) override;
72     Status GetDataFromSource();
73 private:
74     struct IOContext {
75         std::shared_ptr<DataSource> dataSource {nullptr};
76         int64_t offset {0};
77         bool eos {false};
78     };
79     size_t              fileSize_;
80     IOContext           ioContext_;
81     uint32_t            dataOffset_;
82     Seekable            seekable_;
83     uint32_t            wavHeadLength_;
84     WavHeadAttr         wavHeader_ {};
85 };
86 } // namespace WavPlugin
87 } // namespace Plugin
88 } // namespace Media
89 } // namespace OHOS
90 
91 #endif // WAV_DEMUXER_PLUGIN_H
92