lionel.g.landwerlin | 33e90877 | 2015-09-17 14:41:22 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef PPAPI_PROXY_AUDIO_ENCODER_RESOURCE_H_ |
| 6 | #define PPAPI_PROXY_AUDIO_ENCODER_RESOURCE_H_ |
| 7 | |
avi | e029c413 | 2015-12-23 06:45:22 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
| 10 | #include "base/macros.h" |
lionel.g.landwerlin | 33e90877 | 2015-09-17 14:41:22 | [diff] [blame] | 11 | #include "base/memory/ref_counted.h" |
lionel.g.landwerlin | 33e90877 | 2015-09-17 14:41:22 | [diff] [blame] | 12 | #include "ppapi/proxy/connection.h" |
| 13 | #include "ppapi/proxy/plugin_resource.h" |
lionel.g.landwerlin | 764a8c9 | 2015-11-19 01:48:08 | [diff] [blame] | 14 | #include "ppapi/shared_impl/media_stream_buffer_manager.h" |
lionel.g.landwerlin | 33e90877 | 2015-09-17 14:41:22 | [diff] [blame] | 15 | #include "ppapi/shared_impl/resource.h" |
| 16 | #include "ppapi/thunk/ppb_audio_encoder_api.h" |
| 17 | |
| 18 | namespace ppapi { |
| 19 | |
| 20 | class TrackedCallback; |
| 21 | |
| 22 | namespace proxy { |
| 23 | |
lionel.g.landwerlin | 764a8c9 | 2015-11-19 01:48:08 | [diff] [blame] | 24 | class AudioBufferResource; |
lionel.g.landwerlin | 33e90877 | 2015-09-17 14:41:22 | [diff] [blame] | 25 | |
| 26 | class PPAPI_PROXY_EXPORT AudioEncoderResource |
| 27 | : public PluginResource, |
lionel.g.landwerlin | 764a8c9 | 2015-11-19 01:48:08 | [diff] [blame] | 28 | public thunk::PPB_AudioEncoder_API, |
| 29 | public ppapi::MediaStreamBufferManager::Delegate { |
lionel.g.landwerlin | 33e90877 | 2015-09-17 14:41:22 | [diff] [blame] | 30 | public: |
| 31 | AudioEncoderResource(Connection connection, PP_Instance instance); |
| 32 | ~AudioEncoderResource() override; |
| 33 | |
| 34 | thunk::PPB_AudioEncoder_API* AsPPB_AudioEncoder_API() override; |
| 35 | |
| 36 | private: |
lionel.g.landwerlin | 764a8c9 | 2015-11-19 01:48:08 | [diff] [blame] | 37 | // MediaStreamBufferManager::Delegate implementation. |
| 38 | void OnNewBufferEnqueued() override {} |
| 39 | |
| 40 | // PPB_AudioEncoder_API implementation. |
lionel.g.landwerlin | 33e90877 | 2015-09-17 14:41:22 | [diff] [blame] | 41 | int32_t GetSupportedProfiles( |
| 42 | const PP_ArrayOutput& output, |
| 43 | const scoped_refptr<TrackedCallback>& callback) override; |
| 44 | int32_t Initialize(uint32_t channels, |
| 45 | PP_AudioBuffer_SampleRate input_sample_rate, |
| 46 | PP_AudioBuffer_SampleSize input_sample_size, |
| 47 | PP_AudioProfile output_profile, |
| 48 | uint32_t initial_bitrate, |
| 49 | PP_HardwareAcceleration acceleration, |
| 50 | const scoped_refptr<TrackedCallback>& callback) override; |
| 51 | int32_t GetNumberOfSamples() override; |
| 52 | int32_t GetBuffer(PP_Resource* audio_buffer, |
| 53 | const scoped_refptr<TrackedCallback>& callback) override; |
| 54 | int32_t Encode(PP_Resource audio_buffer, |
| 55 | const scoped_refptr<TrackedCallback>& callback) override; |
| 56 | int32_t GetBitstreamBuffer( |
| 57 | PP_AudioBitstreamBuffer* bitstream_buffer, |
| 58 | const scoped_refptr<TrackedCallback>& callback) override; |
| 59 | void RecycleBitstreamBuffer( |
| 60 | const PP_AudioBitstreamBuffer* bitstream_buffer) override; |
| 61 | void RequestBitrateChange(uint32_t bitrate) override; |
| 62 | void Close() override; |
| 63 | |
lionel.g.landwerlin | 764a8c9 | 2015-11-19 01:48:08 | [diff] [blame] | 64 | // PluginResource implementation. |
| 65 | void OnReplyReceived(const ResourceMessageReplyParams& params, |
| 66 | const IPC::Message& msg) override; |
| 67 | |
| 68 | // Message handlers for the host's messages. |
| 69 | void OnPluginMsgGetSupportedProfilesReply( |
| 70 | const PP_ArrayOutput& output, |
| 71 | const ResourceMessageReplyParams& params, |
| 72 | const std::vector<PP_AudioProfileDescription>& profiles); |
| 73 | void OnPluginMsgInitializeReply(const ResourceMessageReplyParams& params, |
| 74 | int32_t number_of_samples, |
| 75 | int32_t audio_buffer_count, |
| 76 | int32_t audio_buffer_size, |
| 77 | int32_t bitstream_buffer_count, |
| 78 | int32_t bitstream_buffer_size); |
| 79 | void OnPluginMsgEncodeReply(const ResourceMessageReplyParams& params, |
| 80 | int32_t buffer_id); |
| 81 | void OnPluginMsgBitstreamBufferReady(const ResourceMessageReplyParams& params, |
| 82 | int32_t buffer_id); |
| 83 | void OnPluginMsgNotifyError(const ResourceMessageReplyParams& params, |
| 84 | int32_t error); |
| 85 | |
| 86 | // Internal utility functions. |
| 87 | void NotifyError(int32_t error); |
| 88 | void TryGetAudioBuffer(); |
| 89 | void TryWriteBitstreamBuffer(); |
| 90 | void ReleaseBuffers(); |
| 91 | |
| 92 | int32_t encoder_last_error_; |
| 93 | |
| 94 | bool initialized_; |
| 95 | |
| 96 | uint32_t number_of_samples_; |
| 97 | |
| 98 | using AudioBufferMap = |
| 99 | std::map<PP_Resource, scoped_refptr<AudioBufferResource>>; |
| 100 | AudioBufferMap audio_buffers_; |
| 101 | |
| 102 | scoped_refptr<TrackedCallback> get_supported_profiles_callback_; |
| 103 | scoped_refptr<TrackedCallback> initialize_callback_; |
| 104 | scoped_refptr<TrackedCallback> get_buffer_callback_; |
| 105 | PP_Resource* get_buffer_data_; |
| 106 | |
| 107 | using EncodeMap = std::map<int32_t, scoped_refptr<TrackedCallback>>; |
| 108 | EncodeMap encode_callbacks_; |
| 109 | |
| 110 | scoped_refptr<TrackedCallback> get_bitstream_buffer_callback_; |
| 111 | PP_AudioBitstreamBuffer* get_bitstream_buffer_data_; |
| 112 | |
| 113 | MediaStreamBufferManager audio_buffer_manager_; |
| 114 | MediaStreamBufferManager bitstream_buffer_manager_; |
| 115 | |
| 116 | // Map of bitstream buffer pointers to buffer ids. |
| 117 | using BufferMap = std::map<void*, int32_t>; |
| 118 | BufferMap bitstream_buffer_map_; |
| 119 | |
lionel.g.landwerlin | 33e90877 | 2015-09-17 14:41:22 | [diff] [blame] | 120 | DISALLOW_COPY_AND_ASSIGN(AudioEncoderResource); |
| 121 | }; |
| 122 | |
| 123 | } // namespace proxy |
| 124 | } // namespace ppapi |
| 125 | |
| 126 | #endif // PPAPI_PROXY_AUDIO_ENCODER_RESOURCE_H_ |