• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 OHOS_SHARING_AUDIO_SINK_H
17 #define OHOS_SHARING_AUDIO_SINK_H
18 
19 #include <condition_variable>
20 #include <memory>
21 #include <mutex>
22 #include <cstdint>
23 #include <thread>
24 #include "audio_renderer.h"
25 
26 namespace OHOS {
27 namespace Sharing {
28 
29 enum PlayerMessageType {
30     MEDIA_PLAYER_INFO_UNKNOWN,
31 };
32 
33 enum PlayOnInfoType { INFO_TYPE_STATE_CHANGE, INFO_TYPE_VOLUME_CHANGE, INFO_TYPE_RESOLUTION_CHANGE };
34 
35 enum PlayerState {
36     PLAYER_STATE_ERROR,
37     PLAYER_STATE_IDLE,
38     PLAYER_STATE_INITIALIZED,
39     PLAYER_STATE_PREPARING,
40     PLAYER_STATE_PREPARED,
41     PLAYER_STATE_STARTED,
42     PLAYER_STATE_PAUSED,
43     PLAYER_STATE_STOPPED,
44     PLAYER_STATE_COMPLETE
45 };
46 
47 enum PlayerErrorType {
48     PLAYER_SUCCESS = 0,
49     PLAYER_ERROR_UNKNOWN,
50     PLAYER_ERROR_INVALID_STATE,
51     PLAYER_ERROR_INVALID_PARAMS,
52     PLAYER_ERROR_EMPTY_INSTANCE
53 };
54 
55 class AudioSink {
56 public:
57     AudioSink(uint32_t playerId);
58     virtual ~AudioSink();
59 
60     int32_t Prepare();
61     int32_t Prepare(int32_t channels, int32_t sampleRate);
62     int32_t Stop();
63     int32_t Start();
64     int32_t Pause();
65     int32_t Drain();
66     int32_t Flush();
67     int32_t Release();
68     void RendererThread();
69 
70     int32_t SetVolume(float volume);
71     int32_t SetParameters(int32_t bitsPerSample, int32_t channels, int32_t sampleRate);
72 
73     int32_t GetVolume(float &volume);
74     int32_t GetBufferSize(int32_t &bufferSize);
75     int32_t GetParameters(int32_t &bitsPerSample, int32_t &channels, int32_t &sampleRate);
76 
77     int32_t Write(uint8_t *buffer, size_t size);
78 
79 private:
80     bool running_ = false;
81     uint32_t playerId_ = -1;
82 
83     std::mutex mutex_;
84     std::unique_ptr<OHOS::AudioStandard::AudioRenderer> audioRenderer_ = nullptr;
85 };
86 
87 } // namespace Sharing
88 } // namespace OHOS
89 #endif