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 PLAYER_TRACK_PARSE_H 17 #define PLAYER_TRACK_PARSE_H 18 19 #include <mutex> 20 #include <vector> 21 #include <unordered_map> 22 #include <map> 23 #include <set> 24 #include <gst/gst.h> 25 #include <gst/player/player.h> 26 #include "format.h" 27 28 namespace OHOS { 29 namespace Media { 30 class PlayerTrackParse { 31 public: 32 static std::shared_ptr<PlayerTrackParse> Create(); 33 int32_t GetVideoTrackInfo(std::vector<Format> &videoTrack); 34 int32_t GetAudioTrackInfo(std::vector<Format> &audioTrack); 35 int32_t GetSubtitleTrackInfo(std::vector<Format> &subtitleTrack); 36 bool FindTrackInfo(); 37 void OnElementSetup(GstElement &elem); 38 void OnElementUnSetup(GstElement &elem); 39 void Stop(); 40 int32_t GetTrackInfo(int32_t index, int32_t &innerIndex, int32_t &trackType); 41 int32_t GetTrackIndex(int32_t innerIndex, int32_t trackType, int32_t &index); 42 PlayerTrackParse(); 43 ~PlayerTrackParse(); 44 45 private: 46 GstPadProbeReturn GetTrackParse(GstPad *pad, GstPadProbeInfo *info); 47 GstPadProbeReturn ParseTrackInfo(GstPad *pad, GstPadProbeInfo *info, Format &format); 48 void ConvertToPlayerKeys(const Format &innerMeta, Format &outMeta) const; 49 bool AddProbeToPad(const GstElement *element, GstPad *pad); 50 bool AddProbeToPadList(GstElement *element, GList &list); 51 static GstPadProbeReturn ProbeCallback(GstPad *pad, GstPadProbeInfo *info, gpointer userData); 52 static void OnPadAddedCb(const GstElement *element, GstPad *pad, gpointer userData); 53 void SetUpDemuxerElementCb(GstElement &elem); 54 55 void SetUpInputSelectElementCb(GstElement &elem); 56 static void OnInputSelectPadAddedCb(const GstElement *element, GstPad *pad, gpointer userData); 57 bool InputSelectAddProbeToPad(const GstElement *element, GstPad *pad); 58 static GstPadProbeReturn InputSelectProbeCallback(GstPad *pad, GstPadProbeInfo *info, gpointer userData); 59 GstPadProbeReturn GetUsedDemux(GstPad *pad, GstPadProbeInfo *info); 60 61 static bool IsSameStreamId(GstPad *padA, GstPad *padB); 62 bool HasSameStreamIdInDemux(GstPad *pad); 63 void UpdateTrackInfo(); 64 void StartUpdateTrackInfo(); 65 int32_t GetInputSelectPadIndex(GstPad *pad); 66 void ParseSubtitlePadCaps(const GstElement *element, GstPad *pad, int32_t index, Format &innerMeta); 67 AddSignalIds(GstElement * element,gulong signalId)68 inline void AddSignalIds(GstElement *element, gulong signalId) 69 { 70 if (signalIds_.find(element) == signalIds_.end()) { 71 signalIds_[element] = {signalId}; 72 } else { 73 signalIds_[element].push_back(signalId); 74 } 75 } RemoveSignalIds(GstElement * element)76 inline void RemoveSignalIds(GstElement *element) 77 { 78 if (signalIds_.find(element) != signalIds_.end()) { 79 for (auto id : signalIds_[element]) { 80 g_signal_handler_disconnect(element, id); 81 } 82 signalIds_.erase(element); 83 } 84 } 85 std::map<GstElement *, std::vector<gulong>> signalIds_; 86 87 struct DemuxInfo { DemuxInfoDemuxInfo88 explicit DemuxInfo(GstElement *value): demux(value) {} 89 ~DemuxInfo() = default; 90 GstElement *demux = nullptr; 91 bool inUse = false; 92 int32_t trackcount = 0; 93 std::map<GstPad *, Format> trackInfos; 94 }; 95 96 struct InputSelectInfo { 97 InputSelectInfo() = default; 98 ~InputSelectInfo() = default; 99 int32_t padCount = 0; 100 std::map<GstPad *, int32_t> padIndexMap; 101 }; 102 103 bool findTrackInfo_ = false; 104 bool updateTrackInfo_ = false; 105 std::atomic<bool> isStopping_ = false; 106 std::set<GstPad *> parsePadSet_; 107 std::map<GstElement *, InputSelectInfo> inputSelectMap_; 108 std::vector<DemuxInfo> trackVec_; 109 std::vector<Format> videoTracks_; 110 std::vector<Format> audioTracks_; 111 std::vector<Format> subtitleTracks_; 112 struct PadInfo { 113 GstPad *pad; 114 gulong probeId; 115 }; 116 std::unordered_map<GstPad *, gulong> padProbes_; 117 std::mutex signalIdMutex_; 118 std::mutex padProbeMutex_; 119 std::mutex trackInfoMutex_; 120 }; 121 } 122 } 123 #endif 124