1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef UTILS_H_
18
19#define UTILS_H_
20
21#include <media/stagefright/foundation/AString.h>
22#include <stdint.h>
23#include <utils/Errors.h>
24#include <utils/RefBase.h>
25#include <system/audio.h>
26#include <media/BufferingSettings.h>
27#include <media/MediaPlayerInterface.h>
28
29namespace android {
30
31#define FOURCC(c1, c2, c3, c4) \
32    ((c1) << 24 | (c2) << 16 | (c3) << 8 | (c4))
33
34uint16_t U16_AT(const uint8_t *ptr);
35uint32_t U32_AT(const uint8_t *ptr);
36uint64_t U64_AT(const uint8_t *ptr);
37
38uint16_t U16LE_AT(const uint8_t *ptr);
39uint32_t U32LE_AT(const uint8_t *ptr);
40uint64_t U64LE_AT(const uint8_t *ptr);
41
42uint64_t ntoh64(uint64_t x);
43uint64_t hton64(uint64_t x);
44
45class MetaData;
46struct AMessage;
47status_t convertMetaDataToMessage(
48        const sp<MetaData> &meta, sp<AMessage> *format);
49void convertMessageToMetaData(
50        const sp<AMessage> &format, sp<MetaData> &meta);
51
52// Returns a pointer to the next NAL start code in buffer of size |length| starting at |data|, or
53// a pointer to the end of the buffer if the start code is not found.
54// TODO: combine this with avc_utils::getNextNALUnit
55const uint8_t *findNextNalStartCode(const uint8_t *data, size_t length);
56
57AString MakeUserAgent();
58
59// Convert a MIME type to a AudioSystem::audio_format
60status_t mapMimeToAudioFormat(audio_format_t& format, const char* mime);
61
62// Convert a aac profile to a AudioSystem::audio_format
63void mapAACProfileToAudioFormat(audio_format_t& format, uint64_t eAacProfile);
64
65// Send information from MetaData to the HAL via AudioSink
66status_t sendMetaDataToHal(sp<MediaPlayerBase::AudioSink>& sink, const sp<MetaData>& meta);
67
68// Check whether the stream defined by meta can be offloaded to hardware
69bool canOffloadStream(const sp<MetaData>& meta, bool hasVideo,
70                      bool isStreaming, audio_stream_type_t streamType);
71
72AString uriDebugString(const AString &uri, bool incognito = false);
73
74struct HLSTime {
75    int32_t mSeq;
76    int64_t mTimeUs;
77    sp<AMessage> mMeta;
78
79    explicit HLSTime(const sp<AMessage> &meta = NULL);
80    int64_t getSegmentTimeUs() const;
81};
82
83bool operator <(const HLSTime &t0, const HLSTime &t1);
84
85// read and write various object to/from AMessage
86
87void writeToAMessage(const sp<AMessage> &msg, const AudioPlaybackRate &rate);
88void readFromAMessage(const sp<AMessage> &msg, AudioPlaybackRate *rate /* nonnull */);
89
90void writeToAMessage(const sp<AMessage> &msg, const AVSyncSettings &sync, float videoFpsHint);
91void readFromAMessage(
92        const sp<AMessage> &msg, AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */);
93
94void writeToAMessage(const sp<AMessage> &msg, const BufferingSettings &buffering);
95void readFromAMessage(const sp<AMessage> &msg, BufferingSettings *buffering /* nonnull */);
96
97AString nameForFd(int fd);
98
99}  // namespace android
100
101#endif  // UTILS_H_
102