1# Media Data Demuxing
2
3You can call the native APIs provided by the AVDemuxer module to demux media data. The demuxing involves extracting media samples such as audio, video, and subtitles from bit stream data, and obtaining information related to Digital Rights Management (DRM).
4
5Currently, two data input types are supported: remote connection (over HTTP) and File Descriptor (FD).
6
7For details about the supported demuxing formats, see [AVCodec Supported Formats](avcodec-support-formats.md#media-data-demuxing).
8
9**Usage Scenario**
10
11- Audio and video playback
12
13  Demux media streams, decode the samples obtained through demuxing, and play the samples.
14
15- Audio and video editing
16
17  Demux media streams, and edit the specified samples.
18
19- Media file format conversion
20
21  Demux media streams, and encapsulate them into a new file format.
22
23## How to Develop
24
25Read [AVDemuxer](../../reference/apis-avcodec-kit/_a_v_demuxer.md) and [AVSource](../../reference/apis-avcodec-kit/_a_v_source.md) for the API reference.
26
27> **NOTE**
28>
29> - To call the demuxer APIs to parse a network playback path, declare the **ohos.permission.INTERNET** permission by following the instructions provided in [Declaring Permissions](../../security/AccessToken/declare-permissions.md).
30> - To call the demuxer APIs to write a local file, request the **ohos.permission.READ_MEDIA** permission by following the instructions provided in [Requesting User Authorization](../../security/AccessToken/request-user-authorization.md).
31> - You can also use **ResourceManager.getRawFd** to obtain the FD of a file packed in the HAP file. For details, see [ResourceManager API Reference](../../reference/apis-localization-kit/js-apis-resource-manager.md#getrawfd9).
32
33### Linking the Dynamic Libraries in the CMake Script
34
35``` cmake
36target_link_libraries(sample PUBLIC libnative_media_codecbase.so)
37target_link_libraries(sample PUBLIC libnative_media_avdemuxer.so)
38target_link_libraries(sample PUBLIC libnative_media_avsource.so)
39target_link_libraries(sample PUBLIC libnative_media_core.so)
40```
41
42> **NOTE**
43>
44> The word 'sample' in the preceding code snippet is only an example. Use the actual project directory name.
45>
46
47### How to Develop
48
491. Add the header files.
50
51   ```c++
52   #include <multimedia/player_framework/native_avdemuxer.h>
53   #include <multimedia/player_framework/native_avsource.h>
54   #include <multimedia/player_framework/native_avcodec_base.h>
55   #include <multimedia/player_framework/native_avformat.h>
56   #include <multimedia/player_framework/native_avbuffer.h>
57   #include <fcntl.h>
58   #include <sys/stat.h>
59   ```
60
612. Create a resource object.
62
63   When using **open** to obtain the FD, convert the value of **filepath** to a [sandbox path](../../file-management/app-sandbox-directory.md#mappings-between-application-sandbox-paths-and-physical-paths) to obtain sandbox resources.
64
65   ```c++
66   // Create the FD. You must have the read permission on the file handle when opening the file. (filePath indicates the path of the file to be demuxed. The file must exist.)
67   std::string filePath = "test.mp4";
68   int fd = open(filePath.c_str(), O_RDONLY);
69   struct stat fileStatus {};
70   size_t fileSize = 0;
71   if (stat(filePath.c_str(), &fileStatus) == 0) {
72      fileSize = static_cast<size_t>(fileStatus.st_size);
73   } else {
74      printf("get stat failed");
75      return;
76   }
77   // Create a source resource object for the FD resource file. If offset is not the start position of the file or size is not the actual file size, the data obtained may be incomplete. Consequently, the source resource object may fail to create or subsequent demuxing may fail.
78   OH_AVSource *source = OH_AVSource_CreateWithFD(fd, 0, fileSize);
79   if (source == nullptr) {
80      printf("create source failed");
81      return;
82   }
83   // (Optional) Create a source resource object for the URI resource file.
84   // OH_AVSource *source = OH_AVSource_CreateWithURI(uri);
85
86   // (Optional) Create a source resource object for the custom data source. Before the operation, you must implement AVSourceReadAt.
87   // Add g_filePath when OH_AVSource_CreateWithDataSource is used.
88   // g_filePath = filePath ;
89   // OH_AVDataSource dataSource = {fileSize, AVSourceReadAt};
90   // OH_AVSource *source = OH_AVSource_CreateWithDataSource(&dataSource);
91   ```
92
93   Implement the **AVSourceReadAt** API before creating the resource object.
94
95   ```c++
96   // Add the header file.
97   #include <fstream>
98   ```
99
100   ```c++
101   static std::string g_filePath;
102
103   enum MediaDataSourceError : int32_t {
104      SOURCE_ERROR_IO = -2,
105      SOURCE_ERROR_EOF = -1
106   };
107
108   int32_t AVSourceReadAt(OH_AVBuffer *data, int32_t length, int64_t pos)
109   {
110      if (data == nullptr) {
111         printf("AVSourceReadAt : data is nullptr!\n");
112         return MediaDataSourceError::SOURCE_ERROR_IO;
113      }
114
115      std::ifstream infile(g_filePath, std::ofstream::binary);
116      if (!infile.is_open()) {
117         printf("AVSourceReadAt : open file failed! file:%s\n", g_filePath.c_str());
118         return MediaDataSourceError::SOURCE_ERROR_IO; // Failed to open the file.
119      }
120
121      infile.seekg(0, std::ios::end);
122      int64_t fileSize = infile.tellg();
123      if (pos >= fileSize) {
124         printf("AVSourceReadAt : pos over or equals file size!\n");
125         return MediaDataSourceError::SOURCE_ERROR_EOF; // pos is already at the end of the file and cannot be read.
126      }
127
128      if (pos + length > fileSize) {
129         length of length = fileSize - pos; // When the sum of pos and length exceeds the file size, the data from pos to the end of the file is read.
130      }
131
132      infile.seekg(pos, std::ios::beg);
133      if (length <= 0) {
134         printf("AVSourceReadAt : raed length less than zero!\n");
135         return MediaDataSourceError::SOURCE_ERROR_IO;
136      }
137      char* buffer = new char[length];
138      infile.read(buffer, length);
139      infile.close();
140
141      memcpy(reinterpret_cast<char *>(OH_AVBuffer_GetAddr(data)),
142         buffer, length);
143      delete[] buffer;
144
145      return length;
146   }
147   ```
1483. Create a demuxer instance.
149   ```c++
150   // Create a demuxer for the resource object.
151   OH_AVDemuxer *demuxer = OH_AVDemuxer_CreateWithSource(source);
152   if (demuxer == nullptr) {
153      printf("create demuxer failed");
154      return;
155   }
156   ```
1574. (Optional) Register a [callback to obtain the media key system information](../../reference/apis-avcodec-kit/_a_v_demuxer.md#demuxer_mediakeysysteminfocallback). If the stream is not a DRM stream or the [media key system information](../../reference/apis-drm-kit/_drm.md#drm_mediakeysysteminfo) has been obtained, you can skip this step.
158
159   In the API for setting DRM information listeners, the callback function can return a demuxer instance. It is suitable for the scenario where multiple demuxer instances are used.
160
161   ```c++
162   // Implement the OnDrmInfoChangedWithObj callback.
163   static void OnDrmInfoChangedWithObj(OH_AVDemuxer *demuxer, DRM_MediaKeySystemInfo *drmInfo)
164   {
165      // Parse the media key system information, including the quantity, DRM type, and corresponding PSSH.
166   }
167
168   Demuxer_MediaKeySystemInfoCallback callback = &OnDrmInfoChangedWithObj;
169   Drm_ErrCode ret = OH_AVDemuxer_SetDemuxerMediaKeySystemInfoCallback(demuxer, callback);
170   ```
171   After the callback is invoked, you can call the API to proactively obtain the media key system information (UUID and corresponding PSSH).
172
173   ```c++
174   DRM_MediaKeySystemInfo mediaKeySystemInfo;
175   OH_AVDemuxer_GetMediaKeySystemInfo(demuxer, &mediaKeySystemInfo);
176   ```
177   After obtaining and parsing DRM information, create [MediaKeySystem](../drm/native-drm-mediakeysystem-management.md) and [MediaKeySession](../drm/native-drm-mediakeysession-management.md) instances of the corresponding DRM scheme to obtain a media key. If required, set the audio decryption configuration by following step 4 in [Audio Decoding](./audio-decoding.md#how-to-develop), and set the video decryption configuration by following step 5 [Surface Output in Video Decoding](./video-decoding.md#surface-mode) or step 4 in [Buffer Output in Video Decoding](./video-decoding.md#buffer mode).
178
1795. (Optional) Obtain the number of tracks. If you know the track information, skip this step.
180
181   ```c++
182   // Obtain the number of tracks from the file source information. You can call the API to obtain file-level attributes. For details, see Table 1 in Appendix 1.
183   OH_AVFormat *sourceFormat = OH_AVSource_GetSourceFormat(source);
184   if (sourceFormat == nullptr) {
185      printf("get source format failed");
186      return;
187   }
188   int32_t trackCount = 0;
189   if (!OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &trackCount)) {
190      printf("get track count from source format failed");
191      return;
192   }
193   OH_AVFormat_Destroy(sourceFormat);
194   ```
195
1966. (Optional) Obtain the track index and format. If you know the track information, skip this step.
197
198   ```c++
199   uint32_t audioTrackIndex = 0;
200   uint32_t videoTrackIndex = 0;
201   int32_t w = 0;
202   int32_t h = 0;
203   int32_t trackType;
204   for (uint32_t index = 0; index < (static_cast<uint32_t>(trackCount)); index++) {
205      // Obtain the track information. You can call the API to obtain track-level attributes. For details, see Table 2 in Appendix.
206      OH_AVFormat *trackFormat = OH_AVSource_GetTrackFormat(source, index);
207      if (trackFormat == nullptr) {
208         printf("get track format failed");
209         return;
210      }
211      if (!OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_TRACK_TYPE, &trackType)) {
212         printf("get track type from track format failed");
213         return;
214      }
215      static_cast<OH_MediaType>(trackType) == OH_MediaType::MEDIA_TYPE_AUD ? audioTrackIndex = index : videoTrackIndex = index;
216      // Obtain the width and height of the video track.
217      if (trackType == OH_MediaType::MEDIA_TYPE_VID) {
218         if (!OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_WIDTH, &w)) {
219            printf("get track width from track format failed");
220            return;
221         }
222         if (!OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_HEIGHT, &h)) {
223            printf("get track height from track format failed");
224            return;
225         }
226      }
227      OH_AVFormat_Destroy(trackFormat);
228   }
229   ```
230
2317. Select a track, from which the demuxer reads data.
232
233   ```c++
234   if(OH_AVDemuxer_SelectTrackByID(demuxer, audioTrackIndex) != AV_ERR_OK){
235      printf("select audio track failed: %d", audioTrackIndex);
236      return;
237   }
238   if(OH_AVDemuxer_SelectTrackByID(demuxer, videoTrackIndex) != AV_ERR_OK){
239      printf("select video track failed: %d", videoTrackIndex);
240      return;
241   }
242   // (Optional) Deselect the track.
243   // OH_AVDemuxer_UnselectTrackByID(demuxer, audioTrackIndex);
244   ```
245
2468. (Optional) Seek to the specified time for the selected track.
247
248   ```c++
249   // Demuxing is performed from this time.
250   // Note:
251   // 1. If OH_AVDemuxer_SeekToTime is called for an MPEG TS file, the target position may be a non-key frame. You can then call OH_AVDemuxer_ReadSampleBuffer to check whether the current frame is a key frame based on the obtained OH_AVCodecBufferAttr. If it is a non-key frame, which causes display issues on the application side, cyclically read the frames until you reach the first key frame, where you can perform processing such as decoding.
252   // 2. If OH_AVDemuxer_SeekToTime is called for an OGG file, the file seeks to the start of the time interval (second) where the input parameter millisecond is located, which may cause a certain number of frame errors.
253   OH_AVDemuxer_SeekToTime(demuxer, 0, OH_AVSeekMode::SEEK_MODE_CLOSEST_SYNC);
254   ```
255
2569. Start demuxing and cyclically obtain samples. The code snippet below uses a file that contains audio and video tracks as an example.
257
258   A **BufferAttr** object contains the following attributes.
259   - **size**: sample size.
260   - **offset**: offset of the data in the AVBuffer. The value is generally 0.
261   - **pts**: timestamp when the file is muxed.
262   - **flags**: sample attributes.
263
264   | flag | Description|
265   | -------- | -------- |
266   | AVCODEC_BUFFER_FLAGS_NONE | Default value.|
267   | AVCODEC_BUFFER_FLAGS_EOS | End of Stream (EOS). The data is empty.|
268   | AVCODEC_BUFFER_FLAGS_SYNC_FRAME | IDR frame or I-frame.|
269   | AVCODEC_BUFFER_FLAGS_INCOMPLETE_FRAME | Incomplete sample. Generally, a complete sample fails to be copied because the buffer is too small.|
270   | AVCODEC_BUFFER_FLAGS_CODEC_DATA | Frame containing parameter set information.|
271   | AVCODEC_BUFFER_FLAGS_DISCARD  | Frames that can be discarded.|
272
273   ```c++
274   // Create a buffer based on the specified size to store the data obtained after demuxing.
275   // It is recommended that the buffer size be greater than the size of the stream to be obtained. In the example, the buffer size is set to the size of a single frame.
276   OH_AVBuffer *buffer = OH_AVBuffer_Create(w * h * 3 >> 1);
277   if (buffer == nullptr) {
278      printf("build buffer failed");
279      return;
280   }
281   OH_AVCodecBufferAttr info;
282   bool videoIsEnd = false;
283   bool audioIsEnd = false;
284   int32_t ret;
285   while (!audioIsEnd || !videoIsEnd) {
286      // Before calling OH_AVDemuxer_ReadSampleBuffer, call OH_AVDemuxer_SelectTrackByID to select the track from which the demuxer reads data.
287      // Obtain the audio sample.
288      if(!audioIsEnd) {
289         ret = OH_AVDemuxer_ReadSampleBuffer(demuxer, audioTrackIndex, buffer);
290         if (ret == AV_ERR_OK) {
291            // Obtain and process the audio sample in the buffer.
292            OH_AVBuffer_GetBufferAttr(buffer, &info);
293            printf("audio info.size: %d\n", info.size);
294            if (info.flags == OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
295               audioIsEnd = true;
296            }
297         }
298      }
299      if(!videoIsEnd) {
300         ret = OH_AVDemuxer_ReadSampleBuffer(demuxer, videoTrackIndex, buffer);
301         if (ret == AV_ERR_OK) {
302            // Obtain and process the video sample in the buffer.
303            OH_AVBuffer_GetBufferAttr(buffer, &info);
304            printf("video info.size: %d\n", info.size);
305            if (info.flags == OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
306               videoIsEnd = true;
307            }
308         }
309      }
310   }
311   OH_AVBuffer_Destroy(buffer);
312   ```
313
31410. Destroy the demuxer instance.
315      ```c++
316      // Manually set the instance to NULL after OH_AVSource_Destroy is called. Do not call this API repeatedly for the same instance; otherwise, a program error occurs.
317      if (OH_AVSource_Destroy(source) != AV_ERR_OK) {
318         printf("destroy source pointer error");
319      }
320      source = NULL;
321      // Manually set the instance to NULL after OH_AVDemuxer_Destroy is called. Do not call this API repeatedly for the same instance; otherwise, a program error occurs.
322      if (OH_AVDemuxer_Destroy(demuxer) != AV_ERR_OK) {
323         printf("destroy demuxer pointer error");
324      }
325      demuxer = NULL;
326      close(fd);
327      ```
328
329## Appendix
330### Supported File-Level Attributes
331
332> **NOTE**
333>
334> Attribute data can be obtained only when the file is parsed normally. If the file information is incorrect or missing, the parsing is abnormal and the corresponding data cannot be obtained.
335>
336> For details about the data type and value range, see [Media Data Key-Value Pairs](../../reference/apis-avcodec-kit/_codec_base.md#media-data-key-value-pairs).
337
338**Table 1** Supported file-level attributes
339| Name| Description|
340| -- | -- |
341|OH_MD_KEY_TITLE|Title.|
342|OH_MD_KEY_ARTIST|Artist.|
343|OH_MD_KEY_ALBUM|Album.|
344|OH_MD_KEY_ALBUM_ARTIST|Album artist.|
345|OH_MD_KEY_DATE|Date.|
346|OH_MD_KEY_COMMENT|Comment.|
347|OH_MD_KEY_GENRE|Genre.|
348|OH_MD_KEY_COPYRIGHT|Copyright.|
349|OH_MD_KEY_LANGUAGE|Language.|
350|OH_MD_KEY_DESCRIPTION|Description.|
351|OH_MD_KEY_LYRICS|Lyrics.|
352|OH_MD_KEY_TRACK_COUNT|Track count.|
353|OH_MD_KEY_DURATION|Duration.|
354|OH_MD_KEY_START_TIME|Start time.|
355
356### Supported Track-Level Attributes
357
358> **NOTE**
359>
360> Attribute data can be obtained only when the file is parsed normally. If the file information is incorrect or missing, the parsing is abnormal and the corresponding data cannot be obtained.
361>
362> For details about the data type and value range, see [Media Data Key-Value Pairs](../../reference/apis-avcodec-kit/_codec_base.md#media-data-key-value-pairs).
363
364**Table 2** Supported track-level attributes
365| Name| Description| Supported by Video Tracks| Supported by Audio Tracks| Supported by Subtitle Tracks|
366| -- | -- | -- | -- | -- |
367|OH_MD_KEY_CODEC_MIME|Stream codec type.|Supported|Supported|Supported|
368|OH_MD_KEY_TRACK_TYPE|Stream track type.|Supported|Supported|Supported|
369|OH_MD_KEY_TRACK_START_TIME|Start time of the stream.|Supported|Supported|Supported|
370|OH_MD_KEY_BITRATE|Stream bit rate.|Supported|Supported|Not supported|
371|OH_MD_KEY_LANGUAGE|Stream language type.|Supported|Supported|Not supported|
372|OH_MD_KEY_CODEC_CONFIG|Codec-specific data. In the case of video, a parameter set is transferred. In the case of audio, parameter configuration information of the decoder is transferred.|Supported|Supported|Not supported|
373|OH_MD_KEY_WIDTH|Video stream width.|Supported|Not supported|Not supported|
374|OH_MD_KEY_HEIGHT|Video stream height.|Supported|Not supported|Not supported|
375|OH_MD_KEY_FRAME_RATE|Video stream frame rate.|Supported|Not supported|Not supported|
376|OH_MD_KEY_ROTATION|Rotation angle of the video stream.|Supported|Not supported|Not supported|
377|OH_MD_KEY_VIDEO_SAR|Aspect ratio of the video stream sample.|Supported|Not supported|Not supported|
378|OH_MD_KEY_PROFILE|Encoding profile of the video stream. This key is valid only for H.265 streams.|Supported|Not supported|Not supported|
379|OH_MD_KEY_RANGE_FLAG|Video YUV value range flag of the video stream. This key is valid only for H.265 streams.|Supported|Not supported|Not supported|
380|OH_MD_KEY_COLOR_PRIMARIES|Video primary color of the video stream. This key is valid only for H.265 streams.|Supported|Not supported|Not supported|
381|OH_MD_KEY_TRANSFER_CHARACTERISTICS|Video transfer characteristics of the video stream. This key is valid only for H.265 streams.|Supported|Not supported|Not supported|
382|OH_MD_KEY_MATRIX_COEFFICIENTS|Video matrix coefficient. This key is valid only for H.265 streams.|Supported|Not supported|Not supported|
383|OH_MD_KEY_VIDEO_IS_HDR_VIVID|Flag indicating whether the video stream is HDR Vivid. This key is valid only for HDR Vivid streams.|Supported|Not supported|Not supported|
384|OH_MD_KEY_AUD_SAMPLE_RATE|Audio stream sampling rate.|Not supported|Supported|Not supported|
385|OH_MD_KEY_AUD_CHANNEL_COUNT|Number of audio stream channels.|Not supported|Supported|Not supported|
386|OH_MD_KEY_CHANNEL_LAYOUT|Encoding channel layout required by the audio stream.|Not supported|Supported|Not supported|
387|OH_MD_KEY_AUDIO_SAMPLE_FORMAT|Audio stream sample format.|Not supported|Supported|Not supported|
388|OH_MD_KEY_AAC_IS_ADTS|AAC format. This key is valid only for AAC streams.|Not supported|Supported|Not supported|
389|OH_MD_KEY_BITS_PER_CODED_SAMPLE|Number of bits per coded sample in the audio stream.|Not supported|Supported|Not supported|
390