• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 AUDIO_SERVICE_CLIENT_H
17 #define AUDIO_SERVICE_CLIENT_H
18 
19 #include <algorithm>
20 #include <array>
21 #include <cstring>
22 #include <iostream>
23 #include <map>
24 #include <memory>
25 #include <mutex>
26 #include <queue>
27 #include <stdlib.h>
28 #include <thread>
29 #include <unistd.h>
30 #include <pulse/pulseaudio.h>
31 #include <pulse/thread-mainloop.h>
32 #include <audio_error.h>
33 #include <audio_info.h>
34 #include <audio_timer.h>
35 
36 #include "audio_capturer.h"
37 #include "audio_policy_manager.h"
38 #include "audio_renderer.h"
39 #include "audio_system_manager.h"
40 #include "event_handler.h"
41 #include "event_runner.h"
42 
43 namespace OHOS {
44 namespace AudioStandard {
45 enum ASClientType {
46     AUDIO_SERVICE_CLIENT_PLAYBACK,
47     AUDIO_SERVICE_CLIENT_RECORD,
48     AUDIO_SERVICE_CLIENT_CONTROLLER
49 };
50 
51 typedef pa_sink_input_info    SinkInputInfo;
52 typedef pa_source_output_info SourceOutputInfo;
53 typedef pa_sink_info          SinkDeviceInfo;
54 typedef pa_source_info        SourceDeviceInfo;
55 typedef pa_client_info        ClientInfo;
56 
57 struct StreamBuffer {
58     uint8_t *buffer; // the virtual address of stream
59     uint32_t bufferLen; // stream length in bytes
60 };
61 
62 struct AudioCache {
63     std::unique_ptr<uint8_t[]> buffer;
64     uint32_t readIndex;
65     uint32_t writeIndex;
66     uint32_t totalCacheSize;
67     bool isFull;
68 };
69 
70 /**
71  * @brief Enumerates the stream states of the current device.
72  *
73  * @since 1.0
74  * @version 1.0
75  */
76 enum State {
77     /** INVALID */
78     INVALID = -1,
79     /** New */
80     NEW,
81     /** Prepared */
82     PREPARED,
83     /** Running */
84     RUNNING,
85     /** Stopped */
86     STOPPED,
87     /** Released */
88     RELEASED,
89     /** Paused */
90     PAUSED
91 };
92 
93 class AudioStreamCallback {
94 public:
95     virtual ~AudioStreamCallback() = default;
96     /**
97      * Called when stream state is updated.
98      *
99      * @param state Indicates the InterruptEvent information needed by client.
100      * For details, refer InterruptEvent struct in audio_info.h
101      */
102     virtual void OnStateChange(const State state, const StateChangeCmdType cmdType = CMD_FROM_CLIENT) = 0;
103 };
104 
105 class AudioRendererCallbacks {
106 public:
107     virtual ~AudioRendererCallbacks();
108     virtual void OnSinkDeviceUpdatedCb() const = 0;
109     // Need to check required state changes to update applications
110     virtual void OnStreamStateChangeCb() const = 0;
111     virtual void OnStreamBufferUnderFlowCb() const = 0;
112     virtual void OnStreamBufferOverFlowCb() const = 0;
113     virtual void OnErrorCb(AudioServiceErrorCodes error) const = 0;
114     virtual void OnEventCb(AudioServiceEventTypes error) const = 0;
115 };
116 
117 class AudioCapturerCallbacks {
118 public:
119     virtual ~AudioCapturerCallbacks();
120     virtual void OnSourceDeviceUpdatedCb() const = 0;
121     // Need to check required state changes to update applications
122     virtual void OnStreamStateChangeCb() const = 0;
123     virtual void OnStreamBufferUnderFlowCb() const = 0;
124     virtual void OnStreamBufferOverFlowCb() const = 0;
125     virtual void OnErrorCb(AudioServiceErrorCodes error) const = 0;
126     virtual void OnEventCb(AudioServiceEventTypes error) const = 0;
127 };
128 
129 class AudioServiceClient : public AudioTimer, public AppExecFwk::EventHandler {
130 public:
131     static constexpr char PA_RUNTIME_DIR[] = "/data/data/.pulse_dir/runtime";
132     static constexpr char PA_STATE_DIR[] = "/data/data/.pulse_dir/state";
133     static constexpr char PA_HOME_DIR[] = "/data/data/.pulse_dir/state";
134 
135     AudioServiceClient();
136     virtual ~AudioServiceClient();
137 
138     /**
139     * Initializes audio service client for the required client type
140     *
141     * @param eClientType indicates the client type like playback, record or controller.
142     * @return Returns {@code 0} if success; returns {@code -1} otherwise.
143     */
144     int32_t Initialize(ASClientType eClientType);
145 
146     // Stream handling APIs
147 
148     /**
149     * Creates & initializes resources based on the audioParams and audioType
150     *
151     * @param audioParams indicate format, sampling rate and number of channels
152     * @param audioType indicate the stream type like music, system, ringtone etc
153     * @return Returns {@code 0} if success; returns {@code -1} otherwise.
154     */
155     int32_t CreateStream(AudioStreamParams audioParams, AudioStreamType audioType);
156 
157     /**
158      * @brief Obtains session ID .
159      *
160      * @return Returns unique session ID for the created session
161     */
162     int32_t GetSessionID(uint32_t &sessionID) const;
163 
164     /**
165     * Starts the stream created using CreateStream
166     *
167     * @return Returns {@code 0} if success; returns {@code -1} otherwise.
168     */
169     int32_t StartStream(StateChangeCmdType cmdType = CMD_FROM_CLIENT);
170 
171     /**
172     * Stops the stream created using CreateStream
173     *
174     * @return Returns {@code 0} if success; returns {@code -1} otherwise.
175     */
176     int32_t StopStream();
177 
178     /**
179     * Flushes the stream created using CreateStream. This is applicable for
180     * playback only
181     *
182     * @return Returns {@code 0} if success; returns {@code -1} otherwise.
183     */
184     int32_t FlushStream();
185 
186     /**
187     * Drains the stream created using CreateStream. This is applicable for
188     * playback only
189     *
190     * @return Returns {@code 0} if success; returns {@code -1} otherwise.
191     */
192     int32_t DrainStream();
193 
194     /**
195     * Pauses the stream
196     *
197     * @return Returns {@code 0} if success; returns {@code -1} otherwise.
198     */
199     int32_t PauseStream(StateChangeCmdType cmdType = CMD_FROM_CLIENT);
200 
201     /**
202     * Update the stream type
203     *
204     * @param audioStreamType Audio stream type
205     * @return Returns {@code 0} if success; returns {@code -1} otherwise.
206     */
207     int32_t SetStreamType(AudioStreamType audioStreamType);
208 
209     /**
210     * Sets the volume of the stream associated with session ID
211     *
212     * @param sessionID indicates the ID for the active stream to be controlled
213     * @param volume indicates volume level between 0 to 65536
214     * @return Returns {@code 0} if success; returns {@code -1} otherwise.
215     */
216     int32_t SetStreamVolume(uint32_t sessionID, uint32_t volume);
217 
218     /**
219     * Get the volume of the stream associated with session ID
220     *
221     * @param sessionID indicates the ID for the active stream to be controlled
222     * @return returns volume level between 0 to 65536
223     */
224     uint32_t GetStreamVolume(uint32_t sessionID);
225 
226     /**
227     * Writes audio data of the stream created using CreateStream to active sink device
228     *
229     * @param buffer contains audio data to write
230     * @param bufferSize indicates the size of audio data in bytes to write from the buffer
231     * @param pError indicates pointer to error which will be filled in case of internal errors
232     * @return returns size of audio data written in bytes.
233     */
234     size_t WriteStream(const StreamBuffer &stream, int32_t &pError);
235     int32_t RenderPrebuf(uint32_t writeLen);
236 
237     /**
238     * Writes audio data of the stream created using CreateStream to active sink device
239     Used when render mode is RENDER_MODE_CALLBACK
240     *
241     * @param buffer contains audio data to write
242     * @param bufferSize indicates the size of audio data in bytes to write from the buffer
243     * @param pError indicates pointer to error which will be filled in case of internal errors
244     * @return returns size of audio data written in bytes.
245     */
246     size_t WriteStreamInCb(const StreamBuffer &stream, int32_t &pError);
247 
248     /**
249     * Reads audio data of the stream created using CreateStream from active source device
250     *
251     * @param StreamBuffer including buffer to be filled with audio data
252     * and bufferSize indicating the size of audio data to read into buffer
253     * @param isBlocking indicates if the read is blocking or not
254     * @return Returns size read if success; returns {@code -1} failure.
255     */
256     int32_t ReadStream(StreamBuffer &stream, bool isBlocking);
257 
258     /**
259     * Release the resources allocated using CreateStream
260     *
261     * @return Returns {@code 0} if success; returns {@code -1} otherwise.
262     */
263     int32_t ReleaseStream(bool releaseRunner = true);
264 
265     /**
266     * Provides the current timestamp for playback/record stream created using CreateStream
267     *
268     * @param timeStamp will be filled up with current timestamp
269     * @return Returns {@code 0} if success; returns {@code -1} otherwise.
270     */
271     int32_t GetCurrentTimeStamp(uint64_t &timeStamp);
272 
273     /**
274     * Provides the current latency for playback/record stream created using CreateStream
275     *
276     * @param latency will be filled up with the current latency in microseconds
277     * @return Returns {@code 0} if success; returns {@code -1} otherwise.
278     */
279     int32_t GetAudioLatency(uint64_t &latency);
280 
281     /**
282     * Provides the playback/record stream parameters created using CreateStream
283     *
284     * @param audioParams will be filled up with stream audio parameters
285     * @return Returns {@code 0} if success; returns {@code -1} otherwise.
286     */
287     int32_t GetAudioStreamParams(AudioStreamParams &audioParams) const;
288 
289     /**
290     * Provides the minimum buffer size required for this audio stream
291     * created using CreateStream
292     * @param minBufferSize will be set to minimum buffer size in bytes
293     * @return Returns {@code 0} if success; returns {@code -1} otherwise.
294     */
295     int32_t GetMinimumBufferSize(size_t &minBufferSize) const;
296 
297     /**
298     * Provides the minimum frame count required for this audio stream
299     * created using CreateStream
300     * @param frameCount will be set to minimum number of frames
301     * @return Returns {@code 0} if success; returns {@code -1} otherwise.
302     */
303     int32_t GetMinimumFrameCount(uint32_t &frameCount) const;
304 
305     /**
306     * Provides the sampling rate for the active audio stream
307     * created using CreateStream
308     *
309     * @return Returns sampling rate in Hz
310     */
311     uint32_t GetSamplingRate() const;
312 
313     /**
314     * Provides the channel count for the active audio stream
315     * created using CreateStream
316     *
317     * @return Returns number of channels
318     */
319     uint8_t GetChannelCount() const;
320 
321     /**
322     * Provides the sample size for the active audio stream
323     * created using CreateStream
324     *
325     * @return Returns sample size in number of bits
326     */
327     uint8_t GetSampleSize() const;
328 
329     // Device volume & route handling APIs
330 
331     // Audio stream callbacks
332 
333     /**
334     * Register for callbacks associated with the playback stream created using CreateStream
335     *
336     * @param cb indicates pointer for registered callbacks
337     * @return none
338     */
339     void RegisterAudioRendererCallbacks(const AudioRendererCallbacks &cb);
340 
341     /**
342     * Register for callbacks associated with the record stream created using CreateStream
343     *
344     * @param cb indicates pointer for registered callbacks
345     * @return none
346     */
347     void RegisterAudioCapturerCallbacks(const AudioCapturerCallbacks &cb);
348 
349     /**
350     * Set the renderer frame position callback
351     *
352     * @param callback indicates pointer for registered callbacks
353     * @return none
354     */
355     void SetRendererPositionCallback(int64_t markPosition, const std::shared_ptr<RendererPositionCallback> &callback);
356 
357     /**
358     * Unset the renderer frame position callback
359     *
360     * @return none
361     */
362     void UnsetRendererPositionCallback();
363 
364     /**
365     * Set the renderer frame period position callback
366     *
367     * @param callback indicates pointer for registered callbacks
368     * @return none
369     */
370     void SetRendererPeriodPositionCallback(int64_t markPosition,
371         const std::shared_ptr<RendererPeriodPositionCallback> &callback);
372 
373     /**
374     * Unset the renderer frame period position callback
375     *
376     * @return none
377     */
378     void UnsetRendererPeriodPositionCallback();
379 
380     /**
381     * Set the capturer frame position callback
382     *
383     * @param callback indicates pointer for registered callbacks
384     * @return none
385     */
386     void SetCapturerPositionCallback(int64_t markPosition, const std::shared_ptr<CapturerPositionCallback> &callback);
387 
388     /**
389     * Unset the capturer frame position callback
390     *
391     * @return none
392     */
393     void UnsetCapturerPositionCallback();
394 
395     /**
396     * Set the capturer frame period position callback
397     *
398     * @param callback indicates pointer for registered callbacks
399     * @return none
400     */
401     void SetCapturerPeriodPositionCallback(int64_t markPosition,
402         const std::shared_ptr<CapturerPeriodPositionCallback> &callback);
403 
404     /**
405     * Unset the capturer frame period position callback
406     *
407     * @return none
408     */
409     void UnsetCapturerPeriodPositionCallback();
410 
411     /**
412      * @brief Set the track volume
413      *
414      * @param volume The volume to be set for the current track.
415      * @return Returns {@link SUCCESS} if volume is successfully set; returns an error code
416      * defined in {@link audio_errors.h} otherwise.
417      */
418     int32_t SetStreamVolume(float volume);
419 
420     /**
421      * @brief Obtains the current track volume
422      *
423      * @return Returns current track volume
424      */
425     float GetStreamVolume();
426 
427     /**
428      * @brief Set the render rate
429      *
430      * @param renderRate The rate at which the stream needs to be rendered.
431      * @return Returns {@link SUCCESS} if render rate is successfully set; returns an error code
432      * defined in {@link audio_errors.h} otherwise.
433      */
434     int32_t SetStreamRenderRate(AudioRendererRate renderRate);
435 
436     /**
437      * @brief Obtains the current render rate
438      *
439      * @return Returns current render rate
440      */
441     AudioRendererRate GetStreamRenderRate();
442 
443     /**
444      * @brief Set the buffer duration in msec
445      *
446      * @param bufferSizeInMsec buffer size in duration.
447      * @return Returns {@link SUCCESS} defined in {@link audio_errors.h} otherwise.
448      */
449     int32_t SetBufferSizeInMsec(int32_t bufferSizeInMsec);
450 
451     /**
452      * @brief Saves StreamCallback
453      *
454      * @param callback callback reference to be saved.
455      * @return none.
456      */
457 
458     void SaveStreamCallback(const std::weak_ptr<AudioStreamCallback> &callback);
459 
460     /**
461      * @brief Sets the render mode. By default the mode is RENDER_MODE_NORMAL.
462      * This API is needs to be used only if RENDER_MODE_CALLBACK is required.
463      *
464      * * @param renderMode The mode of render.
465      * @return  Returns {@link SUCCESS} if render mode is successfully set; returns an error code
466      * defined in {@link audio_errors.h} otherwise.
467      */
468     int32_t SetAudioRenderMode(AudioRenderMode renderMode);
469 
470     /**
471      * @brief Obtains the render mode.
472      *
473      * @return  Returns current render mode.
474      */
475     AudioRenderMode GetAudioRenderMode();
476 
477     /**
478      * @brief Registers the renderer write callback listener.
479      * This API should only be used if RENDER_MODE_CALLBACK is needed.
480      *
481      * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code
482      * defined in {@link audio_errors.h} otherwise.
483      */
484     int32_t SaveWriteCallback(const std::weak_ptr<AudioRendererWriteCallback> &callback);
485     int32_t SetAudioCaptureMode(AudioCaptureMode captureMode);
486     int32_t SaveReadCallback(const std::weak_ptr<AudioCapturerReadCallback> &callback);
487     AudioCaptureMode GetAudioCaptureMode();
488     /**
489      * @brief Set the applicationcache path to access the application resources
490      *
491      * @return none
492      */
493     void SetApplicationCachePath(const std::string cachePath);
494 
495     /**
496      * @brief Verifies the clients permsiion based on appTokenId
497      *
498      * @return Returns whether the authentication was success or not
499      */
500     bool VerifyClientPermission(const std::string &permissionName, uint32_t appTokenId, int32_t appUid,
501         bool privacyFlag, AudioPermissionState state);
502     bool getUsingPemissionFromPrivacy(const std::string &permissionName, uint32_t appTokenId,
503         AudioPermissionState state);
504     int32_t SetStreamLowPowerVolume(float powerVolumeFactor);
505     float GetStreamLowPowerVolume();
506     float GetSingleStreamVol();
507 
508     // Audio timer callback
509     virtual void OnTimeOut() override;
510 
511     void SetClientID(int32_t clientPid, int32_t clientUid);
512     void SendWriteBufferRequestEvent();
513     void HandleWriteRequestEvent();
514     int32_t SetRendererWriteCallback(const std::shared_ptr<AudioRendererWriteCallback> &callback);
515 
516 protected:
517     virtual void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override;
518 private:
519     pa_threaded_mainloop *mainLoop;
520     pa_mainloop_api *api;
521     pa_context *context;
522     pa_stream *paStream;
523     pa_sample_spec sampleSpec;
524 
525     std::mutex dataMutex;
526     std::mutex ctrlMutex;
527     std::mutex capturerMarkReachedMutex_;
528     std::mutex capturerPeriodReachedMutex_;
529     std::mutex rendererMarkReachedMutex_;
530     std::mutex rendererPeriodReachedMutex_;
531     std::mutex runnerMutex_;
532     std::mutex writeCallbackMutex_;
533     bool runnerReleased_ = false;
534 
535     AudioCache acache;
536     const void *internalReadBuffer;
537     size_t internalRdBufLen;
538     size_t internalRdBufIndex;
539     size_t setBufferSize;
540     int32_t streamCmdStatus;
541     int32_t streamDrainStatus;
542     int32_t streamFlushStatus;
543     bool isMainLoopStarted;
544     bool isContextConnected;
545     bool isStreamConnected;
546 
547     std::unique_ptr<uint8_t[]> preBuf_ {nullptr};
548     uint32_t sinkLatencyInMsec_ {0};
549 
550     int32_t clientPid_ = 0;
551     int32_t clientUid_ = 0;
552 
553     std::string appCookiePath = "";
554     std::string cachePath_ = "";
555 
556     float mVolumeFactor;
557     float mPowerVolumeFactor;
558     bool mUnMute_;
559     AudioStreamType mStreamType;
560     AudioSystemManager *mAudioSystemMgr;
561 
562     pa_cvolume cvolume;
563     uint32_t streamIndex;
564     uint32_t sessionID;
565     uint32_t volumeChannels;
566     bool streamInfoUpdated;
567 
568     AudioRendererRate renderRate;
569     AudioRenderMode renderMode_;
570     AudioCaptureMode captureMode_;
571     std::weak_ptr<AudioCapturerReadCallback> readCallback_;
572     std::shared_ptr<AudioRendererWriteCallback> writeCallback_;
573     int64_t mWriteCbStamp = 0; // used to measure callback duration
574     uint32_t mFrameSize = 0;
575     bool mMarkReached = false;
576     uint64_t mFrameMarkPosition = 0;
577     uint64_t mFramePeriodNumber = 0;
578 
579     uint64_t mTotalBytesWritten = 0;
580     uint64_t mFramePeriodWritten = 0;
581     std::shared_ptr<RendererPositionCallback> mRenderPositionCb;
582     std::shared_ptr<RendererPeriodPositionCallback> mRenderPeriodPositionCb;
583 
584     uint64_t mTotalBytesRead = 0;
585     uint64_t mFramePeriodRead = 0;
586     std::shared_ptr<CapturerPositionCallback> mCapturePositionCb;
587     std::shared_ptr<CapturerPeriodPositionCallback> mCapturePeriodPositionCb;
588 
589     std::vector<std::unique_ptr<std::thread>> mPositionCBThreads;
590     std::vector<std::unique_ptr<std::thread>> mPeriodPositionCBThreads;
591 
592     std::weak_ptr<AudioStreamCallback> streamCallback_;
593     State state_;
594     StateChangeCmdType stateChangeCmdType_ = CMD_FROM_CLIENT;
595     pa_stream_success_cb_t PAStreamCorkSuccessCb;
596 
597     // To be set while using audio stream
598     // functionality for callbacks
599     AudioRendererCallbacks *mAudioRendererCallbacks;
600     AudioCapturerCallbacks *mAudioCapturerCallbacks;
601 
602     std::map<uint32_t, SinkDeviceInfo*> sinkDevices;
603     std::map<uint32_t, SourceDeviceInfo*> sourceDevices;
604     std::map<uint32_t, SinkInputInfo*> sinkInputs;
605     std::map<uint32_t, SourceOutputInfo*> sourceOutputs;
606     std::map<uint32_t, ClientInfo*> clientInfo;
607 
608     ASClientType eAudioClientType;
609 
610     uint32_t underFlowCount;
611     int32_t ConnectStreamToPA();
612 
613     // Audio cache related functions. These APIs are applicable only for playback scenarios
614     int32_t InitializeAudioCache();
615     size_t WriteToAudioCache(const StreamBuffer &stream);
616     int32_t DrainAudioCache();
617 
618     int32_t UpdateReadBuffer(uint8_t *buffer, size_t &length, size_t &readSize);
619     int32_t PaWriteStream(const uint8_t *buffer, size_t &length);
620     void HandleRenderPositionCallbacks(size_t bytesWritten);
621     void HandleCapturePositionCallbacks(size_t bytesRead);
622 
623     void WriteStateChangedSysEvents();
624 
625     // Error code used
626     static const int32_t AUDIO_CLIENT_SUCCESS = 0;
627     static const int32_t AUDIO_CLIENT_ERR = -1;
628     static const int32_t AUDIO_CLIENT_INVALID_PARAMS_ERR = -2;
629     static const int32_t AUDIO_CLIENT_INIT_ERR = -3;
630     static const int32_t AUDIO_CLIENT_CREATE_STREAM_ERR = -4;
631     static const int32_t AUDIO_CLIENT_START_STREAM_ERR = -5;
632     static const int32_t AUDIO_CLIENT_READ_STREAM_ERR = -6;
633     static const int32_t AUDIO_CLIENT_WRITE_STREAM_ERR = -7;
634     static const int32_t AUDIO_CLIENT_PA_ERR = -8;
635     static const int32_t AUDIO_CLIENT_PERMISSION_ERR = -9;
636 
637     // Default values
638     static const uint32_t MINIMUM_BUFFER_SIZE = 1024;
639     static const uint32_t DEFAULT_SAMPLING_RATE = 44100;
640     static const uint8_t DEFAULT_CHANNEL_COUNT = 2;
641     static const uint8_t DEFAULT_SAMPLE_SIZE = 2;
642     static const uint32_t DEFAULT_STREAM_VOLUME = 65536;
643     static const std::string GetStreamName(AudioStreamType audioType);
644     static pa_sample_spec ConvertToPAAudioParams(AudioStreamParams audioParams);
645     static AudioStreamParams ConvertFromPAAudioParams(pa_sample_spec paSampleSpec);
646 
647     static constexpr float MAX_STREAM_VOLUME_LEVEL = 1.0f;
648     static constexpr float MIN_STREAM_VOLUME_LEVEL = 0.0f;
649 
650     // audio channel index
651     static const uint8_t CHANNEL1_IDX = 0;
652     static const uint8_t CHANNEL2_IDX = 1;
653     static const uint8_t CHANNEL3_IDX = 2;
654     static const uint8_t CHANNEL4_IDX = 3;
655     static const uint8_t CHANNEL5_IDX = 4;
656     static const uint8_t CHANNEL6_IDX = 5;
657     static const uint8_t CHANNEL7_IDX = 6;
658     static const uint8_t CHANNEL8_IDX = 7;
659 
660     // Resets PA audio client and free up resources if any with this API
661     void ResetPAAudioClient();
662     // For setting some environment variables required while running from hap
663     void SetEnv();
664     int32_t CorkStream();
665 
666     // Callbacks to be implemented
667     static void PAStreamStateCb(pa_stream *stream, void *userdata);
668     static void PAStreamMovedCb(pa_stream *stream, void *userdata);
669     static void PAStreamUnderFlowCb(pa_stream *stream, void *userdata);
670     static void PAContextStateCb(pa_context *context, void *userdata);
671     static void PAStreamReadCb(pa_stream *stream, size_t length, void *userdata);
672     static void PAStreamStartSuccessCb(pa_stream *stream, int32_t success, void *userdata);
673     static void PAStreamStopSuccessCb(pa_stream *stream, int32_t success, void *userdata);
674     static void PAStreamPauseSuccessCb(pa_stream *stream, int32_t success, void *userdata);
675     static void PAStreamWriteCb(pa_stream *stream, size_t length, void *userdata);
676     static void PAStreamDrainSuccessCb(pa_stream *stream, int32_t success, void *userdata);
677     static void PAStreamFlushSuccessCb(pa_stream *stream, int32_t success, void *userdata);
678     static void PAStreamLatencyUpdateCb(pa_stream *stream, void *userdata);
679     static void PAStreamSetBufAttrSuccessCb(pa_stream *stream, int32_t success, void *userdata);
680 
681     static void GetSinkInputInfoCb(pa_context *c, const pa_sink_input_info *i, int eol, void *userdata);
682     static void SetPaVolume(const AudioServiceClient &client);
683 
684     // OnRenderMarkReach SetRenderMarkReached UnsetRenderMarkReach  by eventHandler
685     void SendRenderMarkReachedRequestEvent(uint64_t mFrameMarkPosition);
686     void HandleRenderMarkReachedEvent(uint64_t mFrameMarkPosition);
687     void SendSetRenderMarkReachedRequestEvent(const std::shared_ptr<RendererPositionCallback> &callback);
688     void HandleSetRenderMarkReachedEvent(const std::shared_ptr<RendererPositionCallback> &callback);
689     void SendUnsetRenderMarkReachedRequestEvent();
690     void HandleUnsetRenderMarkReachedEvent();
691 
692     // OnRenderPeriodReach SetRenderPeriodReach UnsetRenderPeriodReach by eventHandler
693     void SendRenderPeriodReachedRequestEvent(uint64_t mFramePeriodNumber);
694     void HandleRenderPeriodReachedEvent(uint64_t mFramePeriodNumber);
695     void SendSetRenderPeriodReachedRequestEvent(const std::shared_ptr<RendererPeriodPositionCallback> &callback);
696     void HandleSetRenderPeriodReachedEvent(const std::shared_ptr<RendererPeriodPositionCallback> &callback);
697     void SendUnsetRenderPeriodReachedRequestEvent();
698     void HandleUnsetRenderPeriodReachedEvent();
699 
700     // OnCapturerMarkReach SetCapturerMarkReach UnsetCapturerMarkReach by eventHandler
701     void SendCapturerMarkReachedRequestEvent(uint64_t mFrameMarkPosition);
702     void HandleCapturerMarkReachedEvent(uint64_t mFrameMarkPosition);
703     void SendSetCapturerMarkReachedRequestEvent(const std::shared_ptr<CapturerPositionCallback> &callback);
704     void HandleSetCapturerMarkReachedEvent(const std::shared_ptr<CapturerPositionCallback> &callback);
705     void SendUnsetCapturerMarkReachedRequestEvent();
706     void HandleUnsetCapturerMarkReachedEvent();
707 
708     // OnCapturerPeriodReach SetCapturerPeriodReach UnsetCapturerPeriodReach by eventHandler
709     void SendCapturerPeriodReachedRequestEvent(uint64_t mFramePeriodNumber);
710     void HandleCapturerPeriodReachedEvent(uint64_t mFramePeriodNumber);
711     void SendSetCapturerPeriodReachedRequestEvent(
712         const std::shared_ptr<CapturerPeriodPositionCallback> &callback);
713     void HandleSetCapturerPeriodReachedEvent(
714         const std::shared_ptr<CapturerPeriodPositionCallback> &callback);
715     void SendUnsetCapturerPeriodReachedRequestEvent();
716     void HandleUnsetCapturerPeriodReachedEvent();
717 
718     enum {
719         WRITE_BUFFER_REQUEST = 0,
720 
721         RENDERER_MARK_REACHED_REQUEST = 1,
722         SET_RENDERER_MARK_REACHED_REQUEST = 2,
723         UNSET_RENDERER_MARK_REACHED_REQUEST = 3,
724 
725         RENDERER_PERIOD_REACHED_REQUEST = 4,
726         SET_RENDERER_PERIOD_REACHED_REQUEST = 5,
727         UNSET_RENDERER_PERIOD_REACHED_REQUEST = 6,
728 
729         CAPTURER_PERIOD_REACHED_REQUEST = 7,
730         SET_CAPTURER_PERIOD_REACHED_REQUEST = 8,
731         UNSET_CAPTURER_PERIOD_REACHED_REQUEST = 9,
732 
733         CAPTURER_MARK_REACHED_REQUEST = 10,
734         SET_CAPTURER_MARK_REACHED_REQUEST = 11,
735         UNSET_CAPTURER_MARK_REACHED_REQUEST = 12,
736     };
737 };
738 } // namespace AudioStandard
739 } // namespace OHOS
740 #endif // AUDIO_SERVICE_CLIENT_H
741