1 /* 2 * Copyright (C) 2021 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 IAVMETADATAHELPER_SERVICE_H 17 #define IAVMETADATAHELPER_SERVICE_H 18 19 #include "avmetadatahelper.h" 20 #include "buffer/avsharedmemory.h" 21 22 namespace OHOS { 23 namespace Media { 24 struct OutputFrame { 25 public: OutputFrameOutputFrame26 OutputFrame(int32_t width, int32_t height, int32_t stride, int32_t bytesPerPixel) 27 : width_(width), 28 height_(height), 29 stride_(stride), 30 bytesPerPixel_(bytesPerPixel), 31 size_(stride_ * height) // interleaved layout 32 { 33 } 34 GetFlattenedSizeOutputFrame35 int32_t GetFlattenedSize() const 36 { 37 return sizeof(OutputFrame) + size_; 38 } 39 GetFlattenedDataOutputFrame40 uint8_t *GetFlattenedData() const 41 { 42 return const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(this)) + sizeof(OutputFrame); 43 } 44 45 int32_t width_; 46 int32_t height_; 47 int32_t stride_; // interleaved layout 48 int32_t bytesPerPixel_; 49 int32_t size_; 50 }; 51 52 struct OutputConfiguration { 53 int32_t dstWidth = -1; 54 int32_t dstHeight = -1; 55 PixelFormat colorFormat = PixelFormat::RGB_565; 56 }; 57 58 class IAVMetadataHelperService { 59 public: 60 virtual ~IAVMetadataHelperService() = default; 61 62 /** 63 * @brief Method to set helper callback. 64 * 65 * @param callback object pointer. 66 * @return Returns {@link MSERR_OK} if the helpercallback is set; returns an error code defined 67 * in {@link media_errors.h} otherwise. 68 */ 69 virtual int32_t SetHelperCallback(const std::shared_ptr<HelperCallback> &callback) = 0; 70 71 /** 72 * Set the media source uri to use. Calling this method before the reset 73 * of the methods in this class. This method maybe time consuming. 74 * @param uri the URI of input media source. 75 * @param usage indicates which scene the avmedatahelper's instance will 76 * be used to, see {@link AVMetadataUsage}. If the usage need to be changed, 77 * this method must be called again. 78 * @return Returns {@link MSERR_OK} if the setting is successful; returns 79 * an error code otherwise. 80 */ 81 virtual int32_t SetSource(const std::string &uri, int32_t usage) = 0; 82 83 /** 84 * @brief Sets the media file descriptor source to resolve. Calling this method 85 * before the reset of the methods in this class. This method maybe time consuming. 86 * @param fd Indicates the file descriptor of media source. 87 * @param offset Indicates the offset of media source in file descriptor. 88 * @param size Indicates the size of media source. 89 * @param usage Indicates which scene the avmedatahelper's instance will 90 * be used to, see {@link AVMetadataUsage}. If the usage need to be changed, 91 * this method must be called again. 92 * @return Returns {@link MSERR_OK} if the setting is successful; returns 93 * an error code otherwise. 94 */ 95 virtual int32_t SetSource(int32_t fd, int64_t offset, int64_t size, int32_t usage) = 0; 96 97 /** 98 * Sets the media data source to resolve. 99 * @param dataSrc A data source instance with the fileSize and a callback {@link IMediaDataSource}. 100 * @return Returns the status code. 101 */ 102 virtual int32_t SetSource(const std::shared_ptr<IMediaDataSource> &dataSrc) = 0; 103 104 /** 105 * Retrieve the meta data associated with the specified key. This method can be 106 * called after the SetSource. 107 * @param key One of the constants listed above at the definition of {@link AVMetadataCode}. 108 * @return Returns the meta data value associate with the given key code on 109 * success; empty string on failure. 110 */ 111 virtual std::string ResolveMetadata(int32_t key) = 0; 112 113 /** 114 * Fetch the album art picture associated with the data source. If there are 115 * more than one pictures, the cover image will be returned preferably. 116 * @return Returns the a chunk of shared memory containing a picture, which can be 117 * null, if such a picture can not be fetched. 118 */ 119 virtual std::shared_ptr<AVSharedMemory> FetchArtPicture() = 0; 120 121 /** 122 * Retrieve all meta data within the listed above at the definition of {@link AVMetadataCode}. 123 * This method must be called after the SetSource. 124 * @return Returns the meta data values on success; empty hash map on failure. 125 */ 126 virtual std::unordered_map<int32_t, std::string> ResolveMetadata() = 0; 127 128 /** 129 * Fetch a representative video frame near a given timestamp by considering the given 130 * option if possible, and return a video frame with given parameters. This method must be 131 * called after the SetSource. 132 * @param timeMs The time position in microseconds where the frame will be fetched. 133 * When fetching the frame at the given time position, there is no guarantee that 134 * the video source has a frame located at the position. When this happens, a frame 135 * nearby will be returned. If timeUs is negative, time position and option will ignored, 136 * and any frame that the implementation considers as representative may be returned. 137 * @param option the hint about how to fetch a frame, see {@link AVMetadataQueryOption} 138 * @param param the desired configuration of returned video frame, see {@link OutputConfiguration}. 139 * @return Returns a chunk of shared memory containing a scaled video frame, which 140 * can be null, if such a frame cannot be fetched. 141 */ 142 virtual std::shared_ptr<AVSharedMemory> FetchFrameAtTime( 143 int64_t timeUs, int32_t option, const OutputConfiguration ¶m) = 0; 144 145 /** 146 * Release the internel resource. After this method called, the service instance 147 * can not be used again. 148 */ 149 virtual void Release() = 0; 150 }; 151 } // namespace Media 152 } // namespace OHOS 153 154 #endif