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 MEDIA_DATA_SOURCE_H_
17  #define MEDIA_DATA_SOURCE_H_
18  
19  #include "buffer/avsharedmemory.h"
20  #include "buffer/avbuffer.h"
21  
22  namespace OHOS {
23  namespace Media {
24  /**
25   * @brief Use with IMediaDataSource::ReadAt.
26   */
27  enum MediaDataSourceError : int32_t {
28      /**
29       * use with ReadAt.the resource is cut off and player will end.
30       * And the player will complete buffers and return an error.
31       */
32      SOURCE_ERROR_IO = -2,
33      /* use with ReadAt.the resource is eos and player will complete. */
34      SOURCE_ERROR_EOF = -1,
35  };
36  
37  /**
38   * @brief the mediaDataSource instance need set to player.
39   *
40   */
41  class IMediaDataSource {
42  public:
43      virtual ~IMediaDataSource() = default;
44  
45      /**
46       * @brief Player use ReadAt to tell user the desired file position and length.(length is number of Bytes)
47       * Then usr filled the mem, and return the actual length of mem.
48       * @param mem The stream mem need to fill. see avsharedmemory.h.
49       * @param length The stream length player want to get.
50       * @param pos The stream pos player want get start.
51       * The length of the filled memory must match the actual length returned.
52       * @return The actual length of stream mem filled, if failed or no mem return MediaDataSourceError.
53       */
54      virtual int32_t ReadAt(const std::shared_ptr<AVSharedMemory> &mem, uint32_t length, int64_t pos = -1) = 0;
55  
56      /**
57       * @brief Get the total size of the stream.
58       * If the user does not know the length of the stream, size should be assigned -1,
59       * player will use the datasource not seekable.
60       * @param size Total size of the stream. If no size set -1.
61       * @return MSERR_OK if ok; others if failed. see media_errors.h
62       */
63      virtual int32_t GetSize(int64_t &size) = 0;
64  
65      // This interface has been deprecated
66      virtual int32_t ReadAt(int64_t pos, uint32_t length, const std::shared_ptr<AVSharedMemory> &mem) = 0;
67      // This interface has been deprecated
68      virtual int32_t ReadAt(uint32_t length, const std::shared_ptr<AVSharedMemory> &mem) = 0;
69  };
70  
71  class IAudioDataSource {
72  public:
73      virtual ~IAudioDataSource() = default;
74  
75      /**
76       * @brief Player use ReadAt to tell user the desired file position and length.(length is number of Bytes)
77       * Then usr filled the mem, and return the actual length of mem.
78       * @param mem The stream mem need to fill. see avsharedmemory.h.
79       * @param length The stream length player want to get.
80       * @param pos The stream pos player want get start.
81       * The length of the filled memory must match the actual length returned.
82       * @return The actual length of stream mem filled, if failed or no mem return MediaDataSourceError.
83       */
84      virtual int32_t ReadAt(std::shared_ptr<AVBuffer> buffer, uint32_t length) = 0;
85  
86      /**
87       * @brief Get the total size of the stream.
88       * If the user does not know the length of the stream, size should be assigned -1,
89       * player will use the datasource not seekable.
90       * @param size Total size of the stream. If no size set -1.
91       * @return MSERR_OK if ok; others if failed. see media_errors.h
92       */
93      virtual int32_t GetSize(int64_t& size) = 0;
94  };
95  
96  } // namespace Media
97  } // namespace OHOS
98  #endif // MEDIA_DATA_SOURCE_H_